import { Sequelize, Transaction } from "sequelize"; import { TBusinessTransaction } from "../../domain"; import { InfrastructureError } from "../InfrastructureError"; export type SequelizeBusinessTransactionType = TBusinessTransaction & { complete(work: (t: Transaction) => Promise): Promise; }; export class SequelizeBusinessTransaction implements SequelizeBusinessTransactionType { private _connection: Sequelize; constructor(connection: Sequelize) { this._connection = connection; } public start(): void { return; } public async complete(work: (t: Transaction) => Promise): Promise { 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, ); } } }