update JSDoc, readme, remove jquery and jsdoc from dependencies, remove sources from jsdoc.conf.js which conflicts with --files from jsdoc2md, implement dropDatabase & tests

pull/11/head
Timothée Rebours 3 years ago
parent dbcc3647d1
commit a17662a66b
  1. 375
      API.md
  2. 53
      CHANGELOG.md
  3. 9
      README.md
  4. 5
      jsdoc.conf.js
  5. 28
      lib/cursor.js
  6. 232
      lib/datastore.js
  7. 93
      lib/persistence.js
  8. 38
      package-lock.json
  9. 5
      package.json
  10. 2
      test/db.async.test.js
  11. 2
      test/db.test.js
  12. 88
      test/persistence.async.test.js
  13. 2
      test/persistence.test.js
  14. 4
      test_lac/openFds.test.js

375
API.md

@ -12,16 +12,10 @@ 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 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 in the one-line-per-document format) every time you load each database within
your application.</p> your application.</p>
<p>You can manually call the compaction function <p>Persistence handles the compaction exposed in the Datastore [compactDatafileAsync](#Datastore+compactDatafileAsync),
with <code>yourDatabase.persistence.compactDatafile</code> which takes no argument. It [setAutocompactionInterval](#Datastore+setAutocompactionInterval).</p>
queues a compaction of the datafile in the executor, to be executed sequentially <p>Since version 3.0.0, using [Datastore.persistence](Datastore.persistence) methods manually is deprecated.</p>
after all pending operations. The datastore will fire a <code>compaction.done</code> event <p>Compaction takes a bit of time (not too much: 130ms for 50k
once compaction is finished.</p>
<p>You can also set automatic compaction at regular intervals
with <code>yourDatabase.persistence.setAutocompactionInterval(interval)</code>, <code>interval</code>
in milliseconds (a minimum of 5s is enforced), and stop automatic compaction
with <code>yourDatabase.persistence.stopAutocompaction()</code>.</p>
<p>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 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.</p> it does, so most projects actually don't need to use it.</p>
<p>Compaction will also immediately remove any documents whose data line has become <p>Compaction will also immediately remove any documents whose data line has become
@ -50,13 +44,13 @@ with <code>appendfsync</code> option set to <code>no</code>.</p></dd>
return 0 return 0
</code></pre></dd> </code></pre></dd>
<dt><a href="#MultipleDocumentsCallback">MultipleDocumentsCallback</a> : <code>function</code></dt> <dt><a href="#MultipleDocumentsCallback">MultipleDocumentsCallback</a> : <code>function</code></dt>
<dd><p>Callback that returns an Array of documents</p></dd> <dd><p>Callback that returns an Array of documents.</p></dd>
<dt><a href="#SingleDocumentCallback">SingleDocumentCallback</a> : <code>function</code></dt> <dt><a href="#SingleDocumentCallback">SingleDocumentCallback</a> : <code>function</code></dt>
<dd><p>Callback that returns a single document</p></dd> <dd><p>Callback that returns a single document.</p></dd>
<dt><a href="#AsyncFunction">AsyncFunction</a><code>Promise.&lt;*&gt;</code></dt> <dt><a href="#AsyncFunction">AsyncFunction</a><code>Promise.&lt;*&gt;</code></dt>
<dd><p>Generic async function</p></dd> <dd><p>Generic async function.</p></dd>
<dt><a href="#GenericCallback">GenericCallback</a> : <code>function</code></dt> <dt><a href="#GenericCallback">GenericCallback</a> : <code>function</code></dt>
<dd><p>Callback with generic parameters</p></dd> <dd><p>Callback with generic parameters.</p></dd>
<dt><a href="#document">document</a> : <code>Object.&lt;string, *&gt;</code></dt> <dt><a href="#document">document</a> : <code>Object.&lt;string, *&gt;</code></dt>
<dd><p>Generic document in NeDB. <dd><p>Generic document in NeDB.
It consists of an Object with anything you want inside.</p></dd> It consists of an Object with anything you want inside.</p></dd>
@ -140,7 +134,7 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="new_Cursor_new"></a> <a name="new_Cursor_new"></a>
### new Cursor(db, query, [mapFn]) ### new Cursor(db, query, [mapFn])
<p>Create a new cursor for this collection</p> <p>Create a new cursor for this collection.</p>
**Params** **Params**
@ -161,9 +155,10 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="Cursor+limit"></a> <a name="Cursor+limit"></a>
### cursor.limit(limit) ⇒ [<code>Cursor</code>](#Cursor) ### cursor.limit(limit) ⇒ [<code>Cursor</code>](#Cursor)
<p>Set a limit to the number of results</p> <p>Set a limit to the number of results for the given Cursor.</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**Returns**: [<code>Cursor</code>](#Cursor) - <p>the same instance of Cursor, (useful for chaining).</p>
**Params** **Params**
- limit <code>Number</code> - limit <code>Number</code>
@ -171,9 +166,10 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="Cursor+skip"></a> <a name="Cursor+skip"></a>
### cursor.skip(skip) ⇒ [<code>Cursor</code>](#Cursor) ### cursor.skip(skip) ⇒ [<code>Cursor</code>](#Cursor)
<p>Skip a number of results</p> <p>Skip a number of results for the given Cursor.</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**Returns**: [<code>Cursor</code>](#Cursor) - <p>the same instance of Cursor, (useful for chaining).</p>
**Params** **Params**
- skip <code>Number</code> - skip <code>Number</code>
@ -181,9 +177,10 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="Cursor+sort"></a> <a name="Cursor+sort"></a>
### cursor.sort(sortQuery) ⇒ [<code>Cursor</code>](#Cursor) ### cursor.sort(sortQuery) ⇒ [<code>Cursor</code>](#Cursor)
<p>Sort results of the query</p> <p>Sort results of the query for the given Cursor.</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**Returns**: [<code>Cursor</code>](#Cursor) - <p>the same instance of Cursor, (useful for chaining).</p>
**Params** **Params**
- sortQuery <code>Object.&lt;string, number&gt;</code> - <p>sortQuery is { field: order }, field can use the dot-notation, order is 1 for ascending and -1 for descending</p> - sortQuery <code>Object.&lt;string, number&gt;</code> - <p>sortQuery is { field: order }, field can use the dot-notation, order is 1 for ascending and -1 for descending</p>
@ -191,9 +188,10 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="Cursor+projection"></a> <a name="Cursor+projection"></a>
### cursor.projection(projection) ⇒ [<code>Cursor</code>](#Cursor) ### cursor.projection(projection) ⇒ [<code>Cursor</code>](#Cursor)
<p>Add the use of a projection</p> <p>Add the use of a projection to the given Cursor.</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**Returns**: [<code>Cursor</code>](#Cursor) - <p>the same instance of Cursor, (useful for chaining).</p>
**Params** **Params**
- projection <code>Object.&lt;string, number&gt;</code> - <p>MongoDB-style projection. {} means take all fields. Then it's { key1: 1, key2: 1 } to take only key1 and key2 - projection <code>Object.&lt;string, number&gt;</code> - <p>MongoDB-style projection. {} means take all fields. Then it's { key1: 1, key2: 1 } to take only key1 and key2
@ -202,10 +200,10 @@ The <code>beforeDeserialization</code> should revert what <code>afterDeserializa
<a name="Cursor+exec"></a> <a name="Cursor+exec"></a>
### cursor.exec(_callback) ### cursor.exec(_callback)
<p>Get all matching elements <p>Callback version of [exec](#Cursor+exec).</p>
Will return pointers to matched elements (shallow copies), returning full copies is the role of find or findOne</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**See**: Cursor#execAsync
**Params** **Params**
- _callback [<code>execCallback</code>](#Cursor..execCallback) - _callback [<code>execCallback</code>](#Cursor..execCallback)
@ -213,10 +211,10 @@ Will return pointers to matched elements (shallow copies), returning full copies
<a name="Cursor+execAsync"></a> <a name="Cursor+execAsync"></a>
### cursor.execAsync() ⇒ <code>Promise.&lt;(Array.&lt;document&gt;\|\*)&gt;</code> ### cursor.execAsync() ⇒ <code>Promise.&lt;(Array.&lt;document&gt;\|\*)&gt;</code>
<p>Async version of [exec](#Cursor+exec).</p> <p>Get all matching elements.
Will return pointers to matched elements (shallow copies), returning full copies is the role of [findAsync](#Datastore+findAsync) or [findOneAsync](#Datastore+findOneAsync).</p>
**Kind**: instance method of [<code>Cursor</code>](#Cursor) **Kind**: instance method of [<code>Cursor</code>](#Cursor)
**See**: Cursor#exec
<a name="Cursor..mapFn"></a> <a name="Cursor..mapFn"></a>
### Cursor~mapFn ⇒ <code>\*</code> \| <code>Promise.&lt;\*&gt;</code> ### Cursor~mapFn ⇒ <code>\*</code> \| <code>Promise.&lt;\*&gt;</code>
@ -234,7 +232,7 @@ Will return pointers to matched elements (shallow copies), returning full copies
**Params** **Params**
- err <code>Error</code> - err <code>Error</code>
- res [<code>Array.&lt;document&gt;</code>](#document) | <code>\*</code> - <p>If an mapFn was given to the Cursor, then the type of this parameter is the one returned by the mapFn.</p> - res [<code>Array.&lt;document&gt;</code>](#document) | <code>\*</code> - <p>If a mapFn was given to the Cursor, then the type of this parameter is the one returned by the mapFn.</p>
<a name="Datastore"></a> <a name="Datastore"></a>
@ -258,12 +256,18 @@ Will return pointers to matched elements (shallow copies), returning full copies
* [.ttlIndexes](#Datastore+ttlIndexes) : <code>Object.&lt;string, number&gt;</code> * [.ttlIndexes](#Datastore+ttlIndexes) : <code>Object.&lt;string, number&gt;</code>
* [.autoloadPromise](#Datastore+autoloadPromise) : <code>Promise</code> * [.autoloadPromise](#Datastore+autoloadPromise) : <code>Promise</code>
* [.compareStrings()](#Datastore+compareStrings) : [<code>compareStrings</code>](#compareStrings) * [.compareStrings()](#Datastore+compareStrings) : [<code>compareStrings</code>](#compareStrings)
* [.compactDatafileAsync()](#Datastore+compactDatafileAsync)
* [.compactDatafile([callback])](#Datastore+compactDatafile)
* [.setAutocompactionInterval(interval)](#Datastore+setAutocompactionInterval)
* [.stopAutocompaction()](#Datastore+stopAutocompaction)
* [.loadDatabase([callback])](#Datastore+loadDatabase) * [.loadDatabase([callback])](#Datastore+loadDatabase)
* [.dropDatabaseAsync()](#Datastore+dropDatabaseAsync) ⇒ <code>Promise</code>
* [.dropDatabase([callback])](#Datastore+dropDatabase)
* [.loadDatabaseAsync()](#Datastore+loadDatabaseAsync) ⇒ <code>Promise</code> * [.loadDatabaseAsync()](#Datastore+loadDatabaseAsync) ⇒ <code>Promise</code>
* [.getAllData()](#Datastore+getAllData) ⇒ [<code>Array.&lt;document&gt;</code>](#document) * [.getAllData()](#Datastore+getAllData) ⇒ [<code>Array.&lt;document&gt;</code>](#document)
* [.ensureIndex(options, [callback])](#Datastore+ensureIndex) * [.ensureIndex(options, [callback])](#Datastore+ensureIndex)
* [.ensureIndexAsync(options)](#Datastore+ensureIndexAsync) ⇒ <code>Promise.&lt;void&gt;</code> * [.ensureIndexAsync(options)](#Datastore+ensureIndexAsync) ⇒ <code>Promise.&lt;void&gt;</code>
* [.removeIndex(fieldName, callback)](#Datastore+removeIndex) * [.removeIndex(fieldName, [callback])](#Datastore+removeIndex)
* [.removeIndexAsync(fieldName)](#Datastore+removeIndexAsync) ⇒ <code>Promise.&lt;void&gt;</code> * [.removeIndexAsync(fieldName)](#Datastore+removeIndexAsync) ⇒ <code>Promise.&lt;void&gt;</code>
* [.insert(newDoc, [callback])](#Datastore+insert) * [.insert(newDoc, [callback])](#Datastore+insert)
* [.insertAsync(newDoc)](#Datastore+insertAsync) ⇒ <code>Promise.&lt;(document\|Array.&lt;document&gt;)&gt;</code> * [.insertAsync(newDoc)](#Datastore+insertAsync) ⇒ <code>Promise.&lt;(document\|Array.&lt;document&gt;)&gt;</code>
@ -273,7 +277,7 @@ Will return pointers to matched elements (shallow copies), returning full copies
* [.findAsync(query, [projection])](#Datastore+findAsync) ⇒ <code>Cursor.&lt;Array.&lt;document&gt;&gt;</code> * [.findAsync(query, [projection])](#Datastore+findAsync) ⇒ <code>Cursor.&lt;Array.&lt;document&gt;&gt;</code>
* [.findOne(query, [projection], [callback])](#Datastore+findOne) ⇒ [<code>Cursor.&lt;document&gt;</code>](#document) \| <code>undefined</code> * [.findOne(query, [projection], [callback])](#Datastore+findOne) ⇒ [<code>Cursor.&lt;document&gt;</code>](#document) \| <code>undefined</code>
* [.findOneAsync(query, projection)](#Datastore+findOneAsync) ⇒ [<code>Cursor.&lt;document&gt;</code>](#document) * [.findOneAsync(query, projection)](#Datastore+findOneAsync) ⇒ [<code>Cursor.&lt;document&gt;</code>](#document)
* [.update(query, update, [options|], [cb])](#Datastore+update) * [.update(query, update, [options|], [callback])](#Datastore+update)
* [.updateAsync(query, update, [options])](#Datastore+updateAsync) ⇒ <code>Promise.&lt;{numAffected: number, affectedDocuments: (Array.&lt;document&gt;\|document\|null), upsert: boolean}&gt;</code> * [.updateAsync(query, update, [options])](#Datastore+updateAsync) ⇒ <code>Promise.&lt;{numAffected: number, affectedDocuments: (Array.&lt;document&gt;\|document\|null), upsert: boolean}&gt;</code>
* [.remove(query, [options], [cb])](#Datastore+remove) * [.remove(query, [options], [cb])](#Datastore+remove)
* [.removeAsync(query, [options])](#Datastore+removeAsync) ⇒ <code>Promise.&lt;number&gt;</code> * [.removeAsync(query, [options])](#Datastore+removeAsync) ⇒ <code>Promise.&lt;number&gt;</code>
@ -370,9 +374,9 @@ after instanciation.</p>
<a name="Datastore+executor"></a> <a name="Datastore+executor"></a>
### neDB.executor : [<code>Executor</code>](#new_Executor_new) ### neDB.executor : [<code>Executor</code>](#new_Executor_new)
<p>The <code>Executor</code> instance for this <code>Datastore</code>. It is used in all methods exposed by the <code>Datastore</code>, any <code>Cursor</code> <p>The <code>Executor</code> instance for this <code>Datastore</code>. It is used in all methods exposed by the [Datastore](#Datastore),
produced by the <code>Datastore</code> and by <code>this.persistence.compactDataFile</code> &amp; <code>this.persistence.compactDataFileAsync</code> any [Cursor](#Cursor) produced by the <code>Datastore</code> and by [compactDatafileAsync](#Datastore+compactDatafileAsync) to ensure operations
to ensure operations are performed sequentially in the database.</p> are performed sequentially in the database.</p>
**Kind**: instance property of [<code>Datastore</code>](#Datastore) **Kind**: instance property of [<code>Datastore</code>](#Datastore)
**Access**: protected **Access**: protected
@ -407,6 +411,41 @@ letters. Native <code>localCompare</code> will most of the time be the right cho
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**Access**: protected **Access**: protected
<a name="Datastore+compactDatafileAsync"></a>
### neDB.compactDatafileAsync()
<p>Queue a compaction/rewrite of the datafile.
It works by rewriting the database file, and compacts it 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.</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
<a name="Datastore+compactDatafile"></a>
### neDB.compactDatafile([callback])
<p>Callback version of [compactDatafileAsync](#Datastore+compactDatafileAsync).</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#compactDatafileAsync
**Params**
- [callback] [<code>NoParamCallback</code>](#NoParamCallback) <code> = () &#x3D;&gt; {}</code>
<a name="Datastore+setAutocompactionInterval"></a>
### neDB.setAutocompactionInterval(interval)
<p>Set automatic compaction every <code>interval</code> ms</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
**Params**
- interval <code>Number</code> - <p>in milliseconds, with an enforced minimum of 5000 milliseconds</p>
<a name="Datastore+stopAutocompaction"></a>
### neDB.stopAutocompaction()
<p>Stop autocompaction (do nothing if automatic compaction was not running)</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
<a name="Datastore+loadDatabase"></a> <a name="Datastore+loadDatabase"></a>
### neDB.loadDatabase([callback]) ### neDB.loadDatabase([callback])
@ -418,6 +457,25 @@ letters. Native <code>localCompare</code> will most of the time be the right cho
- [callback] [<code>NoParamCallback</code>](#NoParamCallback) - [callback] [<code>NoParamCallback</code>](#NoParamCallback)
<a name="Datastore+dropDatabaseAsync"></a>
### neDB.dropDatabaseAsync() ⇒ <code>Promise</code>
<p>Stops auto-compaction, finishes all queued operations, drops the database both in memory and in storage.
<strong>WARNING</strong>: it is not recommended re-using an instance of NeDB if its database has been dropped, it is
preferable to instantiate a new one.</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
<a name="Datastore+dropDatabase"></a>
### neDB.dropDatabase([callback])
<p>Callback version of [dropDatabaseAsync](#Datastore+dropDatabaseAsync).</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#dropDatabaseAsync
**Params**
- [callback] [<code>NoParamCallback</code>](#NoParamCallback)
<a name="Datastore+loadDatabaseAsync"></a> <a name="Datastore+loadDatabaseAsync"></a>
### neDB.loadDatabaseAsync() ⇒ <code>Promise</code> ### neDB.loadDatabaseAsync() ⇒ <code>Promise</code>
@ -469,21 +527,20 @@ automatically remove documents when the system date becomes larger than the date
<a name="Datastore+removeIndex"></a> <a name="Datastore+removeIndex"></a>
### neDB.removeIndex(fieldName, callback) ### neDB.removeIndex(fieldName, [callback])
<p>Remove an index <p>Callback version of [removeIndexAsync](#Datastore+removeIndexAsync).</p>
Previous versions said explicitly the callback was optional, it is now recommended setting one.</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#removeIndexAsync
**Params** **Params**
- fieldName <code>string</code> - <p>Field name of the index to remove. Use the dot notation to remove an index referring to a - fieldName <code>string</code>
field in a nested document.</p> - [callback] [<code>NoParamCallback</code>](#NoParamCallback)
- callback [<code>NoParamCallback</code>](#NoParamCallback) - <p>Optional callback, signature: err</p>
<a name="Datastore+removeIndexAsync"></a> <a name="Datastore+removeIndexAsync"></a>
### neDB.removeIndexAsync(fieldName) ⇒ <code>Promise.&lt;void&gt;</code> ### neDB.removeIndexAsync(fieldName) ⇒ <code>Promise.&lt;void&gt;</code>
<p>Async version of [removeIndex](#Datastore+removeIndex).</p> <p>Remove an index.</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#removeIndex **See**: Datastore#removeIndex
@ -591,41 +648,46 @@ We return the [Cursor](#Cursor) that the user can either <code>await</code> dire
<a name="Datastore+update"></a> <a name="Datastore+update"></a>
### neDB.update(query, update, [options|], [cb]) ### neDB.update(query, update, [options|], [callback])
<p>Update all docs matching query.</p> <p>Callback version of [updateAsync](#Datastore+updateAsync).</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#updateAsync
**Params** **Params**
- query [<code>query</code>](#query) - <p>is the same kind of finding query you use with <code>find</code> and <code>findOne</code></p> - query [<code>query</code>](#query)
- update [<code>document</code>](#document) | <code>\*</code> - <p>specifies how the documents should be modified. It is either a new document or a - update [<code>document</code>](#document) | <code>\*</code>
set of modifiers (you cannot use both together, it doesn't make sense!). Using a new document will replace the - [options|] <code>Object</code> | [<code>updateCallback</code>](#Datastore..updateCallback)
matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can - [.multi] <code>boolean</code> <code> = false</code>
apply them to subdocs. Available field modifiers are <code>$set</code> to change a field's value, <code>$unset</code> to delete a field, - [.upsert] <code>boolean</code> <code> = false</code>
<code>$inc</code> to increment a field's value and <code>$min</code>/<code>$max</code> to change field's value, only if provided value is - [.returnUpdatedDocs] <code>boolean</code> <code> = false</code>
less/greater than current value. To work on arrays, you have <code>$push</code>, <code>$pop</code>, <code>$addToSet</code>, <code>$pull</code>, and the special - [callback] [<code>updateCallback</code>](#Datastore..updateCallback)
<code>$each</code> and <code>$slice</code>.</p>
- [options|] <code>Object</code> | [<code>updateCallback</code>](#Datastore..updateCallback) - <p>Optional options</p>
- [.multi] <code>boolean</code> <code> = false</code> - <p>If true, can update multiple documents</p>
- [.upsert] <code>boolean</code> <code> = false</code> - <p>If true, can insert a new document corresponding to the <code>update</code> rules if
your <code>query</code> doesn't match anything. If your <code>update</code> is a simple object with no modifiers, it is the inserted
document. In the other case, the <code>query</code> is stripped from all operator recursively, and the <code>update</code> is applied to
it.</p>
- [.returnUpdatedDocs] <code>boolean</code> <code> = false</code> - <p>(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.</p>
- [cb] [<code>updateCallback</code>](#Datastore..updateCallback) <code> = () &#x3D;&gt; {}</code> - <p>Optional callback</p>
<a name="Datastore+updateAsync"></a> <a name="Datastore+updateAsync"></a>
### neDB.updateAsync(query, update, [options]) ⇒ <code>Promise.&lt;{numAffected: number, affectedDocuments: (Array.&lt;document&gt;\|document\|null), upsert: boolean}&gt;</code> ### neDB.updateAsync(query, update, [options]) ⇒ <code>Promise.&lt;{numAffected: number, affectedDocuments: (Array.&lt;document&gt;\|document\|null), upsert: boolean}&gt;</code>
<p>Async version of [update](#Datastore+update).</p> <p>Update all docs matching query.</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#update **Returns**: <code>Promise.&lt;{numAffected: number, affectedDocuments: (Array.&lt;document&gt;\|document\|null), upsert: boolean}&gt;</code> - <ul>
<li><code>upsert</code> is <code>true</code> if and only if the update did insert a document, <strong>cannot be true if <code>options.upsert !== true</code></strong>.</li>
<li><code>numAffected</code> is the number of documents affected by the update or insertion (if <code>options.multi</code> is <code>false</code> or <code>options.upsert</code> is <code>true</code>, cannot exceed <code>1</code>);</li>
<li><code>affectedDocuments</code> can be one of the following:
<ul>
<li>If <code>upsert</code> is <code>true</code>, the inserted document;</li>
<li>If <code>options.returnUpdatedDocs</code> is <code>false</code>, <code>null</code>;</li>
<li>If <code>options.returnUpdatedDocs</code> is <code>true</code>:
<ul>
<li>If <code>options.multi</code> is <code>false</code>, the updated document;</li>
<li>If <code>options.multi</code> is <code>false</code>, the array of updated documents.</li>
</ul>
</li>
</ul>
</li>
</ul>
**Params** **Params**
- query [<code>query</code>](#query) - <p>is the same kind of finding query you use with <code>find</code> and <code>findOne</code></p> - query [<code>query</code>](#query) - <p>is the same kind of finding query you use with <code>find</code> and <code>findOne</code>.</p>
- update [<code>document</code>](#document) | <code>\*</code> - <p>specifies how the documents should be modified. It is either a new document or a - update [<code>document</code>](#document) | <code>\*</code> - <p>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 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 matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can
@ -646,27 +708,27 @@ if the update did not actually modify them.</p>
<a name="Datastore+remove"></a> <a name="Datastore+remove"></a>
### neDB.remove(query, [options], [cb]) ### neDB.remove(query, [options], [cb])
<p>Remove all docs matching the query.</p> <p>Callback version of [removeAsync](#Datastore+removeAsync).</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**See**: Datastore#removeAsync
**Params** **Params**
- query [<code>query</code>](#query) - query [<code>query</code>](#query)
- [options] <code>object</code> | [<code>removeCallback</code>](#Datastore..removeCallback) <code> = {}</code> - <p>Optional options</p> - [options] <code>object</code> | [<code>removeCallback</code>](#Datastore..removeCallback) <code> = {}</code>
- [.multi] <code>boolean</code> <code> = false</code> - <p>If true, can update multiple documents</p> - [.multi] <code>boolean</code> <code> = false</code>
- [cb] [<code>removeCallback</code>](#Datastore..removeCallback) <code> = () &#x3D;&gt; {}</code> - <p>Optional callback</p> - [cb] [<code>removeCallback</code>](#Datastore..removeCallback) <code> = () &#x3D;&gt; {}</code>
<a name="Datastore+removeAsync"></a> <a name="Datastore+removeAsync"></a>
### neDB.removeAsync(query, [options]) ⇒ <code>Promise.&lt;number&gt;</code> ### neDB.removeAsync(query, [options]) ⇒ <code>Promise.&lt;number&gt;</code>
<p>Remove all docs matching the query. <p>Remove all docs matching the query.</p>
Use Datastore.removeAsync which has the same signature</p>
**Kind**: instance method of [<code>Datastore</code>](#Datastore) **Kind**: instance method of [<code>Datastore</code>](#Datastore)
**Returns**: <code>Promise.&lt;number&gt;</code> - <p>How many documents were removed</p> **Returns**: <code>Promise.&lt;number&gt;</code> - <p>How many documents were removed</p>
**Params** **Params**
- query [<code>query</code>](#query) - query [<code>query</code>](#query) - <p>MongoDB-style query</p>
- [options] <code>object</code> <code> = {}</code> - <p>Optional options</p> - [options] <code>object</code> <code> = {}</code> - <p>Optional options</p>
- [.multi] <code>boolean</code> <code> = false</code> - <p>If true, can update multiple documents</p> - [.multi] <code>boolean</code> <code> = false</code> - <p>If true, can update multiple documents</p>
@ -674,13 +736,15 @@ Use Datastore.removeAsync which has the same signature</p>
### "event:compaction.done" ### "event:compaction.done"
<p>Compaction event. Happens when the Datastore's Persistence has been compacted. <p>Compaction event. Happens when the Datastore's Persistence has been compacted.
It happens when calling <code>datastore.persistence.compactDatafile</code>, which is called periodically if you have called It happens when calling [compactDatafileAsync](#Datastore+compactDatafileAsync), which is called periodically if you have called
<code>datastore.persistence.setAutocompactionInterval</code>.</p> [setAutocompactionInterval](#Datastore+setAutocompactionInterval).</p>
**Kind**: event emitted by [<code>Datastore</code>](#Datastore) **Kind**: event emitted by [<code>Datastore</code>](#Datastore)
<a name="Datastore..countCallback"></a> <a name="Datastore..countCallback"></a>
### Datastore~countCallback : <code>function</code> ### Datastore~countCallback : <code>function</code>
<p>Callback for [Datastore#countCallback](Datastore#countCallback).</p>
**Kind**: inner typedef of [<code>Datastore</code>](#Datastore) **Kind**: inner typedef of [<code>Datastore</code>](#Datastore)
**Params** **Params**
@ -699,22 +763,15 @@ It happens when calling <code>datastore.persistence.compactDatafile</code>, whic
<a name="Datastore..updateCallback"></a> <a name="Datastore..updateCallback"></a>
### Datastore~updateCallback : <code>function</code> ### Datastore~updateCallback : <code>function</code>
<p>If update was an upsert, <code>upsert</code> flag is set to true, <code>affectedDocuments</code> can be one of the following:</p> <p>See [updateAsync](#Datastore+updateAsync) return type for the definition of the callback parameters.</p>
<ul> <p><strong>WARNING:</strong> Prior to 3.0.0, <code>upsert</code> was either <code>true</code> of falsy (but not <code>false</code>), it is now always a boolean.
<li>For an upsert, the upserted document</li> <code>affectedDocuments</code> could be <code>undefined</code> when <code>returnUpdatedDocs</code> was <code>false</code>, it is now <code>null</code> in these cases.</p>
<li>For an update with returnUpdatedDocs option false, null</li> <p><strong>WARNING:</strong> Prior to 1.8.0, the <code>upsert</code> argument was not given, it was impossible for the developer to determine
<li>For an update with returnUpdatedDocs true and multi false, the updated document</li> during a <code>{ multi: false, returnUpdatedDocs: true, upsert: true }</code> update if it inserted a document or just updated
<li>For an update with returnUpdatedDocs true and multi true, the array of updated documents</li> it.</p>
</ul>
<p><strong>WARNING:</strong> 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.</p>
**Kind**: inner typedef of [<code>Datastore</code>](#Datastore) **Kind**: inner typedef of [<code>Datastore</code>](#Datastore)
**See**: {Datastore#updateAsync}
**Params** **Params**
- err <code>Error</code> - err <code>Error</code>
@ -739,16 +796,10 @@ 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 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 in the one-line-per-document format) every time you load each database within
your application.</p> your application.</p>
<p>You can manually call the compaction function <p>Persistence handles the compaction exposed in the Datastore [compactDatafileAsync](#Datastore+compactDatafileAsync),
with <code>yourDatabase.persistence.compactDatafile</code> which takes no argument. It [setAutocompactionInterval](#Datastore+setAutocompactionInterval).</p>
queues a compaction of the datafile in the executor, to be executed sequentially <p>Since version 3.0.0, using [Datastore.persistence](Datastore.persistence) methods manually is deprecated.</p>
after all pending operations. The datastore will fire a <code>compaction.done</code> event <p>Compaction takes a bit of time (not too much: 130ms for 50k
once compaction is finished.</p>
<p>You can also set automatic compaction at regular intervals
with <code>yourDatabase.persistence.setAutocompactionInterval(interval)</code>, <code>interval</code>
in milliseconds (a minimum of 5s is enforced), and stop automatic compaction
with <code>yourDatabase.persistence.stopAutocompaction()</code>.</p>
<p>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 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.</p> it does, so most projects actually don't need to use it.</p>
<p>Compaction will also immediately remove any documents whose data line has become <p>Compaction will also immediately remove any documents whose data line has become
@ -768,19 +819,9 @@ with <code>appendfsync</code> option set to <code>no</code>.</p>
* [Persistence](#Persistence) * [Persistence](#Persistence)
* [new Persistence()](#new_Persistence_new) * [new Persistence()](#new_Persistence_new)
* _instance_ * ~~[.compactDatafile([callback])](#Persistence+compactDatafile)~~
* [.persistCachedDatabaseAsync()](#Persistence+persistCachedDatabaseAsync) ⇒ <code>Promise.&lt;void&gt;</code> * ~~[.setAutocompactionInterval()](#Persistence+setAutocompactionInterval)~~
* [.compactDatafile([callback])](#Persistence+compactDatafile) * ~~[.stopAutocompaction()](#Persistence+stopAutocompaction)~~
* [.compactDatafileAsync()](#Persistence+compactDatafileAsync)
* [.setAutocompactionInterval(interval)](#Persistence+setAutocompactionInterval)
* [.stopAutocompaction()](#Persistence+stopAutocompaction)
* [.persistNewStateAsync(newDocs)](#Persistence+persistNewStateAsync) ⇒ <code>Promise</code>
* [.treatRawData(rawData)](#Persistence+treatRawData) ⇒ <code>Object</code>
* [.treatRawStreamAsync(rawStream)](#Persistence+treatRawStreamAsync) ⇒ <code>Promise.&lt;{data: Array.&lt;document&gt;, indexes: Object.&lt;string, rawIndex&gt;}&gt;</code>
* [.loadDatabase(callback)](#Persistence+loadDatabase)
* [.loadDatabaseAsync()](#Persistence+loadDatabaseAsync) ⇒ <code>Promise.&lt;void&gt;</code>
* _static_
* [.ensureDirectoryExistsAsync(dir)](#Persistence.ensureDirectoryExistsAsync) ⇒ <code>Promise.&lt;void&gt;</code>
<a name="new_Persistence_new"></a> <a name="new_Persistence_new"></a>
@ -794,127 +835,35 @@ with <code>appendfsync</code> option set to <code>no</code>.</p>
- [.beforeDeserialization] [<code>serializationHook</code>](#serializationHook) - <p>Hook you can use to transform data after it was serialized and before it is written to disk.</p> - [.beforeDeserialization] [<code>serializationHook</code>](#serializationHook) - <p>Hook you can use to transform data after it was serialized and before it is written to disk.</p>
- [.afterSerialization] [<code>serializationHook</code>](#serializationHook) - <p>Inverse of <code>afterSerialization</code>.</p> - [.afterSerialization] [<code>serializationHook</code>](#serializationHook) - <p>Inverse of <code>afterSerialization</code>.</p>
<a name="Persistence+persistCachedDatabaseAsync"></a> <a name="Persistence+compactDatafile"></a>
### persistence.persistCachedDatabaseAsync() ⇒ <code>Promise.&lt;void&gt;</code> ### ~~persistence.compactDatafile([callback])~~
<p>Persist cached database ***Deprecated***
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</p>
<p>This is an internal function, use [compactDatafileAsync](#Persistence+compactDatafileAsync) which uses the [executor](#Datastore+executor).</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence) **Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Access**: protected **See**
<a name="Persistence+compactDatafile"></a>
### persistence.compactDatafile([callback]) - Datastore#compactDatafile
<p>Queue a rewrite of the datafile</p> - Persistence#compactDatafileAsync
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**See**: Persistence#persistCachedDatabaseAsync
**Params** **Params**
- [callback] [<code>NoParamCallback</code>](#NoParamCallback) <code> = () &#x3D;&gt; {}</code> - [callback] [<code>NoParamCallback</code>](#NoParamCallback) <code> = () &#x3D;&gt; {}</code>
<a name="Persistence+compactDatafileAsync"></a>
### persistence.compactDatafileAsync()
<p>Async version of [compactDatafile](#Persistence+compactDatafile).</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**See**: Persistence#compactDatafile
<a name="Persistence+setAutocompactionInterval"></a> <a name="Persistence+setAutocompactionInterval"></a>
### persistence.setAutocompactionInterval(interval) ### ~~persistence.setAutocompactionInterval()~~
<p>Set automatic compaction every <code>interval</code> ms</p> ***Deprecated***
**Kind**: instance method of [<code>Persistence</code>](#Persistence) **Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Params** **See**: Datastore#setAutocompactionInterval
- interval <code>Number</code> - <p>in milliseconds, with an enforced minimum of 5000 milliseconds</p>
<a name="Persistence+stopAutocompaction"></a> <a name="Persistence+stopAutocompaction"></a>
### persistence.stopAutocompaction() ### ~~persistence.stopAutocompaction()~~
<p>Stop autocompaction (do nothing if automatic compaction was not running)</p> ***Deprecated***
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
<a name="Persistence+persistNewStateAsync"></a>
### persistence.persistNewStateAsync(newDocs) ⇒ <code>Promise</code>
<p>Persist new state for the given newDocs (can be insertion, update or removal)
Use an append-only format</p>
<p>Do not use directly, it should only used by a [Datastore](#Datastore) instance.</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence) **Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Params** **See**: Datastore#stopAutocompaction
- newDocs [<code>Array.&lt;document&gt;</code>](#document) - <p>Can be empty if no doc was updated/removed</p>
<a name="Persistence+treatRawData"></a>
### persistence.treatRawData(rawData) ⇒ <code>Object</code>
<p>From a database's raw data, return the corresponding machine understandable collection.</p>
<p>Do not use directly, it should only used by a [Datastore](#Datastore) instance.</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Access**: protected
**Params**
- rawData <code>string</code> - <p>database file</p>
<a name="Persistence+treatRawStreamAsync"></a>
### persistence.treatRawStreamAsync(rawStream) ⇒ <code>Promise.&lt;{data: Array.&lt;document&gt;, indexes: Object.&lt;string, rawIndex&gt;}&gt;</code>
<p>From a database's raw data stream, return the corresponding machine understandable collection
Is only used by a [Datastore](#Datastore) instance.</p>
<p>Is only used in the Node.js version, since [React-Native](module:storageReactNative) &amp;
[browser](module:storageBrowser) storage modules don't provide an equivalent of
[readFileStream](#module_storage.readFileStream).</p>
<p>Do not use directly, it should only used by a [Datastore](#Datastore) instance.</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Access**: protected
**Params**
- rawStream <code>Readable</code>
<a name="Persistence+loadDatabase"></a>
### persistence.loadDatabase(callback)
<p>Load the database</p>
<ol>
<li>Create all indexes</li>
<li>Insert all data</li>
<li>Compact the database</li>
</ol>
<p>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)</p>
<p>Do not use directly as it does not use the [Executor](Datastore.executor), use [loadDatabase](#Datastore+loadDatabase) instead.</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**Access**: protected
**Params**
- callback [<code>NoParamCallback</code>](#NoParamCallback)
<a name="Persistence+loadDatabaseAsync"></a>
### persistence.loadDatabaseAsync() ⇒ <code>Promise.&lt;void&gt;</code>
<p>Async version of [loadDatabase](#Persistence+loadDatabase)</p>
**Kind**: instance method of [<code>Persistence</code>](#Persistence)
**See**: Persistence#loadDatabase
<a name="Persistence.ensureDirectoryExistsAsync"></a>
### Persistence.ensureDirectoryExistsAsync(dir) ⇒ <code>Promise.&lt;void&gt;</code>
<p>Check if a directory stat and create it on the fly if it is not the case.</p>
**Kind**: static method of [<code>Persistence</code>](#Persistence)
**Params**
- dir <code>string</code>
<a name="NoParamCallback"></a> <a name="NoParamCallback"></a>
## NoParamCallback : <code>function</code> ## NoParamCallback : <code>function</code>
@ -943,7 +892,7 @@ This operation is very quick at startup for a big collection (60ms for ~10k docs
<a name="MultipleDocumentsCallback"></a> <a name="MultipleDocumentsCallback"></a>
## MultipleDocumentsCallback : <code>function</code> ## MultipleDocumentsCallback : <code>function</code>
<p>Callback that returns an Array of documents</p> <p>Callback that returns an Array of documents.</p>
**Kind**: global typedef **Kind**: global typedef
**Params** **Params**
@ -954,7 +903,7 @@ This operation is very quick at startup for a big collection (60ms for ~10k docs
<a name="SingleDocumentCallback"></a> <a name="SingleDocumentCallback"></a>
## SingleDocumentCallback : <code>function</code> ## SingleDocumentCallback : <code>function</code>
<p>Callback that returns a single document</p> <p>Callback that returns a single document.</p>
**Kind**: global typedef **Kind**: global typedef
**Params** **Params**
@ -965,7 +914,7 @@ This operation is very quick at startup for a big collection (60ms for ~10k docs
<a name="AsyncFunction"></a> <a name="AsyncFunction"></a>
## AsyncFunction ⇒ <code>Promise.&lt;\*&gt;</code> ## AsyncFunction ⇒ <code>Promise.&lt;\*&gt;</code>
<p>Generic async function</p> <p>Generic async function.</p>
**Kind**: global typedef **Kind**: global typedef
**Params** **Params**
@ -975,7 +924,7 @@ This operation is very quick at startup for a big collection (60ms for ~10k docs
<a name="GenericCallback"></a> <a name="GenericCallback"></a>
## GenericCallback : <code>function</code> ## GenericCallback : <code>function</code>
<p>Callback with generic parameters</p> <p>Callback with generic parameters.</p>
**Kind**: global typedef **Kind**: global typedef
**Params** **Params**

@ -8,18 +8,51 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.0.0] - Unreleased ## [3.0.0] - Unreleased
### Added ### Added
- Added an async interface for all functions - Added a `Promise`-based interface.
- The JSDoc is now much more exhaustive - The JSDoc is now much more exhaustive.
- Added markdown documentation generated from the JSDoc - An auto-generated JSDoc file is generated: [API.md](./API.md).
- Added `Datastore#dropDatabaseAsync` and its callback equivalent.
### Changed ### Changed
- All the functions are now async at the core, and a fully retro-compatible callback-ified version is exposed for the exposed functions. - The `Datastore#update`'s callback has its signature slightly changed. The
- The executor is now much simpler and Promise-based. A mostly retro-compatible shim is still exposed, with the exception that it no longer handles [`arguments`](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Functions/arguments) as the arguments Array. If you use the executor directly, you'll need to convert it to a proper Array beforehand. However [follicle@1.x](https://github.com/seald/follicle) is not compatible, please update to v2. `upsert` flag is always defined either at `true` or `false` but not `null` nor
- As a result, the `async` dependency has been removed completely. To avoid rewriting the tests, shims of some functions of `async` are defined in an utilities file used exclusively in the tests. `undefined`, and `affectedDocuments` is `null` when none is given rather than
- The `Datastore#update`'s callback has its signature slightly changed. The `upsert` flag is always defined either at `true` or `false` but not `null` nor `undefined`, and `affectedDocuments` is `null` when none is given rather than `undefined` (except when there is an error of course). `undefined` (except when there is an error of course).
- - In order to expose a `Promise`-based interface and to remove `async` from the dependencies, many internals have been either rewritten or removed:
### Deprecated - Datastore:
- Formally deprecate giving a string as argument to the `Datastore` constructor - `Datastore#getCandidates` replaced with `Datastore#_getCandidatesAsync`;
- `Datastore#resetIndexes` replaced with `Datastore#_resetIndexes`;
- `Datastore#addToIndexes` replaced with `Datastore#_addToIndexes`;
- `Datastore#removeFromIndexes` replaced with `Datastore#_removeFromIndexes`;
- `Datastore#updateIndexes` replaced with `Datastore#_updateIndexes`;
- `Datastore#_insert` replaced with `Datastore#_insertAsync`;
- `Datastore#_update` replaced with `Datastore#_updateAsync`;
- `Datastore#_remove` replaced with `Datastore#_removeAsync`;
- Persistence:
- `Persistence#loadDatabase` replaced with `Persistence#loadDatabaseAsync`;
- `Persistence#persistCachedDatabase` replaced with `Persistence#persistCachedDatabaseAsync`;
- `Persistence#persistNewState` replaced with `Persistence#persistNewStateAsync`;
- `Persistence#treatRawStream` replaced with `Persistence#treatRawStreamAsync`;
- `Persistence.ensureDirectoryExists` replaced with `Persistence#ensureDirectoryExistsAsync`;
- Cursor:
- `Cursor#_exec` replaced with `Cursor#_execAsync`;
- `Cursor#project` replaced with `Cursor#_project`;
- `Cursor#execFn` has been renamed to `Cursor#mapFn` and no longer supports a callback in its signature, it must be a synchronous function.
- Executor: it has been rewritten entirely without the `async`library.
- `Executor#buffer` & `Executor#queue` do not have the same signatures as before;
- `Executor#push` replaced with `Executor#pushAsync` which is substantially different;
- Storage modules : callback-based functions have been replaced with promise-based functions.
- Model module: it has been slightly re-written for clarity, but no changes in its interface was made.
## Deprecated
- Using a `string` in the constructor of NeDB is now deprecated.
- Using `Datastore#persistence#compactDatafile` is now deprecated, please use `Datastore#compactDatafile` instead.
- Using `Datastore#persistence#setAutocompactionInterval` is now deprecated, please use `Datastore#setAutocompactionInterval` instead.
- Using `Datastore#persistence#stopAutocompaction` is now deprecated, please use `Datastore#stopAutocompaction` instead.
## Removed
- The option for passing `options.nodeWebkitAppName` to the Datastore and the Persistence constructors has been removed.
- `Persistence.getNWAppFilename`;
## [2.2.1] - 2022-01-18 ## [2.2.1] - 2022-01-18

@ -30,13 +30,8 @@ const Datastore = require('@seald-io/nedb')
The API is a subset of MongoDB's API (the most used operations). The API is a subset of MongoDB's API (the most used operations).
### JSDoc ### JSDoc
You can read the markdown version of the JSDoc [in the docs directory](./docs). You can read the markdown version of the JSDoc [in the docs directory](./API.md).
It is generated by running `npm run generateDocs:markdown`. Some links don't It is generated by running `npm run generateDocs:markdown`.
work (when referencing items from other files), because I split manually the
documentation into several files. We should rewrite the links with a custom
configuration of [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown): PR welcome.
You can also generate an HTML version `npm run generateDocs:html`, links from the Readme won't work though.
### Promise-based interface vs callback-based interface ### Promise-based interface vs callback-based interface
Since version 3.0.0, NeDB provides a Promise-based equivalent for each function Since version 3.0.0, NeDB provides a Promise-based equivalent for each function

@ -1,8 +1,5 @@
'use strict' 'use strict'
module.exports = { module.exports = {
plugins: ['plugins/markdown'], plugins: ['plugins/markdown']
source: {
include: ['./lib', './browser-version/lib']
}
} }

@ -16,7 +16,7 @@ const { callbackify } = require('util')
*/ */
class Cursor { class Cursor {
/** /**
* Create a new cursor for this collection * Create a new cursor for this collection.
* @param {Datastore} db - The datastore this cursor is bound to * @param {Datastore} db - The datastore this cursor is bound to
* @param {query} query - The query this cursor will operate on * @param {query} query - The query this cursor will operate on
* @param {Cursor~mapFn} [mapFn] - Handler to be executed after cursor has found the results and before the callback passed to find/findOne/update/remove * @param {Cursor~mapFn} [mapFn] - Handler to be executed after cursor has found the results and before the callback passed to find/findOne/update/remove
@ -65,9 +65,9 @@ class Cursor {
} }
/** /**
* Set a limit to the number of results * Set a limit to the number of results for the given Cursor.
* @param {Number} limit * @param {Number} limit
* @return {Cursor} * @return {Cursor} the same instance of Cursor, (useful for chaining).
*/ */
limit (limit) { limit (limit) {
this._limit = limit this._limit = limit
@ -75,9 +75,9 @@ class Cursor {
} }
/** /**
* Skip a number of results * Skip a number of results for the given Cursor.
* @param {Number} skip * @param {Number} skip
* @return {Cursor} * @return {Cursor} the same instance of Cursor, (useful for chaining).
*/ */
skip (skip) { skip (skip) {
this._skip = skip this._skip = skip
@ -85,9 +85,9 @@ class Cursor {
} }
/** /**
* Sort results of the query * Sort results of the query for the given Cursor.
* @param {Object.<string, number>} sortQuery - sortQuery is { field: order }, field can use the dot-notation, order is 1 for ascending and -1 for descending * @param {Object.<string, number>} sortQuery - sortQuery is { field: order }, field can use the dot-notation, order is 1 for ascending and -1 for descending
* @return {Cursor} * @return {Cursor} the same instance of Cursor, (useful for chaining).
*/ */
sort (sortQuery) { sort (sortQuery) {
this._sort = sortQuery this._sort = sortQuery
@ -95,10 +95,10 @@ class Cursor {
} }
/** /**
* Add the use of a projection * Add the use of a projection to the given Cursor.
* @param {Object.<string, number>} projection - MongoDB-style projection. {} means take all fields. Then it's { key1: 1, key2: 1 } to take only key1 and key2 * @param {Object.<string, number>} projection - 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. * { key1: 0, key2: 0 } to omit only key1 and key2. Except _id, you can't mix takes and omits.
* @return {Cursor} * @return {Cursor} the same instance of Cursor, (useful for chaining).
*/ */
projection (projection) { projection (projection) {
this._projection = projection this._projection = projection
@ -211,23 +211,23 @@ class Cursor {
/** /**
* @callback Cursor~execCallback * @callback Cursor~execCallback
* @param {Error} err * @param {Error} err
* @param {document[]|*} res If an mapFn was given to the Cursor, then the type of this parameter is the one returned by the mapFn. * @param {document[]|*} res If a mapFn was given to the Cursor, then the type of this parameter is the one returned by the mapFn.
*/ */
/** /**
* Get all matching elements * Callback version of {@link Cursor#exec}.
* Will return pointers to matched elements (shallow copies), returning full copies is the role of find or findOne
* @param {Cursor~execCallback} _callback * @param {Cursor~execCallback} _callback
* @see Cursor#execAsync
*/ */
exec (_callback) { exec (_callback) {
callbackify(() => this.execAsync())(_callback) callbackify(() => this.execAsync())(_callback)
} }
/** /**
* Async version of {@link Cursor#exec}. * Get all matching elements.
* Will return pointers to matched elements (shallow copies), returning full copies is the role of {@link Datastore#findAsync} or {@link Datastore#findOneAsync}.
* @return {Promise<document[]|*>} * @return {Promise<document[]|*>}
* @async * @async
* @see Cursor#exec
*/ */
execAsync () { execAsync () {
return this.db.executor.pushAsync(() => this._execAsync()) return this.db.executor.pushAsync(() => this._execAsync())

@ -8,9 +8,6 @@ const model = require('./model.js')
const Persistence = require('./persistence.js') const Persistence = require('./persistence.js')
const { isDate } = require('./utils.js') const { isDate } = require('./utils.js')
// TODO: have one version of the documentation for each function
// TODO: update changelog
// TODO: dropDatabase callback + tests
/** /**
* Callback with no parameter * Callback with no parameter
* @callback NoParamCallback * @callback NoParamCallback
@ -31,28 +28,28 @@ const { isDate } = require('./utils.js')
*/ */
/** /**
* Callback that returns an Array of documents * Callback that returns an Array of documents.
* @callback MultipleDocumentsCallback * @callback MultipleDocumentsCallback
* @param {?Error} err * @param {?Error} err
* @param {?document[]} docs * @param {?document[]} docs
*/ */
/** /**
* Callback that returns a single document * Callback that returns a single document.
* @callback SingleDocumentCallback * @callback SingleDocumentCallback
* @param {?Error} err * @param {?Error} err
* @param {?document} docs * @param {?document} docs
*/ */
/** /**
* Generic async function * Generic async function.
* @callback AsyncFunction * @callback AsyncFunction
* @param {...*} args * @param {...*} args
* @return {Promise<*>} * @return {Promise<*>}
*/ */
/** /**
* Callback with generic parameters * Callback with generic parameters.
* @callback GenericCallback * @callback GenericCallback
* @param {?Error} err * @param {?Error} err
* @param {...*} args * @param {...*} args
@ -60,8 +57,8 @@ const { isDate } = require('./utils.js')
/** /**
* Compaction event. Happens when the Datastore's Persistence has been compacted. * 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 * It happens when calling {@link Datastore#compactDatafileAsync}, which is called periodically if you have called
* `datastore.persistence.setAutocompactionInterval`. * {@link Datastore#setAutocompactionInterval}.
* *
* @event Datastore#event:"compaction.done" * @event Datastore#event:"compaction.done"
* @type {undefined} * @type {undefined}
@ -267,9 +264,9 @@ class Datastore extends EventEmitter {
// This new executor is ready if we don't use persistence // This new executor is ready if we don't use persistence
// If we do, it will only be ready once loadDatabase is called // If we do, it will only be ready once loadDatabase is called
/** /**
* The `Executor` instance for this `Datastore`. It is used in all methods exposed by the `Datastore`, any `Cursor` * The `Executor` instance for this `Datastore`. It is used in all methods exposed by the {@link Datastore},
* produced by the `Datastore` and by `this.persistence.compactDataFile` & `this.persistence.compactDataFileAsync` * any {@link Cursor} produced by the `Datastore` and by {@link Datastore#compactDatafileAsync} to ensure operations
* to ensure operations are performed sequentially in the database. * are performed sequentially in the database.
* @type {Executor} * @type {Executor}
* @protected * @protected
*/ */
@ -310,6 +307,58 @@ class Datastore extends EventEmitter {
else throw err else throw err
}) })
} else this.autoloadPromise = null } else this.autoloadPromise = null
/**
* Interval if {@link Datastore#setAutocompactionInterval} was called.
* @private
* @type {null|number}
*/
this.autocompactionIntervalId = null
}
/**
* Queue a compaction/rewrite of the datafile.
* It works by rewriting the database file, and compacts it 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.
*
* @async
*/
compactDatafileAsync () {
return this.executor.pushAsync(() => this.persistence.persistCachedDatabaseAsync())
}
/**
* Callback version of {@link Datastore#compactDatafileAsync}.
* @param {NoParamCallback} [callback = () => {}]
* @see Datastore#compactDatafileAsync
*/
compactDatafile (callback) {
const promise = this.compactDatafileAsync()
if (typeof callback === 'function') callbackify(() => promise)(callback)
}
/**
* Set automatic compaction every `interval` ms
* @param {Number} interval in milliseconds, with an enforced minimum of 5000 milliseconds
*/
setAutocompactionInterval (interval) {
const minInterval = 5000
const realInterval = Math.max(interval || 0, minInterval)
this.stopAutocompaction()
this.autocompactionIntervalId = setInterval(() => {
this.compactDatafile()
}, realInterval)
}
/**
* Stop autocompaction (do nothing if automatic compaction was not running)
*/
stopAutocompaction () {
if (this.autocompactionIntervalId) {
clearInterval(this.autocompactionIntervalId)
this.autocompactionIntervalId = null
}
} }
/** /**
@ -322,6 +371,27 @@ class Datastore extends EventEmitter {
if (typeof callback === 'function') callbackify(() => promise)(callback) if (typeof callback === 'function') callbackify(() => promise)(callback)
} }
/**
* Stops auto-compaction, finishes all queued operations, drops the database both in memory and in storage.
* **WARNING**: it is not recommended re-using an instance of NeDB if its database has been dropped, it is
* preferable to instantiate a new one.
* @async
* @return {Promise}
*/
dropDatabaseAsync () {
return this.persistence.dropDatabaseAsync() // the executor is exceptionally used by Persistence
}
/**
* Callback version of {@link Datastore#dropDatabaseAsync}.
* @param {NoParamCallback} [callback]
* @see Datastore#dropDatabaseAsync
*/
dropDatabase (callback) {
const promise = this.dropDatabaseAsync()
if (typeof callback === 'function') callbackify(() => promise)(callback)
}
/** /**
* Load the database from the datafile, and trigger the execution of buffered commands if any. * Load the database from the datafile, and trigger the execution of buffered commands if any.
* @async * @async
@ -404,11 +474,10 @@ class Datastore extends EventEmitter {
} }
/** /**
* Remove an index * Callback version of {@link Datastore#removeIndexAsync}.
* Previous versions said explicitly the callback was optional, it is now recommended setting one. * @param {string} fieldName
* @param {string} fieldName Field name of the index to remove. Use the dot notation to remove an index referring to a * @param {NoParamCallback} [callback]
* field in a nested document. * @see Datastore#removeIndexAsync
* @param {NoParamCallback} callback Optional callback, signature: err
*/ */
removeIndex (fieldName, callback = () => {}) { removeIndex (fieldName, callback = () => {}) {
const promise = this.removeIndexAsync(fieldName) const promise = this.removeIndexAsync(fieldName)
@ -416,7 +485,7 @@ class Datastore extends EventEmitter {
} }
/** /**
* Async version of {@link Datastore#removeIndex}. * Remove an index.
* @param {string} fieldName Field name of the index to remove. Use the dot notation to remove an index referring to a * @param {string} fieldName Field name of the index to remove. Use the dot notation to remove an index referring to a
* field in a nested document. * field in a nested document.
* @return {Promise<void>} * @return {Promise<void>}
@ -701,6 +770,7 @@ class Datastore extends EventEmitter {
} }
/** /**
* Callback for {@link Datastore#countCallback}.
* @callback Datastore~countCallback * @callback Datastore~countCallback
* @param {?Error} err * @param {?Error} err
* @param {?number} count * @param {?number} count
@ -817,50 +887,35 @@ class Datastore extends EventEmitter {
} }
/** /**
* If update was an upsert, `upsert` flag is set to true, `affectedDocuments` can be one of the following: * See {@link Datastore#updateAsync} return type for the definition of the callback parameters.
* - For an upsert, the upserted document *
* - For an update with returnUpdatedDocs option false, null * **WARNING:** Prior to 3.0.0, `upsert` was either `true` of falsy (but not `false`), it is now always a boolean.
* - For an update with returnUpdatedDocs true and multi false, the updated document * `affectedDocuments` could be `undefined` when `returnUpdatedDocs` was `false`, it is now `null` in these cases.
* - For an update with returnUpdatedDocs true and multi true, the array of updated documents *
* **WARNING:** Prior to 1.8.0, the `upsert` argument was not given, it was impossible for the developer to determine
* during a `{ multi: false, returnUpdatedDocs: true, upsert: true }` update if it inserted a document or just updated
* it.
* *
* **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.
* @callback Datastore~updateCallback * @callback Datastore~updateCallback
* @param {?Error} err * @param {?Error} err
* @param {?number} numAffected * @param {number} numAffected
* @param {?document[]|?document} affectedDocuments * @param {?document[]|?document} affectedDocuments
* @param {?boolean} upsert * @param {boolean} upsert
* @see {Datastore#updateAsync}
*/ */
/** /**
* Update all docs matching query. * Version without the using {@link Datastore~executor} of {@link Datastore#updateAsync}, use it instead.
*
* Use {@link Datastore#updateAsync} which has the same signature.
* @param {query} query is the same kind of finding query you use with `find` and `findOne`
* @param {document|update} 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!). 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`.
* @param {Object} options options
* @param {boolean} [options.multi = false] If true, can update multiple documents
* @param {boolean} [options.upsert = 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.
* @param {boolean} [options.returnUpdatedDocs = 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.
* *
* @param {query} query
* @param {document|update} update
* @param {Object} options
* @param {boolean} [options.multi = false]
* @param {boolean} [options.upsert = false]
* @param {boolean} [options.returnUpdatedDocs = false]
* @return {Promise<{numAffected: number, affectedDocuments: document[]|document|null, upsert: boolean}>} * @return {Promise<{numAffected: number, affectedDocuments: document[]|document|null, upsert: boolean}>}
* @private * @private
* @see Datastore#updateAsync
*/ */
async _updateAsync (query, update, options) { async _updateAsync (query, update, options) {
const multi = options.multi !== undefined ? options.multi : false const multi = options.multi !== undefined ? options.multi : false
@ -927,42 +982,31 @@ class Datastore extends EventEmitter {
} }
/** /**
* Update all docs matching query. * Callback version of {@link Datastore#updateAsync}.
* @param {query} query is the same kind of finding query you use with `find` and `findOne` * @param {query} query
* @param {document|*} update specifies how the documents should be modified. It is either a new document or a * @param {document|*} update
* set of modifiers (you cannot use both together, it doesn't make sense!). Using a new document will replace the * @param {Object|Datastore~updateCallback} [options|]
* matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can * @param {boolean} [options.multi = false]
* apply them to subdocs. Available field modifiers are `$set` to change a field's value, `$unset` to delete a field, * @param {boolean} [options.upsert = false]
* `$inc` to increment a field's value and `$min`/`$max` to change field's value, only if provided value is * @param {boolean} [options.returnUpdatedDocs = false]
* less/greater than current value. To work on arrays, you have `$push`, `$pop`, `$addToSet`, `$pull`, and the special * @param {Datastore~updateCallback} [callback]
* `$each` and `$slice`. * @see Datastore#updateAsync
* @param {Object|Datastore~updateCallback} [options|] Optional options
* @param {boolean} [options.multi = false] If true, can update multiple documents
* @param {boolean} [options.upsert = 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.
* @param {boolean} [options.returnUpdatedDocs = 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.
* @param {Datastore~updateCallback} [cb = () => {}] Optional callback
* *
*/ */
update (query, update, options, cb) { update (query, update, options, callback) {
if (typeof options === 'function') { if (typeof options === 'function') {
cb = options callback = options
options = {} options = {}
} }
const callback = cb || (() => {})
const _callback = (err, res = {}) => { const _callback = (err, res = {}) => {
callback(err, res.numAffected, res.affectedDocuments, res.upsert) if (callback) callback(err, res.numAffected, res.affectedDocuments, res.upsert)
} }
callbackify((query, update, options) => this.updateAsync(query, update, options))(query, update, options, _callback) callbackify((query, update, options) => this.updateAsync(query, update, options))(query, update, options, _callback)
} }
/** /**
* Async version of {@link Datastore#update}. * Update all docs matching query.
* @param {query} query is the same kind of finding query you use with `find` and `findOne` * @param {query} query is the same kind of finding query you use with `find` and `findOne`.
* @param {document|*} update specifies how the documents should be modified. It is either a new document or a * @param {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!). Using a new document will replace the * 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 * matched docs. Using a set of modifiers will create the fields they need to modify if they don't exist, and you can
@ -979,9 +1023,16 @@ class Datastore extends EventEmitter {
* @param {boolean} [options.returnUpdatedDocs = false] (not Mongo-DB compatible) If true and update is not an upsert, * @param {boolean} [options.returnUpdatedDocs = 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 * 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. * if the update did not actually modify them.
* @async
* @return {Promise<{numAffected: number, affectedDocuments: document[]|document|null, upsert: boolean}>} * @return {Promise<{numAffected: number, affectedDocuments: document[]|document|null, upsert: boolean}>}
* @see Datastore#update * - `upsert` is `true` if and only if the update did insert a document, **cannot be true if `options.upsert !== true`**.
* - `numAffected` is the number of documents affected by the update or insertion (if `options.multi` is `false` or `options.upsert` is `true`, cannot exceed `1`);
* - `affectedDocuments` can be one of the following:
* - If `upsert` is `true`, the inserted document;
* - If `options.returnUpdatedDocs` is `false`, `null`;
* - If `options.returnUpdatedDocs` is `true`:
* - If `options.multi` is `false`, the updated document;
* - If `options.multi` is `false`, the array of updated documents.
* @async
*/ */
updateAsync (query, update, options = {}) { updateAsync (query, update, options = {}) {
return this.executor.pushAsync(() => this._updateAsync(query, update, options)) return this.executor.pushAsync(() => this._updateAsync(query, update, options))
@ -994,15 +1045,14 @@ class Datastore extends EventEmitter {
*/ */
/** /**
* Remove all docs matching the query. * Internal version without using the {@link Datastore#executor} of {@link Datastore#removeAsync}, use it instead.
* *
* Use {@link Datastore#removeAsync} which has the same signature.
* @param {query} query * @param {query} query
* @param {object} [options] Optional options * @param {object} [options]
* @param {boolean} [options.multi = false] If true, can update multiple documents * @param {boolean} [options.multi = false]
* @return {Promise<number>} How many documents were removed * @return {Promise<number>}
* @private * @private
* @see Datastore#_remove * @see Datastore#removeAsync
*/ */
async _removeAsync (query, options = {}) { async _removeAsync (query, options = {}) {
const multi = options.multi !== undefined ? options.multi : false const multi = options.multi !== undefined ? options.multi : false
@ -1024,11 +1074,12 @@ class Datastore extends EventEmitter {
} }
/** /**
* Remove all docs matching the query. * Callback version of {@link Datastore#removeAsync}.
* @param {query} query * @param {query} query
* @param {object|Datastore~removeCallback} [options={}] Optional options * @param {object|Datastore~removeCallback} [options={}]
* @param {boolean} [options.multi = false] If true, can update multiple documents * @param {boolean} [options.multi = false]
* @param {Datastore~removeCallback} [cb = () => {}] Optional callback * @param {Datastore~removeCallback} [cb = () => {}]
* @see Datastore#removeAsync
*/ */
remove (query, options, cb) { remove (query, options, cb) {
if (typeof options === 'function') { if (typeof options === 'function') {
@ -1041,8 +1092,7 @@ class Datastore extends EventEmitter {
/** /**
* Remove all docs matching the query. * Remove all docs matching the query.
* Use Datastore.removeAsync which has the same signature * @param {query} query MongoDB-style query
* @param {query} query
* @param {object} [options={}] Optional options * @param {object} [options={}] Optional options
* @param {boolean} [options.multi = false] If true, can update multiple documents * @param {boolean} [options.multi = false] If true, can update multiple documents
* @return {Promise<number>} How many documents were removed * @return {Promise<number>} How many documents were removed

@ -1,5 +1,5 @@
const path = require('path') const path = require('path')
const { callbackify } = require('util') const { deprecate } = require('util')
const byline = require('./byline') const byline = require('./byline')
const customUtils = require('./customUtils.js') const customUtils = require('./customUtils.js')
const Index = require('./indexes.js') const Index = require('./indexes.js')
@ -13,18 +13,12 @@ const storage = require('./storage.js')
* in the one-line-per-document format) every time you load each database within * in the one-line-per-document format) every time you load each database within
* your application. * your application.
* *
* You can manually call the compaction function * Persistence handles the compaction exposed in the Datastore {@link Datastore#compactDatafileAsync},
* with `yourDatabase.persistence.compactDatafile` which takes no argument. It * {@link Datastore#setAutocompactionInterval}.
* 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 * Since version 3.0.0, using {@link Datastore.persistence} methods manually is deprecated.
* 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 * 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 * 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. * it does, so most projects actually don't need to use it.
* *
@ -86,13 +80,9 @@ class Persistence {
} }
/** /**
* Persist cached database * Internal version without using the {@link Datastore#executor} of {@link Datastore#compactDatafileAsync}, use it instead.
* 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 {@link Persistence#compactDatafileAsync} which uses the [executor]{@link Datastore#executor}.
* @return {Promise<void>} * @return {Promise<void>}
* @protected * @private
*/ */
async persistCachedDatabaseAsync () { async persistCachedDatabaseAsync () {
const lines = [] const lines = []
@ -119,44 +109,29 @@ class Persistence {
} }
/** /**
* Queue a rewrite of the datafile * @see Datastore#compactDatafile
* @deprecated
* @param {NoParamCallback} [callback = () => {}] * @param {NoParamCallback} [callback = () => {}]
* @see Persistence#persistCachedDatabaseAsync * @see Persistence#compactDatafileAsync
*/ */
compactDatafile (callback) { compactDatafile (callback) {
if (typeof callback !== 'function') callback = () => {} deprecate(callback => this.db.compactDatafile(callback), '@seald-io/nedb: calling Datastore#persistence#compactDatafile is deprecated, please use Datastore#compactDatafile, it will be removed in the next major version.')(callback)
callbackify(() => this.compactDatafileAsync())(callback)
}
/**
* Async version of {@link Persistence#compactDatafile}.
* @async
* @see Persistence#compactDatafile
*/
compactDatafileAsync () {
return this.db.executor.pushAsync(() => this.persistCachedDatabaseAsync())
} }
/** /**
* Set automatic compaction every `interval` ms * @see Datastore#setAutocompactionInterval
* @param {Number} interval in milliseconds, with an enforced minimum of 5000 milliseconds * @deprecated
*/ */
setAutocompactionInterval (interval) { setAutocompactionInterval (interval) {
const minInterval = 5000 deprecate(interval => this.db.setAutocompactionInterval(interval), '@seald-io/nedb: calling Datastore#persistence#setAutocompactionInterval is deprecated, please use Datastore#setAutocompactionInterval, it will be removed in the next major version.')(interval)
const realInterval = Math.max(interval || 0, minInterval)
this.stopAutocompaction()
this.autocompactionIntervalId = setInterval(() => {
this.compactDatafile()
}, realInterval)
} }
/** /**
* Stop autocompaction (do nothing if automatic compaction was not running) * @see Datastore#stopAutocompaction
* @deprecated
*/ */
stopAutocompaction () { stopAutocompaction () {
if (this.autocompactionIntervalId) clearInterval(this.autocompactionIntervalId) deprecate(() => this.db.stopAutocompaction(), '@seald-io/nedb: calling Datastore#persistence#stopAutocompaction is deprecated, please use Datastore#stopAutocompaction, it will be removed in the next major version.')()
} }
/** /**
@ -166,6 +141,7 @@ class Persistence {
* Do not use directly, it should only used by a {@link Datastore} instance. * Do not use directly, it should only used by a {@link Datastore} instance.
* @param {document[]} newDocs Can be empty if no doc was updated/removed * @param {document[]} newDocs Can be empty if no doc was updated/removed
* @return {Promise} * @return {Promise}
* @private
*/ */
async persistNewStateAsync (newDocs) { async persistNewStateAsync (newDocs) {
let toPersist = '' let toPersist = ''
@ -195,7 +171,7 @@ class Persistence {
* Do not use directly, it should only used by a {@link Datastore} instance. * Do not use directly, it should only used by a {@link Datastore} instance.
* @param {string} rawData database file * @param {string} rawData database file
* @return {{data: document[], indexes: Object.<string, rawIndex>}} * @return {{data: document[], indexes: Object.<string, rawIndex>}}
* @protected * @private
*/ */
treatRawData (rawData) { treatRawData (rawData) {
const data = rawData.split('\n') const data = rawData.split('\n')
@ -241,7 +217,7 @@ class Persistence {
* @param {Readable} rawStream * @param {Readable} rawStream
* @return {Promise<{data: document[], indexes: Object.<string, rawIndex>}>} * @return {Promise<{data: document[], indexes: Object.<string, rawIndex>}>}
* @async * @async
* @protected * @private
*/ */
treatRawStreamAsync (rawStream) { treatRawStreamAsync (rawStream) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -299,18 +275,9 @@ class Persistence {
* Also, all data is persisted right away, which has the effect of compacting the database file * 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) * 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]{@link Datastore.executor}, use {@link Datastore#loadDatabase} instead. * Do not use directly as it does not use the [Executor]{@link Datastore.executor}, use {@link Datastore#loadDatabaseAsync} instead.
* @param {NoParamCallback} callback
* @protected
*/
loadDatabase (callback = () => {}) {
callbackify(this.loadDatabaseAsync.bind(this))(err => callback(err))
}
/**
* Async version of {@link Persistence#loadDatabase}
* @return {Promise<void>} * @return {Promise<void>}
* @see Persistence#loadDatabase * @private
*/ */
async loadDatabaseAsync () { async loadDatabaseAsync () {
this.db._resetIndexes() this.db._resetIndexes()
@ -347,8 +314,15 @@ class Persistence {
this.db.executor.processBuffer() this.db.executor.processBuffer()
} }
/**
* See {@link Datastore#dropDatabaseAsync}. This function uses {@link Datastore#executor} internally. Decorating this
* function with an {@link Executor#pushAsync} will result in a deadlock.
* @return {Promise<void>}
* @private
* @see Datastore#dropDatabaseAsync
*/
async dropDatabaseAsync () { async dropDatabaseAsync () {
this.stopAutocompaction() // stop autocompaction this.db.stopAutocompaction() // stop autocompaction
this.db.executor.ready = false // prevent queuing new tasks this.db.executor.ready = false // prevent queuing new tasks
this.db.executor.resetBuffer() // remove pending buffered tasks this.db.executor.resetBuffer() // remove pending buffered tasks
await this.db.executor.queue.guardian // wait for the ongoing tasks to end await this.db.executor.queue.guardian // wait for the ongoing tasks to end
@ -360,13 +334,18 @@ class Persistence {
this.db.ttlIndexes = {} this.db.ttlIndexes = {}
// remove datastore file // remove datastore file
await this.db.executor(() => storage.unlinkAsync(this.filename), true) if (!this.db.inMemoryOnly) {
await this.db.executor.pushAsync(async () => {
if (await storage.existsAsync(this.filename)) await storage.unlinkAsync(this.filename)
}, true)
}
} }
/** /**
* Check if a directory stat and create it on the fly if it is not the case. * Check if a directory stat and create it on the fly if it is not the case.
* @param {string} dir * @param {string} dir
* @return {Promise<void>} * @return {Promise<void>}
* @private
*/ */
static async ensureDirectoryExistsAsync (dir) { static async ensureDirectoryExistsAsync (dir) {
await storage.mkdirAsync(dir, { recursive: true }) await storage.mkdirAsync(dir, { recursive: true })

38
package-lock.json generated

@ -20,8 +20,6 @@
"commander": "^7.2.0", "commander": "^7.2.0",
"events": "^3.3.0", "events": "^3.3.0",
"jest": "^27.3.1", "jest": "^27.3.1",
"jquery": "^3.6.0",
"jsdoc": "^3.6.7",
"jsdoc-to-markdown": "^7.1.0", "jsdoc-to-markdown": "^7.1.0",
"karma": "^6.3.2", "karma": "^6.3.2",
"karma-chai": "^0.1.0", "karma-chai": "^0.1.0",
@ -5877,9 +5875,9 @@
} }
}, },
"node_modules/engine.io": { "node_modules/engine.io": {
"version": "6.1.0", "version": "6.1.1",
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.1.0.tgz", "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.1.1.tgz",
"integrity": "sha512-ErhZOVu2xweCjEfYcTdkCnEYUiZgkAcBBAhW4jbIvNG8SLU3orAqoJCiytZjYF7eTpVmmCrLDjLIEaPlUAs1uw==", "integrity": "sha512-AyMc20q8JUUdvKd46+thc9o7yCZ6iC6MoBCChG5Z1XmFMpp+2+y/oKvwpZTUJB0KCjxScw1dV9c2h5pjiYBLuQ==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@types/cookie": "^0.4.1", "@types/cookie": "^0.4.1",
@ -7463,9 +7461,9 @@
} }
}, },
"node_modules/follow-redirects": { "node_modules/follow-redirects": {
"version": "1.14.6", "version": "1.14.7",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.6.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz",
"integrity": "sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==", "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==",
"dev": true, "dev": true,
"funding": [ "funding": [
{ {
@ -9376,12 +9374,6 @@
"@sideway/pinpoint": "^2.0.0" "@sideway/pinpoint": "^2.0.0"
} }
}, },
"node_modules/jquery": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz",
"integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==",
"dev": true
},
"node_modules/js-tokens": { "node_modules/js-tokens": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
@ -21488,9 +21480,9 @@
} }
}, },
"engine.io": { "engine.io": {
"version": "6.1.0", "version": "6.1.1",
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.1.0.tgz", "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.1.1.tgz",
"integrity": "sha512-ErhZOVu2xweCjEfYcTdkCnEYUiZgkAcBBAhW4jbIvNG8SLU3orAqoJCiytZjYF7eTpVmmCrLDjLIEaPlUAs1uw==", "integrity": "sha512-AyMc20q8JUUdvKd46+thc9o7yCZ6iC6MoBCChG5Z1XmFMpp+2+y/oKvwpZTUJB0KCjxScw1dV9c2h5pjiYBLuQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@types/cookie": "^0.4.1", "@types/cookie": "^0.4.1",
@ -22704,9 +22696,9 @@
"peer": true "peer": true
}, },
"follow-redirects": { "follow-redirects": {
"version": "1.14.6", "version": "1.14.7",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.6.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.7.tgz",
"integrity": "sha512-fhUl5EwSJbbl8AR+uYL2KQDxLkdSjZGR36xy46AO7cOMTrCMON6Sa28FmAnC2tRTDbd/Uuzz3aJBv7EBN7JH8A==", "integrity": "sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==",
"dev": true "dev": true
}, },
"for-in": { "for-in": {
@ -24131,12 +24123,6 @@
"@sideway/pinpoint": "^2.0.0" "@sideway/pinpoint": "^2.0.0"
} }
}, },
"jquery": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.6.0.tgz",
"integrity": "sha512-JVzAR/AjBvVt2BmYhxRCSYysDsPcssdmTFnzyLEts9qNwmjmu4JTAMYubEfwVOSwpQ1I1sKKFcxhZCI2buerfw==",
"dev": true
},
"js-tokens": { "js-tokens": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",

@ -52,8 +52,6 @@
"commander": "^7.2.0", "commander": "^7.2.0",
"events": "^3.3.0", "events": "^3.3.0",
"jest": "^27.3.1", "jest": "^27.3.1",
"jquery": "^3.6.0",
"jsdoc": "^3.6.7",
"jsdoc-to-markdown": "^7.1.0", "jsdoc-to-markdown": "^7.1.0",
"karma": "^6.3.2", "karma": "^6.3.2",
"karma-chai": "^0.1.0", "karma-chai": "^0.1.0",
@ -87,8 +85,7 @@
"test:react-native": "jest test/react-native", "test:react-native": "jest test/react-native",
"test:typings": "ts-node ./typings-tests.ts", "test:typings": "ts-node ./typings-tests.ts",
"prepublishOnly": "npm run build:browser", "prepublishOnly": "npm run build:browser",
"generateDocs:markdown": "jsdoc2md --no-cache -c jsdoc.conf.js --param-list-format list --files . > API.md", "generateDocs:markdown": "jsdoc2md --no-cache -c jsdoc.conf.js --param-list-format list --files ./lib/*.js > API.md"
"generateDocs:html": "jsdoc -c jsdoc.conf.js -d docs-html --readme README.md"
}, },
"main": "index.js", "main": "index.js",
"browser": { "browser": {

@ -368,7 +368,7 @@ describe('Database async', function () {
await wait(101) await wait(101)
const doc2 = await d.findOneAsync({}) const doc2 = await d.findOneAsync({})
assert.equal(doc2, null) assert.equal(doc2, null)
await d.persistence.compactDatafileAsync() await d.compactDatafileAsync()
// After compaction, no more mention of the document, correctly removed // After compaction, no more mention of the document, correctly removed
const datafileContents = await fs.readFile(testDb, 'utf8') const datafileContents = await fs.readFile(testDb, 'utf8')
assert.equal(datafileContents.split('\n').length, 2) assert.equal(datafileContents.split('\n').length, 2)

@ -610,7 +610,7 @@ describe('Database', function () {
}) })
}) })
d.persistence.compactDatafile() d.compactDatafile()
}) })
}, 101) }, 101)
}) })

@ -10,6 +10,7 @@ const Persistence = require('../lib/persistence')
const storage = require('../lib/storage') const storage = require('../lib/storage')
const { execFile, fork } = require('child_process') const { execFile, fork } = require('child_process')
const { promisify } = require('util') const { promisify } = require('util')
const { ensureFileDoesntExistAsync } = require('../lib/storage')
const Readable = require('stream').Readable const Readable = require('stream').Readable
describe('Persistence async', function () { describe('Persistence async', function () {
@ -381,7 +382,7 @@ describe('Persistence async', function () {
resolve() resolve()
}) })
}) })
await d.persistence.compactDatafileAsync() await d.compactDatafileAsync()
await compacted // should already be resolved when the function returns, but still awaiting for it await compacted // should already be resolved when the function returns, but still awaiting for it
}) })
@ -898,4 +899,89 @@ describe('Persistence async', function () {
assert.equal(await exists('workspace/existing'), false) assert.equal(await exists('workspace/existing'), false)
}) })
}) // ==== End of 'ensureFileDoesntExist' ==== }) // ==== End of 'ensureFileDoesntExist' ====
describe('dropDatabase', function () {
it('deletes data in memory', async () => {
const inMemoryDB = new Datastore({ inMemoryOnly: true })
await inMemoryDB.insertAsync({ hello: 'world' })
await inMemoryDB.dropDatabaseAsync()
assert.equal(inMemoryDB.getAllData().length, 0)
})
it('deletes data in memory & on disk', async () => {
await d.insertAsync({ hello: 'world' })
await d.dropDatabaseAsync()
assert.equal(d.getAllData().length, 0)
assert.equal(await exists(testDb), false)
})
it('check that executor is drained before drop', async () => {
for (let i = 0; i < 100; i++) {
d.insertAsync({ hello: 'world' }) // no await
}
await d.dropDatabaseAsync() // it should await the end of the inserts
assert.equal(d.getAllData().length, 0)
assert.equal(await exists(testDb), false)
})
it('check that autocompaction is stopped', async () => {
d.setAutocompactionInterval(5000)
await d.insertAsync({ hello: 'world' })
await d.dropDatabaseAsync()
assert.equal(d.autocompactionIntervalId, null)
assert.equal(d.getAllData().length, 0)
assert.equal(await exists(testDb), false)
})
it('check that we can reload and insert afterwards', async () => {
await d.insertAsync({ hello: 'world' })
await d.dropDatabaseAsync()
assert.equal(d.getAllData().length, 0)
assert.equal(await exists(testDb), false)
await d.loadDatabaseAsync()
await d.insertAsync({ hello: 'world' })
assert.equal(d.getAllData().length, 1)
await d.compactDatafileAsync()
assert.equal(await exists(testDb), true)
})
it('check that we can dropDatatabase if the file is already deleted', async () => {
await ensureFileDoesntExistAsync(testDb)
assert.equal(await exists(testDb), false)
await d.dropDatabaseAsync()
assert.equal(await exists(testDb), false)
})
it('Check that TTL indexes are reset', async () => {
await d.ensureIndexAsync({ fieldName: 'expire', expireAfterSeconds: 10 })
const date = new Date()
await d.insertAsync({ hello: 'world', expire: new Date(date.getTime() - 1000 * 20) }) // expired by 10 seconds
assert.equal((await d.findAsync({})).length, 0) // the TTL makes it so that the document is not returned
await d.dropDatabaseAsync()
assert.equal(d.getAllData().length, 0)
assert.equal(await exists(testDb), false)
await d.loadDatabaseAsync()
await d.insertAsync({ hello: 'world', expire: new Date(date.getTime() - 1000 * 20) })
assert.equal((await d.findAsync({})).length, 1) // the TTL index should have been removed
await d.compactDatafileAsync()
assert.equal(await exists(testDb), true)
})
it('Check that the buffer is reset', async () => {
await d.dropDatabaseAsync()
// these 3 will hang until load
d.insertAsync({ hello: 'world' })
d.insertAsync({ hello: 'world' })
d.insertAsync({ hello: 'world' })
assert.equal(d.getAllData().length, 0)
await d.dropDatabaseAsync()
d.insertAsync({ hi: 'world' })
await d.loadDatabaseAsync() // will trigger the buffer execution
assert.equal(d.getAllData().length, 1)
assert.equal(d.getAllData()[0].hi, 'world')
})
}) // ==== End of 'dropDatabase' ====
}) })

@ -446,7 +446,7 @@ describe('Persistence', function () {
done() done()
}) })
d.persistence.compactDatafile() d.compactDatafile()
}) })
describe('Serialization hooks', function () { describe('Serialization hooks', function () {

@ -32,7 +32,7 @@ const test = async () => {
filehandles.push(filehandle) filehandles.push(filehandle)
} }
} catch (error) { } catch (error) {
console.error(`An unexpected error occurred when opening file not too many times at i: ${i} with error: ${error}`) console.error(`An unexpected error occurred when opening file not too many times with error: ${error}`)
process.exit(1) process.exit(1)
} finally { } finally {
for (const filehandle of filehandles) { for (const filehandle of filehandles) {
@ -50,7 +50,7 @@ const test = async () => {
await db.persistence.persistCachedDatabaseAsync() await db.persistence.persistCachedDatabaseAsync()
} }
} catch (error) { } catch (error) {
console.error(`Got unexpected error during one persistence operation at ${i}: with error: ${error}`) console.error(`Got unexpected error during one persistence operation with error: ${error}`)
} }
} }
try { try {

Loading…
Cancel
Save