Skip to content

Commit

Permalink
feat: silent option fix #164
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyDaddy committed Aug 24, 2021
1 parent fdb9af7 commit 8a6c87f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,15 @@ module.exports = Bone => {

static increment(fields, options = {}) {
const { where, paranoid } = options;
const spell = super.update(where);
// pass options to update
const spell = super.update(where, undefined, options);

if (Array.isArray(fields)) {
for (const field of fields) spell.$increment(field);
for (const field of fields) spell.$increment(field, undefined, options);
} else if (fields != null && typeof fields === 'object') {
for (const field in fields) spell.$increment(field, fields[field]);
for (const field in fields) spell.$increment(field, fields[field], options);
} else if (typeof fields === 'string') {
spell.$increment(fields);
spell.$increment(fields, undefined, options);
} else {
throw new Error(`Unexpected fields: ${fields}`);
}
Expand Down Expand Up @@ -610,7 +611,6 @@ module.exports = Bone => {
}
this._validateAttributes(updateValues);
}

return Model.increment(fields, {
...options,
where: { [primaryKey]: this[primaryKey] },
Expand Down
30 changes: 19 additions & 11 deletions src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ function parseSelect(spell, ...names) {
* @returns
*/
function formatValueSet(spell, obj, strict = true) {
const { Model } = spell;
const { Model, silent = false, command } = spell;
const { timestamps } = Model;
const sets = {};
for (const name in obj) {
if (!Model.attributes.hasOwnProperty(name)) {
Expand All @@ -297,6 +298,7 @@ function formatValueSet(spell, obj, strict = true) {
}
}

if (silent && timestamps.updatedAt && name === timestamps.updatedAt && command === 'update') continue;
// raw sql don't need to uncast
if (obj[name] && obj[name].__raw) {
sets[name] = obj[name];
Expand Down Expand Up @@ -691,8 +693,8 @@ class Spell {
* @param {Object} obj - key-values pairs
*/
$insert(obj) {
this.sets = parseSet(this, obj);
this.command = 'insert';
this.sets = parseSet(this, obj);
return this;
}

Expand All @@ -702,14 +704,14 @@ class Spell {
* @param {Object} obj - key-value pairs to SET
*/
$upsert(obj) {
this.sets = parseSet(this, obj);
this.command = 'upsert';
this.sets = parseSet(this, obj);
return this;
}

$bulkInsert(records) {
this.sets = parseSet(this, records);
this.command = 'bulkInsert';
this.sets = parseSet(this, records);
return this;
}

Expand All @@ -732,14 +734,16 @@ class Spell {
* @param {Object} obj - key-value pairs to SET
*/
$update(obj) {
this.sets = parseSet(this, obj);
this.command = 'update';
this.sets = parseSet(this, obj);
return this;
}

$increment(name, by = 1) {
const { Model } = this;

$increment(name, by = 1, opts = {}) {
let { Model, silent = false } = this;
silent = opts.silent;
const { timestamps } = Model;
this.command = 'update';
if (!Number.isFinite(by)) throw new Error(`unexpected increment value ${by}`);
if (!Model.attributes.hasOwnProperty(name)) {
throw new Error(`undefined attribute "${name}"`);
Expand All @@ -757,12 +761,16 @@ class Spell {
__expr: true,
},
};
this.command = 'update';

if (timestamps.updatedAt && !this.sets[timestamps.updatedAt] && !silent) {
this.sets[timestamps.updatedAt] = new Date();
}

return this;
}

$decrement(name, by = 1) {
return this.$increment(name, -by);
$decrement(name, by = 1, opts = {}) {
return this.$increment(name, -by, opts);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,22 +751,32 @@ describe('=> Sequelize adapter', () => {

it('Model.increment()', async () => {
const isbn = 9787550616950;
const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
const book = await Book.create({ isbn, name: 'Book of Cain', price: 10 });
assert.notEqual(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
const clock = sinon.useFakeTimers(fakeDate);
await Book.increment('price', { where: { isbn } });
await book.reload();
assert.equal(book.price, 11);
assert.equal(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());

await Book.increment({ price: 2 }, { where: { isbn } });
await book.reload();
assert.equal(book.price, 13);
clock.restore();
});

it('Model.increment(, { paranoid })', async () => {
const isbn = 9787550616950;
const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
const book = await Book.create({ isbn, name: 'Book of Cain', price: 10 });
assert.notEqual(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
const clock = sinon.useFakeTimers(fakeDate);
await Book.increment('price', { where: { isbn } });
await book.reload();
assert.equal(book.price, 11);
assert.equal(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
clock.restore();

await Book.increment({ price: 2 }, { where: { isbn } });
await book.reload();
Expand All @@ -781,6 +791,20 @@ describe('=> Sequelize adapter', () => {
assert.equal(book.price, 15);
});

it('Model.increment(, { silent })', async () => {
const fakeDate = new Date(`2012-12-14 12:00-08:00`).getTime();
const clock = sinon.useFakeTimers(fakeDate);
const isbn = 9787550616950;
const book = await Book.create({ isbn, name: 'Book of Cain', price: 10 });
assert.equal(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
clock.restore();
await Book.increment('price', { where: { isbn }, silent: true });
await book.reload();
assert.equal(book.price, 11);
console.log(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
assert.equal(new Date(book.updatedAt).toString(), new Date(fakeDate).toString());
});

it('Model.removeAttribute()', async function() {
const Model = sequelize(Bone);
class User extends Model {};
Expand Down
24 changes: 22 additions & 2 deletions test/unit/spell.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = require('assert').strict;
const sinon = require('sinon');

const { connect, raw, Bone } = require('../..');

Expand Down Expand Up @@ -476,16 +477,20 @@ describe('=> Spell', function() {
});

it('increment', () => {
const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
sinon.useFakeTimers(fakeDate);
assert.equal(
Book.where({ isbn: 9787550616950 }).increment('price').toString(),
'UPDATE `books` SET `price` = `price` + 1 WHERE `isbn` = 9787550616950 AND `gmt_deleted` IS NULL'
"UPDATE `books` SET `price` = `price` + 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `isbn` = 9787550616950 AND `gmt_deleted` IS NULL"
);
});

it('decrement', () => {
const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
sinon.useFakeTimers(fakeDate);
assert.equal(
Book.where({ isbn: 9787550616950 }).decrement('price').toString(),
'UPDATE `books` SET `price` = `price` - 1 WHERE `isbn` = 9787550616950 AND `gmt_deleted` IS NULL'
"UPDATE `books` SET `price` = `price` - 1, `gmt_modified` = '2012-12-14 12:00:00.000' WHERE `isbn` = 9787550616950 AND `gmt_deleted` IS NULL"
);
});

Expand Down Expand Up @@ -584,4 +589,19 @@ describe('=> Spell', function() {
"INSERT INTO `articles` (`id`, `title`, `gmt_create`, `gmt_modified`) VALUES (1, 'New Post', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()) ON DUPLICATE KEY UPDATE `id` = LAST_INSERT_ID(`id`), `id`=VALUES(`id`), `title`=VALUES(`title`), `gmt_modified`=VALUES(`gmt_modified`)"
);
});

describe('silent should work', function() {
it('update', function () {
assert.equal(Post.update({ id: 1 }, { title: 'hello' }, { silent: true }).toString(),
"UPDATE `articles` SET `title` = 'hello' WHERE `id` = 1 AND `gmt_deleted` IS NULL");
});

it('increment, decrement', function () {
assert.equal(Book.find({ name: 'hello' }).increment('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` + 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");

assert.equal(Book.find({ name: 'hello' }).decrement('price', 1, { silent: true }).toString(),
"UPDATE `books` SET `price` = `price` - 1 WHERE `name` = 'hello' AND `gmt_deleted` IS NULL");
});
});
});

0 comments on commit 8a6c87f

Please sign in to comment.