Skip to content

Commit e3b6b78

Browse files
authored
Merge pull request #889 from strongloop/PUT-bulk-update
[SEMVER-MAJOR] Disallow bulk updateOrCreate.
2 parents c2a683b + 0b62dd9 commit e3b6b78

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

3.0-RELEASE-NOTES.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ applied to a LoopBack model will throw an error that needs to be handled.
6363
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/944)
6464
for more details.
6565

66-
##Remove deprecated model hooks and model events
66+
## Remove deprecated model hooks and model events
6767

6868
The following deprecated model events are now removed. Please upgrade your code by using
6969
operation hooks in its place.
@@ -91,3 +91,14 @@ See [related code change](https://github.com/strongloop/loopback-datasource-jugg
9191

9292
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/976)
9393
for more details.
94+
95+
## Disallow array input for updateOrCreate function
96+
97+
Allowing updateOrCreate to accept an array as input would only work when
98+
creating new model instances (not updating them), so this support has been
99+
removed. Please use `create` function instead (array inputs only worked to
100+
create new instances with this method before). If you would like to perform a
101+
bulk `updateOrCreate`, you could use `async.each` or `Promise.all` to
102+
repeatedly call `updateOfCreate` for each instance.
103+
104+
See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/889) here.

lib/dao.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ DataAccessObject.upsert = function(data, options, cb) {
465465
assert(typeof options === 'object', 'The options argument must be an object');
466466
assert(typeof cb === 'function', 'The cb argument must be a function');
467467

468+
if (Array.isArray(data)) {
469+
cb(new Error('updateOrCreate does not support bulk mode or any array input'));
470+
return cb.promise;
471+
}
472+
468473
var hookState = {};
469474

470475
var self = this;

test/manipulation.test.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,21 +658,67 @@ describe('manipulation', function() {
658658
});
659659

660660
describe('updateOrCreate', function() {
661-
var ds = getSchema();
662-
var Post;
661+
var Post, Todo;
663662

664-
before('prepare "Post" model', function(done) {
665-
Post = ds.define('Post', {
663+
before('prepare "Post" and "Todo" models', function(done) {
664+
Post = db.define('Post', {
666665
title: { type: String, id: true },
667666
content: { type: String },
668667
});
669-
ds.automigrate('Post', done);
668+
Todo = db.define('Todo', {
669+
content: String,
670+
});
671+
db.automigrate(['Post', 'Todo'], done);
672+
});
673+
674+
beforeEach(function deleteModelsInstances(done) {
675+
Todo.deleteAll(done);
670676
});
671677

672678
it('has an alias "patchOrCreate"', function() {
673679
StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate);
674680
});
675681

682+
it('creates a model when one does not exist', function(done) {
683+
Todo.updateOrCreate({ content: 'a' }, function(err, data) {
684+
if (err) return done(err);
685+
686+
Todo.findById(data.id, function(err, todo) {
687+
should.exist(todo);
688+
should.exist(todo.content);
689+
todo.content.should.equal('a');
690+
691+
done();
692+
});
693+
});
694+
});
695+
696+
it('updates a model if it exists', function(done) {
697+
Todo.create({ content: 'a' }, function(err, todo) {
698+
Todo.updateOrCreate({ id: todo.id, content: 'b' }, function(err, data) {
699+
if (err) return done(err);
700+
701+
should.exist(data);
702+
should.exist(data.id);
703+
data.id.should.equal(todo.id);
704+
should.exist(data.content);
705+
data.content.should.equal('b');
706+
707+
done();
708+
});
709+
});
710+
});
711+
712+
it('throws error for queries with array input', function(done) {
713+
Todo.updateOrCreate([{ content: 'a' }], function(err, data) {
714+
should.exist(err);
715+
err.message.should.containEql('bulk');
716+
should.not.exist(data);
717+
718+
done();
719+
});
720+
});
721+
676722
it('should preserve properties with dynamic setters on create', function(done) {
677723
StubUser.updateOrCreate({ password: 'foo' }, function(err, created) {
678724
if (err) return done(err);

0 commit comments

Comments
 (0)