|
|
@ -76,9 +76,14 @@ class Datastore extends EventEmitter { |
|
|
|
// Queue a load of the database right away and call the onload handler
|
|
|
|
// 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
|
|
|
|
// 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) { |
|
|
|
if (this.autoload) { |
|
|
|
this.loadDatabase(options.onload || (err => { |
|
|
|
this.autoloadPromise = this.loadDatabaseAsync() |
|
|
|
if (err) throw err |
|
|
|
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) |
|
|
|
else expiredDocsIds.push(doc._id) |
|
|
|
}) |
|
|
|
}) |
|
|
|
for (const _id of expiredDocsIds) { |
|
|
|
for (const _id of expiredDocsIds) { |
|
|
|
await new Promise((resolve, reject) => { |
|
|
|
await this._removeAsync({ _id: _id }, {}) |
|
|
|
this._remove({ _id: _id }, {}, err => { |
|
|
|
|
|
|
|
if (err) return reject(err) |
|
|
|
|
|
|
|
return resolve() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else validDocs.push(...docs) |
|
|
|
} else validDocs.push(...docs) |
|
|
|
return validDocs |
|
|
|
return validDocs |
|
|
@ -405,7 +405,7 @@ class Datastore extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
insertAsync (...args) { |
|
|
|
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 option is set, check whether we need to insert the doc
|
|
|
|
if (upsert) { |
|
|
|
if (upsert) { |
|
|
|
// Need to use an internal function not tied to the executor to avoid deadlock
|
|
|
|
const cursor = new Cursor(this, query, x => x, true) |
|
|
|
const cursor = new Cursor(this, query) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const docs = await new Promise((resolve, reject) => { |
|
|
|
// Need to use an internal function not tied to the executor to avoid deadlock
|
|
|
|
cursor.limit(1)._exec((err, docs) => { |
|
|
|
const docs = await cursor.limit(1)._execAsync() |
|
|
|
if (err) reject(err) |
|
|
|
|
|
|
|
else resolve(docs) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (docs.length !== 1) { |
|
|
|
if (docs.length !== 1) { |
|
|
|
let toBeInserted |
|
|
|
let toBeInserted |
|
|
@ -552,13 +547,8 @@ class Datastore extends EventEmitter { |
|
|
|
// strip it from all operators and update it according to updateQuery
|
|
|
|
// strip it from all operators and update it according to updateQuery
|
|
|
|
toBeInserted = model.modify(model.deepCopy(query, true), updateQuery) |
|
|
|
toBeInserted = model.modify(model.deepCopy(query, true), updateQuery) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const newDoc = await this._insertAsync(toBeInserted) |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return { numAffected: 1, affectedDocuments: newDoc, upsert: true } |
|
|
|
this._insert(toBeInserted, (err, newDoc) => { |
|
|
|
|
|
|
|
if (err) return reject(err) |
|
|
|
|
|
|
|
return resolve({ numAffected: 1, affectedDocuments: newDoc, upsert: true }) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// Perform the update
|
|
|
|
// Perform the update
|
|
|
@ -603,7 +593,7 @@ class Datastore extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
updateAsync (...args) { |
|
|
|
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) { |
|
|
|
removeAsync (...args) { |
|
|
|
return this.executor.pushAsync(() => this._removeAsync(args)) |
|
|
|
return this.executor.pushAsync(() => this._removeAsync(...args)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|