Disallow bulk updateOrCreate.

This commit is contained in:
Richard Pringle 2016-04-05 00:55:15 -04:00 committed by Miroslav Bajtoš
parent c2a683bca4
commit 0b62dd94ea
3 changed files with 68 additions and 6 deletions

View File

@ -63,7 +63,7 @@ applied to a LoopBack model will throw an error that needs to be handled.
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/944)
for more details.
##Remove deprecated model hooks and model events
## Remove deprecated model hooks and model events
The following deprecated model events are now removed. Please upgrade your code by using
operation hooks in its place.
@ -91,3 +91,14 @@ See [related code change](https://github.com/strongloop/loopback-datasource-jugg
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/976)
for more details.
## Disallow array input for updateOrCreate function
Allowing updateOrCreate to accept an array as input would only work when
creating new model instances (not updating them), so this support has been
removed. Please use `create` function instead (array inputs only worked to
create new instances with this method before). If you would like to perform a
bulk `updateOrCreate`, you could use `async.each` or `Promise.all` to
repeatedly call `updateOfCreate` for each instance.
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/889) here.

View File

@ -465,6 +465,11 @@ DataAccessObject.upsert = function(data, options, cb) {
assert(typeof options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function');
if (Array.isArray(data)) {
cb(new Error('updateOrCreate does not support bulk mode or any array input'));
return cb.promise;
}
var hookState = {};
var self = this;

View File

@ -658,21 +658,67 @@ describe('manipulation', function() {
});
describe('updateOrCreate', function() {
var ds = getSchema();
var Post;
var Post, Todo;
before('prepare "Post" model', function(done) {
Post = ds.define('Post', {
before('prepare "Post" and "Todo" models', function(done) {
Post = db.define('Post', {
title: { type: String, id: true },
content: { type: String },
});
ds.automigrate('Post', done);
Todo = db.define('Todo', {
content: String,
});
db.automigrate(['Post', 'Todo'], done);
});
beforeEach(function deleteModelsInstances(done) {
Todo.deleteAll(done);
});
it('has an alias "patchOrCreate"', function() {
StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate);
});
it('creates a model when one does not exist', function(done) {
Todo.updateOrCreate({ content: 'a' }, function(err, data) {
if (err) return done(err);
Todo.findById(data.id, function(err, todo) {
should.exist(todo);
should.exist(todo.content);
todo.content.should.equal('a');
done();
});
});
});
it('updates a model if it exists', function(done) {
Todo.create({ content: 'a' }, function(err, todo) {
Todo.updateOrCreate({ id: todo.id, content: 'b' }, function(err, data) {
if (err) return done(err);
should.exist(data);
should.exist(data.id);
data.id.should.equal(todo.id);
should.exist(data.content);
data.content.should.equal('b');
done();
});
});
});
it('throws error for queries with array input', function(done) {
Todo.updateOrCreate([{ content: 'a' }], function(err, data) {
should.exist(err);
err.message.should.containEql('bulk');
should.not.exist(data);
done();
});
});
it('should preserve properties with dynamic setters on create', function(done) {
StubUser.updateOrCreate({ password: 'foo' }, function(err, created) {
if (err) return done(err);