Added notify flag for create and upsert (#1277)

* Added notify flag for create and upsert

* Code styling

* More code styling

* Added unit test for notify

* Fix PR linter
This commit is contained in:
Jonathan Sheely 2017-04-05 13:23:26 -04:00 committed by Sakib Hasan
parent ef143dc5eb
commit 21b08af7a3
2 changed files with 36 additions and 7 deletions

View File

@ -398,10 +398,13 @@ DataAccessObject.create = function(data, options, cb) {
hookState: hookState,
options: options,
};
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
});
if (options.notify !== false) {
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
});
} else {
cb(null, obj);
}
});
});
});
@ -623,9 +626,13 @@ DataAccessObject.upsert = function(data, options, cb) {
options: options,
};
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
});
if (options.notify !== false) {
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
});
} else {
cb(null, obj);
}
}
});
}

View File

@ -363,6 +363,17 @@ module.exports = function(dataSource, should, connectorCapabilities) {
});
});
it('aborts when `after save` fires when option to notify is false', function(done) {
monitorHookExecution();
TestModel.create({name: 'created'}, {notify: false}, function(err, record, created) {
if (err) return done(err);
hookMonitor.names.should.not.containEql('after save');
done();
});
});
it('triggers `before save` hook', function(done) {
TestModel.observe('before save', ctxRecorder.recordAndNext());
@ -2202,6 +2213,17 @@ module.exports = function(dataSource, should, connectorCapabilities) {
});
});
it('aborts when `after save` fires on update or create when option to notify is false', function(done) {
monitorHookExecution();
TestModel.updateOrCreate({name: 'created'}, {notify: false}, function(err, record, created) {
if (err) return done(err);
hookMonitor.names.should.not.containEql('after save');
done();
});
});
it('triggers `after save` hook on create', function(done) {
TestModel.observe('after save', ctxRecorder.recordAndNext());