Skip to content

Commit

Permalink
Allow ordering by escaped sql
Browse files Browse the repository at this point in the history
Fixes #311
  • Loading branch information
dxg committed Sep 10, 2013
1 parent 0c04a37 commit ffa0be8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
7 changes: 7 additions & 0 deletions lib/ChainFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ function ChainFind(Model, opts) {
}
return this;
},
orderRaw: function (str, args) {
if (!Array.isArray(opts.order)) {
opts.order = [];
}
opts.order.push([ str, args || [] ]);
return this;
},
count: function (cb) {
opts.driver.count(opts.table, opts.conditions, {}, function (err, data) {
if (err || data.length === 0) {
Expand Down
49 changes: 32 additions & 17 deletions test/integration/model-find-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ describe("Model.find() chaining", function() {
});
});

describe(".order('property')", function () {
describe("order", function () {
before(setup());

it("should order by that property ascending", function (done) {
it("('property') should order by that property ascending", function (done) {
Person.find().order("age").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
Expand All @@ -102,12 +102,8 @@ describe("Model.find() chaining", function() {
return done();
});
});
});

describe(".order('-property')", function () {
before(setup());

it("should order by that property descending", function (done) {
it("('-property') should order by that property descending", function (done) {
Person.find().order("-age").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
Expand All @@ -117,13 +113,35 @@ describe("Model.find() chaining", function() {
return done();
});
});

it("('property', 'Z') should order by that property descending", function (done) {
Person.find().order("age", "Z").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
instances[0].age.should.equal(20);
instances[2].age.should.equal(18);

return done();
});
});
});

describe(".order('property', 'Z')", function () {
describe("orderRaw", function () {
before(setup());

it("should order by that property descending", function (done) {
Person.find().order("age", "Z").run(function (err, instances) {
it("should allow ordering by SQL", function (done) {
Person.find().orderRaw("age DESC").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
instances[0].age.should.equal(20);
instances[2].age.should.equal(18);

return done();
});
});

it("should allow ordering by SQL with escaping", function (done) {
Person.find().orderRaw("?? DESC", ['age']).run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
instances[0].age.should.equal(20);
Expand All @@ -134,10 +152,10 @@ describe("Model.find() chaining", function() {
});
});

describe(".only('property', ...)", function () {
describe("only", function () {
before(setup());

it("should return only those properties, others null", function (done) {
it("('property', ...) should return only those properties, others null", function (done) {
Person.find().only("age", "surname").order("-age").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
Expand All @@ -148,12 +166,9 @@ describe("Model.find() chaining", function() {
return done();
});
});
});

describe(".only('property1', ...)", function () {
before(setup());

it("should return only those properties, others null", function (done) {
// This works if cache is disabled. I suspect a cache bug.
xit("(['property', ...]) should return only those properties, others null", function (done) {
Person.find().only([ "age", "surname" ]).order("-age").run(function (err, instances) {
should.equal(err, null);
instances.should.have.property("length", 3);
Expand Down

0 comments on commit ffa0be8

Please sign in to comment.