32 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
- .inMemoryOnly :
boolean
- .autoload :
boolean
- .timestampData :
boolean
- .filename :
string
- .persistence :
Persistence
- .executor :
Executor
- .indexes :
Object.<string, Index>
- .ttlIndexes :
Object.<string, number>
- .autoloadPromise :
Promise
- .compareStrings() :
compareStrings
- .loadDatabase(callback)
- .loadDatabaseAsync() ⇒
Promise
- .getAllData() ⇒
Array.<document>
- .resetIndexes(newData)
- .ensureIndex(options, callback)
- .ensureIndexAsync(options) ⇒
Promise.<void>
- .removeIndex(fieldName, callback)
- .removeIndexAsync(fieldName) ⇒
Promise.<void>
- .addToIndexes(doc)
- .removeFromIndexes(doc)
- .updateIndexes(oldDoc, [newDoc])
- .getCandidates(query, [dontExpireStaleDocs], callback)
- .getCandidatesAsync(query, [dontExpireStaleDocs]) ⇒
Promise.<Array.<document>>
- .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>
- "event:compaction.done"
- .inMemoryOnly :
- 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.
Params
- 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
. Giving a string is deprecated, and will be removed in the next major version.- [.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
~
which is used in the temporary files NeDB uses to perform crash-safe writes. - [.inMemoryOnly]
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 theonload
callback, or you can usethis.autoloadPromise
which resolves (or rejects) when autloading is done. - [.onload]
function
-If you use autoloading, this is the handler called after the
loadDatabase
. It takes oneerror
argument. If you use autoloading without specifying this handler, and an error happens during load, an error will be thrown. - [.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
\n
character (or data will be lost). - [.afterSerialization]
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. - [.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.
- [.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. - [.nodeWebkitAppName]
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 thefilename
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 userequire('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.
- [.filename]
datastore.inMemoryOnly : boolean
Determines if the Datastore
keeps data in-memory, or if it saves it in storage. Is not read after
instanciation.
Kind: instance property of Datastore
Access: protected
datastore.autoload : boolean
Determines if the Datastore
should autoload the database upon instantiation. Is not read after instanciation.
Kind: instance property of Datastore
Access: protected
datastore.timestampData : boolean
Determines if the Datastore
should add createdAt
and updatedAt
fields automatically if not set by the user.
Kind: instance property of 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.
Kind: instance property of Datastore
Access: protected
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
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
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
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.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
Kind: instance method of 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
Params
- callback
function
datastore.loadDatabaseAsync() ⇒ Promise
Async version of [loadDatabase](#Datastore+loadDatabase).
Kind: instance method of Datastore
See: Datastore#loadDatabase
datastore.getAllData() ⇒ Array.<document>
Get an array of all the data in the database.
Kind: instance method of Datastore
datastore.resetIndexes(newData)
Reset all currently defined indexes.
Kind: instance method of Datastore
Params
- newData
document
|?Array.<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
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 aDate
object are ignored
- .fieldName
- callback
NoParamCallback
-Callback, signature: err
datastore.ensureIndexAsync(options) ⇒ Promise.<void>
Async version of [ensureIndex](#Datastore+ensureIndex).
Kind: instance method of 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 aDate
object are ignored
- .fieldName
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
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
-Optional callback, signature: err
datastore.removeIndexAsync(fieldName) ⇒ Promise.<void>
Async version of [removeIndex](#Datastore+removeIndex).
Kind: instance method of 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
Access: protected
Params
- doc
document
datastore.removeFromIndexes(doc)
Remove one or several document(s) from all indexes.
This is an internal function.
Kind: instance method of Datastore
Access: protected
Params
- 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.
This is an internal function.
Kind: instance method of Datastore
Params
- oldDoc
document
|Array.<{oldDoc: document, newDoc: document}>
-Document to update, or an
Array
of{oldDoc, newDoc}
pairs. - [newDoc]
document
-Document to replace the oldDoc with. If the first argument is an
Array
of{oldDoc, newDoc}
pairs, this second argument is ignored.
datastore.getCandidates(query, [dontExpireStaleDocs], callback)
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
Access: protected
Params
- 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
-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
Returns: Promise.<Array.<document>>
-
candidates
Access: protected
See: Datastore#getCandidates
Params
- 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>
Async version of [Datastore#insert](Datastore#insert).
Kind: instance method of Datastore
Params
- newDoc
document
|Array.<document>
datastore.count(query, [callback]) ⇒ Cursor.<number>
| undefined
Count all documents matching the query.
Kind: instance method of Datastore
Params
- 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>
Async version of [count](#Datastore+count).
Kind: instance method of Datastore
Returns: Cursor.<number>
-
count
Params
- 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
Params
- 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>>
Async version of [find](#Datastore+find).
Kind: instance method of Datastore
Params
- 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
Params
- query
query
-MongoDB-style query
- [projection]
projection
|SingleDocumentCallback
= {}
-MongoDB-style projection
- [callback]
SingleDocumentCallback
-Optional callback, signature: err, doc
datastore.findOneAsync(query, projection) ⇒ Cursor.<document>
Async version of [findOne](#Datastore+findOne).
Kind: instance method of Datastore
See: Datastore#findOne
Params
- 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
Params
- query
query
-is the same kind of finding query you use with
find
andfindOne
- update
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
. - [options|]
Object
|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 yourquery
doesn't match anything. If yourupdate
is a simple object with no modifiers, it is the inserted document. In the other case, thequery
is stripped from all operator recursively, and theupdate
is applied to it. - [.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.
- [.multi]
- [cb]
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
See: Datastore#update
Params
- query
query
-is the same kind of finding query you use with
find
andfindOne
- update
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
. - [options]
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 yourquery
doesn't match anything. If yourupdate
is a simple object with no modifiers, it is the inserted document. In the other case, thequery
is stripped from all operator recursively, and theupdate
is applied to it. - [.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.
- [.multi]
datastore.remove(query, [options], [cb])
Remove all docs matching the query.
Kind: instance method of Datastore
Params
- query
query
- [options]
object
|removeCallback
= {}
-Optional options
- [.multi]
boolean
= false
-If true, can update multiple documents
- [.multi]
- [cb]
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
Returns: Promise.<number>
-
How many documents were removed
Params
- query
query
- [options]
object
= {}
-Optional options
- [.multi]
boolean
= false
-If true, can update multiple documents
- [.multi]
"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
Params
- err
Error
- count
number
Datastore~findOneCallback : function
Kind: inner typedef of Datastore
Params
- 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
Params
- err
Error
- numAffected
number
- affectedDocuments
?Array.<document>
|document
- upsert
boolean
Datastore~removeCallback : function
Kind: inner typedef of Datastore
Params
- err
Error
- numRemoved
number