Database Operations
Writing, reading, updating, deleting, counting, indexing, reloading and compacting.
Database.insert
Database.insert
argument type
return type
aliases
Array<Model>
Promise<{
docs: Array<Model>;
number: number;
}>
Database.create
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the inserted documents.number
: Is the number of the inserted documents.
Database.find
Database.find
argument type
return type
aliases
{
filter?: Query<Model>;
skip?: number;
limit?: number;
sort?: Sort<Model>;
}
Promise<
Array<Model>
>
Database.read
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
skip
andlimit
are both number, and basically self-explanatory.sort
is an object that its keys must be the same asModel
keys (or some of them), yet the value of those keys must be either1
(for ascending sorting) or-1
(for descending sorting).
You can also use the computed value when filtering (the beauty of object mapping).
For more about query operators and options read: Query API documentation. And for more about the benifit of object mapping in the query API, read: Object Mapping documentation.
Return type
Method return a promise fulfilling with an array of the matched documents. If no documents matches then the array would be empty.
Database.update
Database.update
argument type
return type
aliases
{
filter: Query<Model>;
update: Update<Model>;
multi: boolean;
}
Promise<{
docs: Array<Model>;
number: number;
}>
non
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
update
is an object of update operators and modifiers, similar to the update operators and modifiers of MonogDB.multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
For more about query operators and options read: Query API documentation. And for more about update operators and modifiers read: Update API documentation.
Do not apply update operators to the readonly
values that are the computed properties (e.g. age property in the model above).
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the update documents.number
: Is the number of the updated documents.
Database.upsert
Database.upsert
argument type
return type
aliases
{
filter: Query<Model>;
update: Upsert<Model>;
multi: boolean;
}
Promise<{
docs: Array<Model>;
number: number;
upsert:boolean
}>
non
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
update
is an object of update operators and modifiers, similar to the update operators and modifiers of MonogDB. But with one required field ($setOnInsert
). This field value must the document to be inserted if no documents where found to be updates.multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
For more about query operators and options read: Query API documentation. And for more about update operators and modifiers read: Update API documentation.
Do not apply update operators to the readonly
values that are the computed properties (e.g. age property in the model above).
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the inserted/updated documents.number
: Is the number of the inserted/updated documents.upsert:
is a boolean that should betrue
when an insertion actually occurred, and should befalse
if an update occurred.
Database.count
Database.count
argument type
return type
aliases
Query<Model>
Promise<number>
Database.number
For counting documents that meet a specified query. This method takes the Query<Model>
as an argument. It is similar to the query language of MongoDB, it can accepts direct equality evaluation like {age: 11}
or a field level operator: {age: { $gt: 11 }}
or a top level operator: $or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
Return type
Method return a promise fulfilling with a number, that is the number of the documents matching the given query.
Database.delete
Database.delete
argument type
return type
aliases
{
filter: Query<Model>;
multi: boolean;
}
Promise<{
docs: Array<Model>;
number: number;
}>
Database.remove
Where:
filter
is a query similar to the query language of MongoDB, it can accepts direct equality evaluation like{age: 11}
or a field level operator:{age: { $gt: 11 }}
or a top level operator:$or: [{ age: 11 }, { age: 12 }]
.
Operators are those parameter that starts with the dollar sign $
(e.g. $gt
, $lt
, $or...
etc.). Field names (e.g. name
, age
, yearBorn
) can not start with a dollar sign.
multi
is aboolean
, defaults to false, if true it will update all the matched documents, if it's false it will update only the first matched document.
Return type
Method return a promise fulfilling with object that has the following properties:
docs
: Is an array of the deleted documents.number
: Is the number of the deleted documents.
Database.createIndex
Database.createIndex
argument type
return type
aliases
{
fieldName: string;
unique?: boolean;
sparse?: boolean;
expireAfterSeconds?: number;
}
Promise<{
affectedIndex: string
}>
Database.ensureIndex
fieldName
(required): name of the field to index.unique
(optional, defaults tofalse
): enforce field uniqueness. Note that a unique index will raise an error if you try to index two documents for which the field is not defined.sparse
(optional, defaults tofalse
): don't index documents for which the field is not defined. Use this option along with "unique" if you want to accept multiple documents for which it is not defined.expireAfterSeconds
(number of seconds, optional): if set, the created index is a TTL (time to live) index, that will automatically remove documents when the system date becomes larger than the date on the indexed field plusexpireAfterSeconds
. Documents where the indexed field is not specified or not aDate
object are ignored.
Return type
Method return a promise fulfilling with object that has the following properties:
affectedIndex
: Is a string of the field name that the index created upon.
Database.removeIndex
Database.removeIndex
argument type
return type
aliases
string
Promise<{
affectedIndex: string
}>
non
Return type
Method return a promise fulfilling with object that has the following properties:
affectedIndex
: Is a string of the field name that the index created upon.
Database.reload
Database.reload
argument type
return type
aliases
non
Promise<void>
non
Reloading the persistence layer (e.g. the file) of the database, so any changes that were made external to the instance of the database will be loaded into this instance.
Database.forcefulUnlock
Database.forcefulUnlock
argument type
return type
aliases
non
Promise<void>
non
There's a locking mechanism implemented in the file system persistence adapter to prevent two instances from modifying the same file at once. So one of them has to wait for the other. However, it may occur that one of the instances would crash while modifying the file and the lock would still be there, so the other instance would have to wait forever for the file to be unlocked.
This is why this utility method is provided as a means of forcefully unlocking the file.
Database.compact
Database.compact
argument type
return type
aliases
non
Promise<void>
non
When persisting data (e.g. into a file), the data will be persisted by appending, this is to avoid re-writing of the whole file in case of removing or updating. This behavior might result in an increase in the persistence layer size (e.g. file size). That's why the database provides you with a function to compact the file size:
Database.resetAutoCompaction
Database.resetAutoCompaction
argument type
return type
aliases
number
Promise<void>
non
Resetting (or starting) auto compaction at intervals of time.
Database.stopAutoCompaction
Database.stopAutoCompaction
argument type
return type
aliases
non
Promise<void>
non
Stopping auto compaction.
Last updated