Merge pull request #889 from strongloop/PUT-bulk-update
[SEMVER-MAJOR] Disallow bulk updateOrCreate.
This commit is contained in:
commit
e3b6b7891c
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue