From c82a4600e337f3a458562f35a1e5e6ad0efe13a9 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 17 May 2013 16:13:46 +0200 Subject: [PATCH] Put matching in model.js, makes more sense --- lib/datastore.js | 23 ++++------------------- lib/model.js | 16 ++++++++++++++++ test/db.test.js | 10 ++++++++++ 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index e488c12..71b669c 100644 --- a/lib/datastore.js +++ b/lib/datastore.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); diff --git a/lib/model.js b/lib/model.js index 98f4229..935f6df 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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; diff --git a/test/db.test.js b/test/db.test.js index f104b7a..6f32d06 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -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' ==== //