Skip to content
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

enable afterFind and afterFindOne lifecycle callbacks #1610

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/waterline/methods/find-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,19 @@ module.exports = function findOne( /* criteria?, populates?, explicitCbMaybe?, m
return proceed(undefined, thePopulatedRecord);
}//-•

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// FUTURE: This is where the `afterFindOne()` lifecycle callback would go
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return proceed(undefined, thePopulatedRecord);
// If no afterFindOne callback defined, just proceed.
if (!_.has(WLModel._callbacks, 'afterFindOne')) {
return proceed(undefined, thePopulatedRecord);
}//-•

// Otherwise, run it.
return WLModel._callbacks.afterFindOne(thePopulatedRecord, function(err) {
if (err) {
return proceed(err);
}

return proceed(undefined, thePopulatedRecord);
});

})(function _afterPotentiallyRunningAfterLC(err, thePopulatedRecord){
if (err) { return done(err); }
Expand Down
17 changes: 13 additions & 4 deletions lib/waterline/methods/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,19 @@ module.exports = function find( /* criteria?, populates?, explicitCbMaybe?, meta
return proceed(undefined, populatedRecords);
}//-•

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// FUTURE: This is where the `afterFind()` lifecycle callback would go
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
return proceed(undefined, populatedRecords);
// If no afterFind callback defined, just proceed.
if (!_.has(WLModel._callbacks, 'afterFind')) {
return proceed(undefined, populatedRecords);
}//-•

// Otherwise, run it.
return WLModel._callbacks.afterFind(populatedRecords, function(err) {
if (err) {
return proceed(err);
}

return proceed(undefined, populatedRecords);
});

})(function _afterPotentiallyRunningAfterLC(err, populatedRecords) {
if (err) { return done(err); }
Expand Down
108 changes: 108 additions & 0 deletions test/unit/callbacks/afterFind.find.js
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();
});
});
});
});
65 changes: 65 additions & 0 deletions test/unit/callbacks/afterFindOne.findOne.js
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();
});
});

});
});
65 changes: 65 additions & 0 deletions test/unit/callbacks/afterFindOne.findOrCreate.js
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();
});
});

});
});