Put matching in model.js, makes more sense

pull/2/head
Louis Chatriot 12 years ago
parent 3924b4fdd4
commit c82a4600e3
  1. 23
      lib/datastore.js
  2. 16
      lib/model.js
  3. 10
      test/db.test.js

@ -100,21 +100,6 @@ Datastore.prototype.insert = function (newDoc, cb) {
};
/**
* Check whether object is matched by the given query
*/
Datastore.match = function (obj, query) {
var match = true
, i, k;
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
@ -126,7 +111,7 @@ Datastore.prototype.find = function (query, callback) {
;
for (i = 0; i < self.data.length; i += 1) {
if (Datastore.match(self.data[i], query)) {
if (model.match(self.data[i], query)) {
res.push(model.deepCopy(self.data[i]));
}
}
@ -145,7 +130,7 @@ Datastore.prototype.findOne = function (query, callback) {
;
for (i = 0; i < self.data.length; i += 1) {
if (Datastore.match(self.data[i], query)) {
if (model.match(self.data[i], query)) {
return callback(null, model.deepCopy(self.data[i]));
}
}
@ -226,7 +211,7 @@ Datastore.prototype.update = function (query, updateQuery, options, cb) {
, function () { // Perform the update
try {
self.data.forEach(function (d) {
if (Datastore.match(d, query) && (multi || numReplaced === 0)) {
if (model.match(d, query) && (multi || numReplaced === 0)) {
numReplaced += 1;
newData.push(model.modify(d, updateQuery));
} else {
@ -267,7 +252,7 @@ Datastore.prototype.remove = function (query, options, cb) {
multi = options.multi !== undefined ? options.multi : false;
self.data.forEach(function (d) {
if (Datastore.match(d, query) && (multi || numRemoved === 0)) {
if (model.match(d, query) && (multi || numRemoved === 0)) {
numRemoved += 1;
} else {
newData.push(d);

@ -221,9 +221,25 @@ function modify (obj, updateQuery) {
};
/**
* Tell if a given document matches a query
*/
function match (obj, query) {
var match = true
, i, k;
Object.keys(query).forEach(function (k) {
if (obj[k] !== query[k]) { match = false; }
});
return match;
};
// Interface
module.exports.serialize = serialize;
module.exports.deserialize = deserialize;
module.exports.deepCopy = deepCopy;
module.exports.checkObject = checkObject;
module.exports.modify = modify;
module.exports.match = match;

@ -233,6 +233,16 @@ describe('Database', function () {
], done);
});
it.skip('Can use dot-notation to query subfields', function (done) {
d.insert({ greeting: { english: 'hello' } }, function () {
d.findOne({ "greeting.english": 'hello' }, function (err, doc) {
doc.greeting.english.should.equal('hello');
done();
});
});
});
}); // ==== End of 'Find' ==== //

Loading…
Cancel
Save