UNPKG

3.37 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
5Check 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
11const Datastore = require('nedb-promises')
12let datastore = Datastore.create('/path/to/db.db')
13
14// #1
15datastore.find({ field: true })
16 .then(...)
17 .catch(...)
18
19// #2
20datastore.find({ field: true })
21 .exec(...)
22 .then(...)
23 .catch(...)
24
25// #1 and #2 are equivalent
26
27datastore.findOne({ field: true })
28 .then(...)
29 .catch(...)
30
31datastore.insert({ doc: 'yourdoc' })
32 .then(...)
33 .catch(...)
34
35// or in an async function
36async 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
46npm install --save nedb-promises
47```
48
49### Usage
50Everything 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
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).