-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): experimental transaction export
Don't use this if you don't understand it.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters