|
|
@ -152,14 +152,26 @@ Datastore.prototype.findOne = function (query, callback) { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Persist the whole database |
|
|
|
* Persist the whole database |
|
|
|
|
|
|
|
* @param {Array} data Optional data to persist. If not set, we use the database data |
|
|
|
* @param {Function} cb Optional callback, signature: err |
|
|
|
* @param {Function} cb Optional callback, signature: err |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Datastore.prototype.persistWholeDatabase = function (cb) { |
|
|
|
Datastore.prototype.persistWholeDatabase = function (data, cb) { |
|
|
|
var callback = cb || function () {} |
|
|
|
var callback |
|
|
|
, self = this |
|
|
|
, self = this |
|
|
|
, newContents = ''; |
|
|
|
, newContents = ''; |
|
|
|
|
|
|
|
|
|
|
|
self.data.forEach(function (d) { |
|
|
|
if (!data) { |
|
|
|
|
|
|
|
data = self.data; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof data === 'function') { |
|
|
|
|
|
|
|
cb = data; |
|
|
|
|
|
|
|
data = self.data; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
callback = cb || function () {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data.forEach(function (d) { |
|
|
|
newContents += JSON.stringify(d) + '\n'; |
|
|
|
newContents += JSON.stringify(d) + '\n'; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -167,6 +179,18 @@ Datastore.prototype.persistWholeDatabase = function (cb) { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Update the first doc matching query |
|
|
|
|
|
|
|
* @param {Object} query |
|
|
|
|
|
|
|
* @param {Object} newDoc Will replace the former doc |
|
|
|
|
|
|
|
* @param {Function} cb Optional callback, signature: err, replaced?, newDoc |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Datastore.prototype.updateOne = function (query, newDoc, cb) { |
|
|
|
|
|
|
|
var callback = cb || function () {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var d = new Datastore('workspace/test.db'); |
|
|
|
var d = new Datastore('workspace/test.db'); |
|
|
@ -174,7 +198,6 @@ d.loadDatabase(function (err) { |
|
|
|
console.log(d.data); |
|
|
|
console.log(d.data); |
|
|
|
d.data = [{ff: 'mec'}, {ee: 'test'}]; |
|
|
|
d.data = [{ff: 'mec'}, {ee: 'test'}]; |
|
|
|
|
|
|
|
|
|
|
|
d.persistWholeDatabase(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|