Comparing version 45.2.0 to 46.0.0
@@ -21,3 +21,3 @@ import { CaseType, EventCallbacks, KmoreQueryBuilder, KmoreTransaction, KmoreTransactionConfig, QueryContext, TrxIdQueryMap } from './types.js'; | ||
*/ | ||
abstract transaction(id?: PropertyKey, config?: KmoreTransactionConfig): Promise<KmoreTransaction>; | ||
abstract transaction(config?: KmoreTransactionConfig): Promise<KmoreTransaction>; | ||
abstract getTrxByKmoreQueryId(kmoreQueryId: symbol): KmoreTransaction | undefined; | ||
@@ -24,0 +24,0 @@ abstract getTrxByKmoreTrxId(id: symbol): KmoreTransaction | undefined; |
@@ -67,3 +67,3 @@ import type { DbDict } from 'kmore-types'; | ||
*/ | ||
transaction(id?: PropertyKey, config?: KmoreTransactionConfig): Promise<KmoreTransaction>; | ||
transaction(config?: KmoreTransactionConfig): Promise<KmoreTransaction>; | ||
getTrxByKmoreQueryId(kmoreQueryId: symbol): KmoreTransaction | undefined; | ||
@@ -70,0 +70,0 @@ getTrxByKmoreTrxId(id: symbol): KmoreTransaction | undefined; |
@@ -121,5 +121,6 @@ /* eslint-disable max-lines-per-function */ | ||
*/ | ||
async transaction(id, config) { | ||
async transaction(config) { | ||
const kmoreTrxId = genKmoreTrxId(config?.kmoreTrxId); | ||
delete config?.kmoreTrxId; | ||
const trx = await this.dbh.transaction(void 0, config); | ||
const kmoreTrxId = genKmoreTrxId(id); | ||
const trxActionOnEnd = config?.trxActionOnEnd | ||
@@ -126,0 +127,0 @@ ?? this.trxActionOnEnd ?? 'rollback'; |
@@ -28,2 +28,42 @@ import assert from 'node:assert'; | ||
} | ||
/* | ||
export function extRefTableFnPropertyThen( | ||
kmore: KmoreBase, | ||
refTable: KmoreQueryBuilder, | ||
_ctx: unknown, | ||
): KmoreQueryBuilder { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const applyThenProxy = new Proxy(refTable.then, { | ||
apply: async ( | ||
target: () => Promise<unknown>, | ||
ctx2: KmoreQueryBuilder, | ||
args: unknown[], | ||
) => { | ||
try { | ||
// query response or response data | ||
// undefined means calling builder without tailing then(), | ||
const resp = await Reflect.apply(target, ctx2, args) as unknown | ||
return resp | ||
} | ||
catch (ex) { | ||
const qid = ctx2.kmoreQueryId | ||
const trx = kmore.getTrxByKmoreQueryId(qid) | ||
if (trx) { | ||
await kmore.finishTransaction(trx) | ||
} | ||
throw ex | ||
} | ||
}, | ||
}) | ||
void Object.defineProperty(refTable, 'then', { | ||
...defaultPropDescriptor, | ||
configurable: true, | ||
value: applyThenProxy, | ||
}) | ||
return refTable | ||
} | ||
*/ | ||
//# sourceMappingURL=proxy.apply.js.map |
@@ -27,3 +27,5 @@ import assert from 'assert'; | ||
assert(propKey === 'then', `propKey should be "then", but got: ${propKey.toString()}`); | ||
const getThenProxy = async (done, reject) => { | ||
const getThenProxy = async ( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
done, reject) => { | ||
try { | ||
@@ -41,4 +43,14 @@ // query response or response data | ||
if (typeof done === 'function') { | ||
const data = await done(resp); // await for try/catch | ||
return data; | ||
const resp2 = await done(resp); | ||
if (typeof resp2 === 'object' | ||
&& resp2 !== null | ||
&& typeof resp2[KmoreProxyKey.getThenProxyProcessed] === 'undefined') { | ||
Object.defineProperty(resp2, KmoreProxyKey.getThenProxyProcessed, { | ||
...defaultPropDescriptor, | ||
enumerable: false, | ||
writable: true, | ||
value: true, | ||
}); | ||
} | ||
return resp2; | ||
} | ||
@@ -51,3 +63,3 @@ return resp; | ||
const trx = kmore.getTrxByKmoreQueryId(qid); | ||
if (trx) { | ||
if (trx) { // also processed on event `query-error` | ||
await kmore.finishTransaction(trx); | ||
@@ -64,2 +76,3 @@ } | ||
throw new Error(ex); | ||
// return Promise.reject(new Error(ex)) | ||
} | ||
@@ -79,36 +92,2 @@ else { | ||
} | ||
/* | ||
protected extRefTableFnPropertyThen(refTable: KmoreQueryBuilder): KmoreQueryBuilder { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const applyThenProxy = new Proxy(refTable.then, { | ||
apply: async ( | ||
target: () => Promise<unknown>, | ||
ctx2: KmoreQueryBuilder, | ||
args: unknown[], | ||
) => { | ||
try { | ||
// query response or response data | ||
// undefined means calling builder without tailing then(), | ||
const resp = await Reflect.apply(target, ctx2, args) as unknown | ||
return resp | ||
} | ||
catch (ex) { | ||
const qid = ctx2.kmoreQueryId | ||
const trx = this.getTrxByKmoreQueryId(qid) | ||
if (trx) { | ||
await this.finishTransaction(trx) | ||
} | ||
throw ex | ||
} | ||
}, | ||
}) | ||
void Object.defineProperty(refTable, 'then', { | ||
...defaultPropDescriptor, | ||
configurable: true, | ||
value: applyThenProxy, | ||
}) | ||
return refTable | ||
} */ | ||
//# sourceMappingURL=proxy.get.js.map |
import type { KmoreBase } from './base.js'; | ||
import { KmoreTransaction, KmoreTransactionConfig } from './types.js'; | ||
import type { KmoreTransaction, KmoreTransactionConfig } from './types.js'; | ||
export declare function trxApplyCommandProxy(kmore: KmoreBase, trx: KmoreTransaction): KmoreTransaction; | ||
@@ -4,0 +4,0 @@ interface CreateTrxPropertiesOptions { |
@@ -11,2 +11,5 @@ import assert from 'node:assert'; | ||
kmore.trxMap.delete(ctx.kmoreTrxId); | ||
if (ctx.isCompleted()) { | ||
return; | ||
} | ||
return Reflect.apply(target, ctx, args); | ||
@@ -25,2 +28,5 @@ }, | ||
kmore.trxMap.delete(ctx.kmoreTrxId); | ||
if (ctx.isCompleted()) { | ||
return; | ||
} | ||
return Reflect.apply(target, ctx, args); | ||
@@ -34,2 +40,25 @@ }, | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const savepoint = new Proxy(trx.savepoint, { | ||
apply: async (_target, ctx, args) => { | ||
if (args && typeof args[0] === 'function') { | ||
const msg = `trx.savepoint(arg) arg not support function, | ||
args should be [name?: PropertyKey, config?: KmoreTransactionConfig]`; | ||
throw new Error(msg); | ||
} | ||
const arg0 = args?.[0]; | ||
const trx2 = await ctx.transaction(arg0); | ||
const trx3 = savePointTrx({ | ||
kmore, | ||
trx: trx2, | ||
parentTrx: ctx, | ||
}); | ||
return trx3; | ||
}, | ||
}); | ||
Object.defineProperty(trx, 'savepoint', { | ||
...defaultPropDescriptor, | ||
writable: true, | ||
value: savepoint, | ||
}); | ||
kmore.trxMap.set(trx.kmoreTrxId, trx); | ||
@@ -83,2 +112,19 @@ kmore.trxIdQueryMap.set(trx.kmoreTrxId, new Set()); | ||
} | ||
function savePointTrx(options) { | ||
const { kmore, parentTrx, trx } = options; | ||
const { trxActionOnEnd } = parentTrx; | ||
assert(parentTrx.isTransaction === true, 'parent trx not a transaction when creating savepoint'); | ||
const kmoreTrxId = genKmoreTrxId(parentTrx.kmoreTrxId); | ||
assert(kmoreTrxId, 'kmoreTrxId must be provided from parent trx when creating savepoint'); | ||
assert(trx.isTransaction === true, 'output trx not a transaction when creating savepoint'); | ||
assert(!trx.isCompleted(), 'output trx already completed when creating savepoint'); | ||
const opts = { | ||
kmore, | ||
kmoreTrxId, | ||
trx, | ||
trxActionOnEnd, | ||
}; | ||
const trxNew = createTrxProperties(opts); | ||
return trxNew; | ||
} | ||
//# sourceMappingURL=proxy.trx.js.map |
@@ -9,15 +9,22 @@ import type { TraceContext, Span } from '@mwcp/otel'; | ||
dbId: string; | ||
hrtime: bigint; | ||
kmoreTrxId: symbol; | ||
/** | ||
* Auto transction action (rollback|commit|none) on error (Rejection or Exception), | ||
* | ||
* @default rollback | ||
* @note Error from ONLY builder or fisrt builder.then() can be catched ! | ||
* @CAUTION **Will always rollback if query error inner database even though this value set to 'commit'** | ||
* @default rollback | ||
*/ | ||
trxActionOnEnd: NonNullable<KmoreTransactionConfig['trxActionOnEnd']>; | ||
savepoint: (id?: PropertyKey, config?: KmoreTransactionConfig) => Promise<KmoreTransaction>; | ||
}; | ||
export declare type KmoreTransactionConfig = Knex.TransactionConfig & { | ||
kmoreTrxId?: PropertyKey; | ||
/** | ||
* Atuo trsaction action (rollback|commit|none) on error (Rejection or Exception), | ||
* Auto transction action (rollback|commit|none) on error (Rejection or Exception), | ||
* | ||
* @default rollback | ||
* @note Error from ONLY builder or fisrt builder.then() can be catched ! | ||
* @CAUTION **Will always rollback if query error inner database even though this value set to 'commit'** | ||
* @default rollback | ||
*/ | ||
@@ -219,2 +226,3 @@ trxActionOnEnd?: 'commit' | 'rollback' | 'none'; | ||
export declare type TrxIdQueryMap = Map<symbol, Set<symbol>>; | ||
export declare type TrxSavePointCallback = (trx: KmoreTransaction) => Promise<unknown>; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -42,7 +42,7 @@ import assert from 'assert'; | ||
let refTable = kmore.dbh(refName); | ||
// refTable = extRefTableFnPropertyThen(kmore, refTable, ctx) // must before getProxy | ||
refTable = createQueryBuilderGetProxy(kmore, refTable); | ||
refTable = builderBindEvents(kmore, refTable, caseConvert, ctx, kmoreQueryId); | ||
refTable = builderApplyTransactingProxy(kmore, refTable, ctx); | ||
refTable = extRefTableFnPropertySmartJoin(refTable); | ||
// refTable = kmore.extRefTableFnPropertyThen(refTable) | ||
refTable = builderBindEvents(kmore, refTable, caseConvert, ctx, kmoreQueryId); | ||
void Object.defineProperty(refTable, 'kmoreQueryId', { | ||
@@ -49,0 +49,0 @@ ...defaultPropDescriptor, |
{ | ||
"name": "kmore", | ||
"author": "waiting", | ||
"version": "45.2.0", | ||
"version": "46.0.0", | ||
"description": "A SQL query builder based on knex with powerful TypeScript type support", | ||
@@ -46,4 +46,4 @@ "keywords": [ | ||
"cross-env": "7", | ||
"kmore-cli": "^45.0.0", | ||
"kmore-types": "^45.0.0", | ||
"kmore-cli": "^46.0.0", | ||
"kmore-types": "^46.0.0", | ||
"knex": "^2.3.0", | ||
@@ -82,3 +82,3 @@ "pg": "^8.7.3" | ||
}, | ||
"gitHead": "1a117ae7e6c50f5194e7efe50fd530e3051622b4" | ||
"gitHead": "539f51f9d0ee1d2883558ecf6ab0c47b13c2ad3a" | ||
} |
@@ -37,6 +37,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
*/ | ||
abstract transaction( | ||
id?: PropertyKey, | ||
config?: KmoreTransactionConfig, | ||
): Promise<KmoreTransaction> | ||
abstract transaction(config?: KmoreTransactionConfig): Promise<KmoreTransaction> | ||
@@ -43,0 +40,0 @@ abstract getTrxByKmoreQueryId(kmoreQueryId: symbol): KmoreTransaction | undefined |
@@ -160,10 +160,7 @@ /* eslint-disable max-lines-per-function */ | ||
*/ | ||
async transaction( | ||
id?: PropertyKey, | ||
config?: KmoreTransactionConfig, | ||
): Promise<KmoreTransaction> { | ||
async transaction(config?: KmoreTransactionConfig): Promise<KmoreTransaction> { | ||
const kmoreTrxId = genKmoreTrxId(config?.kmoreTrxId) | ||
delete config?.kmoreTrxId | ||
const trx = await this.dbh.transaction(void 0, config) as KmoreTransaction | ||
const kmoreTrxId = genKmoreTrxId(id) | ||
const trxActionOnEnd: KmoreTransactionConfig['trxActionOnEnd'] = config?.trxActionOnEnd | ||
@@ -170,0 +167,0 @@ ?? this.trxActionOnEnd ?? 'rollback' |
@@ -49,1 +49,43 @@ import assert from 'node:assert' | ||
} | ||
/* | ||
export function extRefTableFnPropertyThen( | ||
kmore: KmoreBase, | ||
refTable: KmoreQueryBuilder, | ||
_ctx: unknown, | ||
): KmoreQueryBuilder { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const applyThenProxy = new Proxy(refTable.then, { | ||
apply: async ( | ||
target: () => Promise<unknown>, | ||
ctx2: KmoreQueryBuilder, | ||
args: unknown[], | ||
) => { | ||
try { | ||
// query response or response data | ||
// undefined means calling builder without tailing then(), | ||
const resp = await Reflect.apply(target, ctx2, args) as unknown | ||
return resp | ||
} | ||
catch (ex) { | ||
const qid = ctx2.kmoreQueryId | ||
const trx = kmore.getTrxByKmoreQueryId(qid) | ||
if (trx) { | ||
await kmore.finishTransaction(trx) | ||
} | ||
throw ex | ||
} | ||
}, | ||
}) | ||
void Object.defineProperty(refTable, 'then', { | ||
...defaultPropDescriptor, | ||
configurable: true, | ||
value: applyThenProxy, | ||
}) | ||
return refTable | ||
} | ||
*/ | ||
@@ -41,4 +41,5 @@ import assert from 'assert' | ||
const getThenProxy = async ( | ||
done?: PromiseLike<unknown> | unknown, | ||
reject?: PromiseLike<unknown> | undefined, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
done?: PromiseLike<unknown> | ((data: unknown) => any), | ||
reject?: PromiseLike<unknown> | ((error: Error) => unknown), | ||
) => { | ||
@@ -60,4 +61,15 @@ | ||
if (typeof done === 'function') { | ||
const data = await done(resp) // await for try/catch | ||
return data | ||
const resp2 = await done(resp) | ||
if (typeof resp2 === 'object' | ||
&& resp2 !== null | ||
&& typeof resp2[KmoreProxyKey.getThenProxyProcessed] === 'undefined' | ||
) { | ||
Object.defineProperty(resp2, KmoreProxyKey.getThenProxyProcessed, { | ||
...defaultPropDescriptor, | ||
enumerable: false, | ||
writable: true, | ||
value: true, | ||
}) | ||
} | ||
return resp2 | ||
} | ||
@@ -70,3 +82,3 @@ return resp | ||
const trx = kmore.getTrxByKmoreQueryId(qid) | ||
if (trx) { | ||
if (trx) { // also processed on event `query-error` | ||
await kmore.finishTransaction(trx) | ||
@@ -85,2 +97,3 @@ } | ||
throw new Error(ex) | ||
// return Promise.reject(new Error(ex)) | ||
} | ||
@@ -102,36 +115,1 @@ else { | ||
/* | ||
protected extRefTableFnPropertyThen(refTable: KmoreQueryBuilder): KmoreQueryBuilder { | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const applyThenProxy = new Proxy(refTable.then, { | ||
apply: async ( | ||
target: () => Promise<unknown>, | ||
ctx2: KmoreQueryBuilder, | ||
args: unknown[], | ||
) => { | ||
try { | ||
// query response or response data | ||
// undefined means calling builder without tailing then(), | ||
const resp = await Reflect.apply(target, ctx2, args) as unknown | ||
return resp | ||
} | ||
catch (ex) { | ||
const qid = ctx2.kmoreQueryId | ||
const trx = this.getTrxByKmoreQueryId(qid) | ||
if (trx) { | ||
await this.finishTransaction(trx) | ||
} | ||
throw ex | ||
} | ||
}, | ||
}) | ||
void Object.defineProperty(refTable, 'then', { | ||
...defaultPropDescriptor, | ||
configurable: true, | ||
value: applyThenProxy, | ||
}) | ||
return refTable | ||
} */ |
@@ -6,3 +6,3 @@ import assert from 'node:assert' | ||
import { defaultPropDescriptor } from './config.js' | ||
import { KmoreTransaction, KmoreTransactionConfig } from './types.js' | ||
import type { KmoreTransaction, KmoreTransactionConfig } from './types.js' | ||
@@ -22,2 +22,3 @@ | ||
kmore.trxMap.delete(ctx.kmoreTrxId) | ||
if (ctx.isCompleted()) { return } | ||
return Reflect.apply(target, ctx, args) | ||
@@ -37,2 +38,3 @@ }, | ||
kmore.trxMap.delete(ctx.kmoreTrxId) | ||
if (ctx.isCompleted()) { return } | ||
return Reflect.apply(target, ctx, args) | ||
@@ -47,3 +49,27 @@ }, | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
const savepoint = new Proxy(trx.savepoint, { | ||
apply: async (_target: typeof trx.savepoint, ctx: KmoreTransaction, args?: unknown[]) => { | ||
if (args && typeof args[0] === 'function') { | ||
const msg = `trx.savepoint(arg) arg not support function, | ||
args should be [name?: PropertyKey, config?: KmoreTransactionConfig]` | ||
throw new Error(msg) | ||
} | ||
const arg0 = args?.[0] as KmoreTransactionConfig | undefined | ||
const trx2 = await ctx.transaction(arg0) as KmoreTransaction | ||
const trx3 = savePointTrx({ | ||
kmore, | ||
trx: trx2, | ||
parentTrx: ctx, | ||
}) | ||
return trx3 | ||
}, | ||
}) | ||
Object.defineProperty(trx, 'savepoint', { | ||
...defaultPropDescriptor, | ||
writable: true, | ||
value: savepoint, | ||
}) | ||
kmore.trxMap.set(trx.kmoreTrxId, trx) | ||
@@ -117,1 +143,28 @@ kmore.trxIdQueryMap.set(trx.kmoreTrxId, new Set()) | ||
interface SavePointTrxOptions { | ||
kmore: KmoreBase | ||
parentTrx: KmoreTransaction | ||
trx: KmoreTransaction | ||
} | ||
function savePointTrx(options: SavePointTrxOptions): KmoreTransaction { | ||
const { kmore, parentTrx, trx } = options | ||
const { trxActionOnEnd } = parentTrx | ||
assert(parentTrx.isTransaction === true, 'parent trx not a transaction when creating savepoint') | ||
const kmoreTrxId = genKmoreTrxId(parentTrx.kmoreTrxId) | ||
assert(kmoreTrxId, 'kmoreTrxId must be provided from parent trx when creating savepoint') | ||
assert(trx.isTransaction === true, 'output trx not a transaction when creating savepoint') | ||
assert(! trx.isCompleted(), 'output trx already completed when creating savepoint') | ||
const opts: CreateTrxPropertiesOptions = { | ||
kmore, | ||
kmoreTrxId, | ||
trx, | ||
trxActionOnEnd, | ||
} | ||
const trxNew = createTrxProperties(opts) | ||
return trxNew | ||
} | ||
@@ -23,15 +23,26 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
dbId: string, | ||
hrtime: bigint, | ||
kmoreTrxId: symbol, | ||
/** | ||
* Auto transction action (rollback|commit|none) on error (Rejection or Exception), | ||
* | ||
* @default rollback | ||
* @note Error from ONLY builder or fisrt builder.then() can be catched ! | ||
* @CAUTION **Will always rollback if query error inner database even though this value set to 'commit'** | ||
* @default rollback | ||
*/ | ||
trxActionOnEnd: NonNullable<KmoreTransactionConfig['trxActionOnEnd']>, | ||
savepoint: ( | ||
id?: PropertyKey, | ||
config?: KmoreTransactionConfig, | ||
) => Promise<KmoreTransaction>, | ||
} | ||
export type KmoreTransactionConfig = Knex.TransactionConfig & { | ||
kmoreTrxId?: PropertyKey, | ||
/** | ||
* Atuo trsaction action (rollback|commit|none) on error (Rejection or Exception), | ||
* Auto transction action (rollback|commit|none) on error (Rejection or Exception), | ||
* | ||
* @default rollback | ||
* @note Error from ONLY builder or fisrt builder.then() can be catched ! | ||
* @CAUTION **Will always rollback if query error inner database even though this value set to 'commit'** | ||
* @default rollback | ||
*/ | ||
@@ -294,1 +305,4 @@ trxActionOnEnd?: 'commit' | 'rollback' | 'none', | ||
export type TrxIdQueryMap = Map<symbol, Set<symbol>> | ||
export type TrxSavePointCallback = (trx: KmoreTransaction) => Promise<unknown> | ||
@@ -75,3 +75,8 @@ import assert from 'assert' | ||
// refTable = extRefTableFnPropertyThen(kmore, refTable, ctx) // must before getProxy | ||
refTable = createQueryBuilderGetProxy(kmore, refTable) | ||
refTable = builderApplyTransactingProxy(kmore, refTable, ctx) | ||
refTable = extRefTableFnPropertySmartJoin(refTable) | ||
refTable = builderBindEvents( | ||
@@ -84,5 +89,2 @@ kmore, | ||
) | ||
refTable = builderApplyTransactingProxy(kmore, refTable, ctx) | ||
refTable = extRefTableFnPropertySmartJoin(refTable) | ||
// refTable = kmore.extRefTableFnPropertyThen(refTable) | ||
@@ -89,0 +91,0 @@ void Object.defineProperty(refTable, 'kmoreQueryId', { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
243711
4121