Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): add hooks to InstanceDestroyOptions type #13491

Merged
merged 6 commits into from Jan 12, 2022

Conversation

apazzolini
Copy link
Contributor

Description of change

In the model destroy method, the code allows for the user to specify hooks, which was missing from the InstanceDestroyOptions type definition.

See:

sequelize/lib/model.js

Lines 4150 to 4197 in 56bb1d6

/**
* Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will either be completely deleted, or have its deletedAt timestamp set to the current time.
*
* @param {object} [options={}] destroy options
* @param {boolean} [options.force=false] If set to true, paranoid models will actually be deleted
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction] Transaction to run query under
* @param {string} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @returns {Promise}
*/
async destroy(options) {
options = {
hooks: true,
force: false,
...options
};
// Run before hook
if (options.hooks) {
await this.constructor.runHooks('beforeDestroy', this, options);
}
const where = this.where(true);
let result;
if (this.constructor._timestampAttributes.deletedAt && options.force === false) {
const attributeName = this.constructor._timestampAttributes.deletedAt;
const attribute = this.constructor.rawAttributes[attributeName];
const defaultValue = Object.prototype.hasOwnProperty.call(attribute, 'defaultValue')
? attribute.defaultValue
: null;
const currentValue = this.getDataValue(attributeName);
const undefinedOrNull = currentValue == null && defaultValue == null;
if (undefinedOrNull || _.isEqual(currentValue, defaultValue)) {
// only update timestamp if it wasn't already set
this.setDataValue(attributeName, new Date());
}
result = await this.save({ ...options, hooks: false });
} else {
result = await this.constructor.queryInterface.delete(this, this.constructor.getTableName(options), where, { type: QueryTypes.DELETE, limit: null, ...options });
}
// Run after hook
if (options.hooks) {
await this.constructor.runHooks('afterDestroy', this, options);
}
return result;
}

@Keimeno Keimeno added the type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Oct 10, 2021
@github-actions github-actions bot added stale and removed stale labels Oct 27, 2021
@github-actions github-actions bot added the stale label Nov 4, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@github-actions github-actions bot added the stale label Nov 30, 2021
@ephys
Copy link
Member

ephys commented Jan 6, 2022

Looks good to me but we should add a typing test to prevent a regression. It's been a while since you've opened this PR, I'll add it for you

@ephys ephys self-assigned this Jan 6, 2022
@ephys ephys removed the stale label Jan 6, 2022
@ephys ephys requested a review from wbourne0 January 6, 2022 15:15
@ephys ephys changed the title Add hooks to InstanceDestroyOptions type fix(types): add hooks to InstanceDestroyOptions type Jan 12, 2022
@ephys ephys merged commit 6d4b553 into sequelize:main Jan 12, 2022
sdepold added a commit that referenced this pull request Jan 22, 2022
Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
Co-authored-by: Zoé <zoe@ephys.dev>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 7, 2022

🎉 This PR is included in version 7.0.0-alpha.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

aliatsis pushed a commit to creditiq/sequelize that referenced this pull request Jun 2, 2022
Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
Co-authored-by: Zoé <zoe@ephys.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @v7 type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants