Comparing version 32.0.1 to 33.0.0
@@ -1,2 +0,1 @@ | ||
import { postProcessResponse, wrapIdentifier } from './helper.js'; | ||
export const defaultPropDescriptor = { | ||
@@ -27,5 +26,3 @@ configurable: false, | ||
pool: { min: 0, max: 30 }, | ||
postProcessResponse, | ||
wrapIdentifier, | ||
}; | ||
//# sourceMappingURL=config.js.map |
@@ -5,3 +5,3 @@ import { RecordCamelKeys, RecordPascalKeys, RecordSnakeKeys } from '@waiting/shared-types'; | ||
export declare function getCurrentTime(dbh: Knex, clientType: KnexConfig['client']): Promise<string>; | ||
export declare function postProcessResponse<T extends PostProcessInput = PostProcessInput>(result: T, queryContext?: QueryContext): T | PostProcessRespRet<T, QueryContext['caseConvert']>; | ||
export declare function postProcessResponse<T extends PostProcessInput = PostProcessInput>(result: T, queryContext?: QueryContext): T | PostProcessRespRet<T, QueryContext['postProcessResponseCaseConvert']>; | ||
export declare type PostProcessPlain = number | string | undefined | null | boolean; | ||
@@ -8,0 +8,0 @@ export declare type PostProcessRecord = Record<string, PostProcessPlain> | object; |
@@ -1,2 +0,2 @@ | ||
import { camelToSnake, camelKeys, snakeKeys, } from '@waiting/shared-core'; | ||
import { camelToSnake, camelKeys, snakeKeys, snakeToCamel, } from '@waiting/shared-core'; | ||
import { CaseType, EnumClient } from './types.js'; | ||
@@ -35,3 +35,3 @@ export async function getCurrentTime(dbh, clientType) { | ||
} | ||
const { caseConvert } = queryContext; | ||
const caseConvert = queryContext.postProcessResponseCaseConvert; | ||
if (!caseConvert) { | ||
@@ -112,9 +112,22 @@ return result; | ||
export function wrapIdentifier(value, origImpl, queryContext) { | ||
if (!queryContext | ||
|| !queryContext.caseConvert | ||
|| queryContext.caseConvert === CaseType.none) { | ||
if (!queryContext) { | ||
return origImpl(value); | ||
} | ||
const ret = origImpl(camelToSnake(value)); | ||
return ret; | ||
switch (queryContext.wrapIdentifierCaseConvert) { | ||
case CaseType.camel: { | ||
const ret = origImpl(snakeToCamel(value)); | ||
return ret; | ||
break; | ||
} | ||
case CaseType.snake: { | ||
const ret = origImpl(camelToSnake(value)); | ||
return ret; | ||
break; | ||
} | ||
case CaseType.pascal: { | ||
throw new TypeError('CaseType.pascal for wrapIdentifierCaseConvert not implemented yet'); | ||
} | ||
default: | ||
return origImpl(value); | ||
} | ||
} | ||
@@ -121,0 +134,0 @@ /** |
@@ -1,5 +0,5 @@ | ||
export * from './config.js'; | ||
export * from './helper.js'; | ||
export * from './kmore.js'; | ||
export * from './types.js'; | ||
export { initKmoreEvent } from './config.js'; | ||
export { getCurrentTime, mergeDoWithInitData, } from './helper.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,5 +0,5 @@ | ||
export * from './config.js'; | ||
export * from './helper.js'; | ||
export * from './kmore.js'; | ||
export * from './types.js'; | ||
export { initKmoreEvent } from './config.js'; | ||
export { getCurrentTime, mergeDoWithInitData, } from './helper.js'; | ||
//# sourceMappingURL=index.js.map |
import type { DbDict } from 'kmore-types'; | ||
import type { Knex } from 'knex'; | ||
import { CaseType, DbQueryBuilder, EventCallbacks, KnexConfig, QuerySpanInfo } from './types.js'; | ||
import { postProcessResponse } from './helper.js'; | ||
import { CaseType, DbQueryBuilder, EventCallbacks, KnexConfig, QueryContext, QuerySpanInfo } from './types.js'; | ||
export declare class Kmore<D = any, Context = any> { | ||
/** | ||
* Original table names, without case convertion. | ||
*/ | ||
readonly refTables: DbQueryBuilder<Context, D, 'ref_', CaseType.none>; | ||
/** | ||
* Create a table reference function property with camel case convertion. | ||
*/ | ||
readonly camelTables: DbQueryBuilder<Context, D, 'ref_', CaseType.camel>; | ||
/** | ||
* Create a table reference function property with snake case convertion. | ||
*/ | ||
readonly snakeTables: DbQueryBuilder<Context, D, 'ref_', CaseType.snake>; | ||
@@ -31,2 +41,3 @@ /** | ||
readonly queryUidSpanMap: Map<string, QuerySpanInfo>; | ||
readonly postProcessResponseSet: Set<typeof postProcessResponse>; | ||
readonly config: KnexConfig; | ||
@@ -38,5 +49,7 @@ readonly dict: DbDict<D>; | ||
readonly eventCallbacks: EventCallbacks<Context> | undefined; | ||
readonly wrapIdentifierCaseConvert: CaseType; | ||
constructor(options: KmoreFactoryOpts<D, Context>); | ||
protected createRefTables<P extends string>(prefix: P, caseConvert: CaseType): DbQueryBuilder<Context, D, P, CaseType>; | ||
protected extRefTableFnProperty(refName: string, caseConvert: CaseType, ctx: Context | undefined): Knex.QueryBuilder; | ||
protected postProcessResponse(result: any, queryContext?: QueryContext): unknown; | ||
} | ||
@@ -55,2 +68,9 @@ export interface KmoreFactoryOpts<D, Ctx = unknown> { | ||
eventCallbacks?: EventCallbacks<Ctx> | undefined; | ||
/** | ||
* Table identifier case convertion, | ||
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']` | ||
* @default CaseType.snake | ||
* @docs https://knexjs.org/guide/#wrapidentifier | ||
*/ | ||
wrapIdentifierCaseConvert?: CaseType | undefined; | ||
} | ||
@@ -57,0 +77,0 @@ export declare function KmoreFactory<D, Ctx = unknown>(options: KmoreFactoryOpts<D, Ctx>): Kmore<D, Ctx>; |
@@ -6,9 +6,19 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { default as _knex } from 'knex'; | ||
import { defaultPropDescriptor } from './config.js'; | ||
import { defaultPropDescriptor, initialConfig } from './config.js'; | ||
import { callCbOnQuery, callCbOnQueryError, callCbOnQueryResp, callCbOnStart } from './event.js'; | ||
import { postProcessResponse, wrapIdentifier } from './helper.js'; | ||
import { CaseType, } from './types.js'; | ||
export class Kmore { | ||
/** | ||
* Original table names, without case convertion. | ||
*/ | ||
refTables; | ||
/** | ||
* Create a table reference function property with camel case convertion. | ||
*/ | ||
camelTables; | ||
// readonly pascalTables: DbQueryBuilder<D, 'ref_', CaseType.pascal> | ||
/** | ||
* Create a table reference function property with snake case convertion. | ||
*/ | ||
snakeTables; | ||
@@ -38,2 +48,3 @@ /** | ||
queryUidSpanMap = new Map(); | ||
postProcessResponseSet = new Set(); | ||
config; | ||
@@ -45,11 +56,31 @@ dict; | ||
eventCallbacks; | ||
wrapIdentifierCaseConvert; | ||
constructor(options) { | ||
const dbId = options.dbId ? options.dbId : Date.now().toString(); | ||
this.dbId = dbId; | ||
this.dbh = options.dbh ? options.dbh : createDbh(options.config); | ||
this.instanceId = options.instanceId ? options.instanceId : Symbol(`${dbId}-` + Date.now().toString()); | ||
this.eventCallbacks = options.eventCallbacks; | ||
this.config = options.config; | ||
const config = { | ||
...initialConfig, | ||
...options.config, | ||
}; | ||
this.config = config; | ||
assert(options.dict, 'options.dict must be defined'); | ||
this.dict = options.dict; | ||
/** | ||
* Table identifier case convertion, | ||
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']` | ||
*/ | ||
this.wrapIdentifierCaseConvert = options.wrapIdentifierCaseConvert ?? CaseType.snake; | ||
if (this.wrapIdentifierCaseConvert !== CaseType.none | ||
&& this.config.wrapIdentifier !== wrapIdentifier) { | ||
this.config.wrapIdentifier = wrapIdentifier; | ||
} | ||
this.postProcessResponseSet.add(postProcessResponse); | ||
if (typeof this.config.postProcessResponse === 'function') { | ||
const fn = this.config.postProcessResponse; | ||
this.postProcessResponseSet.add(fn); | ||
} | ||
delete this.config.postProcessResponse; | ||
this.config.postProcessResponse = (result, queryContext) => this.postProcessResponse(result, queryContext); | ||
this.refTables = this.createRefTables('ref_', CaseType.none); | ||
@@ -59,2 +90,3 @@ this.camelTables = this.createRefTables('ref_', CaseType.camel); | ||
this.snakeTables = this.createRefTables('ref_', CaseType.snake); | ||
this.dbh = options.dbh ? options.dbh : createDbh(this.config); | ||
} | ||
@@ -86,3 +118,4 @@ createRefTables(prefix, caseConvert) { | ||
const opts = { | ||
caseConvert, | ||
wrapIdentifierCaseConvert: this.wrapIdentifierCaseConvert, | ||
postProcessResponseCaseConvert: caseConvert, | ||
}; | ||
@@ -97,2 +130,9 @@ const refTable = this.dbh(refName) | ||
} | ||
postProcessResponse(result, queryContext) { | ||
let ret = result; | ||
for (const fn of this.postProcessResponseSet) { | ||
ret = fn(ret, queryContext); | ||
} | ||
return ret; | ||
} | ||
} | ||
@@ -99,0 +139,0 @@ export function KmoreFactory(options) { |
@@ -56,3 +56,4 @@ import { RecordCamelKeys, RecordPascalKeys, RecordSnakeKeys } from '@waiting/shared-types'; | ||
export interface QueryContext { | ||
caseConvert?: CaseType; | ||
wrapIdentifierCaseConvert: CaseType; | ||
postProcessResponseCaseConvert: CaseType; | ||
} | ||
@@ -59,0 +60,0 @@ export interface OnQueryData { |
{ | ||
"name": "kmore", | ||
"author": "waiting", | ||
"version": "32.0.1", | ||
"version": "33.0.0", | ||
"description": "A SQL query builder based on knex with powerful TypeScript type support", | ||
@@ -46,3 +46,3 @@ "keywords": [ | ||
"cross-env": "7", | ||
"kmore-types": "^32.0.0", | ||
"kmore-types": "^33.0.0", | ||
"knex": "^2.1.0", | ||
@@ -79,3 +79,3 @@ "pg": "^8.7.3", | ||
}, | ||
"gitHead": "8f2057568f401189d75eb1968a4e7727f9c456c0" | ||
"gitHead": "79480dcf8109f25ec1530694dc046c6ad5fd6e86" | ||
} |
@@ -175,3 +175,3 @@ # [kmore](https://waitingsong.github.io/kmore/) | ||
const ret = await refTables.ref_tb_user() | ||
const ret = await camelTables.ref_tb_user() | ||
.innerJoin<CT_USER & CT_USER_EXT>( | ||
@@ -191,3 +191,3 @@ tables.tb_user_ext, | ||
const ret = await refTables.ref_tb_user() | ||
const ret = await camelTables.ref_tb_user() | ||
.innerJoin<CT_USER & CT_USER_EXT>( | ||
@@ -194,0 +194,0 @@ tables.tb_user_ext, |
@@ -23,3 +23,3 @@ # [kmore](https://waitingsong.github.io/kmore/) | ||
```sh | ||
npm install kmore kmore-cli knex | ||
npm install kmore kmore-cli | ||
@@ -175,3 +175,3 @@ # Then add one of the following: | ||
const ret = await refTables.ref_tb_user() | ||
const ret = await camelTables.ref_tb_user() | ||
.innerJoin<CT_USER & CT_USER_EXT>( | ||
@@ -191,3 +191,3 @@ tables.tb_user_ext, | ||
const ret = await refTables.ref_tb_user() | ||
const ret = await camelTables.ref_tb_user() | ||
.innerJoin<CT_USER & CT_USER_EXT>( | ||
@@ -194,0 +194,0 @@ tables.tb_user_ext, |
@@ -1,2 +0,1 @@ | ||
import { postProcessResponse, wrapIdentifier } from './helper.js' | ||
import type { KmoreEvent, KnexConfig } from './types.js' | ||
@@ -33,5 +32,3 @@ | ||
pool: { min: 0, max: 30 }, | ||
postProcessResponse, | ||
wrapIdentifier, | ||
} | ||
@@ -5,2 +5,3 @@ import { | ||
snakeKeys, | ||
snakeToCamel, | ||
} from '@waiting/shared-core' | ||
@@ -65,12 +66,8 @@ import { RecordCamelKeys, RecordPascalKeys, RecordSnakeKeys } from '@waiting/shared-types' | ||
queryContext?: QueryContext, | ||
): T | PostProcessRespRet<T, QueryContext['caseConvert']> { | ||
): T | PostProcessRespRet<T, QueryContext['postProcessResponseCaseConvert']> { | ||
if (! queryContext) { | ||
return result | ||
} | ||
if (! queryContext) { return result } | ||
const { caseConvert } = queryContext | ||
if (! caseConvert) { | ||
return result | ||
} | ||
const caseConvert = queryContext.postProcessResponseCaseConvert | ||
if (! caseConvert) { return result } | ||
@@ -199,10 +196,24 @@ switch (caseConvert) { | ||
if (! queryContext | ||
|| ! queryContext.caseConvert | ||
|| queryContext.caseConvert === CaseType.none) { | ||
return origImpl(value) | ||
if (! queryContext) { return origImpl(value) } | ||
switch (queryContext.wrapIdentifierCaseConvert) { | ||
case CaseType.camel: { | ||
const ret = origImpl(snakeToCamel(value)) | ||
return ret | ||
break | ||
} | ||
case CaseType.snake: { | ||
const ret = origImpl(camelToSnake(value)) | ||
return ret | ||
break | ||
} | ||
case CaseType.pascal: { | ||
throw new TypeError('CaseType.pascal for wrapIdentifierCaseConvert not implemented yet') | ||
} | ||
default: | ||
return origImpl(value) | ||
} | ||
const ret = origImpl(camelToSnake(value)) | ||
return ret | ||
} | ||
@@ -209,0 +220,0 @@ |
export * from './config.js' | ||
export * from './helper.js' | ||
export * from './kmore.js' | ||
export * from './types.js' | ||
export { initKmoreEvent } from './config.js' | ||
export { | ||
getCurrentTime, | ||
mergeDoWithInitData, | ||
} from './helper.js' | ||
@@ -10,4 +10,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { defaultPropDescriptor } from './config.js' | ||
import { defaultPropDescriptor, initialConfig } from './config.js' | ||
import { callCbOnQuery, callCbOnQueryError, callCbOnQueryResp, callCbOnStart } from './event.js' | ||
import { PostProcessInput, postProcessResponse, wrapIdentifier } from './helper.js' | ||
import { | ||
@@ -30,5 +31,17 @@ CaseType, | ||
/** | ||
* Original table names, without case convertion. | ||
*/ | ||
readonly refTables: DbQueryBuilder<Context, D, 'ref_', CaseType.none> | ||
/** | ||
* Create a table reference function property with camel case convertion. | ||
*/ | ||
readonly camelTables: DbQueryBuilder<Context, D, 'ref_', CaseType.camel> | ||
// readonly pascalTables: DbQueryBuilder<D, 'ref_', CaseType.pascal> | ||
/** | ||
* Create a table reference function property with snake case convertion. | ||
*/ | ||
readonly snakeTables: DbQueryBuilder<Context, D, 'ref_', CaseType.snake> | ||
@@ -61,2 +74,3 @@ | ||
readonly queryUidSpanMap = new Map<string, QuerySpanInfo>() | ||
readonly postProcessResponseSet = new Set<typeof postProcessResponse>() | ||
@@ -69,2 +83,3 @@ public readonly config: KnexConfig | ||
public readonly eventCallbacks: EventCallbacks<Context> | undefined | ||
public readonly wrapIdentifierCaseConvert: CaseType | ||
@@ -78,10 +93,36 @@ | ||
this.dbId = dbId | ||
this.dbh = options.dbh ? options.dbh : createDbh(options.config) | ||
this.instanceId = options.instanceId ? options.instanceId : Symbol(`${dbId}-` + Date.now().toString()) | ||
this.eventCallbacks = options.eventCallbacks | ||
this.config = options.config | ||
const config = { | ||
...initialConfig, | ||
...options.config, | ||
} | ||
this.config = config | ||
assert(options.dict, 'options.dict must be defined') | ||
this.dict = options.dict | ||
/** | ||
* Table identifier case convertion, | ||
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']` | ||
*/ | ||
this.wrapIdentifierCaseConvert = options.wrapIdentifierCaseConvert ?? CaseType.snake | ||
if (this.wrapIdentifierCaseConvert !== CaseType.none | ||
&& this.config.wrapIdentifier !== wrapIdentifier) { | ||
this.config.wrapIdentifier = wrapIdentifier | ||
} | ||
this.postProcessResponseSet.add(postProcessResponse) | ||
if (typeof this.config.postProcessResponse === 'function') { | ||
const fn = this.config.postProcessResponse | ||
this.postProcessResponseSet.add(fn) | ||
} | ||
delete this.config.postProcessResponse | ||
this.config.postProcessResponse = ( | ||
result: PostProcessInput, | ||
queryContext?: QueryContext, | ||
) => this.postProcessResponse(result, queryContext) | ||
this.refTables = this.createRefTables<'ref_'>('ref_', CaseType.none) | ||
@@ -91,2 +132,5 @@ this.camelTables = this.createRefTables<'ref_'>('ref_', CaseType.camel) | ||
this.snakeTables = this.createRefTables<'ref_'>('ref_', CaseType.snake) | ||
this.dbh = options.dbh ? options.dbh : createDbh(this.config) | ||
} | ||
@@ -136,3 +180,4 @@ | ||
const opts: QueryContext = { | ||
caseConvert, | ||
wrapIdentifierCaseConvert: this.wrapIdentifierCaseConvert, | ||
postProcessResponseCaseConvert: caseConvert, | ||
} | ||
@@ -180,2 +225,13 @@ const refTable = this.dbh(refName) | ||
protected postProcessResponse( | ||
result: any, | ||
queryContext?: QueryContext, | ||
): unknown { | ||
let ret = result | ||
for (const fn of this.postProcessResponseSet) { | ||
ret = fn(ret, queryContext) | ||
} | ||
return ret | ||
} | ||
} | ||
@@ -195,2 +251,9 @@ | ||
eventCallbacks?: EventCallbacks<Ctx> | undefined | ||
/** | ||
* Table identifier case convertion, | ||
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']` | ||
* @default CaseType.snake | ||
* @docs https://knexjs.org/guide/#wrapidentifier | ||
*/ | ||
wrapIdentifierCaseConvert?: CaseType | undefined | ||
} | ||
@@ -197,0 +260,0 @@ |
@@ -99,3 +99,4 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export interface QueryContext { | ||
caseConvert?: CaseType | ||
wrapIdentifierCaseConvert: CaseType | ||
postProcessResponseCaseConvert: CaseType | ||
} | ||
@@ -102,0 +103,0 @@ |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127014
2031