A dead-simple promise wrapper for nedb.
Check out the docs.
const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')
datastore.find({ field: true })
.then(...)
.catch(...)
datastore.find({ field: true })
.exec(...)
.then(...)
.catch(...)
datastore.findOne({ field: true })
.then(...)
.catch(...)
datastore.insert({ doc: 'yourdoc' })
.then(...)
.catch(...)
async function findSorted(page, perPage = 10) {
return await datastore.find(...)
.sort(...)
.limit(perPage)
.skip(page * perPage)
}
Usage
Everything works as the original module, with four major exceptions.
- There are no callbacks.
loadDatabase
has been renamed to load
.- You should call
Datastore.create(...)
instead of new Datastore(...)
. This way you can access the original nedb properties, such as datastore.persistence
. - As of v2.0.0 the module supports events 😎... Check out the docs about events!
Check out the original docs!
load( )
You don't need to call this as the module will automatically detect if the datastore has been loaded or not upon calling any other method.
const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')
datastore.load(...)
.then(...)
.catch(...)
find( [query], [projection] )
This will return a Cursor object that works the same way it did before except when you call "exec" it takes no arguments and returns a Promise.
update on Cursor objects
With the 1.1.0
update now you can simply call .then(...)
on a Cursor to request the documents in a Promise.
Note that .exec()
is still necessary when .find()
is in the .then()
of a Promise chain (otherwise the promise would be resolved with the Cursor object).
const Datastore = require('nedb-promises')
let datastore = Datastore.create('/path/to/db.db')
datastore.find(...)
.then(...)
.catch(...)
datastore.insert(...)
.then(() => {
return datastore.find(...).exec();
})
.then(
)
findOne( [query], [projection])
Unlike "find" this will not return a Cursor since it makes no sense to sort or limit a single document.
This will simply return a Promise.
other( ... )
All the other methods will take the same arguments as they did before (except the callback) and will return a Promise.
Check out the docs.