Skip to content

Commit

Permalink
fix: bone.update({ deletedAt: null }) and likewise methods should work (
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjake authored Oct 25, 2022
1 parent e43fe1a commit eab20b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/bone.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class Bone {
* @public
* @param {Object} values
* @param {Object?} options
* @returns {number} affected rows
* @returns {Promise<number>} affected rows
* @memberof Bone
*/
async update(values, options = {}) {
Expand Down Expand Up @@ -697,7 +697,7 @@ class Bone {
/**
* Persist changes on current instance back to database with `UPDATE`.
* @private
* @return {number}
* @return {Promise<number>}
*/
async _update(values, options = {}) {
const Model = this.constructor;
Expand Down
7 changes: 5 additions & 2 deletions src/spell.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,16 @@ function joinAssociation(spell, BaseModel, baseName, refName, opts = {}) {
* If Model supports soft delete, and deletedAt isn't specified in whereConditions yet, and the table isn't a subquery, append a default where({ deletedAt: null }).
*/
function scopeDeletedAt(spell) {
const { table, whereConditions, Model } = spell;
const { table, sets, whereConditions, Model } = spell;

const { deletedAt } = Model.timestamps;

// from subquery
if (table.type !== 'id') return;

// UPDATE users SET deleted_at = NULL WHERE id = 42;
if (sets && sets[deletedAt] === null) return;

// deletedAt already specified
for (const condition of whereConditions) {
let found = false;
Expand Down Expand Up @@ -963,7 +966,7 @@ for (const aggregator in AGGREGATOR_MAP) {
value: function Spell_aggregator(name = '*') {
if (name instanceof Raw) {
this.$select(Raw.build(`${func.toUpperCase()}(${name}) AS ${aggregator}`));
return this
return this;
}
if (name !== '*' && parseExpr(name).type !== 'id') {
throw new Error(`unexpected operand ${name} for ${func.toUpperCase()}()`);
Expand Down
8 changes: 8 additions & 0 deletions test/integration/suite/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1620,5 +1620,13 @@ describe('=> Basic', () => {
await gywn.restore();
}, /Error: instance is not persisted yet./);
});

it('should be able to restore with vanilla update', async function() {
const note = await Note.create({ name: 'yes' });
await note.remove();
assert.equal(await Note.findOne(note.id), null);
await note.update({ deleted_at: null });
assert.ok(await Note.findOne(note.id));
});
});
});

0 comments on commit eab20b6

Please sign in to comment.