|
|
|
@ -1,8 +1,7 @@ |
|
|
|
|
/** |
|
|
|
|
* The datastore itself |
|
|
|
|
* TODO |
|
|
|
|
* Queue operations |
|
|
|
|
* Update and removes should only modify the corresponding part of the database |
|
|
|
|
* Update and removes should only modify the corresponding part of the database (or use much faster append-only format) |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
var fs = require('fs') |
|
|
|
@ -10,6 +9,7 @@ var fs = require('fs') |
|
|
|
|
, customUtils = require('./customUtils') |
|
|
|
|
, model = require('./model') |
|
|
|
|
, async = require('async') |
|
|
|
|
, executor = require('./executor') |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -76,7 +76,7 @@ Datastore.treatRawData = function (rawData) { |
|
|
|
|
* Insert a new document |
|
|
|
|
* @param {Function} cb Optional callback, signature: err, insertedDoc |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype.insert = function (newDoc, cb) { |
|
|
|
|
Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
|
var callback = cb || function () {} |
|
|
|
|
, self = this |
|
|
|
|
, persistableNewDoc |
|
|
|
@ -99,6 +99,10 @@ Datastore.prototype.insert = function (newDoc, cb) { |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Datastore.prototype.insert = function () { |
|
|
|
|
executor.push({ this: this, fn: this._insert, arguments: arguments }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Find all documents matching the query |
|
|
|
@ -178,7 +182,7 @@ Datastore.prototype.persistWholeDatabase = function (data, cb) { |
|
|
|
|
* options.upsert If true, document is inserted if the query doesn't match anything |
|
|
|
|
* @param {Function} cb Optional callback, signature: err, numReplaced, upsert (set to true if the update was in fact an upsert) |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
|
Datastore.prototype._update = function (query, updateQuery, options, cb) { |
|
|
|
|
var callback |
|
|
|
|
, self = this |
|
|
|
|
, numReplaced = 0 |
|
|
|
@ -201,7 +205,7 @@ Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
|
} else { |
|
|
|
|
// The upserted document is the query (since for now queries have the same structure as
|
|
|
|
|
// documents), modified by the updateQuery
|
|
|
|
|
return self.insert(model.modify(query, updateQuery), function (err) { |
|
|
|
|
return self._insert(model.modify(query, updateQuery), function (err) { |
|
|
|
|
if (err) { return callback(err); } |
|
|
|
|
return callback(null, 1, true); |
|
|
|
|
}); |
|
|
|
@ -230,6 +234,9 @@ Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
|
} |
|
|
|
|
]); |
|
|
|
|
}; |
|
|
|
|
Datastore.prototype.update = function () { |
|
|
|
|
executor.push({ this: this, fn: this._update, arguments: arguments }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -240,7 +247,7 @@ Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
|
* options.multi If true, can update multiple documents (defaults to false) |
|
|
|
|
* @param {Function} cb Optional callback, signature: err, numRemoved |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype.remove = function (query, options, cb) { |
|
|
|
|
Datastore.prototype._remove = function (query, options, cb) { |
|
|
|
|
var callback |
|
|
|
|
, self = this |
|
|
|
|
, numRemoved = 0 |
|
|
|
@ -265,6 +272,9 @@ Datastore.prototype.remove = function (query, options, cb) { |
|
|
|
|
return callback(null, numRemoved); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
Datastore.prototype.remove = function () { |
|
|
|
|
executor.push({ this: this, fn: this._remove, arguments: arguments }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Datastore; |
|
|
|
|