Skip to content

Commit

Permalink
feat(kmove): add wrapIdentifierIgnoreRule of config
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Feb 22, 2024
1 parent 780007b commit 8abf183
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/kmore/src/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
KmoreTransactionConfig,
QueryContext,
TrxIdQueryMap,
WrapIdentifierIgnoreRule,
} from './types.js'


Expand All @@ -24,6 +25,10 @@ export abstract class KmoreBase<Context = any> {
readonly abstract eventCallbacks: EventCallbacks<Context> | undefined
readonly abstract instanceId: string | symbol
readonly abstract wrapIdentifierCaseConvert: CaseType
/**
* Rules ignoring table identifier case conversion,
*/
readonly abstract wrapIdentifierIgnoreRule: WrapIdentifierIgnoreRule

/**
* kmoreTrxId => Set(kmoreQueryId)
Expand Down
1 change: 1 addition & 0 deletions packages/kmore/src/lib/builder.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function builderBindEvents(

const queryCtxOpts: QueryContext = {
wrapIdentifierCaseConvert: kmore.wrapIdentifierCaseConvert,
wrapIdentifierIgnoreRule: kmore.wrapIdentifierIgnoreRule,
postProcessResponseCaseConvert: caseConvert,
kmoreQueryId,
columns: [],
Expand Down
39 changes: 37 additions & 2 deletions packages/kmore/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import { RecordCamelKeys, RecordPascalKeys, RecordSnakeKeys } from '@waiting/sha
// eslint-disable-next-line import/no-extraneous-dependencies
import type { Knex } from 'knex'

import { CaseType, EnumClient, KnexConfig, QueryContext } from './types.js'
import {
CaseType,
EnumClient,
KnexConfig,
QueryContext,
WrapIdentifierIgnoreRule,
} from './types.js'


export async function getCurrentTime(
Expand Down Expand Up @@ -60,9 +66,20 @@ function parseRespMysql2(res: RespMysql2): string {
: ''
}

export const defaultWrapIdentifierIgnoreRule: WrapIdentifierIgnoreRule = [
'now()',
'CURRENT_DATE',
'CURRENT_TIME',
'CURRENT_TIMESTAMP',
// /\bCURRENT_DATE\b/u,
// /\bCURRENT_TIMESTAMP\b/u,
// /\bDATE_TRUNC/u,
// /\bINTERVAL\b/u,
]


/**
* Convert identifier (field) to snakecase
* Convert identifier (field) to snake/camel case
*/
export function wrapIdentifier(
value: string,
Expand All @@ -79,6 +96,24 @@ export function wrapIdentifier(
}

if (queryContext) {
const { wrapIdentifierIgnoreRule } = queryContext
if (wrapIdentifierIgnoreRule?.length) {
const flag = defaultWrapIdentifierIgnoreRule.some((rule) => {
if (typeof rule === 'string') {
return rule === value
}
else if (rule instanceof RegExp) {
return rule.test(value)
}
else {
return false
}
})
if (flag) {
return value
}
}

switch (queryContext.wrapIdentifierCaseConvert) {
case CaseType.camel: {
ret = origImpl(snakeToCamel(value))
Expand Down
26 changes: 23 additions & 3 deletions packages/kmore/src/lib/kmore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { KmoreBase } from './base.js'
import { createRefTables } from './builder.index.js'
import { DbQueryBuilder, KmoreQueryBuilder } from './builder.types.js'
import { initialConfig } from './config.js'
import { PostProcessInput, postProcessResponse, wrapIdentifier } from './helper.js'
import {
PostProcessInput,
defaultWrapIdentifierIgnoreRule,
postProcessResponse,
wrapIdentifier,
} from './helper.js'
import { createTrxProperties } from './proxy.trx.js'
import {
CaseType,
Expand All @@ -22,6 +27,7 @@ import {
KnexConfig,
QueryContext,
TrxIdQueryMap,
WrapIdentifierIgnoreRule,
} from './types.js'
import { genKmoreTrxId } from './util.js'

Expand Down Expand Up @@ -91,6 +97,11 @@ export class Kmore<D = any, Context = any> extends KmoreBase<Context> {
readonly instanceId: string | symbol
readonly eventCallbacks: EventCallbacks<Context> | undefined
readonly wrapIdentifierCaseConvert: CaseType
/**
* Rules ignoring table identifier case conversion,
* @docs https://knexjs.org/guide/#wrapidentifier
*/
readonly wrapIdentifierIgnoreRule: WrapIdentifierIgnoreRule

constructor(options: KmoreFactoryOpts<D, Context>) {
super()
Expand Down Expand Up @@ -119,11 +130,13 @@ export class Kmore<D = any, Context = any> extends KmoreBase<Context> {
}

/**
* Table identifier case convertion,
* Table identifier case conversion,
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']`
*/
this.wrapIdentifierCaseConvert = options.wrapIdentifierCaseConvert ?? CaseType.snake

this.wrapIdentifierIgnoreRule = options.wrapIdentifierIgnoreRule ?? defaultWrapIdentifierIgnoreRule

if (! this.config.wrapIdentifier) {
if (this.wrapIdentifierCaseConvert === CaseType.none) {
this.config.wrapIdentifier = defaultGlobalWrapIdentifier
Expand Down Expand Up @@ -301,12 +314,19 @@ export interface KmoreFactoryOpts<D, Ctx = unknown> {
*/
eventCallbacks?: EventCallbacks<Ctx> | undefined
/**
* Table identifier case convertion,
* Table identifier case conversion,
* If not CaseType.none, will ignore value of `KnexConfig['wrapIdentifier']`
* @default CaseType.snake
* @docs https://knexjs.org/guide/#wrapidentifier
*/
wrapIdentifierCaseConvert?: CaseType | undefined

/**
* Rules ignoring table identifier case conversion,
* @docs https://knexjs.org/guide/#wrapidentifier
*/
wrapIdentifierIgnoreRule?: WrapIdentifierIgnoreRule | undefined

/**
* Atuo trsaction action (rollback|commit|none) on error (Rejection or Exception),
* @CAUTION **Will always rollback if query error in database even though this value set to 'commit'**
Expand Down
6 changes: 6 additions & 0 deletions packages/kmore/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export enum SmartKey {
crossJoin = 'smartCrossJoin',
}

export type WrapIdentifierIgnoreRule = (string | RegExp)[]


export type EventType = 'query' | 'queryError' | 'queryResponse' | 'start' | 'unknown'

Expand Down Expand Up @@ -100,6 +102,10 @@ export interface KmoreEvent <T = unknown> {

export interface QueryContext {
wrapIdentifierCaseConvert: CaseType
/**
* Rules ignoring table identifier case conversion,
*/
wrapIdentifierIgnoreRule: WrapIdentifierIgnoreRule | undefined
postProcessResponseCaseConvert: CaseType
kmoreQueryId: symbol
columns: Record<string, string>[]
Expand Down

0 comments on commit 8abf183

Please sign in to comment.