mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
add expectTransaction to abstract transactor class
This commit is contained in:
parent
7a0e5b4a97
commit
d213df36dc
@ -9,7 +9,7 @@ import Group, {
|
|||||||
IGroupUser,
|
IGroupUser,
|
||||||
IGroupUserModel,
|
IGroupUserModel,
|
||||||
} from '../types/group';
|
} from '../types/group';
|
||||||
import { expectTransaction, Transactor } from './transactional';
|
import { Transactor } from './transactional';
|
||||||
|
|
||||||
const T = {
|
const T = {
|
||||||
GROUPS: 'groups',
|
GROUPS: 'groups',
|
||||||
@ -210,7 +210,7 @@ export default class GroupStore
|
|||||||
deletableUsers: IGroupUser[],
|
deletableUsers: IGroupUser[],
|
||||||
userName: string,
|
userName: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
expectTransaction(this.db);
|
this.expectTransaction(this.db);
|
||||||
await this.addUsersToGroup(groupId, newUsers, userName);
|
await this.addUsersToGroup(groupId, newUsers, userName);
|
||||||
await this.deleteUsersFromGroup(deletableUsers);
|
await this.deleteUsersFromGroup(deletableUsers);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
import { Knex } from 'knex';
|
import { Knex } from 'knex';
|
||||||
import { Transactional } from 'lib/types/stores/transactional';
|
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> {
|
export abstract class Transactor<T> implements Transactional<T> {
|
||||||
args: any[];
|
args: any[];
|
||||||
|
|
||||||
@ -19,18 +33,8 @@ export abstract class Transactor<T> implements Transactional<T> {
|
|||||||
clone.db = transaction;
|
clone.db = transaction;
|
||||||
return clone as T;
|
return clone as T;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export const expectTransaction = (db: Knex | Knex.Transaction): void => {
|
expectTransaction(db: Knex | Knex.Transaction): void {
|
||||||
if (db.isTransaction) {
|
expectTransaction(db);
|
||||||
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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user