|
|
|
@ -129,7 +129,6 @@ class Datastore extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async ensureIndexAsync (options = {}) { |
|
|
|
|
console.log('exec now') |
|
|
|
|
if (!options.fieldName) { |
|
|
|
|
const err = new Error('Cannot create an index without a fieldName') |
|
|
|
|
err.missingFieldName = true |
|
|
|
@ -451,15 +450,18 @@ class Datastore extends EventEmitter { |
|
|
|
|
* @param {Function} callback Optional callback, signature: err, count |
|
|
|
|
*/ |
|
|
|
|
count (query, callback) { |
|
|
|
|
const cursor = new Cursor(this, query, function (err, docs, callback) { |
|
|
|
|
if (err) { return callback(err) } |
|
|
|
|
return callback(null, docs.length) |
|
|
|
|
}) |
|
|
|
|
const cursor = this.countAsync(query) |
|
|
|
|
|
|
|
|
|
if (typeof callback === 'function') cursor.exec(callback) |
|
|
|
|
if (typeof callback === 'function') callbackify(cursor.execAsync.bind(cursor))(callback) |
|
|
|
|
else return cursor |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
countAsync (query) { |
|
|
|
|
const cursor = new Cursor(this, query, async docs => docs.length, true) |
|
|
|
|
|
|
|
|
|
return cursor // this is a trick, Cursor itself is a thenable, which allows to await it
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Find all documents matching the query |
|
|
|
|
* If no callback is passed, we return the cursor so that user can limit, skip and finally exec |
|
|
|
@ -478,17 +480,17 @@ class Datastore extends EventEmitter { |
|
|
|
|
} // If not assume projection is an object and callback undefined
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const cursor = new Cursor(this, query, function (err, docs, callback) { |
|
|
|
|
if (err) { return callback(err) } |
|
|
|
|
const cursor = this.findAsync(query, projection) |
|
|
|
|
|
|
|
|
|
const res = docs.map(doc => model.deepCopy(doc)) |
|
|
|
|
if (typeof callback === 'function') callbackify(cursor.execAsync.bind(cursor))(callback) |
|
|
|
|
else return cursor |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return callback(null, res) |
|
|
|
|
}) |
|
|
|
|
findAsync (query, projection = {}) { |
|
|
|
|
const cursor = new Cursor(this, query, docs => docs.map(doc => model.deepCopy(doc)), true) |
|
|
|
|
|
|
|
|
|
cursor.projection(projection) |
|
|
|
|
if (typeof callback === 'function') cursor.exec(callback) |
|
|
|
|
else return cursor |
|
|
|
|
return cursor |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -508,17 +510,19 @@ class Datastore extends EventEmitter { |
|
|
|
|
} // If not assume projection is an object and callback undefined
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const cursor = new Cursor(this, query, (err, docs, callback) => { |
|
|
|
|
if (err) return callback(err) |
|
|
|
|
if (docs.length === 1) return callback(null, model.deepCopy(docs[0])) |
|
|
|
|
else return callback(null, null) |
|
|
|
|
}) |
|
|
|
|
const cursor = this.findOneAsync(query, projection) |
|
|
|
|
|
|
|
|
|
cursor.projection(projection).limit(1) |
|
|
|
|
if (typeof callback === 'function') cursor.exec(callback) |
|
|
|
|
if (typeof callback === 'function') callbackify(cursor.execAsync.bind(cursor))(callback) |
|
|
|
|
else return cursor |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
findOneAsync (query, projection = {}) { |
|
|
|
|
const cursor = new Cursor(this, query, docs => docs.length === 1 ? model.deepCopy(docs[0]) : null, true) |
|
|
|
|
|
|
|
|
|
cursor.projection(projection).limit(1) |
|
|
|
|
return cursor |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Update all docs matching query. |
|
|
|
|
* Use Datastore.update which has the same signature |
|
|
|
|