Skip to content

Commit f4dd197

Browse files
committed
Fix Chain find & count with mapsTo keys.
Closes #530
1 parent 0633307 commit f4dd197

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v2.1.19 - 21 Aug 2014
2+
- Fix Chain.find().remove() & Chain.find.count() with mapsTo keys (#530)
3+
14
### v2.1.18 - 29 Jul 2014
25
- Add `alwaysValidate` flag (#540, #352)
36
- Fix mongo hasMany wrong instance bug (#479)

lib/ChainFind.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ var Promise = require("./Promise").Promise;
66
module.exports = ChainFind;
77

88
function ChainFind(Model, opts) {
9+
var prepareConditions = function () {
10+
return Utilities.transformPropertyNames(
11+
opts.conditions, opts.properties
12+
);
13+
};
14+
15+
var prepareOrder = function () {
16+
return Utilities.transformOrderPropertyNames(
17+
opts.order, opts.properties
18+
);
19+
};
20+
921
var promise = null;
1022
var chain = {
1123
find: function () {
@@ -79,7 +91,7 @@ function ChainFind(Model, opts) {
7991
return this;
8092
},
8193
count: function (cb) {
82-
opts.driver.count(opts.table, opts.conditions, {
94+
opts.driver.count(opts.table, prepareConditions(), {
8395
merge : opts.merge
8496
}, function (err, data) {
8597
if (err || data.length === 0) {
@@ -90,9 +102,11 @@ function ChainFind(Model, opts) {
90102
return this;
91103
},
92104
remove: function (cb) {
93-
opts.driver.find([ opts.keys ], opts.table, opts.conditions, {
105+
var keys = _.pluck(opts.keyProperties, 'mapsTo');
106+
107+
opts.driver.find(keys, opts.table, prepareConditions(), {
94108
limit : opts.limit,
95-
order : opts.order,
109+
order : prepareOrder(),
96110
merge : opts.merge,
97111
offset : opts.offset,
98112
exists : opts.exists
@@ -112,7 +126,7 @@ function ChainFind(Model, opts) {
112126
for (var i = 0; i < data.length; i++) {
113127
or = {};
114128
for (var j = 0; j < opts.keys.length; j++) {
115-
or[opts.keys[j]] = data[i][opts.keys[j]];
129+
or[keys[j]] = data[i][keys[j]];
116130
}
117131
conditions.or.push(or);
118132
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite",
1313
"mongodb"
1414
],
15-
"version" : "2.1.18",
15+
"version" : "2.1.19",
1616
"license" : "MIT",
1717
"homepage" : "http://dresende.github.io/node-orm2",
1818
"repository" : "http://github.com/dresende/node-orm2.git",

test/integration/property-maps-to.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,39 @@ describe("Property.mapsTo", function() {
253253
});
254254
});
255255
});
256+
257+
it("should count", function (done) {
258+
Person.create({ firstName: 'Greg', lastName: 'McDoofus', age: 30 }, function (err, person) {
259+
should.not.exist(err);
260+
261+
Person.find({ firstName: 'Greg', lastName: 'McDoofus' }).count(function (err, count) {
262+
should.not.exist(err);
263+
should.equal(count, 1);
264+
done();
265+
});
266+
});
267+
});
268+
269+
it("should chain delete", function (done) {
270+
Person.create({ firstName: 'Alfred', lastName: 'McDoogle', age: 50 }, function (err, person) {
271+
should.not.exist(err);
272+
273+
Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).count(function (err, count) {
274+
should.not.exist(err);
275+
should.equal(count, 1);
276+
277+
Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).remove(function (err) {
278+
should.not.exist(err);
279+
280+
Person.find({ firstName: 'Alfred', lastName: 'McDoogle' }).count(function (err, count) {
281+
should.not.exist(err);
282+
should.equal(count, 0);
283+
284+
done()
285+
});
286+
});
287+
});
288+
});
289+
});
256290
});
257291
});

0 commit comments

Comments
 (0)