|
|
@ -1,8 +1,7 @@ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* The datastore itself |
|
|
|
* The datastore itself |
|
|
|
* TODO |
|
|
|
* TODO |
|
|
|
* Queue operations |
|
|
|
* Update and removes should only modify the corresponding part of the database (or use much faster append-only format) |
|
|
|
* Update and removes should only modify the corresponding part of the database |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
var fs = require('fs') |
|
|
|
var fs = require('fs') |
|
|
@ -10,6 +9,7 @@ var fs = require('fs') |
|
|
|
, customUtils = require('./customUtils') |
|
|
|
, customUtils = require('./customUtils') |
|
|
|
, model = require('./model') |
|
|
|
, model = require('./model') |
|
|
|
, async = require('async') |
|
|
|
, async = require('async') |
|
|
|
|
|
|
|
, executor = require('./executor') |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -76,7 +76,7 @@ Datastore.treatRawData = function (rawData) { |
|
|
|
* Insert a new document |
|
|
|
* Insert a new document |
|
|
|
* @param {Function} cb Optional callback, signature: err, insertedDoc |
|
|
|
* @param {Function} cb Optional callback, signature: err, insertedDoc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype.insert = function (newDoc, cb) { |
|
|
|
Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
var callback = cb || function () {} |
|
|
|
var callback = cb || function () {} |
|
|
|
, self = this |
|
|
|
, self = this |
|
|
|
, persistableNewDoc |
|
|
|
, 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 |
|
|
|
* 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 |
|
|
|
* 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) |
|
|
|
* @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 |
|
|
|
var callback |
|
|
|
, self = this |
|
|
|
, self = this |
|
|
|
, numReplaced = 0 |
|
|
|
, numReplaced = 0 |
|
|
@ -201,7 +205,7 @@ Datastore.prototype.update = function (query, updateQuery, options, cb) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// The upserted document is the query (since for now queries have the same structure as
|
|
|
|
// The upserted document is the query (since for now queries have the same structure as
|
|
|
|
// documents), modified by the updateQuery
|
|
|
|
// 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); } |
|
|
|
if (err) { return callback(err); } |
|
|
|
return callback(null, 1, true); |
|
|
|
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) |
|
|
|
* options.multi If true, can update multiple documents (defaults to false) |
|
|
|
* @param {Function} cb Optional callback, signature: err, numRemoved |
|
|
|
* @param {Function} cb Optional callback, signature: err, numRemoved |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype.remove = function (query, options, cb) { |
|
|
|
Datastore.prototype._remove = function (query, options, cb) { |
|
|
|
var callback |
|
|
|
var callback |
|
|
|
, self = this |
|
|
|
, self = this |
|
|
|
, numRemoved = 0 |
|
|
|
, numRemoved = 0 |
|
|
@ -265,6 +272,9 @@ Datastore.prototype.remove = function (query, options, cb) { |
|
|
|
return callback(null, numRemoved); |
|
|
|
return callback(null, numRemoved); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
Datastore.prototype.remove = function () { |
|
|
|
|
|
|
|
executor.push({ this: this, fn: this._remove, arguments: arguments }); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Datastore; |
|
|
|
module.exports = Datastore; |
|
|
|