Skip to content

Transaction

You may want to run the functions inside a transaction. You can do it as the following:

main.ts
import { createConnection } from "mysql2/promise";
const conn = await createConnection({
host: "localhost",
database: "mydb",
user: "root",
password: "password",
});
//call the functions generated by TypeSQL
try {
await conn.beginTransaction();
await insertIntoFoo(conn, ...);
await insertIntoBar(conn, ...);
// ...
await conn.commit();
} catch (error) {
await conn.rollback();
}