## Classes
Promise
Manage access to data, be it to find, update or remove it.
It extends Promise
so that its methods (which return this
) are chainable & awaitable.
EventEmitter
The Datastore
class is the main class of NeDB.
Under the hood, NeDB's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the datafile, for performance reasons. The database is automatically compacted (i.e. put back in the one-line-per-document format) every time you load each database within your application.
You can manually call the compaction function
with yourDatabase.persistence.compactDatafile
which takes no argument. It
queues a compaction of the datafile in the executor, to be executed sequentially
after all pending operations. The datastore will fire a compaction.done
event
once compaction is finished.
You can also set automatic compaction at regular intervals
with yourDatabase.persistence.setAutocompactionInterval(interval)
, interval
in milliseconds (a minimum of 5s is enforced), and stop automatic compaction
with yourDatabase.persistence.stopAutocompaction()
.
Keep in mind that compaction takes a bit of time (not too much: 130ms for 50k records on a typical development machine) and no other operation can happen when it does, so most projects actually don't need to use it.
Compaction will also immediately remove any documents whose data line has become
corrupted, assuming that the total percentage of all corrupted documents in that
database still falls below the specified corruptAlertThreshold
option's value.
Durability works similarly to major databases: compaction forces the OS to
physically flush data to disk, while appends to the data file do not (the OS is
responsible for flushing the data). That guarantees that a server crash can
never cause complete data loss, while preserving performance. The worst that can
happen is a crash between two syncs, causing a loss of all data between the two
syncs. Usually syncs are 30 seconds appart so that's at most 30 seconds of
data. This post by Antirez on Redis persistence
explains this in more details, NeDB being very close to Redis AOF persistence
with appendfsync
option set to no
.
function
Callback with no parameter
number
String comparison function.
if (a < b) return -1
if (a > b) return 1
return 0
function
Callback that returns an Array of documents
function
Callback that returns a single document
Promise.<*>
Generic async function
function
Callback with generic parameters
Object.<string, *>
Generic document in NeDB. It consists of an Object with anything you want inside.
Object.<string, *>
Nedb query.
Each key of a query references a field name, which can use the dot-notation to reference subfields inside nested documents, arrays, arrays of subdocuments and to match a specific element of an array.
Each value of a query can be one of the following:
string
: matches all documents which have this string as value for the referenced field namenumber
: matches all documents which have this number as value for the referenced field nameRegexp
: matches all documents which have a value that matches the given Regexp
for the referenced field nameobject
: matches all documents which have this object as deep-value for the referenced field name{ field: { $op: value } }
where $op
is any comparison operator:
$lt
, $lte
: less than, less than or equal$gt
, $gte
: greater than, greater than or equal$in
: member of. value
must be an array of values$ne
, $nin
: not equal, not a member of$stat
: checks whether the document posses the property field
. value
should be true or false$regex
: checks whether a string is matched by the regular expression. Contrary to MongoDB, the use of
$options
with $regex
is not supported, because it doesn't give you more power than regex flags. Basic
queries are more readable so only use the $regex
operator when you need to use another operator with it$size
: if the referenced filed is an Array, matches on the size of the array$elemMatch
: matches if at least one array element matches the sub-query entirely$or
and $and
, the syntax is { $op: [query1, query2, ...] }
.$not
, the syntax is { $not: query }
$where
, the syntax is:{ $where: function () {
// object is 'this'
// return a boolean
} }
Object.<string, (0|1)>
Nedb projection.
You can give find
and findOne
an optional second argument, projections
.
The syntax is the same as MongoDB: { a: 1, b: 1 }
to return only the a
and b
fields, { a: 0, b: 0 }
to omit these two fields. You cannot use both
modes at the time, except for _id
which is by default always returned and
which you can choose to omit. You can project on nested documents.
To reference subfields, you can use the dot-notation.
string
The beforeDeserialization
and afterDeserialization
callbacks are hooks which are executed respectively before
parsing each document and after stringifying them. They can be used for example to encrypt the Datastore.
The beforeDeserialization
should revert what afterDeserialization
has done.
Promise
Manage access to data, be it to find, update or remove it.
It extends Promise
so that its methods (which return this
) are chainable & awaitable.
Promise
* [Cursor](#Cursor) ⇐ Promise
* [new Cursor(db, query, [mapFn])](#new_Cursor_new)
* _instance_
* [.db](#Cursor+db) : [Datastore
](#Datastore)
* [.query](#Cursor+query) : [query
](#query)
* [.limit(limit)](#Cursor+limit) ⇒ [Cursor
](#Cursor)
* [.skip(skip)](#Cursor+skip) ⇒ [Cursor
](#Cursor)
* [.sort(sortQuery)](#Cursor+sort) ⇒ [Cursor
](#Cursor)
* [.projection(projection)](#Cursor+projection) ⇒ [Cursor
](#Cursor)
* [.exec(_callback)](#Cursor+exec)
* [.execAsync()](#Cursor+execAsync) ⇒ Promise.<(Array.<document>\|\*)>
* _inner_
* [~mapFn](#Cursor..mapFn) ⇒ \*
\| Promise.<\*>
* [~execCallback](#Cursor..execCallback) : function
### new Cursor(db, query, [mapFn])
Create a new cursor for this collection
**Params** - db [Datastore
](#Datastore) - The datastore this cursor is bound to
- query [query
](#query) - The query this cursor will operate on
- [mapFn] [mapFn
](#Cursor..mapFn) - Handler to be executed after cursor has found the results and before the callback passed to find/findOne/update/remove
### cursor.db : [Datastore
](#Datastore)
**Kind**: instance property of [Cursor
](#Cursor)
**Access**: protected
### cursor.query : [query
](#query)
**Kind**: instance property of [Cursor
](#Cursor)
**Access**: protected
### cursor.limit(limit) ⇒ [Cursor
](#Cursor)
Set a limit to the number of results
**Kind**: instance method of [Cursor
](#Cursor)
**Params**
- limit Number
### cursor.skip(skip) ⇒ [Cursor
](#Cursor)
Skip a number of results
**Kind**: instance method of [Cursor
](#Cursor)
**Params**
- skip Number
### cursor.sort(sortQuery) ⇒ [Cursor
](#Cursor)
Sort results of the query
**Kind**: instance method of [Cursor
](#Cursor)
**Params**
- sortQuery Object.<string, number>
- sortQuery is { field: order }, field can use the dot-notation, order is 1 for ascending and -1 for descending
### cursor.projection(projection) ⇒ [Cursor
](#Cursor)
Add the use of a projection
**Kind**: instance method of [Cursor
](#Cursor)
**Params**
- projection Object.<string, number>
- MongoDB-style projection. {} means take all fields. Then it's { key1: 1, key2: 1 } to take only key1 and key2 { key1: 0, key2: 0 } to omit only key1 and key2. Except _id, you can't mix takes and omits.
### cursor.exec(_callback)Get all matching elements Will return pointers to matched elements (shallow copies), returning full copies is the role of find or findOne
**Kind**: instance method of [Cursor
](#Cursor)
**Params**
- _callback [execCallback
](#Cursor..execCallback)
### cursor.execAsync() ⇒ Promise.<(Array.<document>\|\*)>
Async version of [exec](#Cursor+exec).
**Kind**: instance method of [Cursor
](#Cursor)
**See**: Cursor#exec
### Cursor~mapFn ⇒ \*
\| Promise.<\*>
Has a callback
**Kind**: inner typedef of [Cursor
](#Cursor)
**Params**
- res [Array.<document>
](#document)
### Cursor~execCallback : function
**Kind**: inner typedef of [Cursor
](#Cursor)
**Params**
- err Error
- res [Array.<document>
](#document) | \*
- If an mapFn was given to the Cursor, then the type of this parameter is the one returned by the mapFn.
## Datastore ⇐ [EventEmitter
](http://nodejs.org/api/events.html)
The Datastore
class is the main class of NeDB.
EventEmitter
](http://nodejs.org/api/events.html)
**Emits**: Datastore#event:"compaction.done"
* [Datastore](#Datastore) ⇐ [EventEmitter
](http://nodejs.org/api/events.html)
* [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
](#new_Executor_new)
* [.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)
* [.ensureIndex(options, [callback])](#Datastore+ensureIndex)
* [.ensureIndexAsync(options)](#Datastore+ensureIndexAsync) ⇒ Promise.<void>
* [.removeIndex(fieldName, callback)](#Datastore+removeIndex)
* [.removeIndexAsync(fieldName)](#Datastore+removeIndexAsync) ⇒ Promise.<void>
* [.insert(newDoc, [callback])](#Datastore+insert)
* [.insertAsync(newDoc)](#Datastore+insertAsync) ⇒ Promise.<(document\|Array.<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](#Datastore+loadDatabase) or
[loadDatabaseAsync](#Datastore+loadDatabaseAsync) 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 it is called, so make sure to call it yourself or use the autoload
option.
Also, if loading fails, all commands registered to the [executor](#Datastore+executor) afterwards will not be executed. They will be registered and executed, in sequence, only after a successful loading.
**Params** - optionsobject
| 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. Not used if options.inMemoryOnly
is true
.
boolean
= false
- If set to true, no data will be written in storage. This option has
priority over options.filename
.
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.
NoParamCallback
](#NoParamCallback) - 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.
serializationHook
](#serializationHook) - 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).
serializationHook
](#serializationHook) - 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.
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
### neDB.autoload : boolean
Determines if the Datastore
should autoload the database upon instantiation. Is not read after instanciation.
Datastore
](#Datastore)
**Access**: protected
### neDB.timestampData : boolean
Determines if the Datastore
should add createdAt
and updatedAt
fields automatically if not set by the user.
Datastore
](#Datastore)
**Access**: protected
### neDB.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
### neDB.persistence : [Persistence
](#Persistence)
The Persistence
instance for this Datastore
.
Datastore
](#Datastore)
### neDB.executor : [Executor
](#new_Executor_new)
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
### neDB.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
### neDB.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
### neDB.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)
### neDB.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
### neDB.loadDatabase([callback])
Callback version of [loadDatabaseAsync](#Datastore+loadDatabaseAsync).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#loadDatabaseAsync
**Params**
- [callback] [NoParamCallback
](#NoParamCallback)
### neDB.loadDatabaseAsync() ⇒ Promise
Load the database from the datafile, and trigger the execution of buffered commands if any.
**Kind**: instance method of [Datastore
](#Datastore)
### neDB.getAllData() ⇒ [Array.<document>
](#document)
Get an array of all the data in the database.
**Kind**: instance method of [Datastore
](#Datastore)
### neDB.ensureIndex(options, [callback])
Callback version of [ensureIndex](#Datastore+ensureIndex).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#ensureIndex
**Params**
- options object
- .fieldName string
- [.unique] boolean
= false
- [.sparse] boolean
= false
- [.expireAfterSeconds] number
- [callback] [NoParamCallback
](#NoParamCallback)
### neDB.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.
**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.
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
### neDB.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.
### neDB.insert(newDoc, [callback])Callback version of [insertAsync](#Datastore+insertAsync).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#insertAsync
**Params**
- newDoc [document
](#document) | [Array.<document>
](#document)
- [callback] [SingleDocumentCallback
](#SingleDocumentCallback) | [MultipleDocumentsCallback
](#MultipleDocumentsCallback)
### neDB.insertAsync(newDoc) ⇒ Promise.<(document\|Array.<document>)>
Insert a new document, or new documents.
**Kind**: instance method of [Datastore
](#Datastore)
**Returns**: Promise.<(document\|Array.<document>)>
- The document(s) inserted.
**Params** - newDoc [document
](#document) | [Array.<document>
](#document) - Document or array of documents to insert.
### neDB.count(query, [callback]) ⇒Cursor.<number>
\| undefined
Callback-version of [countAsync](#Datastore+countAsync).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#countAsync
**Params**
- query [query
](#query)
- [callback] [countCallback
](#Datastore..countCallback)
### neDB.countAsync(query) ⇒ Cursor.<number>
Count all documents matching the query.
**Kind**: instance method of [Datastore
](#Datastore)
**Returns**: Cursor.<number>
- count
**Params** - query [query
](#query) - MongoDB-style query
### neDB.find(query, [projection], [callback]) ⇒Cursor.<Array.<document>>
\| undefined
Callback version of [findAsync](#Datastore+findAsync).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#findAsync
**Params**
- query [query
](#query)
- [projection] [projection
](#projection) | [MultipleDocumentsCallback
](#MultipleDocumentsCallback) = {}
- [callback] [MultipleDocumentsCallback
](#MultipleDocumentsCallback)
### neDB.findAsync(query, [projection]) ⇒ Cursor.<Array.<document>>
Find all documents matching the query.
We return the [Cursor](#Cursor) that the user can either await
directly or use to can [limit](#Cursor+limit) or
[skip](#Cursor+skip) before.
Datastore
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- [projection] [projection
](#projection) = {}
- MongoDB-style projection
### neDB.findOne(query, [projection], [callback]) ⇒ [Cursor.<document>
](#document) \| undefined
Callback version of [findOneAsync](#Datastore+findOneAsync).
**Kind**: instance method of [Datastore
](#Datastore)
**See**: Datastore#findOneAsync
**Params**
- query [query
](#query)
- [projection] [projection
](#projection) | [SingleDocumentCallback
](#SingleDocumentCallback) = {}
- [callback] [SingleDocumentCallback
](#SingleDocumentCallback)
### neDB.findOneAsync(query, projection) ⇒ [Cursor.<document>
](#document)
Find one document matching the query.
We return the [Cursor](#Cursor) that the user can either await
directly or use to can [skip](#Cursor+skip) before.
Datastore
](#Datastore)
**Params**
- query [query
](#query) - MongoDB-style query
- projection [projection
](#projection) - MongoDB-style projection
### neDB.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
### neDB.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.
### neDB.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
### neDB.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
## Persistence
Under the hood, NeDB's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the datafile, for performance reasons. The database is automatically compacted (i.e. put back in the one-line-per-document format) every time you load each database within your application.
You can manually call the compaction function
with yourDatabase.persistence.compactDatafile
which takes no argument. It
queues a compaction of the datafile in the executor, to be executed sequentially
after all pending operations. The datastore will fire a compaction.done
event
once compaction is finished.
You can also set automatic compaction at regular intervals
with yourDatabase.persistence.setAutocompactionInterval(interval)
, interval
in milliseconds (a minimum of 5s is enforced), and stop automatic compaction
with yourDatabase.persistence.stopAutocompaction()
.
Keep in mind that compaction takes a bit of time (not too much: 130ms for 50k records on a typical development machine) and no other operation can happen when it does, so most projects actually don't need to use it.
Compaction will also immediately remove any documents whose data line has become
corrupted, assuming that the total percentage of all corrupted documents in that
database still falls below the specified corruptAlertThreshold
option's value.
Durability works similarly to major databases: compaction forces the OS to
physically flush data to disk, while appends to the data file do not (the OS is
responsible for flushing the data). That guarantees that a server crash can
never cause complete data loss, while preserving performance. The worst that can
happen is a crash between two syncs, causing a loss of all data between the two
syncs. Usually syncs are 30 seconds appart so that's at most 30 seconds of
data. This post by Antirez on Redis persistence
explains this in more details, NeDB being very close to Redis AOF persistence
with appendfsync
option set to no
.
Promise.<void>
* [.compactDatafile([callback])](#Persistence+compactDatafile)
* [.compactDatafileAsync()](#Persistence+compactDatafileAsync)
* [.setAutocompactionInterval(interval)](#Persistence+setAutocompactionInterval)
* [.stopAutocompaction()](#Persistence+stopAutocompaction)
* [.persistNewStateAsync(newDocs)](#Persistence+persistNewStateAsync) ⇒ Promise
* [.treatRawData(rawData)](#Persistence+treatRawData) ⇒ Object
* [.treatRawStreamAsync(rawStream)](#Persistence+treatRawStreamAsync) ⇒ Promise.<{data: Array.<document>, indexes: Object.<string, rawIndex>}>
* [.loadDatabase(callback)](#Persistence+loadDatabase)
* [.loadDatabaseAsync()](#Persistence+loadDatabaseAsync) ⇒ Promise.<void>
* _static_
* [.ensureDirectoryExistsAsync(dir)](#Persistence.ensureDirectoryExistsAsync) ⇒ Promise.<void>
### new Persistence()
Create a new Persistence object for database options.db
**Params** - .db [Datastore
](#Datastore)
- [.corruptAlertThreshold] Number
- Optional, threshold after which an alert is thrown if too much data is corrupt
- [.beforeDeserialization] [serializationHook
](#serializationHook) - Hook you can use to transform data after it was serialized and before it is written to disk.
- [.afterSerialization] [serializationHook
](#serializationHook) - Inverse of afterSerialization
.
Promise.<void>
Persist cached database This serves as a compaction function since the cache always contains only the number of documents in the collection while the data file is append-only so it may grow larger
This is an internal function, use [compactDatafileAsync](#Persistence+compactDatafileAsync) which uses the [executor](#Datastore+executor).
**Kind**: instance method of [Persistence
](#Persistence)
**Access**: protected
### persistence.compactDatafile([callback])
Queue a rewrite of the datafile
**Kind**: instance method of [Persistence
](#Persistence)
**See**: Persistence#persistCachedDatabaseAsync
**Params**
- [callback] [NoParamCallback
](#NoParamCallback) = () => {}
### persistence.compactDatafileAsync()
Async version of [compactDatafile](#Persistence+compactDatafile).
**Kind**: instance method of [Persistence
](#Persistence)
**See**: Persistence#compactDatafile
### persistence.setAutocompactionInterval(interval)
Set automatic compaction every interval
ms
Persistence
](#Persistence)
**Params**
- interval Number
- in milliseconds, with an enforced minimum of 5000 milliseconds
### persistence.stopAutocompaction()Stop autocompaction (do nothing if automatic compaction was not running)
**Kind**: instance method of [Persistence
](#Persistence)
### persistence.persistNewStateAsync(newDocs) ⇒ Promise
Persist new state for the given newDocs (can be insertion, update or removal) Use an append-only format
Do not use directly, it should only used by a [Datastore](#Datastore) instance.
**Kind**: instance method of [Persistence
](#Persistence)
**Params**
- newDocs [Array.<document>
](#document) - Can be empty if no doc was updated/removed
### persistence.treatRawData(rawData) ⇒Object
From a database's raw data, return the corresponding machine understandable collection.
Do not use directly, it should only used by a [Datastore](#Datastore) instance.
**Kind**: instance method of [Persistence
](#Persistence)
**Access**: protected
**Params**
- rawData string
- database file
### persistence.treatRawStreamAsync(rawStream) ⇒Promise.<{data: Array.<document>, indexes: Object.<string, rawIndex>}>
From a database's raw data stream, return the corresponding machine understandable collection Is only used by a [Datastore](#Datastore) instance.
Is only used in the Node.js version, since [React-Native](module:storageReactNative) & [browser](module:storageBrowser) storage modules don't provide an equivalent of [readFileStream](#module_storage.readFileStream).
Do not use directly, it should only used by a [Datastore](#Datastore) instance.
**Kind**: instance method of [Persistence
](#Persistence)
**Access**: protected
**Params**
- rawStream Readable
### persistence.loadDatabase(callback)
Load the database
This means pulling data out of the data file or creating it if it doesn't exist Also, all data is persisted right away, which has the effect of compacting the database file This operation is very quick at startup for a big collection (60ms for ~10k docs)
Do not use directly as it does not use the [Executor](Datastore.executor), use [loadDatabase](#Datastore+loadDatabase) instead.
**Kind**: instance method of [Persistence
](#Persistence)
**Access**: protected
**Params**
- callback [NoParamCallback
](#NoParamCallback)
### persistence.loadDatabaseAsync() ⇒ Promise.<void>
Async version of [loadDatabase](#Persistence+loadDatabase)
**Kind**: instance method of [Persistence
](#Persistence)
**See**: Persistence#loadDatabase
### Persistence.ensureDirectoryExistsAsync(dir) ⇒ Promise.<void>
Check if a directory stat and create it on the fly if it is not the case.
**Kind**: static method of [Persistence
](#Persistence)
**Params**
- dir string
## NoParamCallback : function
Callback with no parameter
**Kind**: global typedef **Params** - errError
## compareStrings ⇒ number
String comparison function.
if (a < b) return -1
if (a > b) return 1
return 0
**Kind**: global typedef
**Params**
- a string
- b string
## MultipleDocumentsCallback : function
Callback that returns an Array of documents
**Kind**: global typedef **Params** - errError
- docs [Array.<document>
](#document)
## SingleDocumentCallback : function
Callback that returns a single document
**Kind**: global typedef **Params** - errError
- docs [document
](#document)
## AsyncFunction ⇒ Promise.<\*>
Generic async function
**Kind**: global typedef **Params** - ...args\*
## GenericCallback : function
Callback with generic parameters
**Kind**: global typedef **Params** - errError
- ...args \*
## document : Object.<string, \*>
Generic document in NeDB. It consists of an Object with anything you want inside.
**Kind**: global typedef **Properties** | Name | Type | Description | | --- | --- | --- | | [_id] |string
| Internal _id
of the document, which can be null
or undefined at some points (when not inserted yet for example).
Object.<string, \*>
Nedb query.
Each key of a query references a field name, which can use the dot-notation to reference subfields inside nested documents, arrays, arrays of subdocuments and to match a specific element of an array.
Each value of a query can be one of the following:
string
: matches all documents which have this string as value for the referenced field namenumber
: matches all documents which have this number as value for the referenced field nameRegexp
: matches all documents which have a value that matches the given Regexp
for the referenced field nameobject
: matches all documents which have this object as deep-value for the referenced field name{ field: { $op: value } }
where $op
is any comparison operator:
$lt
, $lte
: less than, less than or equal$gt
, $gte
: greater than, greater than or equal$in
: member of. value
must be an array of values$ne
, $nin
: not equal, not a member of$stat
: checks whether the document posses the property field
. value
should be true or false$regex
: checks whether a string is matched by the regular expression. Contrary to MongoDB, the use of
$options
with $regex
is not supported, because it doesn't give you more power than regex flags. Basic
queries are more readable so only use the $regex
operator when you need to use another operator with it$size
: if the referenced filed is an Array, matches on the size of the array$elemMatch
: matches if at least one array element matches the sub-query entirely$or
and $and
, the syntax is { $op: [query1, query2, ...] }
.$not
, the syntax is { $not: query }
$where
, the syntax is:{ $where: function () {
// object is 'this'
// return a boolean
} }
Object.<string, (0\|1)>
Nedb projection.
You can give find
and findOne
an optional second argument, projections
.
The syntax is the same as MongoDB: { a: 1, b: 1 }
to return only the a
and b
fields, { a: 0, b: 0 }
to omit these two fields. You cannot use both
modes at the time, except for _id
which is by default always returned and
which you can choose to omit. You can project on nested documents.
To reference subfields, you can use the dot-notation.
**Kind**: global typedef ## serializationHook ⇒string
The beforeDeserialization
and afterDeserialization
callbacks are hooks which are executed respectively before
parsing each document and after stringifying them. They can be used for example to encrypt the Datastore.
The beforeDeserialization
should revert what afterDeserialization
has done.
string
## rawIndex
**Kind**: global typedef
**Properties**
| Name | Type |
| --- | --- |
| fieldName | string
|
| [unique] | boolean
|
| [sparse] | boolean
|