Skip to content

Commit

Permalink
allow arguments passed through the custom chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Poole committed Jul 19, 2013
1 parent 041fd1c commit 6fa9b45
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ Person.getById(connection, 1, function(err, person) {

Creates a custom method that is a composition of clauses. Only supports
clauses - where, orderBy, etc. Trying to chain a method that returns results
(all, first, min, etc.) will not work.
(all, first, min, etc.) will not work. This is set to refer to the query
you're constructing.

__Arguments__

Expand All @@ -600,12 +601,12 @@ __Arguments__

__Example__
```javascript
Person.defineClause('clauseName', function(query) {
return query.where('id < 5').orderBy('id').limit(5);
Person.defineClause('clauseName', function(arg1, arg2, ...) {
return this.where('id < ?', arg1).orderBy('id').limit(5);
});

Person.clauseName().all(connection, function(err, people) {
// All the people with id > 5, ordered by id and limited to 5
// All the people with id < arg1, ordered by id and limited to 5
});
```
<a name="modelOnSave" />
Expand Down
3 changes: 1 addition & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ exports.define = function (name, columnDefs, opts) {
// add it to the model
Model[name] = function() {
var query = this.using(null);
chainFn(query);
return query;
return chainFn.apply(query, arguments);
};

Model[name] = persistUtil.bind(name, Model[name], Model);
Expand Down
2 changes: 1 addition & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Query.prototype._defineCustomClauses = function() {
var chainFn = customClauses[name];

this[name] = function() {
return chainFn(this);
return chainFn.apply(this, arguments);
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ exports['Chain'] = nodeunit.testCase({
"age": type.INTEGER
}).hasMany(this.Phone);

this.Person.defineClause('testClause', function(query) {
return query.where('age = ?', 21).where('name like "%Bob%"');
this.Person.defineClause('testClause', function(age) {
return this.where('age = ?', age || 21).where('name like "%Bob%"');
});

testUtils.connect(persist, {}, function(err, connection) {
Expand Down Expand Up @@ -66,7 +66,7 @@ exports['Chain'] = nodeunit.testCase({
self.Phone.all,
self.Person.first,
persist.runSqlAll('SELECT * FROM People'),
self.Person.testClause().all,
self.Person.testClause(21).all,
self.Person.limit(5).testClause().all,
], function(err, results) {
if (err) {
Expand Down

0 comments on commit 6fa9b45

Please sign in to comment.