|
|
@ -34,6 +34,7 @@ function Datastore (options) { |
|
|
|
filename = options.filename; |
|
|
|
filename = options.filename; |
|
|
|
this.inMemoryOnly = options.inMemoryOnly || false; |
|
|
|
this.inMemoryOnly = options.inMemoryOnly || false; |
|
|
|
this.autoload = options.autoload || false; |
|
|
|
this.autoload = options.autoload || false; |
|
|
|
|
|
|
|
this.timestampData = options.timestampData || false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Determine whether in memory or persistent
|
|
|
|
// Determine whether in memory or persistent
|
|
|
@ -283,17 +284,19 @@ Datastore.prototype.getCandidates = function (query) { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
Datastore.prototype._insert = function (newDoc, cb) { |
|
|
|
var callback = cb || function () {} |
|
|
|
var callback = cb || function () {} |
|
|
|
|
|
|
|
, preparedDoc |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
this._insertInCache(newDoc); |
|
|
|
preparedDoc = this.prepareDocumentForInsertion(newDoc) |
|
|
|
|
|
|
|
this._insertInCache(preparedDoc); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
return callback(e); |
|
|
|
return callback(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.persistence.persistNewState(util.isArray(newDoc) ? newDoc : [newDoc], function (err) { |
|
|
|
this.persistence.persistNewState(util.isArray(preparedDoc) ? preparedDoc : [preparedDoc], function (err) { |
|
|
|
if (err) { return callback(err); } |
|
|
|
if (err) { return callback(err); } |
|
|
|
return callback(null, newDoc); |
|
|
|
return callback(null, model.deepCopy(preparedDoc)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -311,6 +314,7 @@ Datastore.prototype.createNewId = function () { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Prepare a document (or array of documents) to be inserted in a database |
|
|
|
* Prepare a document (or array of documents) to be inserted in a database |
|
|
|
|
|
|
|
* Meaning adds _id and createdAt if necessary on a copy of newDoc to avoid any side effect on user input |
|
|
|
* @api private |
|
|
|
* @api private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { |
|
|
|
Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { |
|
|
@ -320,9 +324,8 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { |
|
|
|
preparedDoc = []; |
|
|
|
preparedDoc = []; |
|
|
|
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); }); |
|
|
|
newDoc.forEach(function (doc) { preparedDoc.push(self.prepareDocumentForInsertion(doc)); }); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (newDoc._id === undefined) { newDoc._id = this.createNewId(); } |
|
|
|
|
|
|
|
preparedDoc = model.deepCopy(newDoc); |
|
|
|
preparedDoc = model.deepCopy(newDoc); |
|
|
|
//if (preparedDoc._id === undefined) { preparedDoc._id = this.createNewId(); }
|
|
|
|
if (preparedDoc._id === undefined) { preparedDoc._id = this.createNewId(); } |
|
|
|
if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = new Date(); } |
|
|
|
if (this.timestampData && preparedDoc.createdAt === undefined) { preparedDoc.createdAt = new Date(); } |
|
|
|
model.checkObject(preparedDoc); |
|
|
|
model.checkObject(preparedDoc); |
|
|
|
} |
|
|
|
} |
|
|
@ -334,11 +337,11 @@ Datastore.prototype.prepareDocumentForInsertion = function (newDoc) { |
|
|
|
* If newDoc is an array of documents, this will insert all documents in the cache |
|
|
|
* If newDoc is an array of documents, this will insert all documents in the cache |
|
|
|
* @api private |
|
|
|
* @api private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype._insertInCache = function (newDoc) { |
|
|
|
Datastore.prototype._insertInCache = function (preparedDoc) { |
|
|
|
if (util.isArray(newDoc)) { |
|
|
|
if (util.isArray(preparedDoc)) { |
|
|
|
this._insertMultipleDocsInCache(newDoc); |
|
|
|
this._insertMultipleDocsInCache(preparedDoc); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this.addToIndexes(this.prepareDocumentForInsertion(newDoc)); |
|
|
|
this.addToIndexes(preparedDoc); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -347,10 +350,8 @@ Datastore.prototype._insertInCache = function (newDoc) { |
|
|
|
* inserts and throws the error |
|
|
|
* inserts and throws the error |
|
|
|
* @api private |
|
|
|
* @api private |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype._insertMultipleDocsInCache = function (newDocs) { |
|
|
|
Datastore.prototype._insertMultipleDocsInCache = function (preparedDocs) { |
|
|
|
var i, failingI, error |
|
|
|
var i, failingI, error; |
|
|
|
, preparedDocs = this.prepareDocumentForInsertion(newDocs) |
|
|
|
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < preparedDocs.length; i += 1) { |
|
|
|
for (i = 0; i < preparedDocs.length; i += 1) { |
|
|
|
try { |
|
|
|
try { |
|
|
|