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 {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,14 +655,17 @@ 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 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
@ -664,20 +673,18 @@ class Datastore extends EventEmitter {
this.removeFromIndexes(d)
}
})
} catch (err) {
return callback(err)
}
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

Loading…
Cancel
Save