Can find docs

pull/2/head
Louis Chatriot 12 years ago
parent fa2580eefb
commit 6aeaa80aff
  1. 41
      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'); var d = new Datastore('workspace/test.db');
d.loadDatabase(function (err) { 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(err);
console.log("==============="); console.log('------------------');
console.log(insertedDoc); console.log(docs);
}); });
}); });

Loading…
Cancel
Save