Skip to content

Commit

Permalink
Fix Chain find & count with mapsTo keys.
Browse files Browse the repository at this point in the history
Closes #530
  • Loading branch information
dxg committed Aug 21, 2014
1 parent 0633307 commit f4dd197
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
22 changes: 18 additions & 4 deletions lib/ChainFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions test/integration/property-maps-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
});
});
});
});
});
});

0 comments on commit f4dd197

Please sign in to comment.