Files
outline/server/middlewares/transaction.ts
Apoorv Mishra f4461573de Refactor to accommodate authentication, transaction and pagination states together (#4636)
* fix: refactor to accommodate authentication, transaction and pagination together into ctx.state

* feat: allow passing response type to APIContext
2023-01-04 23:51:44 +05:30

21 lines
675 B
TypeScript

import { Next } from "koa";
import { Transaction } from "sequelize";
import { sequelize } from "@server/database/sequelize";
import { AppContext } from "@server/types";
/**
* Middleware that wraps a route in a database transaction, useful for mutations
* The transaction is available on the context as `ctx.state.transaction` and
* should be passed to all database calls within the route.
*
* @returns The middleware function.
*/
export function transaction() {
return async function transactionMiddleware(ctx: AppContext, next: Next) {
await sequelize.transaction(async (t: Transaction) => {
ctx.state.transaction = t;
return next();
});
};
}