mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 18:39:15 +03:00
28 lines
688 B
JavaScript
28 lines
688 B
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
|
|
import sql from "mssql";
|
|
import db from "#root/db.js";
|
|
|
|
export class UOW {
|
|
/** @type {AsyncLocalStorage<sql.Transaction>} */
|
|
static #als = new AsyncLocalStorage();
|
|
|
|
/** @param {() => Promise<unknown>} callback */
|
|
static async run(callback) {
|
|
const trx = new sql.Transaction(db);
|
|
try {
|
|
await trx.begin();
|
|
const ret = await this.#als.run(trx, callback);
|
|
await trx.commit();
|
|
return ret;
|
|
} catch (error) {
|
|
await trx.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static get request() {
|
|
const trx = this.#als.getStore();
|
|
return trx ? new sql.Request(trx) : new sql.Request(db);
|
|
}
|
|
}
|