1 | ![nedb-promises](https://github.com/bajankristof/nedb-promises/blob/master/logo.svg "nedb-promises")
|
2 |
|
3 | A dead-simple promise wrapper for [nedb](https://github.com/louischatriot/nedb#readme).
|
4 |
|
5 | Check out the [docs](https://github.com/bajankristof/nedb-promises/blob/master/docs.md).
|
6 |
|
7 | ##### IMPORTANT
|
8 | **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`!**
|
9 |
|
10 | ```js
|
11 | const Datastore = require('nedb-promises')
|
12 | let datastore = Datastore.create('/path/to/db.db')
|
13 |
|
14 | // #1
|
15 | datastore.find({ field: true })
|
16 | .then(...)
|
17 | .catch(...)
|
18 |
|
19 | // #2
|
20 | datastore.find({ field: true })
|
21 | .exec(...)
|
22 | .then(...)
|
23 | .catch(...)
|
24 |
|
25 | // #1 and #2 are equivalent
|
26 |
|
27 | datastore.findOne({ field: true })
|
28 | .then(...)
|
29 | .catch(...)
|
30 |
|
31 | datastore.insert({ doc: 'yourdoc' })
|
32 | .then(...)
|
33 | .catch(...)
|
34 |
|
35 | // or in an async function
|
36 | async function findSorted(page, perPage = 10) {
|
37 | return await datastore.find(...)
|
38 | .sort(...)
|
39 | .limit(perPage)
|
40 | .skip(page * perPage)
|
41 | }
|
42 | ```
|
43 |
|
44 | ### Installation
|
45 | ```sh
|
46 | npm install --save nedb-promises
|
47 | ```
|
48 |
|
49 | ### Usage
|
50 | Everything works as the original module, with a couple of exceptions:
|
51 | * There are no callbacks.
|
52 | * `loadDatabase` has been renamed to [`load`](https://github.com/bajankristof/nedb-promises/blob/master/docs.md#Datastore+load).
|
53 | * The cursor's `projection` method has been renamed to [`project`](https://github.com/bajankristof/nedb-promises/blob/master/docs.md#Cursor+project).
|
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 |
|
57 | Check out the [original docs](https://github.com/louischatriot/nedb#readme)!
|
58 |
|
59 | #### load( )
|
60 | 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.
|
61 | ```js
|
62 | const Datastore = require('nedb-promises')
|
63 | let datastore = Datastore.create('/path/to/db.db')
|
64 | datastore.load(...)
|
65 | .then(...)
|
66 | .catch(...)
|
67 | ```
|
68 |
|
69 | #### find( [query], [projection] ), findOne( [query], [projection] ), count( [query] )
|
70 | These 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.
|
71 | The 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
|
74 | const Datastore = require('nedb-promises')
|
75 | let datastore = Datastore.create('/path/to/db.db')
|
76 |
|
77 | //outside Promise chain
|
78 | datastore.find(...)
|
79 | .then(...)
|
80 | .catch(...)
|
81 |
|
82 | //insinde Promise chain
|
83 | datastore.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( ... )
|
97 | All the other methods will take the same arguments as they did before (except the callback) and will return a Promise.
|
98 |
|
99 | Check out the [docs](https://github.com/bajankristof/nedb-promises/blob/master/docs.md).
|