1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

add expectTransaction to abstract transactor class

This commit is contained in:
sighphyre 2022-10-24 11:35:55 +02:00
parent 7a0e5b4a97
commit d213df36dc
2 changed files with 19 additions and 15 deletions

View File

@ -9,7 +9,7 @@ import Group, {
IGroupUser,
IGroupUserModel,
} from '../types/group';
import { expectTransaction, Transactor } from './transactional';
import { Transactor } from './transactional';
const T = {
GROUPS: 'groups',
@ -210,7 +210,7 @@ export default class GroupStore
deletableUsers: IGroupUser[],
userName: string,
): Promise<void> {
expectTransaction(this.db);
this.expectTransaction(this.db);
await this.addUsersToGroup(groupId, newUsers, userName);
await this.deleteUsersFromGroup(deletableUsers);
}

View File

@ -1,6 +1,20 @@
import { Knex } from 'knex';
import { Transactional } from 'lib/types/stores/transactional';
export const expectTransaction = (db: Knex | Knex.Transaction): void => {
if (db.isTransaction) {
return;
}
const isRunningInTest = process.env.NODE_ENV === 'test';
const errorMessage =
'A store method that was expected to be run in a transaction was run outside of a transaction';
if (isRunningInTest) {
throw new Error(errorMessage);
} else {
console.error(errorMessage);
}
};
export abstract class Transactor<T> implements Transactional<T> {
args: any[];
@ -19,18 +33,8 @@ export abstract class Transactor<T> implements Transactional<T> {
clone.db = transaction;
return clone as T;
}
}
export const expectTransaction = (db: Knex | Knex.Transaction): void => {
if (db.isTransaction) {
return;
expectTransaction(db: Knex | Knex.Transaction): void {
expectTransaction(db);
}
const isRunningInTest = process.env.NODE_ENV === 'test';
const errorMessage =
'A store method that was expected to be run in a transaction was run outside of a transaction';
if (isRunningInTest) {
throw new Error(errorMessage);
} else {
console.error(errorMessage);
}
};