fix: performant relations
This commit is contained in:
1 parent
c2a075a6b1
commit
5659d106e3
5 files changed
+914
-31
No files matched your search
+1
-1
@@ -24,7 +24,7 @@
|
|||||||
"pg-god": "^1.0.11",
|
"pg-god": "^1.0.11",
|
||||||
"prettier": "^2.1.1",
|
"prettier": "^2.1.1",
|
||||||
"resolve-cwd": "^3.0.0",
|
"resolve-cwd": "^3.0.0",
|
||||||
"typeorm": "^0.2.30"
|
"typeorm": "^0.2.31"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"publish:next": "lerna publish --canary --preid next --dist-tag next",
|
"publish:next": "lerna publish --canary --preid next --dist-tag next",
|
||||||
|
|||||||
@@ -10,28 +10,22 @@ export class OrderRepository extends Repository<Order> {
|
|||||||
): Promise<Order[]> {
|
): Promise<Order[]> {
|
||||||
const entities = await this.find(optionsWithoutRelations)
|
const entities = await this.find(optionsWithoutRelations)
|
||||||
const entitiesIds = entities.map(({ id }) => id)
|
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(
|
const entitiesIdsWithRelations = await Promise.all(
|
||||||
relations.map(relation => {
|
Object.entries(groupedRelations).map(([_, rels]) => {
|
||||||
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[]
|
|
||||||
)
|
|
||||||
|
|
||||||
return this.findByIds(entitiesIds, {
|
return this.findByIds(entitiesIds, {
|
||||||
select: ["id"],
|
select: ["id"],
|
||||||
relations: partialRelations,
|
relations: rels as string[],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
).then(flatten)
|
).then(flatten)
|
||||||
@@ -47,6 +41,9 @@ export class OrderRepository extends Repository<Order> {
|
|||||||
relations: Array<keyof Order> = [],
|
relations: Array<keyof Order> = [],
|
||||||
optionsWithoutRelations: Omit<FindManyOptions<Order>, "relations"> = {}
|
optionsWithoutRelations: Omit<FindManyOptions<Order>, "relations"> = {}
|
||||||
): Promise<Order> {
|
): Promise<Order> {
|
||||||
|
// Limit 1
|
||||||
|
optionsWithoutRelations.take = 1
|
||||||
|
|
||||||
const result = await this.findWithRelations(
|
const result = await this.findWithRelations(
|
||||||
relations,
|
relations,
|
||||||
optionsWithoutRelations
|
optionsWithoutRelations
|
||||||
|
|||||||
@@ -328,7 +328,9 @@ class OrderService extends BaseService {
|
|||||||
query.select = select
|
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) {
|
if (!raw) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
|
|||||||
+890
-6
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"
|
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
|
||||||
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
|
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
|
||||||
|
|
||||||
typeorm@^0.2.30:
|
typeorm@^0.2.31:
|
||||||
version "0.2.30"
|
version "0.2.31"
|
||||||
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.30.tgz#a0df2256402cbcdde8049a244437560495ce9b38"
|
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.31.tgz#82b8a1b233224f81c738f53b0380386ccf360917"
|
||||||
integrity sha512-qpr8AO3Phi6ZF7qMHOrRdNisVt8jE1KfmW0ooLFcXscA87aJ12aBPyB9cJfxGNjNwd7B3WIK9ZlBveWiqd74QA==
|
integrity sha512-dVvCEVHH48DG0QPXAKfo0l6ecQrl3A8ucGP4Yw4myz4YEDMProebTQo8as83uyES+nrwCbu3qdkL4ncC2+qcMA==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@sqltools/formatter" "1.2.2"
|
"@sqltools/formatter" "1.2.2"
|
||||||
app-root-path "^3.0.0"
|
app-root-path "^3.0.0"
|
||||||
|
|||||||
Reference in new issue
Block a user