From 70229d3da5fce84f8c29da5fb50ef7bfefc7c343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Thu, 21 Oct 2021 22:46:23 +0200 Subject: [PATCH] WIP: ensureIndex --- lib/datastore.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 5b157d3..362a774 100755 --- a/lib/datastore.js +++ b/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 }]) } /**