28 KiB
Datastore ⇐ EventEmitter
The Datastore
class is the main class of NeDB.
Kind: global class
Extends: EventEmitter
Emits: Datastore.event:"compaction.done"
- Datastore ⇐
EventEmitter
- new Datastore(options)
- instance
- .persistence :
Persistence
- .executor :
Executor
- .autoloadPromise :
Promise
- .loadDatabase(callback)
- .loadDatabaseAsync() ⇒
Promise
- .getAllData() ⇒
Array.<document>
- .resetIndexes()
- .ensureIndex(options, callback)
- .ensureIndexAsync(options) ⇒
Promise.<void>
- .removeIndex(fieldName, callback)
- .removeIndexAsync(fieldName) ⇒
Promise.<void>
- .removeFromIndexes(doc)
- .updateIndexes(oldDoc, [newDoc])
- .insertAsync(newDoc) ⇒
Promise.<document>
- .count(query, [callback]) ⇒
Cursor.<number>
|undefined
- .countAsync(query) ⇒
Cursor.<number>
- .find(query, [projection], [callback]) ⇒
Cursor.<Array.<document>>
|undefined
- .findAsync(query, [projection]) ⇒
Cursor.<Array.<document>>
- .findOne(query, projection, callback) ⇒
Cursor.<document>
|undefined
- .findOneAsync(query, projection) ⇒
Cursor.<document>
- .update(query, update, [options], [cb])
- .updateAsync(query, update, [options]) ⇒
Promise.<{numAffected: number, affectedDocuments: (Array.<document>|document|null), upsert: boolean}>
- .remove(query, [options], [cb])
- .removeAsync(query, [options]) ⇒
Promise.<number>
- .persistence :
- static
- inner
- ~countCallback :
function
- ~findOneCallback :
function
- ~updateCallback :
function
- ~removeCallback :
function
- ~countCallback :
new Datastore(options)
Create a new collection, either persistent or in-memory.
If you use a persistent datastore without the autoload
option, you need to call loadDatabase
manually. This
function fetches the data from datafile and prepares the database. Don't forget it! If you use a persistent
datastore, no command (insert, find, update, remove) will be executed before loadDatabase
is called, so make sure
to call it yourself or use the autoload
option.
Param | Type | Default | Description |
---|---|---|---|
options | object | string |
Can be an object or a string. If options is a string, the behavior is the same as in v0.6: it will be interpreted as |
|
[options.filename] | string |
null |
Path to the file where the data is persisted. If left blank, the datastore is automatically considered in-memory only. It cannot end with a |
[options.inMemoryOnly] | boolean |
false |
If set to true, no data will be written in storage. |
[options.timestampData] | boolean |
false |
If set to true, createdAt and updatedAt will be created and populated automatically (if not specified by user) |
[options.autoload] | boolean |
false |
If used, the database will automatically be loaded from the datafile upon creation (you don't need to call |
[options.onload] | function |
If you use autoloading, this is the handler called after the |
|
[options.beforeDeserialization] | function |
Hook you can use to transform data after it was serialized and before it is written to disk. Can be used for example to encrypt data before writing database to disk. This function takes a string as parameter (one line of an NeDB data file) and outputs the transformed string, which must absolutely not contain a |
|
[options.afterSerialization] | function |
Inverse of |
|
[options.corruptAlertThreshold] | number |
0.1 |
Between 0 and 1, defaults to 10%. NeDB will refuse to start if more than this percentage of the datafile is corrupt. 0 means you don't tolerate any corruption, 1 means you don't care. |
[options.compareStrings] | compareStrings |
If specified, it overrides default string comparison which is not well adapted to non-US characters in particular accented letters. Native |
|
[options.nodeWebkitAppName] | string |
Deprecated: if you are using NeDB from whithin a Node Webkit app, specify its name (the same one you use in the |
datastore.persistence : Persistence
The Persistence
instance for this Datastore
.
Kind: instance property of Datastore
datastore.executor : Executor
The Executor
instance for this Datastore
. It is used in all methods exposed by the Datastore
, any Cursor
produced by the Datastore
and by this.persistence.compactDataFile
& this.persistence.compactDataFileAsync
to ensure operations are performed sequentially in the database.
Kind: instance property of Datastore
datastore.autoloadPromise : Promise
A Promise that resolves when the autoload has finished.
The onload callback is not awaited by this Promise, it is started immediately after that.
Kind: instance property of Datastore
datastore.loadDatabase(callback)
Load the database from the datafile, and trigger the execution of buffered commands if any.
Kind: instance method of Datastore
Param | Type |
---|---|
callback | function |
datastore.loadDatabaseAsync() ⇒ Promise
Load the database from the datafile, and trigger the execution of buffered commands if any.
Kind: instance method of Datastore
datastore.getAllData() ⇒ Array.<document>
Get an array of all the data in the database
Kind: instance method of Datastore
datastore.resetIndexes()
Reset all currently defined indexes
Kind: instance method of Datastore
datastore.ensureIndex(options, callback)
Ensure an index is kept for this field. Same parameters as lib/indexes This function acts synchronously on the indexes, however the persistence of the indexes is deferred with the executor. Previous versions said explicitly the callback was optional, it is now recommended setting one.
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
options | object |
||
options.fieldName | string |
Name of the field to index. Use the dot notation to index a field in a nested document. |
|
[options.unique] | boolean |
false |
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. |
[options.sparse] | boolean |
false |
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. |
[options.expireAfterSeconds] | number |
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 plus |
|
callback | NoParamCallback |
Callback, signature: err |
datastore.ensureIndexAsync(options) ⇒ Promise.<void>
Ensure an index is kept for this field. Same parameters as lib/indexes This function acts synchronously on the indexes, however the persistence of the indexes is deferred with the executor. Previous versions said explicitly the callback was optional, it is now recommended setting one.
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
options | object |
||
options.fieldName | string |
Name of the field to index. Use the dot notation to index a field in a nested document. |
|
[options.unique] | boolean |
false |
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. |
[options.sparse] | boolean |
false |
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. |
[options.expireAfterSeconds] | number |
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 plus |
datastore.removeIndex(fieldName, callback)
Remove an index Previous versions said explicitly the callback was optional, it is now recommended setting one.
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
fieldName | string |
Field name of the index to remove. Use the dot notation to remove an index referring to a field in a nested document. |
callback | NoParamCallback |
Optional callback, signature: err |
datastore.removeIndexAsync(fieldName) ⇒ Promise.<void>
Remove an index Previous versions said explicitly the callback was optional, it is now recommended setting one.
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
fieldName | string |
Field name of the index to remove. Use the dot notation to remove an index referring to a field in a nested document. |
datastore.removeFromIndexes(doc)
Remove one or several document(s) from all indexes
Kind: instance method of Datastore
Param | Type |
---|---|
doc | document |
datastore.updateIndexes(oldDoc, [newDoc])
Update one or several documents in all indexes To update multiple documents, oldDoc must be an array of { oldDoc, newDoc } pairs If one update violates a constraint, all changes are rolled back
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
oldDoc | document | Array.<{oldDoc: document, newDoc: document}> |
Document to update, or an |
[newDoc] | document |
Document to replace the oldDoc with. If the first argument is an |
datastore.insertAsync(newDoc) ⇒ Promise.<document>
Insert a new document Private Use Datastore.insertAsync which has the same signature
Kind: instance method of Datastore
Param | Type |
---|---|
newDoc | document | Array.<document> |
datastore.count(query, [callback]) ⇒ Cursor.<number>
| undefined
Count all documents matching the query
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
query | query |
MongoDB-style query |
[callback] | countCallback |
If given, the function will return undefined, otherwise it will return the Cursor. |
datastore.countAsync(query) ⇒ Cursor.<number>
Count all documents matching the query
Kind: instance method of Datastore
Returns: Cursor.<number>
-
count
Param | Type | Description |
---|---|---|
query | query |
MongoDB-style query |
datastore.find(query, [projection], [callback]) ⇒ Cursor.<Array.<document>>
| undefined
Find all documents matching the query If no callback is passed, we return the cursor so that user can limit, skip and finally exec
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
query | query |
MongoDB-style query |
|
[projection] | projection | MultipleDocumentsCallback |
{} |
MongoDB-style projection. If not given, will be interpreted as the callback. |
[callback] | MultipleDocumentsCallback |
Optional callback, signature: err, docs |
datastore.findAsync(query, [projection]) ⇒ Cursor.<Array.<document>>
Find all documents matching the query If no callback is passed, we return the cursor so that user can limit, skip and finally exec
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
query | query |
MongoDB-style query |
|
[projection] | projection |
{} |
MongoDB-style projection |
datastore.findOne(query, projection, callback) ⇒ Cursor.<document>
| undefined
Find one document matching the query
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
query | query |
MongoDB-style query |
projection | projection |
MongoDB-style projection |
callback | SingleDocumentCallback |
Optional callback, signature: err, doc |
datastore.findOneAsync(query, projection) ⇒ Cursor.<document>
Find one document matching the query
Kind: instance method of Datastore
Param | Type | Description |
---|---|---|
query | query |
MongoDB-style query |
projection | projection |
MongoDB-style projection |
datastore.update(query, update, [options], [cb])
Update all docs matching query.
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
query | query |
is the same kind of finding query you use with |
|
update | document | update |
specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!):
|
|
[options] | Object |
Optional options |
|
[options.multi] | boolean |
false |
If true, can update multiple documents |
[options.upsert] | boolean |
false |
If true, can insert a new document corresponding to the |
[options.returnUpdatedDocs] | boolean |
false |
(not Mongo-DB compatible) If true and update is not an upsert, will return the array of documents matched by the find query and updated. Updated documents will be returned even if the update did not actually modify them. |
[cb] | updateCallback |
() => {} |
Optional callback |
datastore.updateAsync(query, update, [options]) ⇒ Promise.<{numAffected: number, affectedDocuments: (Array.<document>|document|null), upsert: boolean}>
Update all docs matching query.
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
query | query |
is the same kind of finding query you use with |
|
update | document | update |
specifies how the documents should be modified. It is either a new document or a set of modifiers (you cannot use both together, it doesn't make sense!):
|
|
[options] | Object |
Optional options |
|
[options.multi] | boolean |
false |
If true, can update multiple documents |
[options.upsert] | boolean |
false |
If true, can insert a new document corresponding to the |
[options.returnUpdatedDocs] | boolean |
false |
(not Mongo-DB compatible) If true and update is not an upsert, will return the array of documents matched by the find query and updated. Updated documents will be returned even if the update did not actually modify them. |
datastore.remove(query, [options], [cb])
Remove all docs matching the query.
Kind: instance method of Datastore
Param | Type | Default | Description |
---|---|---|---|
query | query |
||
[options] | object |
Optional options |
|
[options.multi] | boolean |
false |
If true, can update multiple documents |
[cb] | removeCallback |
() => {} |
Optional callback, signature: err, numRemoved |
datastore.removeAsync(query, [options]) ⇒ Promise.<number>
Remove all docs matching the query. Use Datastore.removeAsync which has the same signature
Kind: instance method of Datastore
Returns: Promise.<number>
-
How many documents were removed
Param | Type | Default | Description |
---|---|---|---|
query | query |
||
[options] | object |
Optional options |
|
[options.multi] | boolean |
false |
If true, can update multiple documents |
"event:compaction.done"
Compaction event. Happens when the Datastore's Persistence has been compacted.
It happens when calling datastore.persistence.compactDatafile
, which is called periodically if you have called
datastore.persistence.setAutocompactionInterval
.
Kind: event emitted by Datastore
Datastore~countCallback : function
Kind: inner typedef of Datastore
Param | Type |
---|---|
err | Error |
count | number |
Datastore~findOneCallback : function
Kind: inner typedef of Datastore
Param | Type |
---|---|
err | Error |
doc | document |
Datastore~updateCallback : function
If update was an upsert, upsert
flag is set to true, affectedDocuments
can be one of the following:
- For an upsert, the upserted document
- For an update with returnUpdatedDocs option false, null
- For an update with returnUpdatedDocs true and multi false, the updated document
- For an update with returnUpdatedDocs true and multi true, the array of updated documents
WARNING: The API was changed between v1.7.4 and v1.8, for consistency and readability reasons. Prior and including to v1.7.4, the callback signature was (err, numAffected, updated) where updated was the updated document in case of an upsert or the array of updated documents for an update if the returnUpdatedDocs option was true. That meant that the type of affectedDocuments in a non multi update depended on whether there was an upsert or not, leaving only two ways for the user to check whether an upsert had occured: checking the type of affectedDocuments or running another find query on the whole dataset to check its size. Both options being ugly, the breaking change was necessary.
Kind: inner typedef of Datastore
Param | Type |
---|---|
err | Error |
numAffected | number |
affectedDocuments | ?Array.<document> | document |
upsert | boolean |
Datastore~removeCallback : function
Kind: inner typedef of Datastore
Param | Type |
---|---|
err | Error |
numRemoved | number |