From f6b0572e1a5ded5eda8e5f215fa32269bcc92283 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sun, 24 Jul 2016 23:32:10 +0100 Subject: [PATCH] Rename update/2->bulkUpdate, deprecate update/2 implement update/3 - update({where, update}) is deprecated now in favor of bulkUpdate - update(id, data) added --- README.md | 4 +--- lib/model.js | 29 ++++++++++++++++++++++++++--- test/basic-querying.test.js | 14 +++++++------- test/model.test.js | 2 +- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a03c059e..2859216e 100644 --- a/README.md +++ b/README.md @@ -273,11 +273,9 @@ user.destroy(); // destroy all instances User.destroyAll(); -// TODO: rename to bulkUpdate and deprecate // update multiple instances (currently only on the sql adapters) -Post.update({ update: { published: 0 }, where: { id: [1, 2, 3] }}); +Post.bulkUpdate({ update: { published: 0 }, where: { id: [1, 2, 3] }}); -// TODO: implement me // update single instance Post.update(1, { published: 1 }); ``` diff --git a/lib/model.js b/lib/model.js index 6ba10bd0..25cf8534 100644 --- a/lib/model.js +++ b/lib/model.js @@ -35,7 +35,10 @@ function AbstractClass(data) { } var promisedClassMethods = [ - 'create', 'all', 'destroyAll', 'upsert', 'updateOrCreate', 'findOrCreate', 'find', 'update', + 'create', 'all', + 'destroyAll', 'upsert', + 'updateOrCreate', 'findOrCreate', + 'find', 'update', 'bulkUpdate', 'findOne', 'exists', 'count' ]; @@ -147,11 +150,31 @@ AbstractClass.defineProperty = function(prop, params) { * @param {Object} params - { where:{uid:'10'}, update:{ Name:'New name' } } * @param callback(err, obj) */ -AbstractClass.update = function(params, cb) { +AbstractClass.update = function(id, data, cb) { + var Model = this; + + if (typeof id === 'object' && arguments.length === 2 && typeof data === 'function') { + return util.deprecate(function() { Model.bulkUpdate(id, data); }, 'Model.update({update: ..., where: ...}): use Model.bulkUpdate instead')(); + } + + Model.bulkUpdate({ where: { id: id }, limit: 1, update: data }, cb); +}; + +/** + * Update records + * + * @param {Object} params - { where: { uid: '10'}, update: { Name: 'New name' }, limit: 1} + * - update {Object}: data to update + * - where {Object}: same as for Model.all, required + * - limit {Number}: same as for Model.all, optional + * - skip {Number}: same as for Model.all, optional + * @param callback(err, obj) + */ +AbstractClass.bulkUpdate = function(params, cb) { var Model = this; if (params instanceof Array) { return when.all(params.map(function(param) { - return Model.update(param); + return Model.bulkUpdate(param); })) .then(function(res) { if (cb) { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 3a89862a..9f285aec 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -318,7 +318,7 @@ describe('basic-querying', function() { }); - describe('update', function() { + describe('bulk', function() { var Model; @@ -336,13 +336,13 @@ describe('basic-querying', function() { context('single update', function() { it('should throw when no sufficient params provided', function() { - return Model.update({ where: { foo: 1 }}) + return Model.bulkUpdate({ where: { foo: 1 }}) .then(function() { throw new Error('Unexpected success'); }) .catch(function(err) { expect(err.message).toBe('Required update'); }); }); it('should throw when no sufficient params provided', function() { - return Model.update({ update: { foo: 1 }}) + return Model.bulkUpdate({ update: { foo: 1 }}) .then(function() { throw new Error('Unexpected success'); }) .catch(function(err) { expect(err.message).toBe('Required where'); }); }); @@ -353,7 +353,7 @@ describe('basic-querying', function() { { foo: 'fuu', bar: 1 } ]) .then(function() { - return Model.update({ + return Model.bulkUpdate({ update: { bar: 2 }, where: { foo: 'fuu' } }); @@ -380,7 +380,7 @@ describe('basic-querying', function() { { foo: 'bar', bar: 1 } ]) .then(function() { - return Model.update({ + return Model.bulkUpdate({ update: { bar: 2 }, where: { foo: 'bar' }, limit: 1 @@ -401,13 +401,13 @@ describe('basic-querying', function() { context('multiple records', function() { it('should throw when no sufficient params provided', function() { - return Model.update([{ where: { foo: 1 }}]) + return Model.bulkUpdate([{ where: { foo: 1 }}]) .then(function() { throw new Error('Unexpected success'); }) .catch(function(err) { expect(err.message).toBe('Required update'); }); }); it('should throw when no sufficient params provided', function() { - return Model.update([{ update: { foo: 1 }}]) + return Model.bulkUpdate([{ update: { foo: 1 }}]) .then(function() { throw new Error('Unexpected success'); }) .catch(function(err) { expect(err.message).toBe('Required where'); }); }); diff --git a/test/model.test.js b/test/model.test.js index 50df2eb6..027acaf5 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -68,7 +68,7 @@ describe('Model', function() { return Model.create({field: 'hello'}) .then(function(inst) { cached = inst; - return Model.update({ + return Model.bulkUpdate({ where: {id: inst.id}, update: {field: 'data'} });