diff --git a/lib/datastore.js b/lib/datastore.js index 1289191..e32e4f3 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -2,6 +2,7 @@ * The datastore itself * TODO * Improve serialization (for types such as Dates, handle new lines in strings) + * Queue inserts, removes and updates */ 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'); d.loadDatabase(function (err) { console.log(d.data); + d.data = [{ff: 'mec'}, {ee: 'test'}]; + d.persistWholeDatabase(); }); + + +