-
Notifications
You must be signed in to change notification settings - Fork 596
enable afterFind and afterFindOne lifecycle callbacks #1610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cristianszwarc
wants to merge
5
commits into
balderdashy:master
Choose a base branch
from
WherewolfNZ:afterFindLifecycle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−8
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
47ffb25
add support for afterFind and afterFindOne lifecycle callbacks
cristianszwarc 709787a
add support for afterFind and afterFindOne lifecycle callbacks
cristianszwarc 058f131
fix misleading comment
cristianszwarc 014f2e8
Merge remote-tracking branch 'origin/afterFindLifecycle'
wulfsolter 5334e49
Merge pull request #1 from WherewolfNZ/master
wulfsolter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| var assert = require('assert'); | ||
| var _ = require('@sailshq/lodash'); | ||
| var Waterline = require('../../../lib/waterline'); | ||
|
|
||
| describe('After Find Lifecycle Callback ::', function() { | ||
| describe('Create ::', function() { | ||
| var person; | ||
|
|
||
| before(function(done) { | ||
| var waterline = new Waterline(); | ||
| var Model = Waterline.Model.extend({ | ||
| identity: 'user', | ||
| datastore: 'foo', | ||
| primaryKey: 'id', | ||
| fetchRecordsOnCreate: true, | ||
| attributes: { | ||
| id: { | ||
| type: 'number' | ||
| }, | ||
| name: { | ||
| type: 'string' | ||
| } | ||
| }, | ||
|
|
||
| afterFind: function(results, cb) { | ||
| results.forEach(function (record) { | ||
| record.name = record.name + ' updated'; | ||
| }); | ||
| return cb(); | ||
| } | ||
| }); | ||
|
|
||
| waterline.registerModel(Model); | ||
|
|
||
| // Fixture Adapter Def | ||
| var adapterDef = { find: function(con, query, cb) { return cb(null, [ | ||
| {id: 1, name: 'John Doe', criteria: query.criteria}, | ||
| {id: 2, name: 'Jane Doe', criteria: query.criteria}, | ||
| ]); }}; | ||
|
|
||
| var connections = { | ||
| 'foo': { | ||
| adapter: 'foobar' | ||
| } | ||
| }; | ||
|
|
||
| waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| person = orm.collections.user; | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should run afterFind and mutate values', function(done) { | ||
| person.find({}, {}, function(err, results) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| assert(_.isArray(results)); | ||
| assert.equal(results[0].name, 'John Doe updated'); | ||
| assert.equal(results[1].name, 'Jane Doe updated'); | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should run afterFind and mutate values when using deferreds', function(done) { | ||
| person.find() | ||
| .limit(1) | ||
| .skip(1) | ||
| .sort([{ name: 'desc' }]) | ||
| .exec(function(err, results) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| assert(_.isArray(results)); | ||
| assert.equal(results[0].name, 'John Doe updated'); | ||
| assert.equal(results[0].criteria.limit, 1); | ||
| assert.equal(results[0].criteria.skip, 1); | ||
| assert.equal(results[0].criteria.sort[0].name, 'DESC'); | ||
| assert.equal(results[1].name, 'Jane Doe updated'); | ||
|
|
||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should not run afterFind when skipAllLifecycleCallbacks is true', function(done) { | ||
| person.find() | ||
| .meta({ | ||
| skipAllLifecycleCallbacks: true | ||
| }) | ||
| .exec(function(err, results) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| assert(_.isArray(results)); | ||
| assert.equal(results[0].name, 'John Doe'); | ||
| assert.equal(results[1].name, 'Jane Doe'); | ||
|
|
||
| return done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| var assert = require('assert'); | ||
| var _ = require('@sailshq/lodash'); | ||
| var Waterline = require('../../../lib/waterline'); | ||
|
|
||
| describe('After FindOne Lifecycle Callback on findOrCreate::', function() { | ||
| describe('Create ::', function() { | ||
| var person; | ||
|
|
||
| before(function(done) { | ||
| var waterline = new Waterline(); | ||
| var Model = Waterline.Model.extend({ | ||
| identity: 'user', | ||
| datastore: 'foo', | ||
| primaryKey: 'id', | ||
| fetchRecordsOnCreate: true, | ||
| attributes: { | ||
| id: { | ||
| type: 'number' | ||
| }, | ||
| name: { | ||
| type: 'string' | ||
| } | ||
| }, | ||
|
|
||
| afterFindOne: function(record, cb) { | ||
| record.name = record.name + ' updated'; | ||
| return cb(); | ||
| } | ||
| }); | ||
|
|
||
| waterline.registerModel(Model); | ||
|
|
||
| // Fixture Adapter Def | ||
| var adapterDef = { find: function(con, query, cb) { return cb(null, [ | ||
| {id: 1, name: 'John Doe', criteria: query.criteria}, | ||
| ]); }}; | ||
|
|
||
| var connections = { | ||
| 'foo': { | ||
| adapter: 'foobar' | ||
| } | ||
| }; | ||
|
|
||
| waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| person = orm.collections.user; | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should run afterFindOne when findOne finds a record', function(done) { | ||
| person.findOne({ id: 1 }, { }, function(err, record) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| assert.equal(record.name, 'John Doe updated'); | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| var assert = require('assert'); | ||
| var _ = require('@sailshq/lodash'); | ||
| var Waterline = require('../../../lib/waterline'); | ||
|
|
||
| describe('After FindOne Lifecycle Callback on findOrCreate::', function() { | ||
| describe('Create ::', function() { | ||
| var person; | ||
|
|
||
| before(function(done) { | ||
| var waterline = new Waterline(); | ||
| var Model = Waterline.Model.extend({ | ||
| identity: 'user', | ||
| datastore: 'foo', | ||
| primaryKey: 'id', | ||
| fetchRecordsOnCreate: true, | ||
| attributes: { | ||
| id: { | ||
| type: 'number' | ||
| }, | ||
| name: { | ||
| type: 'string' | ||
| } | ||
| }, | ||
|
|
||
| afterFindOne: function(record, cb) { | ||
| record.name = record.name + ' updated'; | ||
| return cb(); | ||
| } | ||
| }); | ||
|
|
||
| waterline.registerModel(Model); | ||
|
|
||
| // Fixture Adapter Def | ||
| var adapterDef = { find: function(con, query, cb) { return cb(null, [ | ||
| {id: 1, name: 'John Doe', criteria: query.criteria}, | ||
| ]); }}; | ||
|
|
||
| var connections = { | ||
| 'foo': { | ||
| adapter: 'foobar' | ||
| } | ||
| }; | ||
|
|
||
| waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| person = orm.collections.user; | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should run afterFindOne when findOrCreate finds a record', function(done) { | ||
| person.findOrCreate({ id: 1 }, { name: 'John Doe' }, function(err, record) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
|
|
||
| assert.equal(record.name, 'John Doe updated'); | ||
| return done(); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.