From 72e7ef2bc84c0e7d7bece344c8dba12f6d4c8c76 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 1 May 2013 21:11:26 +0100 Subject: [PATCH] Can persist the data we want, not just the cached version --- lib/datastore.js | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index e32e4f3..7141648 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -152,14 +152,26 @@ Datastore.prototype.findOne = function (query, callback) { /** * 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 */ -Datastore.prototype.persistWholeDatabase = function (cb) { - var callback = cb || function () {} +Datastore.prototype.persistWholeDatabase = function (data, cb) { + var callback , self = this , 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'; }); @@ -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'); @@ -174,7 +198,6 @@ d.loadDatabase(function (err) { console.log(d.data); d.data = [{ff: 'mec'}, {ee: 'test'}]; - d.persistWholeDatabase(); });