|
|
|
@ -247,28 +247,64 @@ Datastore.prototype.getCandidates = function (query) { |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
|
var callback = cb || function () {} |
|
|
|
|
, self = this |
|
|
|
|
, insertedDoc |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
// Ensure the document has the right format
|
|
|
|
|
try { |
|
|
|
|
newDoc._id = customUtils.uid(16); |
|
|
|
|
model.checkObject(newDoc); |
|
|
|
|
insertedDoc = model.deepCopy(newDoc); |
|
|
|
|
this._insertInCache(newDoc); |
|
|
|
|
} catch (e) { |
|
|
|
|
return callback(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Insert in all indexes (also serves to ensure uniqueness)
|
|
|
|
|
try { self.addToIndexes(insertedDoc); } catch (e) { return callback(e); } |
|
|
|
|
|
|
|
|
|
this.persistence.persistNewState([newDoc], function (err) { |
|
|
|
|
this.persistence.persistNewState(util.isArray(newDoc) ? newDoc : [newDoc], function (err) { |
|
|
|
|
if (err) { return callback(err); } |
|
|
|
|
return callback(null, newDoc); |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* If newDoc is an array of documents, this will insert all documents in the cache |
|
|
|
|
* @api private |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype._insertInCache = function (newDoc) { |
|
|
|
|
var insertedDoc; |
|
|
|
|
|
|
|
|
|
if (util.isArray(newDoc)) { this._insertMultipleDocsInCache(newDoc); return; } |
|
|
|
|
|
|
|
|
|
// Ensure the document has the right format
|
|
|
|
|
newDoc._id = customUtils.uid(16); |
|
|
|
|
model.checkObject(newDoc); |
|
|
|
|
insertedDoc = model.deepCopy(newDoc); |
|
|
|
|
|
|
|
|
|
// Insert in all indexes (also serves to ensure uniqueness)
|
|
|
|
|
this.addToIndexes(insertedDoc);
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* If one insertion fails (e.g. because of a unique constraint), roll back all previous |
|
|
|
|
* inserts and throws the error |
|
|
|
|
*/ |
|
|
|
|
Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { |
|
|
|
|
var i, failingI, error; |
|
|
|
|
|
|
|
|
|
for (i = 0; i < newDocs.length; i += 1) { |
|
|
|
|
try { |
|
|
|
|
this._insertInCache(newDocs[i]); |
|
|
|
|
} catch (e) { |
|
|
|
|
error = e; |
|
|
|
|
failingI = i; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (error) { |
|
|
|
|
for (i = 0; i < failingI; i += 1) { |
|
|
|
|
this._removeFromCache(newDocs[i]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Datastore.prototype.insert = function () { |
|
|
|
|
this.executor.push({ this: this, fn: this._insert, arguments: arguments }); |
|
|
|
|
}; |
|
|
|
|