Skip to content

Commit

Permalink
finazlied how clauses are defined, updated readme and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Poole committed Jul 22, 2013
1 parent ca6893f commit e331aa4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
30 changes: 19 additions & 11 deletions README.md
Expand Up @@ -589,26 +589,34 @@ Person.getById(connection, 1, function(err, person) {
<a name="modelDefineClause" />
### Model.defineClause(clauseName, clauses)

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.
Creates a custom method that is a composition of clauses. `this` is set to refer to the query.
you're constructing.

__Arguments__

* clauseName - The name of the clause to be attached to the model
* clauses - The object describing the clauses. Follows a name: arguments
structure.
* clauses - The function that describes the clause composition using a query.

__Example__
```javascript
Person.defineClause('clauseName', {
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
Person.clauseName(5).all(connection, function(err, people) {
// All the people with id < 5, ordered by id and limited to 5
});

Person.defineClause('clauseName2', function(connection, callback) {
return this
.where('id < 5')
.orderBy('id')
.limit(5)
.all(connection, callback);
});

Person.clauseName2(connection, function(err, people) {
// All the people with id < 5, ordered by id and limited to 5
});
```
<a name="modelOnSave" />
Expand Down
6 changes: 3 additions & 3 deletions lib/model.js
Expand Up @@ -226,17 +226,17 @@ exports.define = function (name, columnDefs, opts) {
}*/
}

Model.defineClause = function(name, clauses) {
Model.defineClause = function(name, chainFn) {
if (Model[name] || Query[name] || this.customClauses[name]) {
throw new Error('You cannot define a custom clause using that name as one already exists.');
}

this.customClauses[name] = clauses;
this.customClauses[name] = chainFn;

// add it to the model
Model[name] = function() {
var query = this.using(null);
return query[name].apply(query, arguments);
return chainFn.apply(query, arguments);
};

Model[name] = persistUtil.bind(name, Model[name], Model);
Expand Down
20 changes: 7 additions & 13 deletions lib/query.js
Expand Up @@ -42,21 +42,15 @@ Query.prototype._defineCustomClauses = function() {
var customClauses = this.model.customClauses;

for (var name in customClauses) {
var clauses = customClauses[name];
(function(name, query) {
var chainFn = customClauses[name];
var fn = function() {
return chainFn.apply(query, arguments);
};

this[name] = function() {
for (var clause in clauses) {
if (!this[clause]) {
throw new Error('Clause "' + clause + '" does not exist.');
}

var args = clauses[clause];
this[clause](args);
}

return this;
};
query[name] = persistUtil.bind(name, fn, query);

})(name, this);
}
}

Expand Down
21 changes: 17 additions & 4 deletions test/chain.js
Expand Up @@ -18,9 +18,12 @@ exports['Chain'] = nodeunit.testCase({
"age": type.INTEGER
}).hasMany(this.Phone);

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

this.Person.defineClause('testClause2', function(connection, callback) {
return this.where('age = ?', 21).where('name like "%Bob%"').all(connection, callback);
});

testUtils.connect(persist, {}, function(err, connection) {
Expand Down Expand Up @@ -67,7 +70,9 @@ 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,
self.Person.limit(5).testClause2,
], function(err, results) {
if (err) {
console.error(err);
Expand Down Expand Up @@ -128,6 +133,14 @@ exports['Chain'] = nodeunit.testCase({
test.ok(results[15].length, 1);
test.ok(results[15][0].name, "Bob O'Neill");

// Person.limit(5).testClause
test.ok(results[16].length, 1);
test.ok(results[16][0].name, "Bob O'Neill");

// Person.limit(5).testClause2
test.ok(results[17].length, 1);
test.ok(results[17][0].name, "Bob O'Neill");

test.done();
});
},
Expand Down

0 comments on commit e331aa4

Please sign in to comment.