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 { DtoConfig } from '../types/dto.types';
import { TValidateProps } from '../types/filter.types';
import { BaseSchema } from './base-schema';
@ -20,8 +21,9 @@ export abstract class BaseController<
TStub = never,
P extends string = 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.

View File

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

View File

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

View File

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