diff --git a/packages/core/lib/execution-context.ts b/packages/core/lib/execution-context.ts index ccb16cd5..93e7da43 100644 --- a/packages/core/lib/execution-context.ts +++ b/packages/core/lib/execution-context.ts @@ -22,10 +22,9 @@ export class ExecutionContext< } constructor( - public readonly buildQueryBuilder: () => Knex.QueryBuilder< - TRecord, - TResult - >, + public readonly buildQueryBuilder: ( + trx?: Knex.Transaction, + ) => Knex.QueryBuilder, private readonly _queryBuilder: Knex.QueryBuilder, private readonly _rawBuilder: Knex.RawBuilder, private readonly _constructorRef: Function, diff --git a/packages/core/lib/interfaces/database-options.interface.ts b/packages/core/lib/interfaces/database-options.interface.ts index a86f1a52..1a90d85b 100644 --- a/packages/core/lib/interfaces/database-options.interface.ts +++ b/packages/core/lib/interfaces/database-options.interface.ts @@ -5,13 +5,17 @@ export interface AliasableRepositoryDatabaseOptions { alias?: string; } -export interface DatabaseOptions { +export interface DatabaseOptions { transaction?: Knex.Transaction; - intercept?: RepositoryInterceptors; + intercept?: RepositoryInterceptors; } export interface SelectDatabaseOptions - extends DatabaseOptions, + extends DatabaseOptions< + TRecord, + TResult, + SelectDatabaseOptions + >, AliasableRepositoryDatabaseOptions { [field: string]: unknown; } diff --git a/packages/core/lib/interfaces/knexion-module-options.interface.ts b/packages/core/lib/interfaces/knexion-module-options.interface.ts index bc5d3618..45d09f0b 100644 --- a/packages/core/lib/interfaces/knexion-module-options.interface.ts +++ b/packages/core/lib/interfaces/knexion-module-options.interface.ts @@ -19,7 +19,6 @@ export interface KnexionModuleOptions extends Knex.Config { * @param err error that was thrown * @returns whether to retry connection or not */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any toRetry?: (err: any) => boolean; /** * If `true`, will show verbose error messages on each connection retry. @@ -36,10 +35,8 @@ export interface KnexionModuleAsyncOptions useExisting?: Type; useClass?: Type; useFactory?: ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any ...args: any[] ) => Promise | KnexionModuleOptions; - // eslint-disable-next-line @typescript-eslint/no-explicit-any inject?: any[]; extraProviders?: Provider[]; } diff --git a/packages/core/lib/interfaces/repository-interceptor.interface.ts b/packages/core/lib/interfaces/repository-interceptor.interface.ts index 366f1af0..783ea3f3 100644 --- a/packages/core/lib/interfaces/repository-interceptor.interface.ts +++ b/packages/core/lib/interfaces/repository-interceptor.interface.ts @@ -1,27 +1,32 @@ import { Observable } from 'rxjs'; import { ExecutionContext } from '../execution-context'; +import { DatabaseOptions } from './database-options.interface'; export interface RepositoryInterceptorNext { handle(): Observable; } export interface RepositoryInterceptor< - // eslint-disable-next-line @typescript-eslint/no-explicit-any TRecord = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any TResult = any, + Options extends DatabaseOptions = DatabaseOptions< + TRecord, + TResult + >, TInput = unknown, TOutput = TInput, > { intercept( - context: ExecutionContext, + context: ExecutionContext, next: RepositoryInterceptorNext, ): Observable | Promise>; } export type RepositoryInterceptors< - // eslint-disable-next-line @typescript-eslint/no-explicit-any TRecord = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any TResult = any, -> = RepositoryInterceptor[]; + Options extends DatabaseOptions = DatabaseOptions< + TRecord, + TResult + >, +> = RepositoryInterceptor[]; diff --git a/packages/core/lib/repository.ts b/packages/core/lib/repository.ts index c1f55fcf..a7faab02 100644 --- a/packages/core/lib/repository.ts +++ b/packages/core/lib/repository.ts @@ -53,13 +53,13 @@ export class Repository< this.interceptorsConsumer = new InterceptorsConsumer(); } - public async list( + public async list( options?: SelectDatabaseOptions, - ): Promise { + ): Promise { options = this.addAliasToOptions(options); - return (await this.queryBuilder(options, this.list).select( + return (await this.queryBuilder(options, this.list).select( addPrefixColumn('*', options?.alias), - )) as Promise; + )) as Promise; } public async create( @@ -69,7 +69,6 @@ export class Repository< const [record] = await this.queryBuilder( options, this.create, - // eslint-disable-next-line @typescript-eslint/no-explicit-any ).insert(createPayload as any, '*'); return record as TRecord; } @@ -79,7 +78,7 @@ export class Repository< options?: SelectDatabaseOptions, ): Promise { options = this.addAliasToOptions(options); - const record = await this.queryBuilder(options, this.retrieve) + const record = await this.queryBuilder(options, this.retrieve) .select(addPrefixColumn('*', options?.alias)) .where(addPrefixColumn('id', options?.alias), id) .first(); @@ -98,7 +97,6 @@ export class Repository< ): Promise { const [record] = await this.queryBuilder(options, this.update) .where('id', id) - // eslint-disable-next-line @typescript-eslint/no-explicit-any .update(updatePayload as any, '*'); if (!record) { return null; @@ -136,13 +134,13 @@ export class Repository< public rawBuilder( trx?: Knex.Transaction, ): Knex.RawBuilder; - public rawBuilder(trx?: Knex.Transaction): ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any + public rawBuilder( + trx?: Knex.Transaction, + ): ( valueOrSql: any, ...bindings: readonly Knex.RawBinding[] ) => Knex.Raw { return ( - // eslint-disable-next-line @typescript-eslint/no-explicit-any valueOrSql: any, ...bindings: readonly Knex.RawBinding[] ): Knex.Raw => { @@ -212,7 +210,7 @@ export class Repository< handler?: Function, ): Knex.QueryBuilder { const context = new ExecutionContext( - () => this.queryBuilder(options, handler), + (trx?: Knex.Transaction) => this.pureQueryBuilder(trx), queryBuilder, this.rawBuilder(options?.transaction), this.constructor, diff --git a/packages/sort/lib/interceptors/sort.interceptor.ts b/packages/sort/lib/interceptors/sort.interceptor.ts index 32d140b8..b00d4728 100644 --- a/packages/sort/lib/interceptors/sort.interceptor.ts +++ b/packages/sort/lib/interceptors/sort.interceptor.ts @@ -25,11 +25,7 @@ export class SortInterceptor const sortOptionKey = this.options.optionKey ?? 'sort'; const sort = context.options[sortOptionKey]; - if (!Array.isArray(sort)) { - throw new Error(`'${sortOptionKey}' is not array`); - } - - if (sort) { + if (sort && Array.isArray(sort)) { sort.forEach((property) => { const [direction, column] = getSortDirection(property); context.queryBuilder.orderBy( diff --git a/packages/transform/lib/decorators/field-transform.decorator.ts b/packages/transform/lib/decorators/field-transform.decorator.ts index f4e3d861..0bb1d19d 100644 --- a/packages/transform/lib/decorators/field-transform.decorator.ts +++ b/packages/transform/lib/decorators/field-transform.decorator.ts @@ -1,9 +1,9 @@ import { SetMetadata } from '@nestjs/common'; -import { FieldTransformOptions } from 'lib/interfaces'; +import { FieldTransformOptions } from '../interfaces'; import { FIELD_TRANSFORM_SCHEMA } from '../transform.constants'; -export const FieldTransform = ( - schema: FieldTransformOptions, +export const FieldTransform = ( + schema: FieldTransformOptions, ): ClassDecorator & MethodDecorator => { return SetMetadata(FIELD_TRANSFORM_SCHEMA, schema); }; diff --git a/packages/transform/lib/interceptors/field-transform.interceptor.ts b/packages/transform/lib/interceptors/field-transform.interceptor.ts index d0d3fe5b..2f9f50bb 100644 --- a/packages/transform/lib/interceptors/field-transform.interceptor.ts +++ b/packages/transform/lib/interceptors/field-transform.interceptor.ts @@ -22,7 +22,7 @@ export class FieldTransformInterceptor context: ExecutionContext, next: RepositoryInterceptorNext, ): Observable { - const { schema, resolver } = this.retrieveTransformSchema(context); + const { schema, transformer } = this.retrieveTransformSchema(context); const schemaEntries = Object.entries(schema); if (!schemaEntries.length) { @@ -31,15 +31,11 @@ export class FieldTransformInterceptor return next.handle().pipe( map((result) => { - if (resolver) { - const resolvedData = resolver(result); - if (!resolvedData) { - return null; - } - if (Array.isArray(resolvedData)) { - return this.transformArray(resolvedData, schemaEntries); - } - return this.transform(resolvedData, schemaEntries); + if (transformer) { + const transform = (data: any) => { + return this.transform(data, schemaEntries); + }; + return transformer(transform, result); } if (!result) { @@ -71,7 +67,7 @@ export class FieldTransformInterceptor private retrieveTransformSchema( context: ExecutionContext, - ): FieldTransformOptions { + ): FieldTransformOptions { const handler = context.getHandler(); if (handler) { const handlerSchema = this.reflector.get(FIELD_TRANSFORM_SCHEMA, handler); diff --git a/packages/transform/lib/interfaces/field-transform-options.interface.ts b/packages/transform/lib/interfaces/field-transform-options.interface.ts index 4569622e..7a2b435d 100644 --- a/packages/transform/lib/interfaces/field-transform-options.interface.ts +++ b/packages/transform/lib/interfaces/field-transform-options.interface.ts @@ -4,9 +4,9 @@ export type FieldTransformer = ( export type FieldTransformSchema = Record; -export interface FieldTransformOptions { +export type ItemTransformer = (data: any) => any; + +export interface FieldTransformOptions { schema: FieldTransformSchema; - resolver?: ( - data: TResult | TResult[] | null | unknown, - ) => TResult | TResult[] | null; + transformer?: (transform: ItemTransformer, result: any) => any; }