fix: performant relations
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
"pg-god": "^1.0.11",
|
||||
"prettier": "^2.1.1",
|
||||
"resolve-cwd": "^3.0.0",
|
||||
"typeorm": "^0.2.30"
|
||||
"typeorm": "^0.2.31"
|
||||
},
|
||||
"scripts": {
|
||||
"publish:next": "lerna publish --canary --preid next --dist-tag next",
|
||||
|
||||
@@ -10,28 +10,22 @@ export class OrderRepository extends Repository<Order> {
|
||||
): Promise<Order[]> {
|
||||
const entities = await this.find(optionsWithoutRelations)
|
||||
const entitiesIds = entities.map(({ id }) => id)
|
||||
|
||||
const groupedRelations = {}
|
||||
for (const rel of relations) {
|
||||
const [topLevel] = rel.split(".")
|
||||
if (groupedRelations[topLevel]) {
|
||||
groupedRelations[topLevel].push(rel)
|
||||
} else {
|
||||
groupedRelations[topLevel] = [rel]
|
||||
}
|
||||
}
|
||||
|
||||
const entitiesIdsWithRelations = await Promise.all(
|
||||
relations.map(relation => {
|
||||
const relationParts = relation.split(".") as string[]
|
||||
|
||||
const partialRelations = relationParts.reduce(
|
||||
(acc: string[], _: string, index: number) => {
|
||||
const toPush = []
|
||||
|
||||
for (let i = 0; i <= index; i++) {
|
||||
toPush.push(relationParts[i])
|
||||
}
|
||||
|
||||
acc.push(toPush.join("."))
|
||||
|
||||
return acc
|
||||
},
|
||||
[] as string[]
|
||||
)
|
||||
|
||||
Object.entries(groupedRelations).map(([_, rels]) => {
|
||||
return this.findByIds(entitiesIds, {
|
||||
select: ["id"],
|
||||
relations: partialRelations,
|
||||
relations: rels as string[],
|
||||
})
|
||||
})
|
||||
).then(flatten)
|
||||
@@ -47,6 +41,9 @@ export class OrderRepository extends Repository<Order> {
|
||||
relations: Array<keyof Order> = [],
|
||||
optionsWithoutRelations: Omit<FindManyOptions<Order>, "relations"> = {}
|
||||
): Promise<Order> {
|
||||
// Limit 1
|
||||
optionsWithoutRelations.take = 1
|
||||
|
||||
const result = await this.findWithRelations(
|
||||
relations,
|
||||
optionsWithoutRelations
|
||||
|
||||
@@ -328,7 +328,9 @@ class OrderService extends BaseService {
|
||||
query.select = select
|
||||
}
|
||||
|
||||
const raw = await orderRepo.findOneWithRelations(query.relations, query)
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
const raw = await orderRepo.findOneWithRelations(rels, query)
|
||||
|
||||
if (!raw) {
|
||||
throw new MedusaError(
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -9222,10 +9222,10 @@ typedarray@^0.0.6:
|
||||
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
|
||||
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
|
||||
|
||||
typeorm@^0.2.30:
|
||||
version "0.2.30"
|
||||
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.30.tgz#a0df2256402cbcdde8049a244437560495ce9b38"
|
||||
integrity sha512-qpr8AO3Phi6ZF7qMHOrRdNisVt8jE1KfmW0ooLFcXscA87aJ12aBPyB9cJfxGNjNwd7B3WIK9ZlBveWiqd74QA==
|
||||
typeorm@^0.2.31:
|
||||
version "0.2.31"
|
||||
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.31.tgz#82b8a1b233224f81c738f53b0380386ccf360917"
|
||||
integrity sha512-dVvCEVHH48DG0QPXAKfo0l6ecQrl3A8ucGP4Yw4myz4YEDMProebTQo8as83uyES+nrwCbu3qdkL4ncC2+qcMA==
|
||||
dependencies:
|
||||
"@sqltools/formatter" "1.2.2"
|
||||
app-root-path "^3.0.0"
|
||||
|
||||
Reference in New Issue
Block a user