From f4dd1972f4fe3245bddb8d572cb78434e9a01708 Mon Sep 17 00:00:00 2001 From: Arek W Date: Thu, 21 Aug 2014 21:22:25 +1000 Subject: [PATCH] Fix Chain find & count with mapsTo keys. Closes #530 --- Changelog.md | 3 +++ lib/ChainFind.js | 22 ++++++++++++++---- package.json | 2 +- test/integration/property-maps-to.js | 34 ++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 7f13e234..7722ed2b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +### v2.1.19 - 21 Aug 2014 +- Fix Chain.find().remove() & Chain.find.count() with mapsTo keys (#530) + ### v2.1.18 - 29 Jul 2014 - Add `alwaysValidate` flag (#540, #352) - Fix mongo hasMany wrong instance bug (#479) diff --git a/lib/ChainFind.js b/lib/ChainFind.js index 0cfb9066..f4a5cbaa 100644 --- a/lib/ChainFind.js +++ b/lib/ChainFind.js @@ -6,6 +6,18 @@ var Promise = require("./Promise").Promise; module.exports = ChainFind; function ChainFind(Model, opts) { + var prepareConditions = function () { + return Utilities.transformPropertyNames( + opts.conditions, opts.properties + ); + }; + + var prepareOrder = function () { + return Utilities.transformOrderPropertyNames( + opts.order, opts.properties + ); + }; + var promise = null; var chain = { find: function () { @@ -79,7 +91,7 @@ function ChainFind(Model, opts) { return this; }, count: function (cb) { - opts.driver.count(opts.table, opts.conditions, { + opts.driver.count(opts.table, prepareConditions(), { merge : opts.merge }, function (err, data) { if (err || data.length === 0) { @@ -90,9 +102,11 @@ function ChainFind(Model, opts) { return this; }, remove: function (cb) { - opts.driver.find([ opts.keys ], opts.table, opts.conditions, { + var keys = _.pluck(opts.keyProperties, 'mapsTo'); + + opts.driver.find(keys, opts.table, prepareConditions(), { limit : opts.limit, - order : opts.order, + order : prepareOrder(), merge : opts.merge, offset : opts.offset, exists : opts.exists @@ -112,7 +126,7 @@ function ChainFind(Model, opts) { for (var i = 0; i < data.length; i++) { or = {}; for (var j = 0; j < opts.keys.length; j++) { - or[opts.keys[j]] = data[i][opts.keys[j]]; + or[keys[j]] = data[i][keys[j]]; } conditions.or.push(or); } diff --git a/package.json b/package.json index 7790e44b..a25f9566 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "sqlite", "mongodb" ], - "version" : "2.1.18", + "version" : "2.1.19", "license" : "MIT", "homepage" : "http://dresende.github.io/node-orm2", "repository" : "http://github.com/dresende/node-orm2.git", diff --git a/test/integration/property-maps-to.js b/test/integration/property-maps-to.js index bb6a2cea..20f53673 100644 --- a/test/integration/property-maps-to.js +++ b/test/integration/property-maps-to.js @@ -253,5 +253,39 @@ describe("Property.mapsTo", function() { }); }); }); + + it("should count", function (done) { + Person.create({ firstName: 'Greg', lastName: 'McDoofus', age: 30 }, function (err, person) { + should.not.exist(err); + + Person.find({ firstName: 'Greg', lastName: 'McDoofus' }).count(function (err, count) { + should.not.exist(err); + should.equal(count, 1); + done(); + }); + }); + }); + + it("should chain delete", function (done) { + Person.create({ firstName: 'Alfred', lastName: 'McDoogle', age: 50 }, function (err, person) { + should.not.exist(err); + + Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).count(function (err, count) { + should.not.exist(err); + should.equal(count, 1); + + Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).remove(function (err) { + should.not.exist(err); + + Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).count(function (err, count) { + should.not.exist(err); + should.equal(count, 0); + + done() + }); + }); + }); + }); + }); }); });