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

View File

@ -33,7 +33,7 @@ export abstract class BaseService<
criteria: string | TFilterQuery<T>,
options?: ClassTransformOptions,
projection?: ProjectionType<T>,
): Promise<T> {
): Promise<T | null> {
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>>>(
criteria: string | TFilterQuery<T>,
dto: D,
): Promise<T> {
): Promise<T | null> {
return await this.repository.updateOne(criteria, dto);
}