-
Notifications
You must be signed in to change notification settings - Fork 0
beforeFind callback #2
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,8 @@ var usageError = require('../../utils/usageError'), | |
| waterlineCriteria = require('waterline-criteria'), | ||
| _ = require('lodash'), | ||
| async = require('async'), | ||
| hasOwnProperty = utils.object.hasOwnProperty; | ||
| hasOwnProperty = utils.object.hasOwnProperty, | ||
| callbacks = require('../../utils/callbacksRunner'); | ||
|
|
||
| module.exports = { | ||
|
|
||
|
|
@@ -45,6 +46,10 @@ module.exports = { | |
| // Normalize criteria | ||
| criteria = normalize.criteria(criteria); | ||
|
|
||
| this.beforeCallbacks.call(self,criteria.where,function(err){ | ||
| if (err) {return cb(err);} | ||
| }); | ||
|
||
|
|
||
| // Return Deferred or pass to adapter | ||
| if(typeof cb !== 'function') { | ||
| return new Deferred(this, this.findOne, criteria); | ||
|
|
@@ -224,6 +229,10 @@ module.exports = { | |
| return usageError('Invalid options specified!', usage, cb); | ||
| } | ||
|
|
||
| this.beforeCallbacks.call(self,criteria.where,function(err){ | ||
| if (err) return cb(err); | ||
| }) | ||
|
||
|
|
||
| // Return Deferred or pass to adapter | ||
| if(typeof cb !== 'function') { | ||
| return new Deferred(this, this.find, criteria, options); | ||
|
|
@@ -395,6 +404,15 @@ module.exports = { | |
| }); | ||
| }, | ||
|
|
||
| beforeCallbacks:function(criteria,cb){ | ||
|
||
| var self = this; | ||
| async.series([ | ||
| function(cb){ | ||
| callbacks.beforeFind(self,criteria,cb); | ||
| } | ||
| ]); | ||
| }, | ||
|
|
||
| where: function() { | ||
| this.find.apply(this, Array.prototype.slice.call(arguments)); | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| var Waterline = require('../../../lib/waterline'), | ||
| assert = require('assert'); | ||
|
|
||
| describe('.beforeFind()', function() { | ||
|
|
||
| describe('basic function', function() { | ||
| var person; | ||
|
|
||
| before(function(done) { | ||
| var waterline = new Waterline(); | ||
| var Model = Waterline.Collection.extend({ | ||
| identity: 'user', | ||
| connection: 'foo', | ||
| attributes: { | ||
| name: 'string', | ||
| isDeleted:'boolean' | ||
| }, | ||
|
|
||
| beforeFind: function(values, cb) { | ||
| values.isDeleted = false; | ||
| cb(); | ||
| } | ||
| }); | ||
|
|
||
| waterline.loadCollection(Model); | ||
|
|
||
| // Fixture Adapter Def | ||
| var adapterDef = { | ||
| find: function(con, col, values, cb) { return cb(null, [values.where]);}, | ||
| findOne:function(conn,col,values,cb){return cb(null,values.where);} | ||
| }; | ||
|
|
||
| var connections = { | ||
| 'foo': { | ||
| adapter: 'foobar' | ||
| } | ||
| }; | ||
|
|
||
| waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) { | ||
| if(err) done(err); | ||
| person = colls.collections.user; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| // /** | ||
| // * find | ||
| // */ | ||
|
|
||
| describe('.find()', function() { | ||
|
|
||
| it('should run beforeFind and mutate values', function(done) { | ||
|
|
||
| person.find({name:'billy'}, function(err, users) { | ||
| assert(!err); | ||
| assert(users[0].isDeleted === false); | ||
| done(); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
|
|
||
| // /** | ||
| // * findOne | ||
| // */ | ||
|
|
||
| describe('.findOne()', function() { | ||
|
|
||
| it('should run beforefind and mutate values', function(done) { | ||
|
|
||
| person.findOne({name:'billy'}, function(err, user) { | ||
| assert(!err); | ||
| assert(user.isDeleted === false); | ||
| done(); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| }); | ||
|
|
||
|
|
||
| /** | ||
| * Test Callbacks can be defined as arrays and run in order. | ||
| */ | ||
|
|
||
| describe('array of functions', function() { | ||
| var person; | ||
|
|
||
| before(function(done) { | ||
| var waterline = new Waterline(); | ||
| var Model = Waterline.Collection.extend({ | ||
| identity: 'user', | ||
| connection: 'foo', | ||
| attributes: { | ||
| name: 'string' | ||
| }, | ||
|
|
||
| beforeFind: [ | ||
| // Function 1 | ||
| function(values, cb) { | ||
| values.name = values.name + ' fn1'; | ||
| cb(); | ||
| }, | ||
|
|
||
| // Function 2 | ||
| function(values, cb) { | ||
| values.name = values.name + ' fn2'; | ||
| cb(); | ||
| } | ||
| ] | ||
| }); | ||
|
|
||
| waterline.loadCollection(Model); | ||
|
|
||
| // Fixture Adapter Def | ||
| var adapterDef = { | ||
| find: function(con, col, values, cb) { return cb(null, [values.where,]);}, | ||
| findOne:function(conn,col,values,cb){return cb(null,values.where);} | ||
| }; | ||
|
|
||
| var connections = { | ||
| 'foo': { | ||
| adapter: 'foobar' | ||
| } | ||
| }; | ||
|
|
||
| waterline.initialize({ adapters: { foobar: adapterDef }, connections: connections }, function(err, colls) { | ||
| if(err) done(err); | ||
| person = colls.collections.user; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should run the functions in order on find()', function(done) { | ||
| person.find({ name: 'test' }, function(err, users) { | ||
| assert(!err); | ||
| assert(users[0].name === 'test fn1 fn2'); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should run the functions in order on findOne()', function(done) { | ||
| person.findOne({ name: 'test' }, function(err, user) { | ||
| assert(!err); | ||
| assert(user.name === 'test fn1 fn2'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this guys first point is a problem I think. If no criteria is passed to
Model.findthenvalueswill be null when it gets into thebeforeFindcallback.balderdashy#902 (comment)
Might need to do
criteria.where || {}or something like that...not totally sure of the implications there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution suggested here balderdashy#902 (comment)