mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 14:38:50 +00:00
feat(db): experimental transaction export
Don't use this if you don't understand it.
This commit is contained in:
1 parent
43e0df4a5b
commit
572c6ede88
2 files changed
+56
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { getPoolConnection } from './connection';
|
||||
import { logError } from '../logger';
|
||||
import { CFXCallback, CFXParameters } from '../types';
|
||||
import { setCallback } from '../utils/setCallback';
|
||||
|
||||
export const startTransaction = async (
|
||||
invokingResource: string,
|
||||
queries: (...args: any[]) => Promise<boolean>,
|
||||
parameters: CFXParameters,
|
||||
cb?: CFXCallback,
|
||||
isPromise?: boolean
|
||||
) => {
|
||||
cb = setCallback(parameters, cb);
|
||||
|
||||
const conn = await getPoolConnection();
|
||||
|
||||
if (!conn) return;
|
||||
|
||||
let response = false;
|
||||
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
|
||||
const commit = await queries({
|
||||
query: async (sql: string, values?: CFXParameters) => {
|
||||
const [rows] = await conn.query(sql, values);
|
||||
return rows;
|
||||
},
|
||||
});
|
||||
|
||||
response = !!commit;
|
||||
|
||||
response ? conn.commit() : conn.rollback();
|
||||
} catch (err: any) {
|
||||
conn.rollback();
|
||||
logError(invokingResource, cb, isPromise, err);
|
||||
} finally {
|
||||
conn.release();
|
||||
}
|
||||
|
||||
return cb ? cb(response) : response;
|
||||
};
|
||||
@@ -73,6 +73,19 @@ MySQL.transaction = (
|
||||
rawTransaction(invokingResource, queries, parameters, cb, isPromise);
|
||||
};
|
||||
|
||||
global.exports(
|
||||
'experimentalTransaction',
|
||||
async (
|
||||
transactions: () => Promise<boolean>,
|
||||
parameters: CFXParameters,
|
||||
cb: CFXCallback,
|
||||
invokingResource = GetInvokingResource(),
|
||||
isPromise?: boolean
|
||||
) => {
|
||||
return await startTransaction(invokingResource, transactions, parameters, cb, isPromise);
|
||||
}
|
||||
);
|
||||
|
||||
MySQL.prepare = (
|
||||
query: string,
|
||||
parameters: CFXParameters,
|
||||
@@ -108,6 +121,7 @@ function provide(resourceName: string, method: string, cb: Function) {
|
||||
|
||||
import ghmatti from './compatibility/ghmattimysql';
|
||||
import mysqlAsync from './compatibility/mysql-async';
|
||||
import { startTransaction } from 'database/startTransaction';
|
||||
|
||||
for (const key in MySQL) {
|
||||
const exp = MySQL[key];
|
||||
|
||||
Reference in new issue
Block a user