Skip to content
Merged
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
12 changes: 6 additions & 6 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
}

var context = {
Model: Model,
Model: Model,
query: byIdQuery(Model, id),
hookState: hookState,
options: options
Expand Down Expand Up @@ -410,7 +410,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
self.getDataSource().connector
.updateOrCreate(Model.modelName, update, done);

function done(err, data, result) {
function done(err, data, info) {
var obj;
if (data && !(data instanceof Model)) {
inst._initProperties(data, { persisted: true });
Expand All @@ -427,7 +427,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
var context = {
Model: Model,
instance: obj,
isNewInstance: result ? result.isNewInstance : undefined,
isNewInstance: info ? info.isNewInstance : undefined,
hookState: hookState,
options: options
};
Expand Down Expand Up @@ -1356,11 +1356,11 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
cb = cb || utils.createPromiseCallback();
where = where || {};
options = options || {};

assert(typeof where === 'object', 'The where argument must be an object');
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');

var hookState = {};

var query = { where: where };
Expand Down Expand Up @@ -1593,7 +1593,7 @@ DataAccessObject.prototype.save = function (options, cb) {

assert(typeof options === 'object', 'The options argument should be an object');
assert(typeof cb === 'function', 'The cb argument should be a function');

var hookState = {};

if (options.validate === undefined) {
Expand Down
11 changes: 8 additions & 3 deletions test/persistence-hooks.suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,19 @@ module.exports = function(dataSource, should) {
extra: undefined
},
isNewInstance: false,
options: { throws: false, validate: true } }));
options: { throws: false, validate: true }
}));
done();
});
});

it('triggers `after save` hook on create', function(done) {
TestModel.observe('after save', pushContextAndNext());

var instance = new TestModel({ name: 'created' });
var instance = new TestModel(
{ id: 'new-id', name: 'created' },
{ persisted: true });

instance.save(function(err, instance) {
if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({
Expand All @@ -542,7 +546,8 @@ module.exports = function(dataSource, should) {
name: 'created',
extra: undefined
},
isNewInstance: true
isNewInstance: true,
options: { throws: false, validate: true }
}));
done();
});
Expand Down