WIP: remove

pull/11/head
Timothée Rebours 3 years ago
parent a694d6ee14
commit 4c5fc252c2
  1. 39
      lib/datastore.js

@ -154,13 +154,15 @@ class Datastore extends EventEmitter {
* @param {String} fieldName * @param {String} fieldName
* @param {Function} callback Optional callback, signature: err * @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 = () => {}) { removeIndex (fieldName, callback = () => {}) {
callbackify(this.removeIndexAsync.bind(this))(fieldName, callback)
}
async removeIndexAsync (fieldName) {
delete this.indexes[fieldName] delete this.indexes[fieldName]
this.persistence.persistNewState([{ $$indexRemoved: fieldName }], err => { await this.persistence.persistNewStateAsync([{ $$indexRemoved: fieldName }])
if (err) return callback(err)
return callback(null)
})
} }
/** /**
@ -438,7 +440,7 @@ class Datastore extends EventEmitter {
} }
insertAsync () { 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 }) 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. * Remove all docs matching the query.
* Use Datastore.remove which has the same signature * Use Datastore.remove which has the same signature
@ -649,14 +655,17 @@ class Datastore extends EventEmitter {
options = {} options = {}
} }
const callback = cb || (() => {}) const callback = cb || (() => {})
callbackify(this._removeAsync.bind(this))(query, options, callback)
}
async _removeAsync (query, options = {}) {
const multi = options.multi !== undefined ? options.multi : false const multi = options.multi !== undefined ? options.multi : false
this.getCandidates(query, true, (err, candidates) => { const candidates = await this.getCandidatesAsync(query, true)
if (err) return callback(err)
const removedDocs = [] const removedDocs = []
let numRemoved = 0 let numRemoved = 0
try {
candidates.forEach(d => { candidates.forEach(d => {
if (model.match(d, query) && (multi || numRemoved === 0)) { if (model.match(d, query) && (multi || numRemoved === 0)) {
numRemoved += 1 numRemoved += 1
@ -664,20 +673,18 @@ class Datastore extends EventEmitter {
this.removeFromIndexes(d) this.removeFromIndexes(d)
} }
}) })
} catch (err) {
return callback(err)
}
this.persistence.persistNewState(removedDocs, err => { await this.persistence.persistNewStateAsync(removedDocs)
if (err) return callback(err) return numRemoved
return callback(null, numRemoved)
})
})
} }
remove () { remove () {
this.executor.push({ this: this, fn: this._remove, arguments: arguments }) 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 module.exports = Datastore

Loading…
Cancel
Save