/* eslint-disable import/no-extraneous-dependencies */ import { DbDict } from 'kmore-types' import { Knex, knex } from 'knex' import { Observable, Subject } from 'rxjs' import { filter } from 'rxjs/operators' import { defaultPropDescriptor } from './config' import { bindOnQuery, bindOnQueryError, bindOnQueryResp } from './event' import { DbQueryBuilder, KmoreEvent, KnexConfig, OnQueryData, OnQueryErrorData, OnQueryErrorErr, OnQueryRespRaw, QueryResponse, } from './types' export class Kmore { readonly refTables: DbQueryBuilder protected readonly subject: Subject /** * Generics parameter, do NOT access as variable! * Use under typescript development only. * * @example ```ts * const km = kmoreFactore({}) * type DbModel = typeof km.DbModel * type Uid = DbModel['tb_user']['uid'] // equal to number * ``` */ readonly DbModel: D /** * Generics parameter, do NOT access as variable! * Use under typescript development only. * * @example ```ts * const km = kmoreFactore({}) * type Dict = typeof kmore['Dict'] * type Uid = Dict['columns']['tb_user']['alias']['tbUserUid'] // equal to literal 'tb_user.uid' * ``` */ readonly Dict: DbDict constructor( public readonly config: KnexConfig, public readonly dict: DbDict, public dbh: Knex, public instanceId: string | symbol, // private readonly eventCallback?: (event: KmoreEvent) => void, ) { const dbhBindEvent = dbh .on('query', (data: OnQueryData) => bindOnQuery(this.subject, void 0, data)) .on( 'query-response', (_: QueryResponse, respRaw: OnQueryRespRaw) => bindOnQueryResp(this.subject, void 0, _, respRaw), ) .on( 'query-error', (err: OnQueryErrorErr, data: OnQueryErrorData) => bindOnQueryError(this.subject, void 0, err, data), ) this.dbh = dbhBindEvent this.refTables = this.createRefTables(dbh, 'ref_') this.subject = new Subject() } register( eventFilterCallback?: (ev: KmoreEvent, identifier?: K) => boolean, identifier?: K, ): Observable> { const stream$ = this.subject.asObservable() as Observable> const ret$ = stream$.pipe( filter((ev) => { const flag = typeof eventFilterCallback === 'function' ? eventFilterCallback(ev, identifier) : true return flag }), ) return ret$ } unsubscribe(): void { this.subject.unsubscribe() } protected createRefTables(dbh: Knex, prefix: string): DbQueryBuilder { const rb = {} as DbQueryBuilder Object.keys(this.dict.tables).forEach((refName) => { const name = `${prefix}${refName}` Object.defineProperty(rb, name, { ...defaultPropDescriptor, value: (identifier?: unknown) => { const id = typeof identifier === 'undefined' ? this.instanceId : identifier return this.extRefTableFnProperty(dbh, refName, id) }, // must dynamically!! }) Object.defineProperty(rb[name as keyof typeof rb], 'name', { ...defaultPropDescriptor, value: name, }) }) return rb } protected extRefTableFnProperty( dbh: Knex, refName: string, identifier?: unknown, ): Knex.QueryBuilder { let refTable = dbh(refName) if (typeof identifier !== 'undefined') { refTable = refTable .on('query', (data: OnQueryData) => bindOnQuery(this.subject, identifier, data)) .on( 'query-response', (_: QueryResponse, respRaw: OnQueryRespRaw) => bindOnQueryResp(this.subject, identifier, _, respRaw), ) .on( 'query-error', (err: OnQueryErrorErr, data: OnQueryErrorData) => bindOnQueryError(this.subject, identifier, err, data), ) } return refTable } // private triggerEvent(event: KmoreEvent): void { // if (this.eventCallback) { // this.eventCallback(event) // } // void 0 // } } export interface KmoreFactoryOpts { config: KnexConfig dict: DbDict instanceId?: string | symbol dbh?: Knex dbId?: string } export type EventCallback = (event: KmoreEvent) => void export function kmoreFactory(options: KmoreFactoryOpts): Kmore { const dbId = options.dbId ? options.dbId : '' const dbh: Knex = options.dbh ? options.dbh : knex(options.config) const instanceId = options.instanceId ? options.instanceId : Symbol(`${dbId}-` + Date.now().toString()) const km = new Kmore( options.config, options.dict, dbh, instanceId, ) return km }