nedb-promises
Advanced tools
Comparing version 6.0.3 to 6.1.0
{ | ||
"name": "nedb-promises", | ||
"version": "6.0.3", | ||
"version": "6.1.0", | ||
"description": "A dead-simple promise wrapper for nedb.", | ||
@@ -23,3 +23,3 @@ "main": "index.js", | ||
"author": "Kristóf Baján", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -30,9 +30,9 @@ "url": "https://github.com/bajankristof/nedb-promises/issues" | ||
"dependencies": { | ||
"@seald-io/nedb": "^2.2.0" | ||
"@seald-io/nedb": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.6.0", | ||
"jest": "^27.4.7", | ||
"jest": "^28.1.1", | ||
"jsdoc-to-markdown": "^7.1.0" | ||
} | ||
} |
@@ -7,11 +7,8 @@ const OriginalCursor = require('@seald-io/nedb/lib/cursor'); | ||
class Cursor { | ||
constructor(original, prerequisite, callback) { | ||
if ( ! (original instanceof OriginalCursor)) { | ||
constructor(datastore, op, ...args) { | ||
const cursor = datastore.__original[op](...args); | ||
if (!(cursor instanceof OriginalCursor)) { | ||
throw new TypeError(`Unexpected ${typeof original}, expected: Cursor (nedb/lib/cursor)`); | ||
} | ||
if ( ! (prerequisite instanceof Promise)) { | ||
prerequisite = Promise.resolve(); | ||
} | ||
Object.defineProperties(this, { | ||
@@ -22,18 +19,25 @@ __original: { | ||
writable: false, | ||
value: original, | ||
value: cursor, | ||
}, | ||
__prerequisite: { | ||
__datastore: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: prerequisite, | ||
value: datastore, | ||
}, | ||
__callback: { | ||
__op: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: callback, | ||
value: op, | ||
}, | ||
__args: { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: args, | ||
}, | ||
}); | ||
@@ -112,16 +116,12 @@ } | ||
*/ | ||
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); | ||
}); | ||
}); | ||
}); | ||
async exec() { | ||
await this.__datastore.load(); | ||
try { | ||
const result = await this.__original.execAsync(); | ||
this.__datastore.broadcastSuccess(this.__op, result, ...this.__args); | ||
return result; | ||
} catch (error) { | ||
this.__datastore.broadcastError(this.__op, result, ...this.__args); | ||
throw error; | ||
} | ||
} | ||
@@ -128,0 +128,0 @@ |
@@ -64,2 +64,40 @@ const EventEmitter = require('events'); | ||
/** | ||
* Create a database instance. | ||
* | ||
* Use this over `new Datastore(...)` to access | ||
* original nedb datastore properties, such as | ||
* `datastore.persistence`. | ||
* | ||
* Note that this method only creates the `Datastore` | ||
* class instance, not the datastore file itself. | ||
* The file will only be created once an operation | ||
* is issued against the datastore or if you call | ||
* the `load` instance method explicitly. | ||
* | ||
* The path (if specified) will be 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) { | ||
return Object.prototype.hasOwnProperty.call(target.__original, key) | ||
? (target.__original[key] = value) | ||
: (target[key] = value); | ||
}, | ||
}); | ||
} | ||
/** | ||
* Datastore constructor... | ||
@@ -86,2 +124,8 @@ * | ||
const datastore = new OriginalDatastore( | ||
typeof pathOrOptions === 'string' | ||
? { filename: pathOrOptions } | ||
: pathOrOptions, | ||
); | ||
Object.defineProperties(this, { | ||
@@ -98,3 +142,3 @@ __loaded: { | ||
writable: false, | ||
value: new OriginalDatastore(pathOrOptions), | ||
value: datastore, | ||
}, | ||
@@ -121,14 +165,5 @@ }); | ||
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(); | ||
} | ||
}); | ||
}); | ||
this.__loaded = this.__original.loadDatabaseAsync() | ||
.then(() => this.broadcastSuccess('load')) | ||
.catch((error) => { this.broadcastError('load', error); throw error; }); | ||
} | ||
@@ -166,14 +201,3 @@ | ||
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, 'find', query, projection); | ||
} | ||
@@ -203,14 +227,3 @@ | ||
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, 'findOne', query, projection); | ||
} | ||
@@ -227,17 +240,12 @@ | ||
*/ | ||
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); | ||
} | ||
}); | ||
}); | ||
}); | ||
async insert(docs) { | ||
await this.load(); | ||
try { | ||
const result = await this.__original.insertAsync(docs); | ||
this.broadcastSuccess('insert', docs); | ||
return result; | ||
} catch (error) { | ||
this.broadcastError('insert', error, docs); | ||
throw error; | ||
} | ||
} | ||
@@ -261,25 +269,13 @@ | ||
*/ | ||
update(query, update, options = {}) { | ||
return this.load().then(() => { | ||
return new Promise((resolve, reject) => { | ||
this.__original.update( | ||
query, | ||
update, | ||
options, | ||
(error, numAffected, affectedDocuments) => { | ||
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); | ||
} | ||
}, | ||
); | ||
}); | ||
}); | ||
async update(query, update, options = {}) { | ||
await this.load(); | ||
try { | ||
const { numAffected, affectedDocuments } = await this.__original.updateAsync(query, update, options); | ||
const result = options.returnUpdatedDocs ? affectedDocuments : numAffected; | ||
this.broadcastSuccess('update', result, query, update, options); | ||
return result; | ||
} catch (error) { | ||
this.broadcastError('update', error, query, update, options); | ||
throw error; | ||
} | ||
} | ||
@@ -297,21 +293,12 @@ | ||
*/ | ||
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); | ||
} | ||
}, | ||
); | ||
}); | ||
}); | ||
async remove(query = {}, options = {}) { | ||
await this.load(); | ||
try { | ||
const result = await this.__original.removeAsync(query, options); | ||
this.broadcastSuccess('remove', result, query, options); | ||
return result; | ||
} catch (error) { | ||
this.broadcastError('remove', error, query, options); | ||
throw error; | ||
} | ||
} | ||
@@ -338,14 +325,3 @@ | ||
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); | ||
} | ||
}, | ||
); | ||
return new Cursor(this, 'count', query); | ||
} | ||
@@ -359,15 +335,11 @@ | ||
*/ | ||
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(); | ||
} | ||
}); | ||
}); | ||
async ensureIndex(options) { | ||
try { | ||
const result = await this.__original.ensureIndexAsync(options); | ||
this.broadcastSuccess('ensureIndex', result, options); | ||
return result; | ||
} catch (error) { | ||
this.broadcastError('ensureIndex', error, field); | ||
throw error; | ||
} | ||
} | ||
@@ -381,53 +353,42 @@ | ||
*/ | ||
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(); | ||
} | ||
}); | ||
}); | ||
async removeIndex(field) { | ||
try { | ||
const result = await this.__original.removeIndexAsync(field); | ||
this.broadcastSuccess('removeIndex', result, field); | ||
return result; | ||
} catch (error) { | ||
this.broadcastError('removeIndex', error, field); | ||
throw error; | ||
} | ||
} | ||
/** | ||
* Create a database instance. | ||
* | ||
* Use this over `new Datastore(...)` to access | ||
* original nedb datastore properties, such as | ||
* `datastore.persistence`. | ||
* | ||
* Note that this method only creates the `Datastore` | ||
* class instance, not the datastore file itself. | ||
* The file will only be created once an operation | ||
* is issued against the datastore or if you call | ||
* the `load` instance method explicitly. | ||
* Broadcasts operation success messages. | ||
* | ||
* The path (if specified) will be relative to `process.cwd()` | ||
* (unless an absolute path was passed). | ||
* | ||
* For more information visit: | ||
* https://github.com/louischatriot/nedb#creatingloading-a-database | ||
* @param {string} op | ||
* @param {*} result | ||
* @param {...*} args | ||
* | ||
* @param {string|Object} [pathOrOptions] | ||
* @return {Proxy.<static>} | ||
* @return {undefined} | ||
* @private | ||
*/ | ||
static create(pathOrOptions) { | ||
return new Proxy(new this(pathOrOptions), { | ||
get(target, key) { | ||
return target[key] | ||
? target[key] | ||
: target.__original[key]; | ||
}, | ||
broadcastSuccess(op, result, ...args) { | ||
this.emit(op, this, result, ...args); | ||
return this; | ||
} | ||
set(target, key, value) { | ||
return Object.prototype.hasOwnProperty.call(target.__original, key) | ||
? (target.__original[key] = value) | ||
: (target[key] = value); | ||
}, | ||
}); | ||
/** | ||
* Broadcasts operation error messages. | ||
* | ||
* @param {string} op | ||
* @param {Error} error | ||
* @param {...*} args | ||
* | ||
* @return {undefined} | ||
* @private | ||
*/ | ||
broadcastError(op, error, ...args) { | ||
this.emit(`${op}Error`, this, error, ...args); | ||
this.emit('__error__', this, op, error, ...args); | ||
return this; | ||
} | ||
@@ -434,0 +395,0 @@ } |
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
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
63651
1211
+ Added@seald-io/nedb@3.1.0(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removed@seald-io/nedb@2.2.2(transitive)
- Removedasync@0.2.10(transitive)
Updated@seald-io/nedb@^3.0.0