UNPKG

3.14 kBMarkdownView Raw
1
2![nedb-promises](https://github.com/bajankristof/nedb-promises/blob/master/logo.svg "nedb-promises")
3
4A dead-simple promise wrapper for [nedb](https://github.com/louischatriot/nedb#readme).
5
6Check out the [docs](https://github.com/bajankristof/nedb-promises/blob/master/docs.md).
7
8##### IMPORTANT
9**As of `nedb-promises` `5.0.0` [nedb](https://github.com/louischatriot/nedb#readme) package has been replaced with a fork of the original package, [@seald-io/nedb](https://github.com/seald/nedb) to solve some vulnerability issues originating from `nedb`!**
10
11```js
12const Datastore = require('nedb-promises')
13let datastore = Datastore.create('/path/to/db.db')
14
15// #1
16datastore.find({ field: true })
17 .then(...)
18 .catch(...)
19
20// #2
21datastore.find({ field: true })
22 .exec(...)
23 .then(...)
24 .catch(...)
25
26// #1 and #2 are equivalent
27
28datastore.findOne({ field: true })
29 .then(...)
30 .catch(...)
31
32datastore.insert({ doc: 'yourdoc' })
33 .then(...)
34 .catch(...)
35
36// or in an async function
37async function findSorted(page, perPage = 10) {
38 return await datastore.find(...)
39 .sort(...)
40 .limit(perPage)
41 .skip(page * perPage)
42}
43```
44
45### Installation
46```sh
47npm install --save nedb-promises
48```
49
50### Usage
51Everything works as the original module, with four major exceptions.
52* There are no callbacks.
53* `loadDatabase` has been renamed to `load`.
54* You should call `Datastore.create(...)` instead of `new Datastore(...)`. This way you can access the original [nedb](https://github.com/louischatriot/nedb#readme) properties, such as `datastore.persistence`.
55* As of v2.0.0 the module supports events 😎... Check out the [docs about events](https://github.com/bajankristof/nedb-promises/blob/master/docs.md#Datastore)!
56
57Check out the [original docs](https://github.com/louischatriot/nedb#readme)!
58
59#### load( )
60You 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.
61```js
62const Datastore = require('nedb-promises')
63let datastore = Datastore.create('/path/to/db.db')
64datastore.load(...)
65 .then(...)
66 .catch(...)
67```
68
69#### find( [query], [projection] ), findOne( [query], [projection] ), count( [query] )
70These methods 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.
71The cool thing about this implementation of the Cursor is that it behaves like a Promise. Meaning that you can `await` it and you can call `.then()` on it.
72
73```js
74const Datastore = require('nedb-promises')
75let datastore = Datastore.create('/path/to/db.db')
76
77//outside Promise chain
78datastore.find(...)
79 .then(...)
80 .catch(...)
81
82//insinde Promise chain
83datastore.insert(...)
84 .then(() => {
85 return datastore.find(...)
86 })
87 .then(
88 // use the retrieved documents
89 )
90
91;(async () => {
92 await datastore.find(...).sort(...).limit()
93})()
94```
95
96#### other( ... )
97All the other methods will take the same arguments as they did before (except the callback) and will return a Promise.
98
99Check out the [docs](https://github.com/bajankristof/nedb-promises/blob/master/docs.md).