Merge pull request #889 from strongloop/PUT-bulk-update

[SEMVER-MAJOR] Disallow bulk updateOrCreate.
This commit is contained in:
Miroslav Bajtoš 2016-08-05 13:33:55 +02:00 committed by GitHub
commit e3b6b7891c
3 changed files with 68 additions and 6 deletions

View File

@ -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) See [related code change](https://github.com/strongloop/loopback-datasource-juggler/pull/976)
for more details. 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 options === 'object', 'The options argument must be an object');
assert(typeof cb === 'function', 'The cb argument must be a function'); 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 hookState = {};
var self = this; var self = this;

View File

@ -658,21 +658,67 @@ describe('manipulation', function() {
}); });
describe('updateOrCreate', function() { describe('updateOrCreate', function() {
var ds = getSchema(); var Post, Todo;
var Post;
before('prepare "Post" model', function(done) { before('prepare "Post" and "Todo" models', function(done) {
Post = ds.define('Post', { Post = db.define('Post', {
title: { type: String, id: true }, title: { type: String, id: true },
content: { type: String }, 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() { it('has an alias "patchOrCreate"', function() {
StubUser.updateOrCreate.should.equal(StubUser.patchOrCreate); 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) { it('should preserve properties with dynamic setters on create', function(done) {
StubUser.updateOrCreate({ password: 'foo' }, function(err, created) { StubUser.updateOrCreate({ password: 'foo' }, function(err, created) {
if (err) return done(err); if (err) return done(err);