64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { Sequelize, Transaction } from "sequelize";
|
|
import { TBusinessTransaction } from "../../domain";
|
|
import { InfrastructureError } from "../InfrastructureError";
|
|
|
|
export type SequelizeBusinessTransactionType = TBusinessTransaction & {
|
|
complete<T>(work: (t: Transaction) => Promise<T>): Promise<T>;
|
|
};
|
|
|
|
export class SequelizeBusinessTransaction
|
|
implements SequelizeBusinessTransactionType
|
|
{
|
|
private _connection: Sequelize;
|
|
|
|
constructor(connection: Sequelize) {
|
|
this._connection = connection;
|
|
}
|
|
|
|
public start(): void {
|
|
return;
|
|
}
|
|
|
|
public async complete<T>(work: (t: Transaction) => Promise<T>): Promise<T> {
|
|
try {
|
|
return await this._connection.transaction(work);
|
|
} catch (error: unknown) {
|
|
//error instanceof BaseError;
|
|
|
|
/*
|
|
{
|
|
name: "SequelizeValidationError",
|
|
errors: [
|
|
{
|
|
message: "Customer.entity_type cannot be null",
|
|
type: "notNull Violation",
|
|
path: "entity_type",
|
|
value: null,
|
|
origin: "CORE",
|
|
instance: {
|
|
dataValues: {
|
|
id: "85ac4089-6ad7-4058-a16a-adf7fbbfe388",
|
|
created_at: "2023-08-02T10:42:49.248Z",
|
|
},
|
|
...
|
|
...
|
|
},
|
|
isNewRecord: true,
|
|
},
|
|
validatorKey: "is_null",
|
|
validatorName: null,
|
|
validatorArgs: [
|
|
],
|
|
},
|
|
],
|
|
}
|
|
*/
|
|
|
|
throw InfrastructureError.create(
|
|
InfrastructureError.UNEXCEPTED_ERROR,
|
|
(error as Error).message,
|
|
);
|
|
}
|
|
}
|
|
}
|