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.<Array.<Object>></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.<Array.<Object>></code>
|
23 | Execute the cursor.
|
24 |
|
25 | You can use the same cursor methods
|
26 | that you could with the original module:
|
27 |
|
28 | https://github.com/louischatriot/nedb#sorting-and-paginating
|
29 |
|
30 | Since the Cursor has a `then` and a `catch` method
|
31 | JavaScript identifies it as a thenable object
|
32 | thus 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
|
38 | await datastore.find(...)
|
39 | .sort(...)
|
40 | .limit(...)
|
41 | ```
|
42 | **Example**
|
43 | ```js
|
44 | // the previous is the same as:
|
45 | await datastore.find(...)
|
46 | .sort(...)
|
47 | .limit(...)
|
48 | .exec()
|
49 | ```
|
50 | <a name="Cursor+then"></a>
|
51 |
|
52 | ### cursor.then(fulfilled, [rejected]) ⇒ <code>Promise</code>
|
53 | Execute the cursor and set promise callbacks.
|
54 |
|
55 | For more information visit:
|
56 | https://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>
|
76 | Execute the cursor and set promise error callback.
|
77 |
|
78 | For more information visit:
|
79 | https://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
|
99 | in EventEmitter class and implements each method as an event
|
100 | plus additional error events. It also inherits the `compaction.done`
|
101 | event from nedb but for consistency, in this library the event
|
102 | was renamed to `compactionDone`.
|
103 |
|
104 | All event callbacks will be passed the same type of values,
|
105 | the first being the datastore, then the operation result (if there is any)
|
106 | and then the arguments of the called method. (Check out the first example!)
|
107 |
|
108 | All events have a matching error event that goes by the name of `${method}Error`,
|
109 | for example `findError` or `loadError`. The callbacks of these events will receive
|
110 | the same parameters as the normal event handlers except that instead of the
|
111 | operation result there will be an operation error. (Check out the second example!)
|
112 |
|
113 | A generic `__error__` event is also available. This event will be emitted at any of
|
114 | the above error events. The callbacks of this event will receive the same parameters
|
115 | as the specific error event handlers except that there will be one more parameter
|
116 | passed between the datastore and the error object, that being the name of the method
|
117 | that 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.<undefined></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.<(Object\|Array.<Object>)></code>
|
126 | * [.update(query, update, [options])](#Datastore+update) ⇒ <code>Promise.<(number\|Object\|Array.<Object>)></code>
|
127 | * [.remove([query], [options])](#Datastore+remove) ⇒ <code>Promise.<number></code>
|
128 | * [.count([query])](#Datastore+count) ⇒ [<code>Cursor</code>](#Cursor)
|
129 | * [.ensureIndex(options)](#Datastore+ensureIndex) ⇒ <code>Promise.<undefined></code>
|
130 | * [.removeIndex(field)](#Datastore+removeIndex) ⇒ <code>Promise.<undefined></code>
|
131 | * _static_
|
132 | * [.create([pathOrOptions])](#Datastore.create) ⇒ <code>Proxy.<static></code>
|
133 |
|
134 | <a name="new_Datastore_new"></a>
|
135 |
|
136 | ### new Datastore([pathOrOptions])
|
137 | Datastore constructor...
|
138 |
|
139 | You should use `Datastore.create(...)` instead
|
140 | of `new Datastore(...)`. With that you can access
|
141 | the original datastore's properties such as `datastore.persistence`.
|
142 |
|
143 | Create a Datastore instance.
|
144 |
|
145 | Note that the datastore will be created
|
146 | relative to `process.cwd()`
|
147 | (unless an absolute path was passed).
|
148 |
|
149 | It's basically the same as the original:
|
150 | https://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
|
166 | let datastore = Datastore.create()
|
167 | datastore.on('update', (datastore, result, query, update, options) => {
|
168 | })
|
169 | datastore.on('load', (datastore) => {
|
170 | // this event doesn't have a result
|
171 | })
|
172 | datastore.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 | })
|
177 | datastore.on('compactionDone', (datastore) => {
|
178 | // inherited from nedb's compaction.done event
|
179 | })
|
180 | ```
|
181 | **Example**
|
182 | ```js
|
183 | let datastore = Datastore.create()
|
184 | datastore.on('updateError', (datastore, error, query, update, options) => {
|
185 | })
|
186 | datastore.on('loadError', (datastore, error) => {
|
187 | })
|
188 | datastore.on('ensureIndexError', (datastore, error, options) => {
|
189 | })
|
190 | ```
|
191 | **Example**
|
192 | ```js
|
193 | let datastore = Datastore.create()
|
194 | datastore.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.<undefined></code>
|
202 | Load 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)
|
208 | Find documents that match a query.
|
209 |
|
210 | It's basically the same as the original:
|
211 | https://github.com/louischatriot/nedb#finding-documents
|
212 |
|
213 | There 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
|
232 | datastore.find({ ... }).sort({ ... }).exec().then(...)
|
233 | ```
|
234 | **Example**
|
235 | ```js
|
236 | datastore.find({ ... }).sort({ ... }).then(...)
|
237 | ```
|
238 | **Example**
|
239 | ```js
|
240 | // in an async function
|
241 | await datastore.find({ ... }).sort({ ... })
|
242 | ```
|
243 | <a name="Datastore+findOne"></a>
|
244 |
|
245 | ### datastore.findOne([query], [projection]) ⇒ [<code>Cursor</code>](#Cursor)
|
246 | Find a document that matches a query.
|
247 |
|
248 | It's basically the same as the original:
|
249 | https://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
|
268 | datastore.findOne({ ... }).then(...)
|
269 | ```
|
270 | **Example**
|
271 | ```js
|
272 | // in an async function
|
273 | await datastore.findOne({ ... }).sort({ ... })
|
274 | ```
|
275 | <a name="Datastore+insert"></a>
|
276 |
|
277 | ### datastore.insert(docs) ⇒ <code>Promise.<(Object\|Array.<Object>)></code>
|
278 | Insert a document or documents.
|
279 |
|
280 | It's basically the same as the original:
|
281 | https://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.<Object></code></td>
|
293 | </tr> </tbody>
|
294 | </table>
|
295 |
|
296 | <a name="Datastore+update"></a>
|
297 |
|
298 | ### datastore.update(query, update, [options]) ⇒ <code>Promise.<(number\|Object\|Array.<Object>)></code>
|
299 | Update documents that match a query.
|
300 |
|
301 | It's basically the same as the original:
|
302 | https://github.com/louischatriot/nedb#updating-documents
|
303 |
|
304 | If you set `options.returnUpdatedDocs`,
|
305 | the returned promise will resolve with
|
306 | an object (if `options.multi` is `false`) or
|
307 | with 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.<number></code>
|
329 | Remove documents that match a query.
|
330 |
|
331 | It's basically the same as the original:
|
332 | https://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)
|
352 | Count documents that match a query.
|
353 |
|
354 | It's basically the same as the original:
|
355 | https://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
|
372 | datastore.count({ ... }).limit(...).then(...)
|
373 | ```
|
374 | **Example**
|
375 | ```js
|
376 | // in an async function
|
377 | await datastore.count({ ... })
|
378 | // or
|
379 | await datastore.count({ ... }).sort(...).limit(...)
|
380 | ```
|
381 | <a name="Datastore+ensureIndex"></a>
|
382 |
|
383 | ### datastore.ensureIndex(options) ⇒ <code>Promise.<undefined></code>
|
384 | https://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.<undefined></code>
|
402 | https://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.<static></code>
|
420 | Create a database instance.
|
421 |
|
422 | Use this over `new Datastore(...)` to access
|
423 | original nedb datastore properties, such as
|
424 | `datastore.persistence`.
|
425 |
|
426 | Note that the datastore will be created
|
427 | relative to `process.cwd()`
|
428 | (unless an absolute path was passed).
|
429 |
|
430 | For more information visit:
|
431 | https://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 |
|