Skip to content

Commit

Permalink
fix: small adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyazgara committed Oct 27, 2022
1 parent 1879a66 commit 692dc70
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 50 deletions.
7 changes: 3 additions & 4 deletions packages/core/lib/execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export class ExecutionContext<
}

constructor(
public readonly buildQueryBuilder: () => Knex.QueryBuilder<
TRecord,
TResult
>,
public readonly buildQueryBuilder: (
trx?: Knex.Transaction,
) => Knex.QueryBuilder<TRecord, TResult>,
private readonly _queryBuilder: Knex.QueryBuilder<TRecord, TResult>,
private readonly _rawBuilder: Knex.RawBuilder<TRecord, TResult>,
private readonly _constructorRef: Function,
Expand Down
10 changes: 7 additions & 3 deletions packages/core/lib/interfaces/database-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ export interface AliasableRepositoryDatabaseOptions {
alias?: string;
}

export interface DatabaseOptions<TRecord, TResult> {
export interface DatabaseOptions<TRecord, TResult, Options = unknown> {
transaction?: Knex.Transaction;
intercept?: RepositoryInterceptors<TRecord, TResult>;
intercept?: RepositoryInterceptors<TRecord, TResult, Options>;
}

export interface SelectDatabaseOptions<TRecord, TResult>
extends DatabaseOptions<TRecord, TResult>,
extends DatabaseOptions<
TRecord,
TResult,
SelectDatabaseOptions<TRecord, TResult>
>,
AliasableRepositoryDatabaseOptions {
[field: string]: unknown;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -36,10 +35,8 @@ export interface KnexionModuleAsyncOptions
useExisting?: Type<KnexionOptionsFactory>;
useClass?: Type<KnexionOptionsFactory>;
useFactory?: (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: any[]
) => Promise<KnexionModuleOptions> | KnexionModuleOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inject?: any[];
extraProviders?: Provider[];
}
17 changes: 11 additions & 6 deletions packages/core/lib/interfaces/repository-interceptor.interface.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { Observable } from 'rxjs';
import { ExecutionContext } from '../execution-context';
import { DatabaseOptions } from './database-options.interface';

export interface RepositoryInterceptorNext<T = unknown> {
handle(): Observable<T>;
}

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<TRecord, TResult> = DatabaseOptions<
TRecord,
TResult
>,
TInput = unknown,
TOutput = TInput,
> {
intercept(
context: ExecutionContext<TRecord, TResult>,
context: ExecutionContext<TRecord, TResult, Options>,
next: RepositoryInterceptorNext<TInput>,
): Observable<TOutput> | Promise<Observable<TOutput>>;
}

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<TRecord, TResult>[];
Options extends DatabaseOptions<TRecord, TResult> = DatabaseOptions<
TRecord,
TResult
>,
> = RepositoryInterceptor<TRecord, TResult, Options>[];
20 changes: 9 additions & 11 deletions packages/core/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ export class Repository<
this.interceptorsConsumer = new InterceptorsConsumer();
}

public async list<TResult = TRecord[]>(
public async list<TResult = TRecord, TOutput = TResult[]>(
options?: SelectDatabaseOptions<TRecord, TResult>,
): Promise<TResult> {
): Promise<TOutput> {
options = this.addAliasToOptions(options);
return (await this.queryBuilder<TResult>(options, this.list).select(
return (await this.queryBuilder(options, this.list).select(
addPrefixColumn('*', options?.alias),
)) as Promise<TResult>;
)) as Promise<TOutput>;
}

public async create(
Expand All @@ -69,7 +69,6 @@ export class Repository<
const [record] = await this.queryBuilder<TRecord>(
options,
this.create,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
).insert(createPayload as any, '*');
return record as TRecord;
}
Expand All @@ -79,7 +78,7 @@ export class Repository<
options?: SelectDatabaseOptions<TRecord, TResult>,
): Promise<TResult | null> {
options = this.addAliasToOptions(options);
const record = await this.queryBuilder<TResult>(options, this.retrieve)
const record = await this.queryBuilder(options, this.retrieve)
.select(addPrefixColumn('*', options?.alias))
.where(addPrefixColumn('id', options?.alias), id)
.first();
Expand All @@ -98,7 +97,6 @@ export class Repository<
): Promise<TRecord | null> {
const [record] = await this.queryBuilder<TRecord>(options, this.update)
.where('id', id)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.update(updatePayload as any, '*');
if (!record) {
return null;
Expand Down Expand Up @@ -136,13 +134,13 @@ export class Repository<
public rawBuilder<TResult>(
trx?: Knex.Transaction,
): Knex.RawBuilder<TRecord, TResult>;
public rawBuilder<TResult>(trx?: Knex.Transaction): (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public rawBuilder<TResult>(
trx?: Knex.Transaction,
): (
valueOrSql: any,
...bindings: readonly Knex.RawBinding[]
) => Knex.Raw<TResult> {
return (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
valueOrSql: any,
...bindings: readonly Knex.RawBinding[]
): Knex.Raw<TResult> => {
Expand Down Expand Up @@ -212,7 +210,7 @@ export class Repository<
handler?: Function,
): Knex.QueryBuilder<TRecord, TResult> {
const context = new ExecutionContext(
() => this.queryBuilder(options, handler),
(trx?: Knex.Transaction) => this.pureQueryBuilder(trx),
queryBuilder,
this.rawBuilder(options?.transaction),
this.constructor,
Expand Down
6 changes: 1 addition & 5 deletions packages/sort/lib/interceptors/sort.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ export class SortInterceptor<TRecord, TResult>
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <TResult>(
schema: FieldTransformOptions<TResult>,
export const FieldTransform = (
schema: FieldTransformOptions,
): ClassDecorator & MethodDecorator => {
return SetMetadata(FIELD_TRANSFORM_SCHEMA, schema);
};
18 changes: 7 additions & 11 deletions packages/transform/lib/interceptors/field-transform.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class FieldTransformInterceptor<TRecord, TResult>
context: ExecutionContext<TRecord, TResult>,
next: RepositoryInterceptorNext<any>,
): Observable<any> {
const { schema, resolver } = this.retrieveTransformSchema(context);
const { schema, transformer } = this.retrieveTransformSchema(context);
const schemaEntries = Object.entries<FieldTransformer>(schema);

if (!schemaEntries.length) {
Expand All @@ -31,15 +31,11 @@ export class FieldTransformInterceptor<TRecord, TResult>

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) {
Expand Down Expand Up @@ -71,7 +67,7 @@ export class FieldTransformInterceptor<TRecord, TResult>

private retrieveTransformSchema(
context: ExecutionContext<TRecord, TResult>,
): FieldTransformOptions<TResult> {
): FieldTransformOptions {
const handler = context.getHandler();
if (handler) {
const handlerSchema = this.reflector.get(FIELD_TRANSFORM_SCHEMA, handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export type FieldTransformer<TValue = any, TResult = unknown> = (

export type FieldTransformSchema = Record<string, FieldTransformer>;

export interface FieldTransformOptions<TResult> {
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;
}

0 comments on commit 692dc70

Please sign in to comment.