Skip to content

Commit

Permalink
feat: Added ability to add additional argument types to EagerLoadClosure
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulyk committed May 5, 2023
1 parent 87595dc commit ef53106
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export function setEagerDataSource(dataSource: DataSource | Connection) {
eagerDataSource = dataSource;
}

function flatRelations(relations: RelationDefinitions): RelationDefinitions {
export function flatRelations<Args extends any[] = []>(relations: RelationDefinitions<Args>): RelationObjectDefinition<Args> {
if (typeof relations === 'string') {
return {[relations]: undefined};
}
if (Array.isArray(relations)) {
return relations.reduce((relations, item) => Object.assign(relations, flatRelations(item)), {});
return relations.reduce((relations: RelationObjectDefinition<Args>, item:RelationDefinitions<Args>) => Object.assign(relations, flatRelations(item)), {});
}
return relations;
}
Expand Down
9 changes: 6 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export type EagerContext = {
filter: (callback: ((entity: any) => any)) => unknown;
lateral: (callback: LateralCallback, alias?: string) => void;
}
export type EagerLoadClosure = (builder: SelectQueryBuilder<any>, context: EagerContext) => void;
export type RelationObjectDefinition = { [key: string]: EagerLoadClosure | undefined };
export type RelationDefinitions = string | (string | RelationDefinitions)[] | RelationObjectDefinition;
export type EagerLoadClosure<Args extends any[] = []> = (builder: SelectQueryBuilder<any>, context: EagerContext, ...args: Args) => void;
export type RelationObjectDefinition<Args extends any[] = []> = { [key: string]: EagerLoadClosure<Args> | undefined };
export type RelationDefinitions<Args extends any[] = []> =
string
| (string | RelationDefinitions<Args>)[]
| RelationObjectDefinition<Args>;

0 comments on commit ef53106

Please sign in to comment.