Skip to content

Commit

Permalink
Rename update/2->bulkUpdate, deprecate update/2 implement update/3
Browse files Browse the repository at this point in the history
- update({where, update}) is deprecated now in favor of bulkUpdate
- update(id, data) added
  • Loading branch information
1602 committed Jul 24, 2016
1 parent da75159 commit f6b0572
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -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 });
```
Expand Down
29 changes: 26 additions & 3 deletions lib/model.js
Expand Up @@ -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'
];

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions test/basic-querying.test.js
Expand Up @@ -318,7 +318,7 @@ describe('basic-querying', function() {

});

describe('update', function() {
describe('bulk', function() {

var Model;

Expand All @@ -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'); });
});
Expand All @@ -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' }
});
Expand All @@ -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
Expand All @@ -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'); });
});
Expand Down
2 changes: 1 addition & 1 deletion test/model.test.js
Expand Up @@ -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'}
});
Expand Down

0 comments on commit f6b0572

Please sign in to comment.