Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
feat: allow primary key in update()
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfay committed Jun 3, 2018
1 parent d2b8d56 commit c606a8d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ db.tests.insert({
}).then(test => {...});
```

Last bit of housekeeping: `alice` still doesn't have a role, however, and we may have added more users without roles as well. Let's perform a bulk update to ensure that we're giving people the right access levels:
Last bit of housekeeping: `alice` still doesn't have a role, and we may have added more users without roles as well. Let's perform a bulk update to ensure that we're giving people the right access levels:

```javascript
db.auth.users.update({
Expand Down
2 changes: 1 addition & 1 deletion docs/persistence.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ db.tests.insert({

## update

`update` alters existing records in a table, given a criteria object and a map of column names to the new values. It returns a promise for an array containing the updated records.
`update` alters existing records in a table, given a criteria object (or unary primary key) and a map of column names to the new values. It returns a promise for an array containing the updated record(s) if passed a criteria object, or a promise for an object if passed a primary key.

```javascript
db.tests.update({
Expand Down
11 changes: 8 additions & 3 deletions lib/statement/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ const Update = function (source, changes, criteria = {}, options = {}) {
this.stream = options.stream;

if (source.isPkSearch(criteria, options)) {
// update doesn't support primitive searches, but this ensures that
// document pk searches use the standard generator
this.where = where(criteria, this.params.length);
if (_.isPlainObject(criteria)) {
// id:val search
this.where = where(criteria, this.params.length);
} else {
// primitive unary pk search
this.where = where(_.fromPairs([[source.pk[0], criteria]]), this.params.length);
this.single = true;
}
} else {
this.where = where(criteria, this.params.length, this.generator);
}
Expand Down
18 changes: 7 additions & 11 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ Table.prototype.insert = function (data, options) {
* criteria object and a map of fields to new values. Multi-row updates are
* only possible through the latter usage.
*
* @param {Object} criteria - A criteria object.
* @param {Object} changes - If using a criteria object without a primary key,
* a hash of column names to their new values.
* @param {String|Number|Object} criteria - Primary key of the record, or a
* criteria object.
* @param {Object} changes - A map of columns to their new values.
* @param {Object} [options] - {@link https://dmfay.github.io/massive-js/options.html|Update options}.
* @return {Promise} If updating a single record by its primary key, the
* modified record; otherwise, an array containing any modified records.
Expand All @@ -103,11 +103,7 @@ Table.prototype.update = function (criteria, changes, options = {}) {
return Promise.reject(new Error('Update requires a hash of fields=>values to update to'));
} else if (_.isEmpty(changes)) {
// there's nothing to update, so just return the matching records
if (options.single) {
return this.findOne(criteria);
}

return this.find(criteria);
return this.find(criteria, options);
}

const update = new Update(this, changes, criteria, options);
Expand Down Expand Up @@ -172,10 +168,10 @@ Table.prototype.destroy = function (criteria, options) {
};

/**
* Save a document to the database. This function replaces the entire document
* body.
* Save a document to the database. This function will create or replace the
* entire document body.
*
* @param {Object} doc - The document to write.
* @param {Object} doc - The document to persist.
* @return {Promise} The updated document.
*/
Table.prototype.saveDoc = function (doc) {
Expand Down
28 changes: 25 additions & 3 deletions test/table/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,33 @@ describe('update', function () {
return db.instance.$pool.end();
});

it('updates multiple normal_pk', function () {
return db.normal_pk.update({'id >': 2}, {field1: 'zeta'}).then(res => {
it('does nothing without changes and returns an array', function () {
return db.normal_pk.update({id: 1}, {}).then(res => {
assert.equal(res.length, 1);
assert.equal(res[0].id, 1);
assert.equal(res[0].field1, 'alpha');
});
});

it('does nothing without changes and returns an object for unary updates', function () {
return db.normal_pk.update(1, {}).then(res => {
assert.equal(res.id, 1);
assert.equal(res.field1, 'alpha');
});
});

it('updates a single record by primary key', function () {
return db.normal_pk.update(1, {field1: 'zeta'}).then(res => {
assert.equal(res.id, 1);
assert.equal(res.field1, 'zeta');
});
});

it('updates records by criteria', function () {
return db.normal_pk.update({'id >': 2}, {field1: 'eta'}).then(res => {
assert.equal(res.length, 1);
assert.equal(res[0].id, 3);
assert.equal(res[0].field1, 'zeta');
assert.equal(res[0].field1, 'eta');
});
});

Expand Down

0 comments on commit c606a8d

Please sign in to comment.