diff --git a/.changeset/gentle-islands-arrive.md b/.changeset/gentle-islands-arrive.md new file mode 100644 index 0000000000..dbeaac3c86 --- /dev/null +++ b/.changeset/gentle-islands-arrive.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix: Repository util mention of entity specifics diff --git a/packages/medusa/src/utils/repository.ts b/packages/medusa/src/utils/repository.ts index b7155a3d44..b386c41bcd 100644 --- a/packages/medusa/src/utils/repository.ts +++ b/packages/medusa/src/utils/repository.ts @@ -1,8 +1,6 @@ import { flatten, groupBy, map, merge } from "lodash" import { EntityMetadata, Repository, SelectQueryBuilder } from "typeorm" -import { FindWithoutRelationsOptions } from "../repositories/customer-group" - -// TODO: All the utilities except applyOrdering needs to be re worked depending on the outcome of the product repository +import { ExtendedFindConfig } from "../types/common" /** * Custom query entity, it is part of the creation of a custom findWithRelationsAndCount needs. @@ -12,27 +10,43 @@ import { FindWithoutRelationsOptions } from "../repositories/customer-group" * @param groupedRelations * @param withDeleted * @param select + * @param customJoinBuilders */ export async function queryEntityWithIds( repository: Repository, entityIds: string[], groupedRelations: { [toplevel: string]: string[] }, withDeleted = false, - select: (keyof T)[] = [] + select: (keyof T)[] = [], + customJoinBuilders: (( + qb: SelectQueryBuilder, + alias: string, + toplevel: string + ) => boolean)[] = [] ): Promise { - const alias = repository.constructor.name + const alias = repository.metadata.name.toLowerCase() return await Promise.all( Object.entries(groupedRelations).map(async ([toplevel, rels]) => { - let querybuilder = repository.createQueryBuilder(`${alias}`) + let querybuilder = repository.createQueryBuilder(alias) if (select && select.length) { querybuilder.select(select.map((f) => `${alias}.${f as string}`)) } - querybuilder = querybuilder.leftJoinAndSelect( - `${alias}.${toplevel}`, - toplevel - ) + let shouldAttachDefault = true + for (const customJoinBuilder of customJoinBuilders) { + const result = customJoinBuilder(querybuilder, alias, toplevel) + shouldAttachDefault = shouldAttachDefault && result + } + + // If the toplevel relation has been attached with a customJoinBuilder and the function return false then + // do not attach the toplevel join bellow. + if (shouldAttachDefault) { + querybuilder = querybuilder.leftJoinAndSelect( + `${alias}.${toplevel}`, + toplevel + ) + } for (const rel of rels) { const [_, rest] = rel.split(".") @@ -54,7 +68,7 @@ export async function queryEntityWithIds( .withDeleted() } else { querybuilder = querybuilder.where( - `${alias}.deleted_at IS NULL AND products.id IN (:...entitiesIds)`, + `${alias}.deleted_at IS NULL AND ${alias}.id IN (:...entitiesIds)`, { entitiesIds: entityIds, } @@ -77,14 +91,14 @@ export async function queryEntityWithIds( */ export async function queryEntityWithoutRelations( repository: Repository, - optionsWithoutRelations: FindWithoutRelationsOptions, + optionsWithoutRelations: Omit, "relations">, shouldCount = false, customJoinBuilders: (( qb: SelectQueryBuilder, alias: string ) => void)[] = [] ): Promise<[T[], number]> { - const alias = repository.constructor.name + const alias = repository.metadata.name.toLowerCase() const qb = repository .createQueryBuilder(alias)