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 | ```js
|
6 | const Datastore = require('nedb-promises');
|
7 | let db = Datastore.create('/path/to/db.db');
|
8 |
|
9 | // #1
|
10 | db.find({ field: true })
|
11 | .then(...)
|
12 | .catch(...);
|
13 |
|
14 | // #2
|
15 | db.find({ field: true })
|
16 | .exec(...)
|
17 | .then(...)
|
18 | .catch(...);
|
19 |
|
20 | // #1 and #2 are equivalent
|
21 |
|
22 | db.findOne({ field: true })
|
23 | .then(...)
|
24 | .catch(...);
|
25 |
|
26 | db.insert({ doc: 'yourdoc', createdAt: Date.now() })
|
27 | .then(...)
|
28 | .catch(...);
|
29 | ```
|
30 |
|
31 | ### Usage
|
32 | Everything works as the original module, with three major exceptions.
|
33 | * There are no callbacks.
|
34 | * `loadDatabase` has been renamed to `load`.
|
35 | * 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`.
|
36 |
|
37 | [Check out the original documentation!](https://github.com/louischatriot/nedb#readme)
|
38 |
|
39 | #### load( )
|
40 | 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.
|
41 | ```js
|
42 | const Datastore = require('nedb-promises');
|
43 | let db = Datastore.create('/path/to/db.db');
|
44 | db.load(...)
|
45 | .then(...)
|
46 | .catch(...)
|
47 | ```
|
48 |
|
49 | #### find( query [, projection ] )
|
50 | 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.
|
51 |
|
52 | ##### update on Cursor objects
|
53 | With the `1.1.0` update now you can simply call `.then(...)` on a Cursor to request the documents in a Promise.
|
54 |
|
55 | 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).
|
56 |
|
57 | ```js
|
58 | const Datastore = require('nedb-promises');
|
59 | let db = Datastore.create('/path/to/db.db');
|
60 |
|
61 | //outside Promise chain
|
62 | db.find(...)
|
63 | .then(...)
|
64 | .catch(...)
|
65 |
|
66 | //insinde Promise chain
|
67 | db.insert(...)
|
68 | .then(() => {
|
69 | return db.find(...).exec();
|
70 | })
|
71 | .then(
|
72 | // use the retrieved documents
|
73 | );
|
74 | ```
|
75 |
|
76 | #### findOne( query [, projection ])
|
77 | Unlike "find" this will not return a Cursor since it makes no sense to sort or limit a single document.
|
78 | This will simply return a Promise.
|
79 |
|
80 | #### other( ... )
|
81 | All the other methods will take the same arguments as they did before (except the callback) and will return a Promise.
|
82 |
|
83 | ##### update on the update( ... ) method
|
84 | This method now (`1.1.1`) accepts upsert and returnUpdatedDocs as it would with the original module.
|
85 |
|
86 | #### Special thanks to [npeterkamps](https://github.com/npeterkamps) for the useful pull requests!
|
87 |
|
88 | [Check out the original documentation!](https://github.com/louischatriot/nedb#readme) |
\ | No newline at end of file |