|
|
|
@ -76,9 +76,14 @@ class Datastore extends EventEmitter { |
|
|
|
|
// Queue a load of the database right away and call the onload handler
|
|
|
|
|
// By default (no onload handler), if there is an error there, no operation will be possible so warn the user by throwing an exception
|
|
|
|
|
if (this.autoload) { |
|
|
|
|
this.loadDatabase(options.onload || (err => { |
|
|
|
|
if (err) throw err |
|
|
|
|
})) |
|
|
|
|
this.autoloadPromise = this.loadDatabaseAsync() |
|
|
|
|
this.autoloadPromise |
|
|
|
|
.then(() => { |
|
|
|
|
if (options.onload) options.onload() |
|
|
|
|
}, err => { |
|
|
|
|
if (options.onload) options.onload(err) |
|
|
|
|
else throw err |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -298,12 +303,7 @@ class Datastore extends EventEmitter { |
|
|
|
|
else expiredDocsIds.push(doc._id) |
|
|
|
|
}) |
|
|
|
|
for (const _id of expiredDocsIds) { |
|
|
|
|
await new Promise((resolve, reject) => { |
|
|
|
|
this._remove({ _id: _id }, {}, err => { |
|
|
|
|
if (err) return reject(err) |
|
|
|
|
return resolve() |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
await this._removeAsync({ _id: _id }, {}) |
|
|
|
|
} |
|
|
|
|
} else validDocs.push(...docs) |
|
|
|
|
return validDocs |
|
|
|
@ -405,7 +405,7 @@ class Datastore extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
insertAsync (...args) { |
|
|
|
|
return this.executor.push({ this: this, fn: this._insertAsync, arguments: args, async: true }) |
|
|
|
|
return this.executor.pushAsync(() => this._insertAsync(...args)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -530,15 +530,10 @@ class Datastore extends EventEmitter { |
|
|
|
|
|
|
|
|
|
// If upsert option is set, check whether we need to insert the doc
|
|
|
|
|
if (upsert) { |
|
|
|
|
// Need to use an internal function not tied to the executor to avoid deadlock
|
|
|
|
|
const cursor = new Cursor(this, query) |
|
|
|
|
const cursor = new Cursor(this, query, x => x, true) |
|
|
|
|
|
|
|
|
|
const docs = await new Promise((resolve, reject) => { |
|
|
|
|
cursor.limit(1)._exec((err, docs) => { |
|
|
|
|
if (err) reject(err) |
|
|
|
|
else resolve(docs) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
// Need to use an internal function not tied to the executor to avoid deadlock
|
|
|
|
|
const docs = await cursor.limit(1)._execAsync() |
|
|
|
|
|
|
|
|
|
if (docs.length !== 1) { |
|
|
|
|
let toBeInserted |
|
|
|
@ -552,13 +547,8 @@ class Datastore extends EventEmitter { |
|
|
|
|
// strip it from all operators and update it according to updateQuery
|
|
|
|
|
toBeInserted = model.modify(model.deepCopy(query, true), updateQuery) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this._insert(toBeInserted, (err, newDoc) => { |
|
|
|
|
if (err) return reject(err) |
|
|
|
|
return resolve({ numAffected: 1, affectedDocuments: newDoc, upsert: true }) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
const newDoc = await this._insertAsync(toBeInserted) |
|
|
|
|
return { numAffected: 1, affectedDocuments: newDoc, upsert: true } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// Perform the update
|
|
|
|
@ -603,7 +593,7 @@ class Datastore extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateAsync (...args) { |
|
|
|
|
return this.executor.pushAsync(() => this._updateAsync(args)) |
|
|
|
|
return this.executor.pushAsync(() => this._updateAsync(...args)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -651,7 +641,7 @@ class Datastore extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
removeAsync (...args) { |
|
|
|
|
return this.executor.pushAsync(() => this._removeAsync(args)) |
|
|
|
|
return this.executor.pushAsync(() => this._removeAsync(...args)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|