fix(medusa): Repository util mention of entity specifics (#2965)
This commit is contained in:
1 parent
32b038fc3f
commit
28bec599ae
2 files changed
+32
-13
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: Repository util mention of entity specifics
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import { flatten, groupBy, map, merge } from "lodash"
|
import { flatten, groupBy, map, merge } from "lodash"
|
||||||
import { EntityMetadata, Repository, SelectQueryBuilder } from "typeorm"
|
import { EntityMetadata, Repository, SelectQueryBuilder } from "typeorm"
|
||||||
import { FindWithoutRelationsOptions } from "../repositories/customer-group"
|
import { ExtendedFindConfig } from "../types/common"
|
||||||
|
|
||||||
// TODO: All the utilities except applyOrdering needs to be re worked depending on the outcome of the product repository
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom query entity, it is part of the creation of a custom findWithRelationsAndCount needs.
|
* 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 groupedRelations
|
||||||
* @param withDeleted
|
* @param withDeleted
|
||||||
* @param select
|
* @param select
|
||||||
|
* @param customJoinBuilders
|
||||||
*/
|
*/
|
||||||
export async function queryEntityWithIds<T>(
|
export async function queryEntityWithIds<T>(
|
||||||
repository: Repository<T>,
|
repository: Repository<T>,
|
||||||
entityIds: string[],
|
entityIds: string[],
|
||||||
groupedRelations: { [toplevel: string]: string[] },
|
groupedRelations: { [toplevel: string]: string[] },
|
||||||
withDeleted = false,
|
withDeleted = false,
|
||||||
select: (keyof T)[] = []
|
select: (keyof T)[] = [],
|
||||||
|
customJoinBuilders: ((
|
||||||
|
qb: SelectQueryBuilder<T>,
|
||||||
|
alias: string,
|
||||||
|
toplevel: string
|
||||||
|
) => boolean)[] = []
|
||||||
): Promise<T[]> {
|
): Promise<T[]> {
|
||||||
const alias = repository.constructor.name
|
const alias = repository.metadata.name.toLowerCase()
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
Object.entries(groupedRelations).map(async ([toplevel, rels]) => {
|
Object.entries(groupedRelations).map(async ([toplevel, rels]) => {
|
||||||
let querybuilder = repository.createQueryBuilder(`${alias}`)
|
let querybuilder = repository.createQueryBuilder(alias)
|
||||||
|
|
||||||
if (select && select.length) {
|
if (select && select.length) {
|
||||||
querybuilder.select(select.map((f) => `${alias}.${f as string}`))
|
querybuilder.select(select.map((f) => `${alias}.${f as string}`))
|
||||||
}
|
}
|
||||||
|
|
||||||
querybuilder = querybuilder.leftJoinAndSelect(
|
let shouldAttachDefault = true
|
||||||
`${alias}.${toplevel}`,
|
for (const customJoinBuilder of customJoinBuilders) {
|
||||||
toplevel
|
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) {
|
for (const rel of rels) {
|
||||||
const [_, rest] = rel.split(".")
|
const [_, rest] = rel.split(".")
|
||||||
@@ -54,7 +68,7 @@ export async function queryEntityWithIds<T>(
|
|||||||
.withDeleted()
|
.withDeleted()
|
||||||
} else {
|
} else {
|
||||||
querybuilder = querybuilder.where(
|
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,
|
entitiesIds: entityIds,
|
||||||
}
|
}
|
||||||
@@ -77,14 +91,14 @@ export async function queryEntityWithIds<T>(
|
|||||||
*/
|
*/
|
||||||
export async function queryEntityWithoutRelations<T>(
|
export async function queryEntityWithoutRelations<T>(
|
||||||
repository: Repository<T>,
|
repository: Repository<T>,
|
||||||
optionsWithoutRelations: FindWithoutRelationsOptions,
|
optionsWithoutRelations: Omit<ExtendedFindConfig<T, unknown>, "relations">,
|
||||||
shouldCount = false,
|
shouldCount = false,
|
||||||
customJoinBuilders: ((
|
customJoinBuilders: ((
|
||||||
qb: SelectQueryBuilder<T>,
|
qb: SelectQueryBuilder<T>,
|
||||||
alias: string
|
alias: string
|
||||||
) => void)[] = []
|
) => void)[] = []
|
||||||
): Promise<[T[], number]> {
|
): Promise<[T[], number]> {
|
||||||
const alias = repository.constructor.name
|
const alias = repository.metadata.name.toLowerCase()
|
||||||
|
|
||||||
const qb = repository
|
const qb = repository
|
||||||
.createQueryBuilder(alias)
|
.createQueryBuilder(alias)
|
||||||
|
|||||||
Reference in new issue
Block a user