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