fix: findOne and findById types

This commit is contained in:
abdou6666 2024-12-30 10:33:33 +01:00
parent 62e18ab591
commit d3123268aa
2 changed files with 8 additions and 8 deletions

View File

@ -241,7 +241,7 @@ export abstract class BaseRepository<
query: Query<T | null, T>, query: Query<T | null, T>,
cls: new () => R, cls: new () => R,
options?: ClassTransformOptions, options?: ClassTransformOptions,
): Promise<R> { ): Promise<R | null> {
const doc = await query.lean(this.leanOpts).exec(); const doc = await query.lean(this.leanOpts).exec();
return plainToClass(cls, doc, options ?? this.transformOpts); return plainToClass(cls, doc, options ?? this.transformOpts);
} }
@ -256,8 +256,8 @@ export abstract class BaseRepository<
} }
return typeof criteria === 'string' return typeof criteria === 'string'
? this.model.findById(criteria, projection) ? this.model.findById<HydratedDocument<T>>(criteria, projection)
: this.model.findOne<T>(criteria, projection); : this.model.findOne<HydratedDocument<T>>(criteria, projection);
} }
async findOne( async findOne(
@ -267,7 +267,7 @@ export abstract class BaseRepository<
) { ) {
if (!criteria) { if (!criteria) {
// @TODO : Issue a warning ? // @TODO : Issue a warning ?
return Promise.resolve(undefined); return Promise.resolve(null);
} }
const query = this.findOneQuery(criteria, projection); const query = this.findOneQuery(criteria, projection);
@ -277,7 +277,7 @@ export abstract class BaseRepository<
async findOneAndPopulate( async findOneAndPopulate(
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
): Promise<TFull> { ): Promise<TFull | null> {
this.ensureCanPopulate(); this.ensureCanPopulate();
const query = this.findOneQuery(criteria, projection).populate( const query = this.findOneQuery(criteria, projection).populate(
this.populate, this.populate,
@ -474,7 +474,7 @@ export abstract class BaseRepository<
async updateOne<D extends Partial<U>>( async updateOne<D extends Partial<U>>(
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
dto: UpdateQuery<D>, dto: UpdateQuery<D>,
): Promise<T> { ): Promise<T | null> {
const query = this.model.findOneAndUpdate<T>( const query = this.model.findOneAndUpdate<T>(
{ {
...(typeof criteria === 'string' ? { _id: criteria } : criteria), ...(typeof criteria === 'string' ? { _id: criteria } : criteria),

View File

@ -33,7 +33,7 @@ export abstract class BaseService<
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
options?: ClassTransformOptions, options?: ClassTransformOptions,
projection?: ProjectionType<T>, projection?: ProjectionType<T>,
): Promise<T> { ): Promise<T | null> {
return await this.repository.findOne(criteria, options, projection); return await this.repository.findOne(criteria, options, projection);
} }
@ -173,7 +173,7 @@ export abstract class BaseService<
async updateOne<D extends Partial<Omit<T, keyof BaseSchema>>>( async updateOne<D extends Partial<Omit<T, keyof BaseSchema>>>(
criteria: string | TFilterQuery<T>, criteria: string | TFilterQuery<T>,
dto: D, dto: D,
): Promise<T> { ): Promise<T | null> {
return await this.repository.updateOne(criteria, dto); return await this.repository.updateOne(criteria, dto);
} }