hotfix(medusa): Efficient product querying
This commit is contained in:
@@ -6,9 +6,17 @@ import { Product } from "../models/product"
|
|||||||
export class ProductRepository extends Repository<Product> {
|
export class ProductRepository extends Repository<Product> {
|
||||||
public async findWithRelations(
|
public async findWithRelations(
|
||||||
relations: Array<keyof Product> = [],
|
relations: Array<keyof Product> = [],
|
||||||
optionsWithoutRelations: Omit<FindManyOptions<Product>, "relations"> = {}
|
idsOrOptionsWithoutRelations: Omit<
|
||||||
|
FindManyOptions<Product>,
|
||||||
|
"relations"
|
||||||
|
> = {}
|
||||||
): Promise<Product[]> {
|
): Promise<Product[]> {
|
||||||
const entities = await this.find(optionsWithoutRelations)
|
let entities
|
||||||
|
if (Array.isArray(idsOrOptionsWithoutRelations)) {
|
||||||
|
entities = await this.findByIds(idsOrOptionsWithoutRelations)
|
||||||
|
} else {
|
||||||
|
entities = await this.find(idsOrOptionsWithoutRelations)
|
||||||
|
}
|
||||||
const entitiesIds = entities.map(({ id }) => id)
|
const entitiesIds = entities.map(({ id }) => id)
|
||||||
|
|
||||||
const groupedRelations = {}
|
const groupedRelations = {}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class ProductService extends BaseService {
|
|||||||
* @param {Object} listOptions - the query object for find
|
* @param {Object} listOptions - the query object for find
|
||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
list(selector = {}, config = { relations: [], skip: 0, take: 20 }) {
|
async list(selector = {}, config = { relations: [], skip: 0, take: 20 }) {
|
||||||
const productRepo = this.manager_.getCustomRepository(
|
const productRepo = this.manager_.getCustomRepository(
|
||||||
this.productRepository_
|
this.productRepository_
|
||||||
)
|
)
|
||||||
@@ -101,7 +101,7 @@ class ProductService extends BaseService {
|
|||||||
query.select = config.select
|
query.select = config.select
|
||||||
}
|
}
|
||||||
|
|
||||||
const rels = query.relations
|
let rels = query.relations
|
||||||
delete query.relations
|
delete query.relations
|
||||||
|
|
||||||
if (q) {
|
if (q) {
|
||||||
@@ -110,27 +110,28 @@ class ProductService extends BaseService {
|
|||||||
delete where.description
|
delete where.description
|
||||||
delete where.title
|
delete where.title
|
||||||
|
|
||||||
query.join = {
|
const raw = await productRepo
|
||||||
alias: "product",
|
.createQueryBuilder("product")
|
||||||
leftJoinAndSelect: {
|
.leftJoinAndSelect("product.variants", "variant")
|
||||||
variant: "product.variants",
|
.leftJoinAndSelect("product.collection", "collection")
|
||||||
collection: "product.collection",
|
.select(["product.id"])
|
||||||
},
|
.where(where)
|
||||||
}
|
.andWhere(
|
||||||
|
|
||||||
query.where = qb => {
|
|
||||||
qb.where(where)
|
|
||||||
|
|
||||||
qb.andWhere(
|
|
||||||
new Brackets(qb => {
|
new Brackets(qb => {
|
||||||
qb.where(`product.title ILIKE :q`, { q: `%${q}%` })
|
qb.where(`product.description ILIKE :q`, { q: `%${q}%` })
|
||||||
|
.orWhere(`product.title ILIKE :q`, { q: `%${q}%` })
|
||||||
.orWhere(`product.description ILIKE :q`, { q: `%${q}%` })
|
.orWhere(`product.description ILIKE :q`, { q: `%${q}%` })
|
||||||
.orWhere(`variant.title ILIKE :q`, { q: `%${q}%` })
|
.orWhere(`variant.title ILIKE :q`, { q: `%${q}%` })
|
||||||
.orWhere(`variant.sku ILIKE :q`, { q: `%${q}%` })
|
.orWhere(`variant.sku ILIKE :q`, { q: `%${q}%` })
|
||||||
.orWhere(`collection.title ILIKE :q`, { q: `%${q}%` })
|
.orWhere(`collection.title ILIKE :q`, { q: `%${q}%` })
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
.getMany()
|
||||||
|
|
||||||
|
return productRepo.findWithRelations(
|
||||||
|
rels,
|
||||||
|
raw.map(i => i.id)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return productRepo.findWithRelations(rels, query)
|
return productRepo.findWithRelations(rels, query)
|
||||||
|
|||||||
Reference in New Issue
Block a user