Skip to content
Browse files

finazlied how clauses are defined, updated readme and tests

  • Loading branch information...
1 parent ca6893f commit e331aa4d806c058062bc7bc459bab2c51e1e898d Robert Poole committed Jul 19, 2013
Showing with 46 additions and 31 deletions.
  1. +19 −11 README.md
  2. +3 −3 lib/model.js
  3. +7 −13 lib/query.js
  4. +17 −4 test/chain.js
View
30 README.md
@@ -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" />
View
6 lib/model.js
@@ -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);
View
20 lib/query.js
@@ -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);
}
}
View
21 test/chain.js
@@ -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) {
@@ -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);
@@ -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();
});
},

0 comments on commit e331aa4

Please sign in to comment.
Something went wrong with that request. Please try again.