UNPKG

2.78 kBMarkdownView Raw
1![nedb-promises](https://github.com/bajankristof/nedb-promises/blob/master/logo.svg "nedb-promises")
2
3A dead-simple promise wrapper for [nedb](https://github.com/louischatriot/nedb#readme).
4
5```js
6const Datastore = require('nedb-promises');
7let db = Datastore.create('/path/to/db.db');
8
9// #1
10db.find({ field: true })
11 .then(...)
12 .catch(...);
13
14// #2
15db.find({ field: true })
16 .exec(...)
17 .then(...)
18 .catch(...);
19
20// #1 and #2 are equivalent
21
22db.findOne({ field: true })
23 .then(...)
24 .catch(...);
25
26db.insert({ doc: 'yourdoc', createdAt: Date.now() })
27 .then(...)
28 .catch(...);
29```
30
31### Usage
32Everything 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( )
40You 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
42const Datastore = require('nedb-promises');
43let db = Datastore.create('/path/to/db.db');
44db.load(...)
45 .then(...)
46 .catch(...)
47```
48
49#### find( query [, projection ] )
50This 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
53With the `1.1.0` update now you can simply call `.then(...)` on a Cursor to request the documents in a Promise.
54
55Note 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
58const Datastore = require('nedb-promises');
59let db = Datastore.create('/path/to/db.db');
60
61//outside Promise chain
62db.find(...)
63 .then(...)
64 .catch(...)
65
66//insinde Promise chain
67db.insert(...)
68 .then(() => {
69 return db.find(...).exec();
70 })
71 .then(
72 // use the retrieved documents
73 );
74```
75
76#### findOne( query [, projection ])
77Unlike "find" this will not return a Cursor since it makes no sense to sort or limit a single document.
78This will simply return a Promise.
79
80#### other( ... )
81All 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
84This 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