Skip to content

Commit

Permalink
feat: rename ALS back to CLS (#15427)
Browse files Browse the repository at this point in the history
* feat: rename ALS to CLS again

* apply review comments

* rename als -> cls test file

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
ephys and WikiRik committed Dec 19, 2022
1 parent 8a32374 commit 766b2ea
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/model-internals.ts
Expand Up @@ -148,9 +148,9 @@ export function throwInvalidInclude(include: any): never {
Got ${NodeUtil.inspect(include)} instead`);
}

export function setTransactionFromAls(options: Transactionable, sequelize: Sequelize): void {
export function setTransactionFromCls(options: Transactionable, sequelize: Sequelize): void {
if (options.transaction === undefined) {
options.transaction = sequelize.getCurrentAlsTransaction();
options.transaction = sequelize.getCurrentClsTransaction();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/model.d.ts
Expand Up @@ -66,7 +66,7 @@ export interface Transactionable {
/**
* The transaction in which this query must be run.
*
* If {@link Options.disableAlsTransactions} has not been set to true, and a transaction is running in the current ALS context,
* If {@link Options.disableClsTransactions} has not been set to true, and a transaction is running in the current AsyncLocalStorage context,
* that transaction will be used, unless null or a Transaction is manually specified here.
*/
transaction?: Transaction | null | undefined;
Expand Down
24 changes: 12 additions & 12 deletions src/model.js
Expand Up @@ -31,7 +31,7 @@ const { QueryTypes } = require('./query-types');
const sequelizeErrors = require('./errors');
const DataTypes = require('./data-types');
const { Op } = require('./operators');
const { _validateIncludedElements, combineIncludes, throwInvalidInclude, setTransactionFromAls } = require('./model-internals');
const { _validateIncludedElements, combineIncludes, throwInvalidInclude, setTransactionFromCls } = require('./model-internals');
const { noDoubleNestedGroup, scopeRenamedToWithScope, schemaRenamedToWithSchema, noModelDropSchema } = require('./utils/deprecations');

// This list will quickly become dated, but failing to maintain this list just means
Expand Down Expand Up @@ -1195,7 +1195,7 @@ ${associationOwner._getAssociationDebugList()}`);
tableNames[this.getTableName(options)] = true;
options = cloneDeep(options);

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

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

Expand Down Expand Up @@ -1512,7 +1512,7 @@ ${associationOwner._getAssociationDebugList()}`);
options = cloneDeep(options);
options = _.defaults(options, { hooks: true });

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

options.raw = true;
if (options.hooks) {
Expand Down Expand Up @@ -1793,7 +1793,7 @@ ${associationOwner._getAssociationDebugList()}`);
}
}

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

const internalTransaction = !options.transaction;
let values;
Expand Down Expand Up @@ -1954,7 +1954,7 @@ ${associationOwner._getAssociationDebugList()}`);
...cloneDeep(options),
};

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

const modelDefinition = this.modelDefinition;

Expand Down Expand Up @@ -2061,7 +2061,7 @@ ${associationOwner._getAssociationDebugList()}`);
const now = new Date();
options = cloneDeep(options);

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

options.model = this;

Expand Down Expand Up @@ -2424,7 +2424,7 @@ ${associationOwner._getAssociationDebugList()}`);
static async destroy(options) {
options = cloneDeep(options);

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

this._injectScope(options);

Expand Down Expand Up @@ -2523,7 +2523,7 @@ ${associationOwner._getAssociationDebugList()}`);
...options,
};

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

