const TRANSACTION_NAMESPACE = "sequelize-transaction";

const cls = require("continuation-local-storage"),
    namespace = cls.createNamespace(TRANSACTION_NAMESPACE);

SequelizeOrigin.useCLS(namespace);

export const ns = namespace;

export const sequelize = new Sequelize({
    host,
    database,
    dialect: "mysql",
    username,
    password,
    modelPaths: [
        path.resolve(__dirname, "..", "model/**/*.repository{.ts,.js}"),
    ],
    logging: !profile.isProduction && false,
    modelMatch: (filename: string, member: string) => {
        if (filename === member) {
            return true;
        }

        const removeRepository = filename.split(".")[0];
        return removeRepository === member;
    },
});

export const transactionManager: TransactionManager = {
    transaction<T>(fn, options = {propagation: "REQUIRED"}) {
        if (options.propagation !== "REQUIRED") {
            return sequelize.transaction(() => fn());
        }

        const tx = cls.getNamespace(TRANSACTION_NAMESPACE).get("transaction");
        return tx ? fn() : sequelize.transaction(() => fn());
    },
};


'nodeJS > sequelize' 카테고리의 다른 글

Sequelize select객체에 대한 생각  (0) 2019.03.29
outer join 테이블 조건 where로 걸기  (0) 2019.03.24
sequelize 설정  (0) 2019.03.10

+ Recent posts