UNPKG

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