options.type = QueryTypes.RAW;
options.model = this;
Expand Down Expand Up @@ -2586,7 +2586,7 @@ ${associationOwner._getAssociationDebugList()}`);
static async update(values, options) {
options = cloneDeep(options);

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

this._injectScope(options);
this._optionsMustContainWhere(options);
Expand Down Expand Up @@ -3515,7 +3515,7 @@ Instead of specifying a Model, either:
validate: true,
});

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

const modelDefinition = this.constructor.modelDefinition;

Expand Down Expand Up @@ -3896,7 +3896,7 @@ Instead of specifying a Model, either:
...options,
};

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

const modelDefinition = this.constructor.modelDefinition;

Expand Down Expand Up @@ -3979,7 +3979,7 @@ Instead of specifying a Model, either:
...options,
};

setTransactionFromAls(options, this.sequelize);
setTransactionFromCls(options, this.sequelize);

// Run before hook
if (options.hooks) {
Expand Down
24 changes: 12 additions & 12 deletions src/sequelize-typescript.ts
Expand Up @@ -177,10 +177,10 @@ export abstract class SequelizeTypeScript {
beforeAssociate = legacyBuildAddHook(instanceSequelizeHooks, 'beforeAssociate');
afterAssociate = legacyBuildAddHook(instanceSequelizeHooks, 'afterAssociate');

#transactionAls: AsyncLocalStorage<Transaction> | undefined;
#transactionCls: AsyncLocalStorage<Transaction> | undefined;

private _setupTransactionAls() {
this.#transactionAls = new AsyncLocalStorage<Transaction>();
private _setupTransactionCls() {
this.#transactionCls = new AsyncLocalStorage<Transaction>();
}

addModels(models: ModelStatic[]) {
Expand All @@ -198,10 +198,10 @@ export abstract class SequelizeTypeScript {
/**
* Returns the transaction that is associated to the current asynchronous operation.
* This method returns undefined if no transaction is active in the current asynchronous operation,
* or if {@link Options.disableAlsTransactions} is true.
* or if {@link Options.disableClsTransactions} is true.
*/
getCurrentAlsTransaction(): Transaction | undefined {
return this.#transactionAls?.getStore();
getCurrentClsTransaction(): Transaction | undefined {
return this.#transactionCls?.getStore();
}

/**
Expand All @@ -222,12 +222,12 @@ export abstract class SequelizeTypeScript {
* ```
*
* By default, Sequelize uses AsyncLocalStorage to automatically pass the transaction to all queries executed inside the callback (unless you already pass one or set the `transaction` option to null).
* This can be disabled by setting {@link Options.disableAlsTransactions} to true. You will then need to pass transactions to your queries manually.
* This can be disabled by setting {@link Options.disableClsTransactions} to true. You will then need to pass transactions to your queries manually.
*
* ```ts
* const sequelize = new Sequelize({
* // ...
* disableAlsTransactions: true,
* disableClsTransactions: true,
* })
*
* await sequelize.transaction(transaction => {
Expand Down Expand Up @@ -292,20 +292,20 @@ export abstract class SequelizeTypeScript {
return result;
};

const als = this.#transactionAls;
if (!als) {
const cls = this.#transactionCls;
if (!cls) {
return wrappedCallback();
}

return als.run(transaction, wrappedCallback);
return cls.run(transaction, wrappedCallback);
}

/**
* We highly recommend using {@link Sequelize#transaction} instead.
* If you really want to use the manual solution, don't forget to commit or rollback your transaction once you are done with it.
*
* Transactions started by this method are not automatically passed to queries. You must pass the transaction object manually,
* even if {@link Options.disableAlsTransactions} is false.
* even if {@link Options.disableClsTransactions} is false.
*
* @example
* ```ts
Expand Down
4 changes: 2 additions & 2 deletions src/sequelize.d.ts
Expand Up @@ -453,10 +453,10 @@ export interface Options extends Logging {
* Disable the use of AsyncLocalStorage to automatically pass transactions started by {@link Sequelize#transaction}.
* You will need to pass transactions around manually if you disable this.
*/
disableAlsTransactions?: boolean;
disableClsTransactions?: boolean;
}

export interface NormalizedOptions extends PartlyRequired<Options, 'transactionType' | 'isolationLevel' | 'noTypeValidation' | 'dialectOptions' | 'dialect' | 'timezone' | 'disableAlsTransactions'> {
export interface NormalizedOptions extends PartlyRequired<Options, 'transactionType' | 'isolationLevel' | 'noTypeValidation' | 'dialectOptions' | 'dialect' | 'timezone' | 'disableClsTransactions'> {
readonly replication: NormalizedReplicationOptions;
}

Expand Down
8 changes: 4 additions & 4 deletions src/sequelize.js
Expand Up @@ -258,7 +258,7 @@ export class Sequelize extends SequelizeTypeScript {
benchmark: false,
minifyAliases: false,
logQueryParameters: false,
disableAlsTransactions: false,
disableClsTransactions: false,
...options,
pool: _.defaults(options.pool || {}, {
max: 5,
Expand All @@ -270,8 +270,8 @@ export class Sequelize extends SequelizeTypeScript {
};

// TODO: remove & assign property directly once this constructor has been migrated to the SequelizeTypeScript class
if (!this.options.disableAlsTransactions) {
this._setupTransactionAls();
if (!this.options.disableClsTransactions) {
this._setupTransactionCls();
}

if (!this.options.dialect) {
Expand Down Expand Up @@ -695,7 +695,7 @@ Use Sequelize#query if you wish to use replacements.`);

return await retry(async () => {
if (options.transaction === undefined) {
options.transaction = this.getCurrentAlsTransaction();
options.transaction = this.getCurrentClsTransaction();
}

checkTransaction();
Expand Down

0 comments on commit 766b2ea

Please sign in to comment.