Can persist the whole database to disk

pull/2/head
Louis Chatriot 12 years ago
parent ad2a0700dc
commit 964f7a9953
  1. 23
      lib/datastore.js

@ -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();
}); });

Loading…
Cancel
Save