WIP: ensureIndex

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

@ -125,12 +125,17 @@ class Datastore extends EventEmitter {
* @param {Function} callback Optional callback, signature: err
*/
ensureIndex (options = {}, callback = () => {}) {
callbackify(this.ensureIndexAsync.bind(this))(options, callback)
}
async ensureIndexAsync (options = {}) {
console.log('exec now')
if (!options.fieldName) {
const err = new Error('Cannot create an index without a fieldName')
err.missingFieldName = true
return callback(err)
throw err
}
if (this.indexes[options.fieldName]) return callback(null)
if (this.indexes[options.fieldName]) return
this.indexes[options.fieldName] = new Index(options)
if (options.expireAfterSeconds !== undefined) this.ttlIndexes[options.fieldName] = options.expireAfterSeconds // With this implementation index creation is not necessary to ensure TTL but we stick with MongoDB's API here
@ -139,14 +144,11 @@ class Datastore extends EventEmitter {
this.indexes[options.fieldName].insert(this.getAllData())
} catch (e) {
delete this.indexes[options.fieldName]
return callback(e)
throw e
}
// We may want to force all options to be persisted including defaults, not just the ones passed the index creation function
this.persistence.persistNewState([{ $$indexCreated: options }], err => {
if (err) return callback(err)
return callback(null)
})
await this.persistence.persistNewStateAsync([{ $$indexCreated: options }])
}
/**

Loading…
Cancel
Save