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:
parent
ef143dc5eb
commit
21b08af7a3
21
lib/dao.js
21
lib/dao.js
|
@ -398,10 +398,13 @@ DataAccessObject.create = function(data, options, cb) {
|
||||||
hookState: hookState,
|
hookState: hookState,
|
||||||
options: options,
|
options: options,
|
||||||
};
|
};
|
||||||
|
if (options.notify !== false) {
|
||||||
Model.notifyObserversOf('after save', context, function(err) {
|
Model.notifyObserversOf('after save', context, function(err) {
|
||||||
cb(err, obj);
|
cb(err, obj);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
cb(null, obj);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -623,9 +626,13 @@ DataAccessObject.upsert = function(data, options, cb) {
|
||||||
options: options,
|
options: options,
|
||||||
};
|
};
|
||||||
|
|
||||||
Model.notifyObserversOf('after save', context, function(err) {
|
if (options.notify !== false) {
|
||||||
cb(err, obj);
|
Model.notifyObserversOf('after save', context, function(err) {
|
||||||
});
|
cb(err, obj);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
cb(null, obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
it('triggers `before save` hook', function(done) {
|
||||||
TestModel.observe('before save', ctxRecorder.recordAndNext());
|
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) {
|
it('triggers `after save` hook on create', function(done) {
|
||||||
TestModel.observe('after save', ctxRecorder.recordAndNext());
|
TestModel.observe('after save', ctxRecorder.recordAndNext());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue