|
|
@ -2,6 +2,7 @@ |
|
|
|
* The datastore itself |
|
|
|
* The datastore itself |
|
|
|
* TODO |
|
|
|
* TODO |
|
|
|
* Improve serialization (for types such as Dates, handle new lines in strings) |
|
|
|
* Improve serialization (for types such as Dates, handle new lines in strings) |
|
|
|
|
|
|
|
* Queue inserts, removes and updates |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
var fs = require('fs') |
|
|
|
var fs = require('fs') |
|
|
@ -149,13 +150,35 @@ Datastore.prototype.findOne = function (query, callback) { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Persist the whole database |
|
|
|
|
|
|
|
* @param {Function} cb Optional callback, signature: err |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Datastore.prototype.persistWholeDatabase = function (cb) { |
|
|
|
|
|
|
|
var callback = cb || function () {} |
|
|
|
|
|
|
|
, self = this |
|
|
|
|
|
|
|
, newContents = ''; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.data.forEach(function (d) { |
|
|
|
|
|
|
|
newContents += JSON.stringify(d) + '\n'; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFile(self.filename, newContents, 'utf8', callback); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var d = new Datastore('workspace/test.db'); |
|
|
|
var d = new Datastore('workspace/test.db'); |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
console.log(d.data); |
|
|
|
console.log(d.data); |
|
|
|
|
|
|
|
d.data = [{ff: 'mec'}, {ee: 'test'}]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
d.persistWholeDatabase(); |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|