Permalink
Browse files

define clauses differently

  • Loading branch information...
1 parent ca6893f commit 8943028db7f97ac22af101667c6d1a8e4e4eec30 Robert Poole committed Jul 19, 2013
Showing with 13 additions and 18 deletions.
  1. +4 −3 lib/model.js
  2. +2 −12 lib/query.js
  3. +7 −3 test/chain.js
View
@@ -226,17 +226,18 @@ 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);
+ chainFn(query);
+ return query;
};
Model[name] = persistUtil.bind(name, Model[name], Model);
View
@@ -42,21 +42,11 @@ Query.prototype._defineCustomClauses = function() {
var customClauses = this.model.customClauses;
for (var name in customClauses) {
- var clauses = customClauses[name];
+ var chainFn = customClauses[name];
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;
+ return chainFn(this);
};
-
}
}
View
@@ -18,9 +18,8 @@ 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(query) {
+ return query.where('age = ?', 21).where('name like "%Bob%"');
});
testUtils.connect(persist, {}, function(err, connection) {
@@ -68,6 +67,7 @@ exports['Chain'] = nodeunit.testCase({
self.Person.first,
persist.runSqlAll('SELECT * FROM People'),
self.Person.testClause().all,
+ self.Person.limit(5).testClause().all,
], function(err, results) {
if (err) {
console.error(err);
@@ -128,6 +128,10 @@ 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");
+
test.done();
});
},

0 comments on commit 8943028

Please sign in to comment.