diff --git a/src/core/Dao.ts b/src/core/Dao.ts index a4739d2..372ee55 100644 --- a/src/core/Dao.ts +++ b/src/core/Dao.ts @@ -30,10 +30,7 @@ export type _QueryDeepPartialEntity = { | (() => string); }; -export interface QueryOption< - E, - S extends FindOptionsSelect | undefined = undefined, -> { +export interface QueryOption | E = E> { /** * Page number */ @@ -87,21 +84,23 @@ export interface QueryOption< export type ReadManyOption< E, - S extends FindOptionsSelect | undefined = undefined, + S extends FindOptionsSelect | E = E > = QueryOption; -export type SelectedFields | undefined> = - S extends FindOptionsSelect - ? { - [K in keyof S as S[K] extends true ? K : never]: K extends keyof T - ? T[K] - : never; - } - : T; +export type SelectedFields< + T, + S extends FindOptionsSelect | T +> = S extends FindOptionsSelect + ? { + [K in keyof S as S[K] extends true ? K : never]: K extends keyof T + ? T[K] + : never; + } + : T; export type SelectedRead< E, - S extends FindOptionsSelect | undefined = undefined, + S extends FindOptionsSelect | E = E > = S extends undefined ? E : SelectedFields; export type Class = @@ -172,7 +171,7 @@ export class Dao { * @param manager EntityManager to be used for the operation (optional). Use only for transactions * @returns Result with the entity */ - async read | undefined = undefined>( + async read | Entity = Entity>( value: | string | number @@ -188,7 +187,7 @@ export class Dao { if (typeof value === "number" || typeof value === "string") { options = { where: { id: value } }; } else { - options = value; + options = value as any; } const result = (await repository.findOne(options)) as SelectedRead< Entity, @@ -435,7 +434,7 @@ export class Dao { * Read a paginated list of entities * @returns Result with the list of entities */ - async readMany | undefined = undefined>( + async readMany | Entity = Entity>( options?: ReadManyOption, manager?: EntityManager ): Promise[]>>> { @@ -484,10 +483,9 @@ export class Dao { findOptions["take"] = count; } - const result = (await repository.find(findOptions)) as SelectedRead< - Entity, - S - >[]; + const result = (await repository.find( + findOptions as any + )) as SelectedRead[]; log.debug("Successfully found", `${this.entityName}/readMany`, { page, count,