Skip to content

Commit

Permalink
fix: pass CLS transaction to model hooks (#15818)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
joker00777 and WikiRik committed Mar 22, 2023
1 parent 1e68681 commit 46d3553
Show file tree
Hide file tree
Showing 2 changed files with 347 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/model.js
Expand Up @@ -1759,6 +1759,14 @@ class Model {
tableNames[this.getTableName(options)] = true;
options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

_.defaults(options, { hooks: true });

// set rejectOnEmpty option, defaults to model options
Expand Down Expand Up @@ -1965,6 +1973,14 @@ class Model {
}
options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

if (options.limit === undefined) {
const uniqueSingleColumns = _.chain(this.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value();

Expand Down Expand Up @@ -2075,6 +2091,15 @@ class Model {
static async count(options) {
options = Utils.cloneDeep(options);
options = _.defaults(options, { hooks: true });

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

options.raw = true;
if (options.hooks) {
await this.runHooks('beforeCount', options);
Expand Down Expand Up @@ -2521,6 +2546,14 @@ class Model {
...Utils.cloneDeep(options)
};

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

const createdAtAttr = this._timestampAttributes.createdAt;
const updatedAtAttr = this._timestampAttributes.updatedAt;
const hasPrimary = this.primaryKeyField in values || this.primaryKeyAttribute in values;
Expand Down Expand Up @@ -2616,6 +2649,14 @@ class Model {
const now = Utils.now(this.sequelize.options.dialect);
options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

options.model = this;

if (!options.includeValidated) {
Expand Down Expand Up @@ -2972,6 +3013,14 @@ class Model {
static async destroy(options) {
options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

this._injectScope(options);

if (!options || !(options.where || options.truncate)) {
Expand Down Expand Up @@ -3062,6 +3111,14 @@ class Model {
...options
};

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

options.type = QueryTypes.RAW;
options.model = this;

Expand Down Expand Up @@ -3127,6 +3184,14 @@ class Model {
static async update(values, options) {
options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

this._injectScope(options);
this._optionsMustContainWhere(options);

Expand Down Expand Up @@ -3917,6 +3982,15 @@ class Model {
}

options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

options = _.defaults(options, {
hooks: true,
validate: true
Expand Down Expand Up @@ -4237,6 +4311,15 @@ class Model {
if (Array.isArray(options)) options = { fields: options };

options = Utils.cloneDeep(options);

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

const setOptions = Utils.cloneDeep(options);
setOptions.attributes = options.fields;
this.set(values, setOptions);
Expand Down Expand Up @@ -4271,6 +4354,14 @@ class Model {
...options
};

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

// Run before hook
if (options.hooks) {
await this.constructor.runHooks('beforeDestroy', this, options);
Expand Down Expand Up @@ -4340,6 +4431,14 @@ class Model {
...options
};

// Add CLS transaction
if (options.transaction === undefined && this.sequelize.constructor._cls) {
const t = this.sequelize.constructor._cls.get('transaction');
if (t) {
options.transaction = t;
}
}

// Run before hook
if (options.hooks) {
await this.constructor.runHooks('beforeRestore', this, options);
Expand Down

0 comments on commit 46d3553

Please sign in to comment.