EventEmitter
The Datastore
class is the main class of NeDB.
EventEmitter
**Emits**: Datastore#event:"compaction.done"
* [Datastore](#Datastore) ⇐ EventEmitter
* [new Datastore(options)](#new_Datastore_new)
* _instance_
* [.inMemoryOnly](#Datastore+inMemoryOnly) : boolean
* [.autoload](#Datastore+autoload) : boolean
* [.timestampData](#Datastore+timestampData) : boolean
* [.filename](#Datastore+filename) : string
* [.persistence](#Datastore+persistence) : [Persistence
](#Persistence)
* [.executor](#Datastore+executor) : [Executor
](#Executor)
* [.indexes](#Datastore+indexes) : Object.<string, Index>
* [.ttlIndexes](#Datastore+ttlIndexes) : Object.<string, number>
* [.autoloadPromise](#Datastore+autoloadPromise) : Promise
* [.compareStrings()](#Datastore+compareStrings) : [compareStrings
](#compareStrings)
* [.loadDatabase(callback)](#Datastore+loadDatabase)
* [.loadDatabaseAsync()](#Datastore+loadDatabaseAsync) ⇒ Promise
* [.getAllData()](#Datastore+getAllData) ⇒ [Array.<document>
](#document)
* [.resetIndexes(newData)](#Datastore+resetIndexes)
* [.ensureIndex(options, callback)](#Datastore+ensureIndex)
* [.ensureIndexAsync(options)](#Datastore+ensureIndexAsync) ⇒ Promise.<void>
* [.removeIndex(fieldName, callback)](#Datastore+removeIndex)
* [.removeIndexAsync(fieldName)](#Datastore+removeIndexAsync) ⇒ Promise.<void>
* [.addToIndexes(doc)](#Datastore+addToIndexes)
* [.removeFromIndexes(doc)](#Datastore+removeFromIndexes)
* [.updateIndexes(oldDoc, [newDoc])](#Datastore+updateIndexes)
* [.getCandidates(query, [dontExpireStaleDocs], callback)](#Datastore+getCandidates)
* [.getCandidatesAsync(query, [dontExpireStaleDocs])](#Datastore+getCandidatesAsync) ⇒ Promise.<Array.<document>>
* [.insertAsync(newDoc)](#Datastore+insertAsync) ⇒ [Promise.<document>
](#document)
* [.count(query, [callback])](#Datastore+count) ⇒ Cursor.<number>
\| undefined
* [.countAsync(query)](#Datastore+countAsync) ⇒ Cursor.<number>
* [.find(query, [projection], [callback])](#Datastore+find) ⇒ Cursor.<Array.<document>>
\| undefined
* [.findAsync(query, [projection])](#Datastore+findAsync) ⇒ Cursor.<Array.<document>>
* [.findOne(query, [projection], [callback])](#Datastore+findOne) ⇒ [Cursor.<document>
](#document) \| undefined
* [.findOneAsync(query, projection)](#Datastore+findOneAsync) ⇒ [Cursor.<document>
](#document)
* [.update(query, update, [options|], [cb])](#Datastore+update)
* [.updateAsync(query, update, [options])](#Datastore+updateAsync) ⇒ Promise.<{numAffected: number, affectedDocuments: (Array.<document>\|document\|null), upsert: boolean}>
* [.remove(query, [options], [cb])](#Datastore+remove)
* [.removeAsync(query, [options])](#Datastore+removeAsync) ⇒ Promise.<number>
* ["event:compaction.done"](#Datastore+event_compaction.done)
* _inner_
* [~countCallback](#Datastore..countCallback) : function
* [~findOneCallback](#Datastore..findOneCallback) : function
* [~updateCallback](#Datastore..updateCallback) : function
* [~removeCallback](#Datastore..removeCallback) : function
### 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.
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
. Giving a string is deprecated, and will be removed in the
next major version.
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 ~
which is used in the temporary files NeDB uses to
perform crash-safe writes.
boolean
= false
- If set to true, no data will be written in storage.
- [.timestampData]boolean
= false
- If set to true, createdAt and updatedAt will be created and populated automatically (if not specified by user)
- [.autoload]boolean
= false
- If used, the database will automatically be loaded from the datafile
upon creation (you don't need to call loadDatabase
). Any command issued before load is finished is buffered and
will be executed when load is done. When autoloading is done, you can either use the onload
callback, or you can
use this.autoloadPromise
which resolves (or rejects) when autloading is done.
function
- If you use autoloading, this is the handler called after the loadDatabase
. It
takes one error
argument. If you use autoloading without specifying this handler, and an error happens during
load, an error will be thrown.
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 \n
character (or data will be lost).
function
- Inverse of afterSerialization
. Make sure to include both and not
just one, or you risk data loss. For the same reason, make sure both functions are inverses of one another. Some
failsafe mechanisms are in place to prevent data loss if you misuse the serialization hooks: NeDB checks that never
one is declared without the other, and checks that they are reverse of one another by testing on random strings of
various lengths. In addition, if too much data is detected as corrupt, NeDB will refuse to start as it could mean
you're not using the deserialization hook corresponding to the serialization hook used before.
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.
- [.compareStrings] [compareStrings
](#compareStrings) - If specified, it overrides default string comparison which is not
well adapted to non-US characters in particular accented letters. Native localCompare
will most of the time be
the right choice.
string
- Deprecated: if you are using NeDB from whithin a Node Webkit app,
specify its name (the same one you use in the package.json
) in this field and the filename
will be relative to
the directory Node Webkit uses to store the rest of the application's data (local storage etc.). It works on Linux,
OS X and Windows. Now that you can use require('nw.gui').App.dataPath
in Node Webkit to get the path to the data
directory for your application, you should not use this option anymore and it will be removed.
boolean
Determines if the Datastore
keeps data in-memory, or if it saves it in storage. Is not read after
instanciation.
Datastore
](#Datastore)
**Access**: protected
### datastore.autoload : boolean
Determines if the Datastore
should autoload the database upon instantiation. Is not read after instanciation.
Datastore
](#Datastore)
**Access**: protected
### datastore.timestampData : boolean
Determines if the Datastore
should add createdAt
and updatedAt
fields automatically if not set by the user.
Datastore
](#Datastore)
**Access**: protected
### datastore.filename : string
If null, it means inMemoryOnly
is true
. The filename
is the name given to the storage module. Is not read
after instanciation.
Datastore
](#Datastore)
**Access**: protected
### datastore.persistence : [Persistence
](#Persistence)
The Persistence
instance for this Datastore
.
Datastore
](#Datastore)
### datastore.executor : [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.
Datastore
](#Datastore)
**Access**: protected
### datastore.indexes : Object.<string, Index>
Indexed by field name, dot notation can be used. _id is always indexed and since _ids are generated randomly the underlying binary search tree is always well-balanced
**Kind**: instance property of [Datastore
](#Datastore)
**Access**: protected
### datastore.ttlIndexes : Object.<string, number>
Stores the time to live (TTL) of the indexes created. The key represents the field name, the value the number of seconds after which data with this index field should be removed.
**Kind**: instance property of [Datastore
](#Datastore)
**Access**: protected
### 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)
### datastore.compareStrings() : [compareStrings
](#compareStrings)
Overrides default string comparison which is not well adapted to non-US characters in particular accented
letters. Native localCompare
will most of the time be the right choice
Datastore
](#Datastore)
**Access**: protected
### datastore.loadDatabase(callback)
Load the database from the datafile, and trigger the execution of buffered commands if any.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- callback function
### datastore.loadDatabaseAsync() ⇒ Promise
Async version of [loadDatabase](#Datastore+loadDatabase).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#loadDatabase
### datastore.getAllData() ⇒ [Array.<document>
](#document)
Get an array of all the data in the database.
**Kind**: instance method of [Datastore
](#Datastore)
### datastore.resetIndexes(newData)
Reset all currently defined indexes.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- newData [document
](#document) | [?Array.<document>
](#document)
### 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
](#Datastore)
**Params**
- options object
- .fieldName string
- Name of the field to index. Use the dot notation to index a field in a nested document.
- [.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.
- [.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.
- [.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 expireAfterSeconds
. Documents where the indexed field is not specified or not a Date
object are ignored
NoParamCallback
](#NoParamCallback) - Callback, signature: err
### datastore.ensureIndexAsync(options) ⇒Promise.<void>
Async version of [ensureIndex](#Datastore+ensureIndex).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#ensureIndex
**Params**
- options object
- .fieldName string
- Name of the field to index. Use the dot notation to index a field in a nested document.
- [.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.
- [.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.
- [.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 expireAfterSeconds
. Documents where the indexed field is not specified or not a Date
object are ignored
Remove an index Previous versions said explicitly the callback was optional, it is now recommended setting one.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- 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
](#NoParamCallback) - Optional callback, signature: err
### datastore.removeIndexAsync(fieldName) ⇒Promise.<void>
Async version of [removeIndex](#Datastore+removeIndex).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#removeIndex
**Params**
- 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.addToIndexes(doc)Add one or several document(s) to all indexes.
This is an internal function.
**Kind**: instance method of [Datastore
](#Datastore)
**Access**: protected
**Params**
- doc [document
](#document)
### datastore.removeFromIndexes(doc)
Remove one or several document(s) from all indexes.
This is an internal function.
**Kind**: instance method of [Datastore
](#Datastore)
**Access**: protected
**Params**
- doc [document
](#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.
This is an internal function.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- oldDoc [document
](#document) | Array.<{oldDoc: document, newDoc: document}>
- Document to update, or an Array
of
{oldDoc, newDoc}
pairs.
document
](#document) - Document to replace the oldDoc with. If the first argument is an Array
of
{oldDoc, newDoc}
pairs, this second argument is ignored.
Return the list of candidates for a given query Crude implementation for now, we return the candidates given by the first usable index if any We try the following query types, in this order: basic match, $in match, comparison match One way to make it better would be to enable the use of multiple indexes if the first usable index returns too much data. I may do it in the future.
Returned candidates will be scanned to find and remove all expired documents
This is an internal function.
**Kind**: instance method of [Datastore
](#Datastore)
**Access**: protected
**Params**
- query [query
](#query)
- [dontExpireStaleDocs] boolean
| function
= false
- If true don't remove stale docs. Useful for the remove function which shouldn't be impacted by expirations. If argument is not given, it is used as the callback.
- callback [MultipleDocumentsCallback
](#MultipleDocumentsCallback) - Signature err, candidates
### datastore.getCandidatesAsync(query, [dontExpireStaleDocs]) ⇒Promise.<Array.<document>>
Async version of [getCandidates](#Datastore+getCandidates).
This is an internal function.
**Kind**: instance method of [Datastore
](#Datastore)
**Returns**: Promise.<Array.<document>>
- candidates
**Access**: protected **See**: Datastore#getCandidates **Params** - query [query
](#query)
- [dontExpireStaleDocs] boolean
= false
- If true don't remove stale docs. Useful for the remove function which shouldn't be impacted by expirations.
### datastore.insertAsync(newDoc) ⇒ [Promise.<document>
](#document)
Async version of [Datastore#insert](Datastore#insert).
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- newDoc [document
](#document) | [Array.<document>
](#document)
### datastore.count(query, [callback]) ⇒ Cursor.<number>
\| undefined
Count all documents matching the query.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- [callback] [countCallback
](#Datastore..countCallback) - If given, the function will return undefined, otherwise it will return the Cursor.
### datastore.countAsync(query) ⇒Cursor.<number>
Async version of [count](#Datastore+count).
**Kind**: instance method of [Datastore
](#Datastore)
**Returns**: Cursor.<number>
- count
**Params** - query [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
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- [projection] [projection
](#projection) | [MultipleDocumentsCallback
](#MultipleDocumentsCallback) = {}
- MongoDB-style projection. If not given, will be interpreted as the callback.
- [callback] [MultipleDocumentsCallback
](#MultipleDocumentsCallback) - Optional callback, signature: err, docs
### datastore.findAsync(query, [projection]) ⇒Cursor.<Array.<document>>
Async version of [find](#Datastore+find).
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- [projection] [projection
](#projection) = {}
- MongoDB-style projection
### datastore.findOne(query, [projection], [callback]) ⇒ [Cursor.<document>
](#document) \| undefined
Find one document matching the query.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- [projection] [projection
](#projection) | [SingleDocumentCallback
](#SingleDocumentCallback) = {}
- MongoDB-style projection
- [callback] [SingleDocumentCallback
](#SingleDocumentCallback) - Optional callback, signature: err, doc
### datastore.findOneAsync(query, projection) ⇒ [Cursor.<document>
](#document)
Async version of [findOne](#Datastore+findOne).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#findOne
**Params**
- query [query
](#query) - MongoDB-style query
- projection [projection
](#projection) - MongoDB-style projection
### datastore.update(query, update, [options|], [cb])Update all docs matching query.
**Kind**: instance method of [Datastore
](#Datastore)
**Params**
- query [query
](#query) - is the same kind of finding query you use with find
and findOne
document
](#document) | \*
- 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!). Using a new document will replace the
matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can
apply them to subdocs. Available field modifiers are $set
to change a field's value, $unset
to delete a field,
$inc
to increment a field's value and $min
/$max
to change field's value, only if provided value is
less/greater than current value. To work on arrays, you have $push
, $pop
, $addToSet
, $pull
, and the special
$each
and $slice
.
Object
| [updateCallback
](#Datastore..updateCallback) - Optional options
- [.multi]boolean
= false
- If true, can update multiple documents
- [.upsert]boolean
= false
- If true, can insert a new document corresponding to the update
rules if
your query
doesn't match anything. If your update
is a simple object with no modifiers, it is the inserted
document. In the other case, the query
is stripped from all operator recursively, and the update
is applied to
it.
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
](#Datastore..updateCallback) = () => {}
- Optional callback
### datastore.updateAsync(query, update, [options]) ⇒Promise.<{numAffected: number, affectedDocuments: (Array.<document>\|document\|null), upsert: boolean}>
Async version of [update](#Datastore+update).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#update
**Params**
- query [query
](#query) - is the same kind of finding query you use with find
and findOne
document
](#document) | \*
- 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!). Using a new document will replace the
matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can
apply them to subdocs. Available field modifiers are $set
to change a field's value, $unset
to delete a field,
$inc
to increment a field's value and $min
/$max
to change field's value, only if provided value is
less/greater than current value. To work on arrays, you have $push
, $pop
, $addToSet
, $pull
, and the special
$each
and $slice
.
Object
= {}
- Optional options
- [.multi]boolean
= false
- If true, can update multiple documents
- [.upsert]boolean
= false
- If true, can insert a new document corresponding to the update
rules if
your query
doesn't match anything. If your update
is a simple object with no modifiers, it is the inserted
document. In the other case, the query
is stripped from all operator recursively, and the update
is applied to
it.
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
](#Datastore)
**Params**
- query [query
](#query)
- [options] object
| [removeCallback
](#Datastore..removeCallback) = {}
- Optional options
- [.multi]boolean
= false
- If true, can update multiple documents
- [cb] [removeCallback
](#Datastore..removeCallback) = () => {}
- Optional callback
### 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
](#Datastore)
**Returns**: Promise.<number>
- How many documents were removed
**Params** - query [query
](#query)
- [options] object
= {}
- Optional 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
.
Datastore
](#Datastore)
### Datastore~countCallback : function
**Kind**: inner typedef of [Datastore
](#Datastore)
**Params**
- err Error
- count number
### Datastore~findOneCallback : function
**Kind**: inner typedef of [Datastore
](#Datastore)
**Params**
- err Error
- doc [document
](#document)
### Datastore~updateCallback : function
If update was an upsert, upsert
flag is set to true, affectedDocuments
can be one of the following:
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
](#Datastore)
**Params**
- err Error
- numAffected number
- affectedDocuments [?Array.<document>
](#document) | [document
](#document)
- upsert boolean
### Datastore~removeCallback : function
**Kind**: inner typedef of [Datastore
](#Datastore)
**Params**
- err Error
- numRemoved number