diff --git a/lib/datastore.js b/lib/datastore.js index 77a19ef..5b157d3 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -154,13 +154,15 @@ class Datastore extends EventEmitter { * @param {String} fieldName * @param {Function} callback Optional callback, signature: err */ + // TODO: contrary to what is said in the JSDoc, this function should probably be called through the executor, it persists a new state removeIndex (fieldName, callback = () => {}) { + callbackify(this.removeIndexAsync.bind(this))(fieldName, callback) + } + + async removeIndexAsync (fieldName) { delete this.indexes[fieldName] - this.persistence.persistNewState([{ $$indexRemoved: fieldName }], err => { - if (err) return callback(err) - return callback(null) - }) + await this.persistence.persistNewStateAsync([{ $$indexRemoved: fieldName }]) } /** @@ -438,7 +440,7 @@ class Datastore extends EventEmitter { } insertAsync () { - this.executor.push({ this: this, fn: this._insertAsync, arguments: arguments, async: true }) + return this.executor.push({ this: this, fn: this._insertAsync, arguments: arguments, async: true }) } /** @@ -632,6 +634,10 @@ class Datastore extends EventEmitter { this.executor.push({ this: this, fn: this._update, arguments: arguments }) } + updateAsync () { + return this.executor.pushAsync({ this: this, fn: this._updateAsync, arguments: arguments }) + } + /** * Remove all docs matching the query. * Use Datastore.remove which has the same signature @@ -649,35 +655,36 @@ class Datastore extends EventEmitter { options = {} } const callback = cb || (() => {}) + + callbackify(this._removeAsync.bind(this))(query, options, callback) + } + + async _removeAsync (query, options = {}) { const multi = options.multi !== undefined ? options.multi : false - this.getCandidates(query, true, (err, candidates) => { - if (err) return callback(err) - const removedDocs = [] - let numRemoved = 0 + const candidates = await this.getCandidatesAsync(query, true) + const removedDocs = [] + let numRemoved = 0 - try { - candidates.forEach(d => { - if (model.match(d, query) && (multi || numRemoved === 0)) { - numRemoved += 1 - removedDocs.push({ $$deleted: true, _id: d._id }) - this.removeFromIndexes(d) - } - }) - } catch (err) { - return callback(err) + candidates.forEach(d => { + if (model.match(d, query) && (multi || numRemoved === 0)) { + numRemoved += 1 + removedDocs.push({ $$deleted: true, _id: d._id }) + this.removeFromIndexes(d) } - - this.persistence.persistNewState(removedDocs, err => { - if (err) return callback(err) - return callback(null, numRemoved) - }) }) + + await this.persistence.persistNewStateAsync(removedDocs) + return numRemoved } remove () { this.executor.push({ this: this, fn: this._remove, arguments: arguments }) } + + removeAsync () { + return this.executor.pushAsync({ this: this, fn: this._removeAsync, arguments: arguments }) + } } module.exports = Datastore