UNPKG

12.1 kBMarkdownView Raw
1## Classes
2
3<dl>
4<dt><a href="#Cursor">Cursor</a></dt>
5<dd></dd>
6<dt><a href="#Datastore">Datastore</a></dt>
7<dd></dd>
8</dl>
9
10<a name="Cursor"></a>
11
12## Cursor
13**Kind**: global class
14
15* [Cursor](#Cursor)
16 * [.exec()](#Cursor+exec) ⇒ <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code>
17 * [.then(fulfilled, [rejected])](#Cursor+then) ⇒ <code>Promise</code>
18 * [.catch(rejected)](#Cursor+catch) ⇒ <code>Promise</code>
19
20<a name="Cursor+exec"></a>
21
22### cursor.exec() ⇒ <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code>
23Execute the cursor.
24
25You can use the same cursor methods
26that you could with the original module:
27
28https://github.com/louischatriot/nedb#sorting-and-paginating
29
30Since the Cursor has a `then` and a `catch` method
31JavaScript identifies it as a thenable object
32thus you can await it in async functions.
33
34**Kind**: instance method of [<code>Cursor</code>](#Cursor)
35**Example**
36```js
37// in an async function
38await datastore.find(...)
39 .sort(...)
40 .limit(...)
41```
42**Example**
43```js
44// the previous is the same as:
45await datastore.find(...)
46 .sort(...)
47 .limit(...)
48 .exec()
49```
50<a name="Cursor+then"></a>
51
52### cursor.then(fulfilled, [rejected]) ⇒ <code>Promise</code>
53Execute the cursor and set promise callbacks.
54
55For more information visit:
56https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
57
58**Kind**: instance method of [<code>Cursor</code>](#Cursor)
59<table>
60 <thead>
61 <tr>
62 <th>Param</th><th>Type</th>
63 </tr>
64 </thead>
65 <tbody>
66<tr>
67 <td>fulfilled</td><td><code>function</code></td>
68 </tr><tr>
69 <td>[rejected]</td><td><code>function</code></td>
70 </tr> </tbody>
71</table>
72
73<a name="Cursor+catch"></a>
74
75### cursor.catch(rejected) ⇒ <code>Promise</code>
76Execute the cursor and set promise error callback.
77
78For more information visit:
79https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
80
81**Kind**: instance method of [<code>Cursor</code>](#Cursor)
82<table>
83 <thead>
84 <tr>
85 <th>Param</th><th>Type</th>
86 </tr>
87 </thead>
88 <tbody>
89<tr>
90 <td>rejected</td><td><code>function</code></td>
91 </tr> </tbody>
92</table>
93
94<a name="Datastore"></a>
95
96## Datastore
97**Kind**: global class
98**Summary**: As of v2.0.0 the Datastore class extends node's built
99in EventEmitter class and implements each method as an event
100plus additional error events. It also inherits the `compaction.done`
101event from nedb but for consistency, in this library the event
102was renamed to `compactionDone`.
103
104All event callbacks will be passed the same type of values,
105the first being the datastore, then the operation result (if there is any)
106and then the arguments of the called method. (Check out the first example!)
107
108All events have a matching error event that goes by the name of `${method}Error`,
109for example `findError` or `loadError`. The callbacks of these events will receive
110the same parameters as the normal event handlers except that instead of the
111operation result there will be an operation error. (Check out the second example!)
112
113A generic `__error__` event is also available. This event will be emitted at any of
114the above error events. The callbacks of this event will receive the same parameters
115as the specific error event handlers except that there will be one more parameter
116passed between the datastore and the error object, that being the name of the method
117that failed. (Check out the third example!)
118
119* [Datastore](#Datastore)
120 * [new Datastore([pathOrOptions])](#new_Datastore_new)
121 * _instance_
122 * [.load()](#Datastore+load) ⇒ <code>Promise.&lt;undefined&gt;</code>
123 * [.find([query], [projection])](#Datastore+find) ⇒ [<code>Cursor</code>](#Cursor)
124 * [.findOne([query], [projection])](#Datastore+findOne) ⇒ [<code>Cursor</code>](#Cursor)
125 * [.insert(docs)](#Datastore+insert) ⇒ <code>Promise.&lt;(Object\|Array.&lt;Object&gt;)&gt;</code>
126 * [.update(query, update, [options])](#Datastore+update) ⇒ <code>Promise.&lt;(number\|Object\|Array.&lt;Object&gt;)&gt;</code>
127 * [.remove([query], [options])](#Datastore+remove) ⇒ <code>Promise.&lt;number&gt;</code>
128 * [.count([query])](#Datastore+count) ⇒ [<code>Cursor</code>](#Cursor)
129 * [.ensureIndex(options)](#Datastore+ensureIndex) ⇒ <code>Promise.&lt;undefined&gt;</code>
130 * [.removeIndex(field)](#Datastore+removeIndex) ⇒ <code>Promise.&lt;undefined&gt;</code>
131 * _static_
132 * [.create([pathOrOptions])](#Datastore.create) ⇒ <code>Proxy.&lt;static&gt;</code>
133
134<a name="new_Datastore_new"></a>
135
136### new Datastore([pathOrOptions])
137Datastore constructor...
138
139You should use `Datastore.create(...)` instead
140of `new Datastore(...)`. With that you can access
141the original datastore's properties such as `datastore.persistence`.
142
143Create a Datastore instance.
144
145Note that the datastore will be created
146relative to `process.cwd()`
147(unless an absolute path was passed).
148
149It's basically the same as the original:
150https://github.com/louischatriot/nedb#creatingloading-a-database
151
152<table>
153 <thead>
154 <tr>
155 <th>Param</th><th>Type</th>
156 </tr>
157 </thead>
158 <tbody>
159<tr>
160 <td>[pathOrOptions]</td><td><code>string</code> | <code>Object</code></td>
161 </tr> </tbody>
162</table>
163
164**Example**
165```js
166let datastore = Datastore.create()
167datastore.on('update', (datastore, result, query, update, options) => {
168})
169datastore.on('load', (datastore) => {
170 // this event doesn't have a result
171})
172datastore.on('ensureIndex', (datastore, options) => {
173 // this event doesn't have a result
174 // but it has the options argument which will be passed to the
175 // event handlers
176})
177datastore.on('compactionDone', (datastore) => {
178 // inherited from nedb's compaction.done event
179})
180```
181**Example**
182```js
183let datastore = Datastore.create()
184datastore.on('updateError', (datastore, error, query, update, options) => {
185})
186datastore.on('loadError', (datastore, error) => {
187})
188datastore.on('ensureIndexError', (datastore, error, options) => {
189})
190```
191**Example**
192```js
193let datastore = Datastore.create()
194datastore.on('__error__', (datastore, event, error, ...args) => {
195 // for example
196 // datastore, 'find', error, [{ foo: 'bar' }, {}]
197})
198```
199<a name="Datastore+load"></a>
200
201### datastore.load() ⇒ <code>Promise.&lt;undefined&gt;</code>
202Load the datastore.
203
204**Kind**: instance method of [<code>Datastore</code>](#Datastore)
205<a name="Datastore+find"></a>
206
207### datastore.find([query], [projection]) ⇒ [<code>Cursor</code>](#Cursor)
208Find documents that match a query.
209
210It's basically the same as the original:
211https://github.com/louischatriot/nedb#finding-documents
212
213There are differences minor in how the cursor works though.
214
215**Kind**: instance method of [<code>Datastore</code>](#Datastore)
216<table>
217 <thead>
218 <tr>
219 <th>Param</th><th>Type</th>
220 </tr>
221 </thead>
222 <tbody>
223<tr>
224 <td>[query]</td><td><code>Object</code></td>
225 </tr><tr>
226 <td>[projection]</td><td><code>Object</code></td>
227 </tr> </tbody>
228</table>
229
230**Example**
231```js
232datastore.find({ ... }).sort({ ... }).exec().then(...)
233```
234**Example**
235```js
236datastore.find({ ... }).sort({ ... }).then(...)
237```
238**Example**
239```js
240// in an async function
241await datastore.find({ ... }).sort({ ... })
242```
243<a name="Datastore+findOne"></a>
244
245### datastore.findOne([query], [projection]) ⇒ [<code>Cursor</code>](#Cursor)
246Find a document that matches a query.
247
248It's basically the same as the original:
249https://github.com/louischatriot/nedb#finding-documents
250
251**Kind**: instance method of [<code>Datastore</code>](#Datastore)
252<table>
253 <thead>
254 <tr>
255 <th>Param</th><th>Type</th>
256 </tr>
257 </thead>
258 <tbody>
259<tr>
260 <td>[query]</td><td><code>Object</code></td>
261 </tr><tr>
262 <td>[projection]</td><td><code>Object</code></td>
263 </tr> </tbody>
264</table>
265
266**Example**
267```js
268datastore.findOne({ ... }).then(...)
269```
270**Example**
271```js
272// in an async function
273await datastore.findOne({ ... }).sort({ ... })
274```
275<a name="Datastore+insert"></a>
276
277### datastore.insert(docs) ⇒ <code>Promise.&lt;(Object\|Array.&lt;Object&gt;)&gt;</code>
278Insert a document or documents.
279
280It's basically the same as the original:
281https://github.com/louischatriot/nedb#inserting-documents
282
283**Kind**: instance method of [<code>Datastore</code>](#Datastore)
284<table>
285 <thead>
286 <tr>
287 <th>Param</th><th>Type</th>
288 </tr>
289 </thead>
290 <tbody>
291<tr>
292 <td>docs</td><td><code>Object</code> | <code>Array.&lt;Object&gt;</code></td>
293 </tr> </tbody>
294</table>
295
296<a name="Datastore+update"></a>
297
298### datastore.update(query, update, [options]) ⇒ <code>Promise.&lt;(number\|Object\|Array.&lt;Object&gt;)&gt;</code>
299Update documents that match a query.
300
301It's basically the same as the original:
302https://github.com/louischatriot/nedb#updating-documents
303
304If you set `options.returnUpdatedDocs`,
305the returned promise will resolve with
306an object (if `options.multi` is `false`) or
307with an array of objects.
308
309**Kind**: instance method of [<code>Datastore</code>](#Datastore)
310<table>
311 <thead>
312 <tr>
313 <th>Param</th><th>Type</th>
314 </tr>
315 </thead>
316 <tbody>
317<tr>
318 <td>query</td><td><code>Object</code></td>
319 </tr><tr>
320 <td>update</td><td><code>Object</code></td>
321 </tr><tr>
322 <td>[options]</td><td><code>Object</code></td>
323 </tr> </tbody>
324</table>
325
326<a name="Datastore+remove"></a>
327
328### datastore.remove([query], [options]) ⇒ <code>Promise.&lt;number&gt;</code>
329Remove documents that match a query.
330
331It's basically the same as the original:
332https://github.com/louischatriot/nedb#removing-documents
333
334**Kind**: instance method of [<code>Datastore</code>](#Datastore)
335<table>
336 <thead>
337 <tr>
338 <th>Param</th><th>Type</th>
339 </tr>
340 </thead>
341 <tbody>
342<tr>
343 <td>[query]</td><td><code>Object</code></td>
344 </tr><tr>
345 <td>[options]</td><td><code>Object</code></td>
346 </tr> </tbody>
347</table>
348
349<a name="Datastore+count"></a>
350
351### datastore.count([query]) ⇒ [<code>Cursor</code>](#Cursor)
352Count documents that match a query.
353
354It's basically the same as the original:
355https://github.com/louischatriot/nedb#counting-documents
356
357**Kind**: instance method of [<code>Datastore</code>](#Datastore)
358<table>
359 <thead>
360 <tr>
361 <th>Param</th><th>Type</th>
362 </tr>
363 </thead>
364 <tbody>
365<tr>
366 <td>[query]</td><td><code>Object</code></td>
367 </tr> </tbody>
368</table>
369
370**Example**
371```js
372datastore.count({ ... }).limit(...).then(...)
373```
374**Example**
375```js
376// in an async function
377await datastore.count({ ... })
378// or
379await datastore.count({ ... }).sort(...).limit(...)
380```
381<a name="Datastore+ensureIndex"></a>
382
383### datastore.ensureIndex(options) ⇒ <code>Promise.&lt;undefined&gt;</code>
384https://github.com/louischatriot/nedb#indexing
385
386**Kind**: instance method of [<code>Datastore</code>](#Datastore)
387<table>
388 <thead>
389 <tr>
390 <th>Param</th><th>Type</th>
391 </tr>
392 </thead>
393 <tbody>
394<tr>
395 <td>options</td><td><code>Object</code></td>
396 </tr> </tbody>
397</table>
398
399<a name="Datastore+removeIndex"></a>
400
401### datastore.removeIndex(field) ⇒ <code>Promise.&lt;undefined&gt;</code>
402https://github.com/louischatriot/nedb#indexing
403
404**Kind**: instance method of [<code>Datastore</code>](#Datastore)
405<table>
406 <thead>
407 <tr>
408 <th>Param</th><th>Type</th>
409 </tr>
410 </thead>
411 <tbody>
412<tr>
413 <td>field</td><td><code>string</code></td>
414 </tr> </tbody>
415</table>
416
417<a name="Datastore.create"></a>
418
419### Datastore.create([pathOrOptions]) ⇒ <code>Proxy.&lt;static&gt;</code>
420Create a database instance.
421
422Use this over `new Datastore(...)` to access
423original nedb datastore properties, such as
424`datastore.persistence`.
425
426Note that the datastore will be created
427relative to `process.cwd()`
428(unless an absolute path was passed).
429
430For more information visit:
431https://github.com/louischatriot/nedb#creatingloading-a-database
432
433**Kind**: static method of [<code>Datastore</code>](#Datastore)
434<table>
435 <thead>
436 <tr>
437 <th>Param</th><th>Type</th>
438 </tr>
439 </thead>
440 <tbody>
441<tr>
442 <td>[pathOrOptions]</td><td><code>string</code> | <code>Object</code></td>
443 </tr> </tbody>
444</table>
445