|
|
@ -262,33 +262,49 @@ Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* If newDoc is an array of documents, this will insert all documents in the cache |
|
|
|
* Prepare a document (or array of documents) to be inserted in a database |
|
|
|
* @api private |
|
|
|
* @api private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype._insertInCache = function (newDoc) { |
|
|
|
Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { |
|
|
|
var insertedDoc; |
|
|
|
var preparedDoc, self = this; |
|
|
|
|
|
|
|
|
|
|
|
if (util.isArray(newDoc)) { this._insertMultipleDocsInCache(newDoc); return; } |
|
|
|
if (util.isArray(newDoc)) { |
|
|
|
|
|
|
|
preparedDoc = []; |
|
|
|
|
|
|
|
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); }); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
newDoc._id = customUtils.uid(16); |
|
|
|
|
|
|
|
preparedDoc = model.deepCopy(newDoc); |
|
|
|
|
|
|
|
model.checkObject(preparedDoc); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Ensure the document has the right format
|
|
|
|
return preparedDoc; |
|
|
|
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 newDoc is an array of documents, this will insert all documents in the cache |
|
|
|
|
|
|
|
* @api private |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Datastore.prototype._insertInCache = function (newDoc) { |
|
|
|
|
|
|
|
if (util.isArray(newDoc)) { |
|
|
|
|
|
|
|
this._insertMultipleDocsInCache(newDoc); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
this.addToIndexes(this.prepareDocumentForInsertion(newDoc));
|
|
|
|
|
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* If one insertion fails (e.g. because of a unique constraint), roll back all previous |
|
|
|
* If one insertion fails (e.g. because of a unique constraint), roll back all previous |
|
|
|
* inserts and throws the error |
|
|
|
* inserts and throws the error |
|
|
|
|
|
|
|
* @api private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { |
|
|
|
Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { |
|
|
|
var i, failingI, error; |
|
|
|
var i, failingI, error |
|
|
|
|
|
|
|
, preparedDocs = this.prepareDocumentForInsertion(newDocs) |
|
|
|
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < newDocs.length; i += 1) { |
|
|
|
for (i = 0; i < preparedDocs.length; i += 1) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this._insertInCache(newDocs[i]); |
|
|
|
this.addToIndexes(preparedDocs[i]); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
error = e; |
|
|
|
error = e; |
|
|
|
failingI = i; |
|
|
|
failingI = i; |
|
|
@ -298,7 +314,7 @@ Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { |
|
|
|
|
|
|
|
|
|
|
|
if (error) { |
|
|
|
if (error) { |
|
|
|
for (i = 0; i < failingI; i += 1) { |
|
|
|
for (i = 0; i < failingI; i += 1) { |
|
|
|
this._removeFromCache(newDocs[i]); |
|
|
|
this.removeFromIndexes(preparedDocs[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
throw error; |
|
|
|
throw error; |
|
|
|