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