ts-sql-query
Advanced tools
Comparing version 1.56.0 to 1.57.0
@@ -30,7 +30,12 @@ "use strict"; | ||
pushTransactionStack() { | ||
if (this.onCommit || this.onCommitStack || this.onRollback || this.onRollbackStack) { | ||
if (this.onCommit || this.onCommitStack || this.onRollback || this.onRollbackStack || this.beforeCommit || this.beforeCommitStack) { | ||
if (!this.beforeCommitStack) { | ||
this.beforeCommitStack = []; | ||
} | ||
this.beforeCommitStack.push(this.beforeCommit || undefined); | ||
this.beforeCommit = undefined; | ||
if (!this.onCommitStack) { | ||
this.onCommitStack = []; | ||
} | ||
this.onCommitStack.push(this.onCommit); | ||
this.onCommitStack.push(this.onCommit || undefined); | ||
this.onCommit = undefined; | ||
@@ -40,3 +45,3 @@ if (!this.onRollbackStack) { | ||
} | ||
this.onRollbackStack.push(this.onRollback); | ||
this.onRollbackStack.push(this.onRollback || undefined); | ||
this.onRollback = undefined; | ||
@@ -46,2 +51,11 @@ } | ||
popTransactionStack() { | ||
if (this.beforeCommitStack) { | ||
this.beforeCommit = this.beforeCommitStack.pop(); | ||
if (this.beforeCommitStack.length <= 0) { | ||
this.beforeCommitStack = undefined; | ||
} | ||
} | ||
else { | ||
this.beforeCommit = undefined; | ||
} | ||
if (this.onCommitStack) { | ||
@@ -66,2 +80,20 @@ this.onCommit = this.onCommitStack.pop(); | ||
} | ||
executeBeforeNextCommit(fn) { | ||
if (!this.queryRunner.isMocked() && !this.isTransactionActive()) { | ||
throw new Error('There is no open transaction'); | ||
} | ||
if (this.onRollback === null) { | ||
throw new Error('You cannot call executeBeforeNextCommit inside an executeAfterNextRollback'); | ||
} | ||
if (this.onCommit === null) { | ||
throw new Error('You cannot call executeBeforeNextCommit inside an executeAfterNextCommit'); | ||
} | ||
if (this.beforeCommit === null) { | ||
throw new Error('You cannot call executeBeforeNextCommit inside an executeBeforeNextCommit'); | ||
} | ||
if (!this.beforeCommit) { | ||
this.beforeCommit = []; | ||
} | ||
this.beforeCommit.push(fn); | ||
} | ||
executeAfterNextCommit(fn) { | ||
@@ -71,2 +103,8 @@ if (!this.queryRunner.isMocked() && !this.isTransactionActive()) { | ||
} | ||
if (this.onRollback === null) { | ||
throw new Error('You cannot call executeAfterNextCommit inside an executeAfterNextRollback'); | ||
} | ||
if (this.onCommit === null) { | ||
throw new Error('You cannot call executeAfterNextCommit inside an executeAfterNextCommit'); | ||
} | ||
if (!this.onCommit) { | ||
@@ -81,2 +119,5 @@ this.onCommit = []; | ||
} | ||
if (this.onRollback === null) { | ||
throw new Error('You cannot call executeAfterNextRollback inside an executeAfterNextRollback'); | ||
} | ||
if (!this.onRollback) { | ||
@@ -93,5 +134,17 @@ this.onRollback = []; | ||
try { | ||
return fn(); | ||
const result = fn(); | ||
if ((0, PromiseProvider_1.isPromise)(result)) { | ||
return result.then((fnResult) => { | ||
const beforeCommit = this.beforeCommit; | ||
this.beforeCommit = null; | ||
return (0, PromiseProvider_1.callDeferredFunctionsStoppingOnError)('before next commit', beforeCommit, fnResult, source); | ||
}); | ||
} | ||
else if (this.beforeCommit) { | ||
throw new Error('before next commit not supported with high level transaction that return an array'); | ||
} | ||
return result; | ||
} | ||
catch (e) { | ||
// TODO: remove when no more array in transaction | ||
this.popTransactionStack(); | ||
@@ -102,3 +155,3 @@ throw e; | ||
const onCommit = this.onCommit; | ||
this.popTransactionStack(); | ||
this.onCommit = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next commit', onCommit, result, source); | ||
@@ -108,4 +161,6 @@ }, (e) => { | ||
const onRollback = this.onRollback; | ||
this.onRollback = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next rollback', onRollback, undefined, source, e, throwError); | ||
}).finally(() => { | ||
this.popTransactionStack(); | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next rollback', onRollback, undefined, source, e, throwError); | ||
}); | ||
@@ -133,6 +188,31 @@ } | ||
const source = new Error('Query executed at'); | ||
const beforeCommit = this.beforeCommit; | ||
if (beforeCommit) { | ||
this.beforeCommit = null; | ||
const result = (0, PromiseProvider_1.callDeferredFunctionsStoppingOnError)('before next commit', beforeCommit, undefined, source); | ||
if ((0, PromiseProvider_1.isPromise)(result)) { | ||
return result.then(() => { | ||
try { | ||
return this.queryRunner.executeCommit().then(() => { | ||
const onCommit = this.onCommit; | ||
this.onCommit = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next commit', onCommit, undefined, source); | ||
}, (e) => { | ||
// Transaction only closed when commit successful, in case of error there is still an open transaction | ||
// No rollback yet, then no executeAfterNextRollback will be executed | ||
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source); | ||
}); | ||
} | ||
catch (e) { | ||
throw new chained_error_1.default(e); | ||
} | ||
}).then(() => { | ||
this.popTransactionStack(); | ||
}); | ||
} | ||
} | ||
try { | ||
return this.queryRunner.executeCommit().then(() => { | ||
const onCommit = this.onCommit; | ||
this.popTransactionStack(); | ||
this.onCommit = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next commit', onCommit, undefined, source); | ||
@@ -143,2 +223,4 @@ }, (e) => { | ||
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source); | ||
}).then(() => { | ||
this.popTransactionStack(); | ||
}); | ||
@@ -155,3 +237,3 @@ } | ||
const onRollback = this.onRollback; | ||
this.popTransactionStack(); | ||
this.onRollback = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next rollback', onRollback, undefined, source); | ||
@@ -161,4 +243,6 @@ }, (e) => { | ||
const onRollback = this.onRollback; | ||
this.onRollback = null; | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next rollback', onRollback, undefined, source, e, throwError); | ||
}).finally(() => { | ||
this.popTransactionStack(); | ||
return (0, PromiseProvider_1.callDeferredFunctions)('after next rollback', onRollback, undefined, source, e, throwError); | ||
}); | ||
@@ -165,0 +249,0 @@ } |
{ | ||
"name": "ts-sql-query", | ||
"version": "1.56.0", | ||
"version": "1.57.0", | ||
"description": "Type-safe SQL query builder like QueryDSL or JOOQ in Java or Linq in .Net for TypeScript with MariaDB, MySql, Oracle, PostgreSql, Sqlite and SqlServer support.", | ||
@@ -60,3 +60,3 @@ "license": "MIT", | ||
"@types/any-db-transaction": "^2.2.30", | ||
"@types/better-sqlite3": "^5.4.0", | ||
"@types/better-sqlite3": "^7.6.8", | ||
"@types/geojson": "^7946.0.7", | ||
@@ -71,2 +71,3 @@ "@types/mssql": "^6.0.7", | ||
"@types/uuid": "^8.3.4", | ||
"@sqlite.org/sqlite-wasm": "^3.44.2-build1", | ||
"any-db": "^2.1.0", | ||
@@ -78,3 +79,3 @@ "any-db-mssql": "^0.1.0", | ||
"any-db-transaction": "^2.3.0", | ||
"better-sqlite3": "^7.1.1", | ||
"better-sqlite3": "^9.2.2", | ||
"binary-uuid": "^2.0.3", | ||
@@ -81,0 +82,0 @@ "loopback-connector-mssql": "^3.8.0", |
@@ -11,2 +11,3 @@ export type PromiseProvider = PromiseConstructorLike & { | ||
export declare function isPromise(value: any): value is Promise<unknown>; | ||
export declare function callDeferredFunctions<T>(name: string, fns: Array<() => void | Promise<void>> | undefined, result: T, source: Error, transactionError?: Error, throwError?: Error): T | Promise<T>; | ||
export declare function callDeferredFunctions<T>(name: string, fns: Array<() => void | Promise<void>> | null | undefined, result: T, source: Error, transactionError?: Error, throwError?: Error): T | Promise<T>; | ||
export declare function callDeferredFunctionsStoppingOnError<T>(name: string, fns: Array<() => void | Promise<void>> | null | undefined, result: T, source: Error, transactionError?: Error, throwError?: Error): T | Promise<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.callDeferredFunctions = exports.isPromise = void 0; | ||
exports.callDeferredFunctionsStoppingOnError = exports.callDeferredFunctions = exports.isPromise = void 0; | ||
const chained_error_1 = require("chained-error"); | ||
@@ -11,2 +11,10 @@ const attachSource_1 = require("./attachSource"); | ||
function callDeferredFunctions(name, fns, result, source, transactionError, throwError) { | ||
return internalCallDeferredFunctions(false, name, fns, result, source, transactionError, throwError); | ||
} | ||
exports.callDeferredFunctions = callDeferredFunctions; | ||
function callDeferredFunctionsStoppingOnError(name, fns, result, source, transactionError, throwError) { | ||
return internalCallDeferredFunctions(true, name, fns, result, source, transactionError, throwError); | ||
} | ||
exports.callDeferredFunctionsStoppingOnError = callDeferredFunctionsStoppingOnError; | ||
function internalCallDeferredFunctions(stopOnFistError, name, fns, result, source, transactionError, throwError) { | ||
if (!fns) { | ||
@@ -48,3 +56,3 @@ if (throwError) { | ||
const fn = fns[i]; | ||
promise = promise.then(callDeferredFunctionAsThen.bind(undefined, fn, errorContainer, true), callDeferredFunctionAsThen.bind(undefined, fn, errorContainer, false)); | ||
promise = promise.then(callDeferredFunctionAsThen.bind(undefined, fn, errorContainer, true), stopOnFistError ? undefined : callDeferredFunctionAsThen.bind(undefined, fn, errorContainer, false)); | ||
} | ||
@@ -76,3 +84,2 @@ } | ||
} | ||
exports.callDeferredFunctions = callDeferredFunctions; | ||
function callDeferredFunctionAsThen(fn, errorContainer, isThen, executionError) { | ||
@@ -79,0 +86,0 @@ if (!isThen && executionError) { |
Sorry, the diff of this file is too big to display
2311995
275
38597
44