## Classes
- Cursor
- Datastore
## Cursor
**Kind**: global class
* [Cursor](#Cursor)
* [.sort()](#Cursor+sort) ⇒ [Cursor
](#Cursor)
* [.skip()](#Cursor+skip) ⇒ [Cursor
](#Cursor)
* [.limit()](#Cursor+limit) ⇒ [Cursor
](#Cursor)
* [.project()](#Cursor+project) ⇒ [Cursor
](#Cursor)
* [.exec()](#Cursor+exec) ⇒ Promise.<Array.<Object>>
* [.then(fulfilled, [rejected])](#Cursor+then) ⇒ Promise
* [.catch(rejected)](#Cursor+catch) ⇒ Promise
### cursor.sort() ⇒ [Cursor
](#Cursor)
Sort the queried documents.
See: https://github.com/louischatriot/nedb#sorting-and-paginating
**Kind**: instance method of [Cursor
](#Cursor)
### cursor.skip() ⇒ [Cursor
](#Cursor)
Skip some of the queried documents.
See: https://github.com/louischatriot/nedb#sorting-and-paginating
**Kind**: instance method of [Cursor
](#Cursor)
### cursor.limit() ⇒ [Cursor
](#Cursor)
Limit the queried documents.
See: https://github.com/louischatriot/nedb#sorting-and-paginating
**Kind**: instance method of [Cursor
](#Cursor)
### cursor.project() ⇒ [Cursor
](#Cursor)
Set the document projection.
See: https://github.com/louischatriot/nedb#projections
**Kind**: instance method of [Cursor
](#Cursor)
### cursor.exec() ⇒ Promise.<Array.<Object>>
Execute the cursor.
Since the Cursor has a `then` and a `catch` method
JavaScript identifies it as a thenable object
thus you can await it in async functions.
**Kind**: instance method of [Cursor
](#Cursor)
**Example**
```js
// in an async function
await datastore.find(...)
.sort(...)
.limit(...)
```
**Example**
```js
// the previous is the same as:
await datastore.find(...)
.sort(...)
.limit(...)
.exec()
```
### cursor.then(fulfilled, [rejected]) ⇒ Promise
Execute the cursor and set promise callbacks.
For more information visit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
**Kind**: instance method of [Cursor
](#Cursor)
Param | Type |
fulfilled | function |
[rejected] | function |
### cursor.catch(rejected) ⇒ Promise
Execute the cursor and set promise error callback.
For more information visit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
**Kind**: instance method of [Cursor
](#Cursor)
Param | Type |
rejected | function |
## Datastore
**Kind**: global class
**Summary**: As of v2.0.0 the Datastore class extends node's built
in EventEmitter class and implements each method as an event
plus additional error events. It also inherits the `compaction.done`
event from nedb but for consistency, in this library the event
was renamed to `compactionDone`.
All event callbacks will be passed the same type of values,
the first being the datastore, then the operation result (if there is any)
and then the arguments of the called method. (Check out the first example!)
All events have a matching error event that goes by the name of `${method}Error`,
for example `findError` or `loadError`. The callbacks of these events will receive
the same parameters as the normal event handlers except that instead of the
operation result there will be an operation error. (Check out the second example!)
A generic `__error__` event is also available. This event will be emitted at any of
the above error events. The callbacks of this event will receive the same parameters
as the specific error event handlers except that there will be one more parameter
passed between the datastore and the error object, that being the name of the method
that failed. (Check out the third example!)
* [Datastore](#Datastore)
* [new Datastore([pathOrOptions])](#new_Datastore_new)
* _instance_
* [.load()](#Datastore+load) ⇒ Promise.<undefined>
* [.find([query], [projection])](#Datastore+find) ⇒ [Cursor
](#Cursor)
* [.findOne([query], [projection])](#Datastore+findOne) ⇒ [Cursor
](#Cursor)
* [.insert(docs)](#Datastore+insert) ⇒ Promise.<(Object\|Array.<Object>)>
* [.insertOne(doc)](#Datastore+insertOne) ⇒ Promise.<Object>
* [.insertMany(docs)](#Datastore+insertMany) ⇒ Promise.<Array.<Object>>
* [.update(query, update, [options])](#Datastore+update) ⇒ Promise.<(number\|Object\|Array.<Object>)>
* [.updateOne(query, update, [options])](#Datastore+updateOne) ⇒ Promise.<(number\|Object)>
* [.updateMany(query, update, [options])](#Datastore+updateMany) ⇒ Promise.<(number\|Array.<Object>)>
* [.remove([query], [options])](#Datastore+remove) ⇒ Promise.<number>
* [.removeOne([query], [options])](#Datastore+removeOne) ⇒ Promise.<number>
* [.removeMany([query], [options])](#Datastore+removeMany) ⇒ Promise.<number>
* [.deleteOne([query], [options])](#Datastore+deleteOne) ⇒ Promise.<number>
* [.deleteMany([query], [options])](#Datastore+deleteMany) ⇒ Promise.<number>
* [.count([query])](#Datastore+count) ⇒ [Cursor
](#Cursor)
* [.ensureIndex(options)](#Datastore+ensureIndex) ⇒ Promise.<undefined>
* [.removeIndex(field)](#Datastore+removeIndex) ⇒ Promise.<undefined>
* _static_
* [.create([pathOrOptions])](#Datastore.create) ⇒ Proxy.<static>
### new Datastore([pathOrOptions])
Datastore constructor...
You should use `Datastore.create(...)` instead
of `new Datastore(...)`. With that you can access
the original datastore's properties such as `datastore.persistence`.
Create a Datastore instance.
Note that the datastore will be created
relative to `process.cwd()`
(unless an absolute path was passed).
It's basically the same as the original:
https://github.com/louischatriot/nedb#creatingloading-a-database
Param | Type |
[pathOrOptions] | string | Object |
**Example**
```js
let datastore = Datastore.create()
datastore.on('update', (datastore, result, query, update, options) => {
})
datastore.on('load', (datastore) => {
// this event doesn't have a result
})
datastore.on('ensureIndex', (datastore, options) => {
// this event doesn't have a result
// but it has the options argument which will be passed to the
// event handlers
})
datastore.on('compactionDone', (datastore) => {
// inherited from nedb's compaction.done event
})
```
**Example**
```js
let datastore = Datastore.create()
datastore.on('updateError', (datastore, error, query, update, options) => {
})
datastore.on('loadError', (datastore, error) => {
})
datastore.on('ensureIndexError', (datastore, error, options) => {
})
```
**Example**
```js
let datastore = Datastore.create()
datastore.on('__error__', (datastore, event, error, ...args) => {
// for example
// datastore, 'find', error, [{ foo: 'bar' }, {}]
})
```
### datastore.load() ⇒ Promise.<undefined>
Load the datastore.
Note that you don't necessarily have to call
this method to load the datastore as it will
automatically be called and awaited on any
operation issued against the datastore
(i.e.: `find`, `findOne`, etc.).
**Kind**: instance method of [Datastore
](#Datastore)
### datastore.find([query], [projection]) ⇒ [Cursor
](#Cursor)
Find documents that match the specified `query`.
It's basically the same as the original:
https://github.com/louischatriot/nedb#finding-documents
There are differences minor in how the cursor works though.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[projection] | Object |
**Example**
```js
datastore.find({ ... }).sort({ ... }).exec().then(...)
```
**Example**
```js
datastore.find({ ... }).sort({ ... }).then(...)
```
**Example**
```js
// in an async function
await datastore.find({ ... }).sort({ ... })
```
### datastore.findOne([query], [projection]) ⇒ [Cursor
](#Cursor)
Find a document that matches the specified `query`.
It's basically the same as the original:
https://github.com/louischatriot/nedb#finding-documents
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[projection] | Object |
**Example**
```js
datastore.findOne({ ... }).then(...)
```
**Example**
```js
// in an async function
await datastore.findOne({ ... }).sort({ ... })
```
### datastore.insert(docs) ⇒ Promise.<(Object\|Array.<Object>)>
Insert a document or documents.
It's basically the same as the original:
https://github.com/louischatriot/nedb#inserting-documents
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
docs | Object | Array.<Object> |
### datastore.insertOne(doc) ⇒ Promise.<Object>
Insert a single document.
This is just an alias for `insert` with object destructuring
to ensure a single document.
**Kind**: instance method of [Datastore
](#Datastore)
### datastore.insertMany(docs) ⇒ Promise.<Array.<Object>>
Insert multiple documents.
This is just an alias for `insert` with array destructuring
to ensure multiple documents.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
docs | Array.<Object> |
### datastore.update(query, update, [options]) ⇒ Promise.<(number\|Object\|Array.<Object>)>
Update documents that match the specified `query`.
It's basically the same as the original:
https://github.com/louischatriot/nedb#updating-documents
If you set `options.returnUpdatedDocs`,
the returned promise will resolve with
an object (if `options.multi` is `false`) or
with an array of objects.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
query | Object |
update | Object |
[options] | Object |
### datastore.updateOne(query, update, [options]) ⇒ Promise.<(number\|Object)>
Update a single document that matches the specified `query`.
This is just an alias for `update` with `options.multi` set to `false`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
query | Object |
update | Object |
[options] | Object |
### datastore.updateMany(query, update, [options]) ⇒ Promise.<(number\|Array.<Object>)>
Update multiple documents that match the specified `query`.
This is just an alias for `update` with `options.multi` set to `true`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
query | Object |
update | Object |
[options] | Object |
### datastore.remove([query], [options]) ⇒ Promise.<number>
Remove documents that match the specified `query`.
It's basically the same as the original:
https://github.com/louischatriot/nedb#removing-documents
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[options] | Object |
### datastore.removeOne([query], [options]) ⇒ Promise.<number>
Remove the first document that matches the specified `query`.
This is just an alias for `remove` with `options.multi` set to `false`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[options] | Object |
### datastore.removeMany([query], [options]) ⇒ Promise.<number>
Remove all documents that match the specified `query`.
This is just an alias for `remove` with `options.multi` set to `true`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[options] | Object |
### datastore.deleteOne([query], [options]) ⇒ Promise.<number>
Remove the first document that matches the specified `query`.
This is just an alias for `removeOne`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[options] | Object |
### datastore.deleteMany([query], [options]) ⇒ Promise.<number>
Remove all documents that match the specified `query`.
This is just an alias for `removeMany`.
**Kind**: instance method of [Datastore
](#Datastore)
Param | Type |
[query] | Object |
[options] | Object |
### datastore.count([query]) ⇒ [Cursor
](#Cursor)
Count documents matching the specified `query`.
It's basically the same as the original:
https://github.com/louischatriot/nedb#counting-documents
**Kind**: instance method of [Datastore
](#Datastore)
**Example**
```js
datastore.count({ ... }).limit(...).then(...)
```
**Example**
```js
// in an async function
await datastore.count({ ... })
// or
await datastore.count({ ... }).sort(...).limit(...)
```
### datastore.ensureIndex(options) ⇒ Promise.<undefined>
https://github.com/louischatriot/nedb#indexing
**Kind**: instance method of [Datastore
](#Datastore)
### datastore.removeIndex(field) ⇒ Promise.<undefined>
https://github.com/louischatriot/nedb#indexing
**Kind**: instance method of [Datastore
](#Datastore)
### Datastore.create([pathOrOptions]) ⇒ Proxy.<static>
Create a database instance.
Use this over `new Datastore(...)` to access
original nedb datastore properties, such as
`datastore.persistence`.
Note that this method only creates the `Datastore`
class instance, not the datastore file itself.
The file will only be created once an operation
is issued against the datastore or if you call
the `load` instance method explicitly.
The path (if specified) will be relative to `process.cwd()`
(unless an absolute path was passed).
For more information visit:
https://github.com/louischatriot/nedb#creatingloading-a-database
**Kind**: static method of [Datastore
](#Datastore)
Param | Type |
[pathOrOptions] | string | Object |