fix: add base-controller DTO typing

This commit is contained in:
yassinedorbozgithub 2025-01-12 09:13:04 +01:00
parent 8a1039148c
commit c6c1e2a755
4 changed files with 16 additions and 19 deletions

View File

@ -10,6 +10,7 @@ import { NotFoundException } from '@nestjs/common';
import { TFilterQuery } from '@/utils/types/filter.types'; import { TFilterQuery } from '@/utils/types/filter.types';
import { DtoConfig } from '../types/dto.types';
import { TValidateProps } from '../types/filter.types'; import { TValidateProps } from '../types/filter.types';
import { BaseSchema } from './base-schema'; import { BaseSchema } from './base-schema';
@ -20,8 +21,9 @@ export abstract class BaseController<
TStub = never, TStub = never,
P extends string = never, P extends string = never,
TFull extends Omit<T, P> = never, TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object,
> { > {
constructor(protected readonly service: BaseService<T, P, TFull>) {} constructor(protected readonly service: BaseService<T, P, TFull, Dto>) {}
/** /**
* Checks if the given populate fields are allowed based on the allowed fields list. * Checks if the given populate fields are allowed based on the allowed fields list.

View File

@ -72,7 +72,7 @@ export abstract class BaseRepository<
P extends string = never, P extends string = never,
TFull extends Omit<T, P> = never, TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object, Dto extends DtoConfig = object,
U = Omit<T, keyof BaseSchema>, U extends Omit<T, keyof BaseSchema> = Omit<T, keyof BaseSchema>,
D = Document<T>, D = Document<T>,
> { > {
private readonly transformOpts = { excludePrefixes: ['_', 'password'] }; private readonly transformOpts = { excludePrefixes: ['_', 'password'] };

View File

@ -18,6 +18,7 @@ export abstract class BaseSeeder<
P extends string = never, P extends string = never,
TFull extends Omit<T, P> = never, TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object, Dto extends DtoConfig = object,
U extends Omit<T, keyof BaseSchema> = Omit<T, keyof BaseSchema>,
> { > {
constructor( constructor(
protected readonly repository: BaseRepository<T, P, TFull, Dto>, protected readonly repository: BaseRepository<T, P, TFull, Dto>,
@ -32,9 +33,7 @@ export abstract class BaseSeeder<
return count === 0; return count === 0;
} }
async seed<D extends Omit<T, keyof BaseSchema>>( async seed(models: DtoInfer<DtoAction.Create, Dto, U>[]): Promise<boolean> {
models: DtoInfer<DtoAction.Create, Dto, D>[],
): Promise<boolean> {
if (await this.isEmpty()) { if (await this.isEmpty()) {
await this.repository.createMany(models); await this.repository.createMany(models);
return true; return true;

View File

@ -24,6 +24,7 @@ export abstract class BaseService<
P extends string = never, P extends string = never,
TFull extends Omit<T, P> = never, TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object, Dto extends DtoConfig = object,
U extends Omit<T, keyof BaseSchema> = Omit<T, keyof BaseSchema>,
> { > {
constructor( constructor(
protected readonly repository: BaseRepository<T, P, TFull, Dto>, protected readonly repository: BaseRepository<T, P, TFull, Dto>,
@ -144,9 +145,7 @@ export abstract class BaseService<
return await this.repository.count(criteria); return await this.repository.count(criteria);
} }
async create<D extends Omit<T, keyof BaseSchema>>( async create(dto: DtoInfer<DtoAction.Create, Dto, U>): Promise<T> {
dto: DtoInfer<DtoAction.Create, Dto, D>,
): Promise<T> {
try { try {
return await this.repository.create(dto); return await this.repository.create(dto);
} catch (error) { } catch (error) {
@ -159,9 +158,9 @@ export abstract class BaseService<
} }
} }
async findOneOrCreate<D extends Omit<T, keyof BaseSchema>>( async findOneOrCreate(
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
dto: DtoInfer<DtoAction.Create, Dto, D>, dto: DtoInfer<DtoAction.Create, Dto, U>,
): Promise<T> { ): Promise<T> {
const result = await this.findOne(criteria); const result = await this.findOne(criteria);
if (!result) { if (!result) {
@ -170,24 +169,21 @@ export abstract class BaseService<
return result; return result;
} }
async createMany<D extends Omit<T, keyof BaseSchema>>( async createMany(
dtoArray: DtoInfer<DtoAction.Create, Dto, D>[], dtoArray: DtoInfer<DtoAction.Create, Dto, U>[],
): Promise<T[]> { ): Promise<T[]> {
return await this.repository.createMany(dtoArray); return await this.repository.createMany(dtoArray);
} }
async updateOne<D extends Partial<Omit<T, keyof BaseSchema>>>( async updateOne(
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
dto: D, dto: Partial<U>,
options?: QueryOptions<D> | null, options?: QueryOptions<Partial<U>> | null,
): Promise<T | null> { ): Promise<T | null> {
return await this.repository.updateOne(criteria, dto, options); return await this.repository.updateOne(criteria, dto, options);
} }
async updateMany<D extends Partial<Omit<T, keyof BaseSchema>>>( async updateMany(filter: TFilterQuery<T>, dto: Partial<U>) {
filter: TFilterQuery<T>,
dto: D,
) {
return await this.repository.updateMany(filter, dto); return await this.repository.updateMany(filter, dto);
} }