nedb-promises
Advanced tools
Comparing version 4.0.3 to 4.0.4
@@ -7,2 +7,8 @@ # Changelog | ||
## 4.0.4 | ||
### Updated | ||
- vulnerable dependencies | ||
- tabs to spaces in code | ||
- typescript doc file | ||
## 4.0.1 | ||
@@ -9,0 +15,0 @@ ### Changed |
26
docs.md
@@ -39,4 +39,4 @@ ## Classes | ||
await datastore.find(...) | ||
.sort(...) | ||
.limit(...) | ||
.sort(...) | ||
.limit(...) | ||
``` | ||
@@ -47,5 +47,5 @@ **Example** | ||
await datastore.find(...) | ||
.sort(...) | ||
.limit(...) | ||
.exec() | ||
.sort(...) | ||
.limit(...) | ||
.exec() | ||
``` | ||
@@ -120,3 +120,3 @@ <a name="Cursor+then"></a> | ||
* [Datastore](#Datastore) | ||
* [new Datastore([options])](#new_Datastore_new) | ||
* [new Datastore([pathOrOptions])](#new_Datastore_new) | ||
* _instance_ | ||
@@ -133,7 +133,7 @@ * [.load()](#Datastore+load) ⇒ <code>Promise.<undefined></code> | ||
* _static_ | ||
* [.create(options)](#Datastore.create) ⇒ <code>Proxy.<static></code> | ||
* [.create([pathOrOptions])](#Datastore.create) ⇒ <code>Proxy.<static></code> | ||
<a name="new_Datastore_new"></a> | ||
### new Datastore([options]) | ||
### new Datastore([pathOrOptions]) | ||
Datastore constructor... | ||
@@ -143,3 +143,3 @@ | ||
of `new Datastore(...)`. With that you can access | ||
the original datastore's properties such as `datastore.persistance`. | ||
the original datastore's properties such as `datastore.persistence`. | ||
@@ -163,3 +163,3 @@ Create a Datastore instance. | ||
<tr> | ||
<td>[options]</td><td><code>Object</code></td> | ||
<td>[pathOrOptions]</td><td><code>string</code> | <code>Object</code></td> | ||
</tr> </tbody> | ||
@@ -420,3 +420,3 @@ </table> | ||
### Datastore.create(options) ⇒ <code>Proxy.<static></code> | ||
### Datastore.create([pathOrOptions]) ⇒ <code>Proxy.<static></code> | ||
Create a database instance. | ||
@@ -426,3 +426,3 @@ | ||
original nedb datastore properties, such as | ||
`datastore.persistance`. | ||
`datastore.persistence`. | ||
@@ -445,5 +445,5 @@ Note that the datastore will be created | ||
<tr> | ||
<td>options</td><td><code>string</code> | <code>Object</code></td> | ||
<td>[pathOrOptions]</td><td><code>string</code> | <code>Object</code></td> | ||
</tr> </tbody> | ||
</table> | ||
@@ -74,3 +74,3 @@ import { EventEmitter } from 'events' | ||
* of `new Datastore(...)`. With that you can access | ||
* the original datastore's properties such as `datastore.persistance`. | ||
* the original datastore's properties such as `datastore.persistence`. | ||
* | ||
@@ -187,3 +187,3 @@ * It's basically the same as the original: | ||
* original nedb datastore properties, such as | ||
* `datastore.persistance`. | ||
* `datastore.persistence`. | ||
* | ||
@@ -193,3 +193,3 @@ * For more information visit: | ||
*/ | ||
static create(options: Nedb.DatastoreOptions): Datastore | ||
static create(pathOrOptions?: string | Nedb.DatastoreOptions): Datastore | ||
} | ||
@@ -196,0 +196,0 @@ |
{ | ||
"name": "nedb-promises", | ||
"version": "4.0.3", | ||
"version": "4.0.4", | ||
"description": "A dead-simple promise wrapper for nedb.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,4 +34,4 @@ ![nedb-promises](https://github.com/bajankristof/nedb-promises/blob/master/logo.svg "nedb-promises") | ||
async function findSorted(page, perPage = 10) { | ||
return await datastore.find(...) | ||
.sort(...) | ||
return await datastore.find(...) | ||
.sort(...) | ||
.limit(perPage) | ||
@@ -38,0 +38,0 @@ .skip(page * perPage) |
const | ||
OriginalCursor = require('nedb/lib/cursor') | ||
OriginalCursor = require('nedb/lib/cursor') | ||
@@ -8,121 +8,121 @@ /** | ||
class Cursor { | ||
constructor(original, prerequisite, callback) { | ||
if ( ! (original instanceof OriginalCursor)) { | ||
throw new TypeError(`Unexpected ${typeof original}, expected: Cursor (nedb/lib/cursor)`) | ||
} | ||
constructor(original, prerequisite, callback) { | ||
if ( ! (original instanceof OriginalCursor)) { | ||
throw new TypeError(`Unexpected ${typeof original}, expected: Cursor (nedb/lib/cursor)`) | ||
} | ||
if ( ! (prerequisite instanceof Promise)) { | ||
prerequisite = Promise.resolve() | ||
} | ||
if ( ! (prerequisite instanceof Promise)) { | ||
prerequisite = Promise.resolve() | ||
} | ||
Object.defineProperties(this, { | ||
__original: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: original | ||
}, | ||
Object.defineProperties(this, { | ||
__original: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: original | ||
}, | ||
__prerequisite: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: prerequisite | ||
}, | ||
__prerequisite: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: prerequisite | ||
}, | ||
__callback: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: callback | ||
} | ||
}) | ||
} | ||
__callback: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: callback | ||
} | ||
}) | ||
} | ||
sort() { | ||
this.__original.sort.apply(this.__original, arguments) | ||
return this | ||
} | ||
sort() { | ||
this.__original.sort.apply(this.__original, arguments) | ||
return this | ||
} | ||
skip() { | ||
this.__original.skip.apply(this.__original, arguments) | ||
return this | ||
} | ||
skip() { | ||
this.__original.skip.apply(this.__original, arguments) | ||
return this | ||
} | ||
limit() { | ||
this.__original.limit.apply(this.__original, arguments) | ||
return this | ||
} | ||
limit() { | ||
this.__original.limit.apply(this.__original, arguments) | ||
return this | ||
} | ||
/** | ||
* Execute the cursor. | ||
* | ||
* You can use the same cursor methods | ||
* that you could with the original module: | ||
* | ||
* https://github.com/louischatriot/nedb#sorting-and-paginating | ||
* | ||
* Since the Cursor has a `then` and a `catch` method | ||
* JavaScript identifies it as a thenable object | ||
* thus you can await it in async functions. | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.find(...) | ||
* .sort(...) | ||
* .limit(...) | ||
* | ||
* @example | ||
* // the previous is the same as: | ||
* await datastore.find(...) | ||
* .sort(...) | ||
* .limit(...) | ||
* .exec() | ||
* | ||
* @return {Promise.<Object[]>} | ||
*/ | ||
exec() { | ||
return this.__prerequisite.then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.exec((error, result) => { | ||
if ('function' === typeof this.__callback) { | ||
this.__callback(error, result) | ||
} | ||
/** | ||
* Execute the cursor. | ||
* | ||
* You can use the same cursor methods | ||
* that you could with the original module: | ||
* | ||
* https://github.com/louischatriot/nedb#sorting-and-paginating | ||
* | ||
* Since the Cursor has a `then` and a `catch` method | ||
* JavaScript identifies it as a thenable object | ||
* thus you can await it in async functions. | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.find(...) | ||
* .sort(...) | ||
* .limit(...) | ||
* | ||
* @example | ||
* // the previous is the same as: | ||
* await datastore.find(...) | ||
* .sort(...) | ||
* .limit(...) | ||
* .exec() | ||
* | ||
* @return {Promise.<Object[]>} | ||
*/ | ||
exec() { | ||
return this.__prerequisite.then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.exec((error, result) => { | ||
if ('function' === typeof this.__callback) { | ||
this.__callback(error, result) | ||
} | ||
return error | ||
? reject(error) | ||
: resolve(result) | ||
}) | ||
}) | ||
}) | ||
} | ||
return error | ||
? reject(error) | ||
: resolve(result) | ||
}) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Execute the cursor and set promise callbacks. | ||
* | ||
* For more information visit: | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
* | ||
* @param {Function} fulfilled | ||
* @param {Function} [rejected] | ||
* @return {Promise} | ||
*/ | ||
then(fulfilled, rejected) { | ||
return this.exec().then(fulfilled, rejected) | ||
} | ||
/** | ||
* Execute the cursor and set promise callbacks. | ||
* | ||
* For more information visit: | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then | ||
* | ||
* @param {Function} fulfilled | ||
* @param {Function} [rejected] | ||
* @return {Promise} | ||
*/ | ||
then(fulfilled, rejected) { | ||
return this.exec().then(fulfilled, rejected) | ||
} | ||
/** | ||
* Execute the cursor and set promise error callback. | ||
* | ||
* For more information visit: | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | ||
* | ||
* @param {Function} rejected | ||
* @return {Promise} | ||
*/ | ||
catch(rejected) { | ||
return this.exec().catch(rejected) | ||
} | ||
/** | ||
* Execute the cursor and set promise error callback. | ||
* | ||
* For more information visit: | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | ||
* | ||
* @param {Function} rejected | ||
* @return {Promise} | ||
*/ | ||
catch(rejected) { | ||
return this.exec().catch(rejected) | ||
} | ||
} | ||
module.exports = Cursor |
const | ||
Cursor = require('./Cursor'), | ||
EventEmitter = require('events'), | ||
OriginalDatastore = require('nedb') | ||
Cursor = require('./Cursor'), | ||
EventEmitter = require('events'), | ||
OriginalDatastore = require('nedb') | ||
@@ -59,347 +59,347 @@ /** | ||
class Datastore extends EventEmitter { | ||
/** | ||
* Datastore constructor... | ||
* | ||
* You should use `Datastore.create(...)` instead | ||
* of `new Datastore(...)`. With that you can access | ||
* the original datastore's properties such as `datastore.persistance`. | ||
* | ||
* Create a Datastore instance. | ||
* | ||
* Note that the datastore will be created | ||
* relative to `process.cwd()` | ||
* (unless an absolute path was passed). | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#creatingloading-a-database | ||
* | ||
* @param {Object} [options] | ||
* @return {static} | ||
*/ | ||
constructor(options) { | ||
super() | ||
/** | ||
* Datastore constructor... | ||
* | ||
* You should use `Datastore.create(...)` instead | ||
* of `new Datastore(...)`. With that you can access | ||
* the original datastore's properties such as `datastore.persistence`. | ||
* | ||
* Create a Datastore instance. | ||
* | ||
* Note that the datastore will be created | ||
* relative to `process.cwd()` | ||
* (unless an absolute path was passed). | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#creatingloading-a-database | ||
* | ||
* @param {string|Object} [pathOrOptions] | ||
* @return {static} | ||
*/ | ||
constructor(pathOrOptions) { | ||
super() | ||
Object.defineProperties(this, { | ||
__loaded: { | ||
enumerable: false, | ||
writable: true, | ||
value: null | ||
}, | ||
Object.defineProperties(this, { | ||
__loaded: { | ||
enumerable: false, | ||
writable: true, | ||
value: null | ||
}, | ||
__original: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: false, | ||
value: new OriginalDatastore(options) | ||
} | ||
}) | ||
} | ||
__original: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: false, | ||
value: new OriginalDatastore(pathOrOptions) | ||
} | ||
}) | ||
} | ||
/** | ||
* Load the datastore. | ||
* @return {Promise.<undefined>} | ||
*/ | ||
load() { | ||
if ( ! (this.__loaded instanceof Promise)) { | ||
this.__loaded = new Promise((resolve, reject) => { | ||
this.__original.loadDatabase((error) => { | ||
if (error) { | ||
this.emit('loadError', this, error) | ||
this.emit('__error__', this, 'load', error) | ||
reject(error) | ||
} else { | ||
this.emit('load', this) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
/** | ||
* Load the datastore. | ||
* @return {Promise.<undefined>} | ||
*/ | ||
load() { | ||
if ( ! (this.__loaded instanceof Promise)) { | ||
this.__loaded = new Promise((resolve, reject) => { | ||
this.__original.loadDatabase((error) => { | ||
if (error) { | ||
this.emit('loadError', this, error) | ||
this.emit('__error__', this, 'load', error) | ||
reject(error) | ||
} else { | ||
this.emit('load', this) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
return this.__loaded | ||
} | ||
return this.__loaded | ||
} | ||
/** | ||
* Find documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#finding-documents | ||
* | ||
* There are differences minor in how the cursor works though. | ||
* | ||
* @example | ||
* datastore.find({ ... }).sort({ ... }).exec().then(...) | ||
* | ||
* @example | ||
* datastore.find({ ... }).sort({ ... }).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.find({ ... }).sort({ ... }) | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [projection] | ||
* @return {Cursor} | ||
*/ | ||
find(query = {}, projection) { | ||
if (typeof projection === 'function') { | ||
projection = {} | ||
} | ||
/** | ||
* Find documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#finding-documents | ||
* | ||
* There are differences minor in how the cursor works though. | ||
* | ||
* @example | ||
* datastore.find({ ... }).sort({ ... }).exec().then(...) | ||
* | ||
* @example | ||
* datastore.find({ ... }).sort({ ... }).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.find({ ... }).sort({ ... }) | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [projection] | ||
* @return {Cursor} | ||
*/ | ||
find(query = {}, projection) { | ||
if (typeof projection === 'function') { | ||
projection = {} | ||
} | ||
return new Cursor( | ||
this.__original.find(query, projection), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('findError', this, error, query, projection) | ||
this.emit('__error__', this, 'find', error, query, projection) | ||
} else { | ||
this.emit('find', this, result, query, projection) | ||
} | ||
} | ||
) | ||
} | ||
return new Cursor( | ||
this.__original.find(query, projection), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('findError', this, error, query, projection) | ||
this.emit('__error__', this, 'find', error, query, projection) | ||
} else { | ||
this.emit('find', this, result, query, projection) | ||
} | ||
} | ||
) | ||
} | ||
/** | ||
* Find a document that matches a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#finding-documents | ||
* | ||
* @example | ||
* datastore.findOne({ ... }).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.findOne({ ... }).sort({ ... }) | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [projection] | ||
* @return {Cursor} | ||
*/ | ||
findOne(query = {}, projection) { | ||
if (typeof projection === 'function') { | ||
projection = {} | ||
} | ||
/** | ||
* Find a document that matches a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#finding-documents | ||
* | ||
* @example | ||
* datastore.findOne({ ... }).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.findOne({ ... }).sort({ ... }) | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [projection] | ||
* @return {Cursor} | ||
*/ | ||
findOne(query = {}, projection) { | ||
if (typeof projection === 'function') { | ||
projection = {} | ||
} | ||
return new Cursor( | ||
this.__original.findOne(query, projection), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('findOneError', this, error, query, projection) | ||
this.emit('__error__', this, 'findOne', error, query, projection) | ||
} else { | ||
this.emit('findOne', this, result, query, projection) | ||
} | ||
} | ||
) | ||
} | ||
return new Cursor( | ||
this.__original.findOne(query, projection), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('findOneError', this, error, query, projection) | ||
this.emit('__error__', this, 'findOne', error, query, projection) | ||
} else { | ||
this.emit('findOne', this, result, query, projection) | ||
} | ||
} | ||
) | ||
} | ||
/** | ||
* Insert a document or documents. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#inserting-documents | ||
* | ||
* @param {Object|Object[]} docs | ||
* @return {Promise.<Object|Object[]>} | ||
*/ | ||
insert(docs) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.insert(docs, (error, result) => { | ||
if (error) { | ||
this.emit('insertError', this, error, docs) | ||
this.emit('__error__', this, 'insert', error, docs) | ||
reject(error) | ||
} else { | ||
this.emit('insert', this, result, docs) | ||
resolve(result) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Insert a document or documents. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#inserting-documents | ||
* | ||
* @param {Object|Object[]} docs | ||
* @return {Promise.<Object|Object[]>} | ||
*/ | ||
insert(docs) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.insert(docs, (error, result) => { | ||
if (error) { | ||
this.emit('insertError', this, error, docs) | ||
this.emit('__error__', this, 'insert', error, docs) | ||
reject(error) | ||
} else { | ||
this.emit('insert', this, result, docs) | ||
resolve(result) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Update documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#updating-documents | ||
* | ||
* If you set `options.returnUpdatedDocs`, | ||
* the returned promise will resolve with | ||
* an object (if `options.multi` is `false`) or | ||
* with an array of objects. | ||
* | ||
* @param {Object} query | ||
* @param {Object} update | ||
* @param {Object} [options] | ||
* @return {Promise.<number|Object|Object[]>} | ||
*/ | ||
update(query, update, options = {}) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.update( | ||
query, | ||
update, | ||
options, | ||
(error, numAffected, affectedDocuments, upsert) => { | ||
if (error) { | ||
this.emit('updateError', this, error, query, update, options) | ||
this.emit('__error__', this, 'update', error, query, update, options) | ||
reject(error) | ||
} else { | ||
let result = options.returnUpdatedDocs | ||
? affectedDocuments | ||
: numAffected | ||
this.emit('update', this, result, query, update, options) | ||
resolve(result) | ||
} | ||
} | ||
) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Update documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#updating-documents | ||
* | ||
* If you set `options.returnUpdatedDocs`, | ||
* the returned promise will resolve with | ||
* an object (if `options.multi` is `false`) or | ||
* with an array of objects. | ||
* | ||
* @param {Object} query | ||
* @param {Object} update | ||
* @param {Object} [options] | ||
* @return {Promise.<number|Object|Object[]>} | ||
*/ | ||
update(query, update, options = {}) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.update( | ||
query, | ||
update, | ||
options, | ||
(error, numAffected, affectedDocuments, upsert) => { | ||
if (error) { | ||
this.emit('updateError', this, error, query, update, options) | ||
this.emit('__error__', this, 'update', error, query, update, options) | ||
reject(error) | ||
} else { | ||
let result = options.returnUpdatedDocs | ||
? affectedDocuments | ||
: numAffected | ||
this.emit('update', this, result, query, update, options) | ||
resolve(result) | ||
} | ||
} | ||
) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Remove documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#removing-documents | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [options] | ||
* @return {Promise.<number>} | ||
*/ | ||
remove(query = {}, options = {}) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.remove( | ||
query, | ||
options, | ||
(error, result) => { | ||
if (error) { | ||
this.emit('removeError', this, error, query, options) | ||
this.emit('__error__', this, 'remove', error, query, options) | ||
reject(error) | ||
} else { | ||
this.emit('remove', this, result, query, options) | ||
resolve(result) | ||
} | ||
} | ||
) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Remove documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#removing-documents | ||
* | ||
* @param {Object} [query] | ||
* @param {Object} [options] | ||
* @return {Promise.<number>} | ||
*/ | ||
remove(query = {}, options = {}) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.remove( | ||
query, | ||
options, | ||
(error, result) => { | ||
if (error) { | ||
this.emit('removeError', this, error, query, options) | ||
this.emit('__error__', this, 'remove', error, query, options) | ||
reject(error) | ||
} else { | ||
this.emit('remove', this, result, query, options) | ||
resolve(result) | ||
} | ||
} | ||
) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Count documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#counting-documents | ||
* | ||
* @example | ||
* datastore.count({ ... }).limit(...).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.count({ ... }) | ||
* // or | ||
* await datastore.count({ ... }).sort(...).limit(...) | ||
* | ||
* @param {Object} [query] | ||
* @return {Cursor} | ||
*/ | ||
count(query = {}) { | ||
return new Cursor( | ||
this.__original.count(query), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('countError', this, error, query) | ||
this.emit('__error__', this, 'count', error, query) | ||
} else { | ||
this.emit('count', this, result, query) | ||
} | ||
} | ||
) | ||
} | ||
/** | ||
* Count documents that match a query. | ||
* | ||
* It's basically the same as the original: | ||
* https://github.com/louischatriot/nedb#counting-documents | ||
* | ||
* @example | ||
* datastore.count({ ... }).limit(...).then(...) | ||
* | ||
* @example | ||
* // in an async function | ||
* await datastore.count({ ... }) | ||
* // or | ||
* await datastore.count({ ... }).sort(...).limit(...) | ||
* | ||
* @param {Object} [query] | ||
* @return {Cursor} | ||
*/ | ||
count(query = {}) { | ||
return new Cursor( | ||
this.__original.count(query), | ||
this.load(), | ||
(error, result) => { | ||
if (error) { | ||
this.emit('countError', this, error, query) | ||
this.emit('__error__', this, 'count', error, query) | ||
} else { | ||
this.emit('count', this, result, query) | ||
} | ||
} | ||
) | ||
} | ||
/** | ||
* https://github.com/louischatriot/nedb#indexing | ||
* | ||
* @param {Object} options | ||
* @return {Promise.<undefined>} | ||
*/ | ||
ensureIndex(options) { | ||
return new Promise((resolve, reject) => { | ||
this.__original.ensureIndex(options, (error) => { | ||
if (error) { | ||
this.emit('ensureIndexError', this, error, options) | ||
this.emit('__error__', this, 'ensureIndex', error, options) | ||
reject(error) | ||
} else { | ||
this.emit('ensureIndex', this, options) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
/** | ||
* https://github.com/louischatriot/nedb#indexing | ||
* | ||
* @param {Object} options | ||
* @return {Promise.<undefined>} | ||
*/ | ||
ensureIndex(options) { | ||
return new Promise((resolve, reject) => { | ||
this.__original.ensureIndex(options, (error) => { | ||
if (error) { | ||
this.emit('ensureIndexError', this, error, options) | ||
this.emit('__error__', this, 'ensureIndex', error, options) | ||
reject(error) | ||
} else { | ||
this.emit('ensureIndex', this, options) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
/** | ||
* https://github.com/louischatriot/nedb#indexing | ||
* | ||
* @param {string} field | ||
* @return {Promise.<undefined>} | ||
*/ | ||
removeIndex(field) { | ||
return new Promise((resolve, reject) => { | ||
this.__original.removeIndex(field, (error) => { | ||
if (error) { | ||
this.emit('removeIndexError', this, error, field) | ||
this.emit('__error__', this, 'removeIndex', error, field) | ||
reject(error) | ||
} else { | ||
this.emit('removeIndex', this, field) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
/** | ||
* https://github.com/louischatriot/nedb#indexing | ||
* | ||
* @param {string} field | ||
* @return {Promise.<undefined>} | ||
*/ | ||
removeIndex(field) { | ||
return new Promise((resolve, reject) => { | ||
this.__original.removeIndex(field, (error) => { | ||
if (error) { | ||
this.emit('removeIndexError', this, error, field) | ||
this.emit('__error__', this, 'removeIndex', error, field) | ||
reject(error) | ||
} else { | ||
this.emit('removeIndex', this, field) | ||
resolve() | ||
} | ||
}) | ||
}) | ||
} | ||
/** | ||
* Create a database instance. | ||
* | ||
* Use this over `new Datastore(...)` to access | ||
* original nedb datastore properties, such as | ||
* `datastore.persistance`. | ||
* | ||
* Note that the datastore will be created | ||
* relative to `process.cwd()` | ||
* (unless an absolute path was passed). | ||
* | ||
* For more information visit: | ||
* https://github.com/louischatriot/nedb#creatingloading-a-database | ||
* | ||
* @param {string|Object} options | ||
* @return {Proxy.<static>} | ||
*/ | ||
static create(options) { | ||
return new Proxy(new this(options), { | ||
get(target, key) { | ||
/** | ||
* Create a database instance. | ||
* | ||
* Use this over `new Datastore(...)` to access | ||
* original nedb datastore properties, such as | ||
* `datastore.persistence`. | ||
* | ||
* Note that the datastore will be created | ||
* relative to `process.cwd()` | ||
* (unless an absolute path was passed). | ||
* | ||
* For more information visit: | ||
* https://github.com/louischatriot/nedb#creatingloading-a-database | ||
* | ||
* @param {string|Object} [pathOrOptions] | ||
* @return {Proxy.<static>} | ||
*/ | ||
static create(pathOrOptions) { | ||
return new Proxy(new this(pathOrOptions), { | ||
get(target, key) { | ||
return target[key] | ||
? target[key] | ||
: target.__original[key] | ||
}, | ||
}, | ||
set(target, key, value) { | ||
set(target, key, value) { | ||
return target.__original.hasOwnProperty(key) | ||
? (target.__original[key] = value) | ||
: (target[key] = value) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
module.exports = Datastore |
const | ||
fs = require('fs'), | ||
path = require('path'), | ||
{ expect } = require('chai'), | ||
Datastore = require('../src/Datastore') | ||
fs = require('fs'), | ||
path = require('path'), | ||
{ expect } = require('chai'), | ||
Datastore = require('../src/Datastore') | ||
@@ -10,33 +10,33 @@ const root = path.dirname(__dirname) | ||
describe('testing datastore creation', () => { | ||
describe('new Datastore(\'test.db\')', () => { | ||
it('should create test.db based on string filename', () => { | ||
let datastore = Datastore.create('test.db') | ||
return datastore.load() | ||
.then(() => { | ||
fs.unlinkSync( | ||
path.join(root, 'test.db') | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('new Datastore(\'test.db\')', () => { | ||
it('should create test.db based on string filename', () => { | ||
let datastore = Datastore.create('test.db') | ||
return datastore.load() | ||
.then(() => { | ||
fs.unlinkSync( | ||
path.join(root, 'test.db') | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('new Datastore({ filename: \'test.db\' })', () => { | ||
it('sould create test.db based on object parameters', () => { | ||
let datastore = Datastore.create({ filename: 'test.db' }) | ||
return datastore.load() | ||
.then(() => { | ||
fs.unlinkSync( | ||
path.join(root, 'test.db') | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('new Datastore({ filename: \'test.db\' })', () => { | ||
it('sould create test.db based on object parameters', () => { | ||
let datastore = Datastore.create({ filename: 'test.db' }) | ||
return datastore.load() | ||
.then(() => { | ||
fs.unlinkSync( | ||
path.join(root, 'test.db') | ||
) | ||
}) | ||
}) | ||
}) | ||
describe('new Datastore()', () => { | ||
it('should create in memory only database', (done) => { | ||
let datastore = Datastore.create() | ||
expect(datastore.__original.inMemoryOnly).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
}) | ||
describe('new Datastore()', () => { | ||
it('should create in memory only database', (done) => { | ||
let datastore = Datastore.create() | ||
expect(datastore.__original.inMemoryOnly).to.equal(true) | ||
done() | ||
}) | ||
}) | ||
}) |
const | ||
{ expect } = require('chai'), | ||
Datastore = require('../src/Datastore') | ||
{ expect } = require('chai'), | ||
Datastore = require('../src/Datastore') | ||
describe('testing document insertion', () => { | ||
let documents = [ | ||
{ name: 'first document' }, | ||
{ name: 'second document' }, | ||
{ name: 'third document' } | ||
] | ||
let documents = [ | ||
{ name: 'first document' }, | ||
{ name: 'second document' }, | ||
{ name: 'third document' } | ||
] | ||
describe('single', () => { | ||
it('should insert single document', () => { | ||
let datastore = Datastore.create() | ||
return datastore.insert(documents[0]) | ||
.then((inserted) => { | ||
expect(inserted).to.be.an('object').that.have.all.keys('_id', 'name') | ||
}) | ||
}) | ||
}) | ||
describe('single', () => { | ||
it('should insert single document', () => { | ||
let datastore = Datastore.create() | ||
return datastore.insert(documents[0]) | ||
.then((inserted) => { | ||
expect(inserted).to.be.an('object').that.have.all.keys('_id', 'name') | ||
}) | ||
}) | ||
}) | ||
describe('bulk', () => { | ||
it('should insert multiple documents', () => { | ||
let datastore = Datastore.create() | ||
return datastore.insert(documents) | ||
.then((inserted) => { | ||
expect(inserted).to.be.an('array').that.have.lengthOf(3) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('bulk', () => { | ||
it('should insert multiple documents', () => { | ||
let datastore = Datastore.create() | ||
return datastore.insert(documents) | ||
.then((inserted) => { | ||
expect(inserted).to.be.an('array').that.have.lengthOf(3) | ||
}) | ||
}) | ||
}) | ||
}) |
const | ||
{ expect } = require('chai'), | ||
Datastore = require('../src/Datastore') | ||
Datastore = require('../src/Datastore') | ||
describe('testing document finding', () => { | ||
let documents = [ | ||
{ name: 'first document' }, | ||
{ name: 'second document' }, | ||
{ name: 'third document' } | ||
] | ||
let documents = [ | ||
{ name: 'first document' }, | ||
{ name: 'second document' }, | ||
{ name: 'third document' } | ||
] | ||
describe('single', () => { | ||
let datastore = Datastore.create(), | ||
describe('single', () => { | ||
let datastore = Datastore.create(), | ||
insert = datastore.insert(documents) | ||
it('should find the first inserted doc', () => { | ||
return insert | ||
.then((inserted) => { | ||
return datastore.findOne() | ||
}).then((result) => { | ||
expect(result).to.be.an('object').that.has.all.keys('_id', 'name') | ||
}) | ||
}) | ||
it('should find the first inserted doc', () => { | ||
return insert | ||
.then((inserted) => { | ||
return datastore.findOne() | ||
}).then((result) => { | ||
expect(result).to.be.an('object').that.has.all.keys('_id', 'name') | ||
}) | ||
}) | ||
@@ -33,15 +33,15 @@ it('should find the last inserted doc when sorting backwards', () => { | ||
}) | ||
}) | ||
}) | ||
describe('bulk', () => { | ||
let datastore = Datastore.create() | ||
it('should find all inserted docs', () => { | ||
return datastore.insert(documents) | ||
.then(() => { | ||
return datastore.find().exec() | ||
}).then((result) => { | ||
expect(result).to.be.an('array').that.has.lengthOf(3) | ||
}) | ||
}) | ||
}) | ||
describe('bulk', () => { | ||
let datastore = Datastore.create() | ||
it('should find all inserted docs', () => { | ||
return datastore.insert(documents) | ||
.then(() => { | ||
return datastore.find().exec() | ||
}).then((result) => { | ||
expect(result).to.be.an('array').that.has.lengthOf(3) | ||
}) | ||
}) | ||
}) | ||
@@ -48,0 +48,0 @@ describe('find().then()', () => { |
@@ -81,2 +81,2 @@ const | ||
}) | ||
}) | ||
}) |
@@ -18,2 +18,2 @@ const | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52742
976