From 6aeaa80aff903144bfe61b3176b3461dbef570e8 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Wed, 1 May 2013 20:52:32 +0100 Subject: [PATCH] Can find docs --- lib/datastore.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index d93dd5f..55dcdcd 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -97,12 +97,47 @@ Datastore.prototype.insert = function (newDoc, cb) { }; +/** + * Check whether object is matched by the given query + */ +Datastore.match = function (obj, query) { + var match = true; + + Object.keys(query).forEach(function (k) { + if (obj[k] !== query[k]) { match = false; } + }); + + return match; +}; + + +/** + * Find all documents matching the query + * @param {Object} query MongoDB-style query + */ +Datastore.prototype.find = function (query, callback) { + var res = [] + , self = this + ; + + self.data.forEach(function (d) { + if (Datastore.match(d, query)) { + res.push(d); + } + }); + + return callback(null, res); +}; + + var d = new Datastore('workspace/test.db'); d.loadDatabase(function (err) { - d.insert({ onetest: "oh yeah" }, function (err, insertedDoc) { + console.log(d.data); + d.find({ te: "un" }, function (err, docs) { console.log(err); - console.log("==============="); - console.log(insertedDoc); + console.log('------------------'); + console.log(docs); + }); });