hotfix(medusa): Optimize product retrieval (#185)
This commit is contained in:
@@ -11,7 +11,8 @@ const eventBusService = {
|
||||
describe("ProductService", () => {
|
||||
describe("retrieve", () => {
|
||||
const productRepo = MockRepository({
|
||||
findOne: () => Promise.resolve({ id: IdMap.getId("ironman") }),
|
||||
findOneWithRelations: () =>
|
||||
Promise.resolve({ id: IdMap.getId("ironman") }),
|
||||
})
|
||||
const productService = new ProductService({
|
||||
manager: MockManager,
|
||||
@@ -25,8 +26,8 @@ describe("ProductService", () => {
|
||||
it("successfully retrieves a product", async () => {
|
||||
const result = await productService.retrieve(IdMap.getId("ironman"))
|
||||
|
||||
expect(productRepo.findOne).toHaveBeenCalledTimes(1)
|
||||
expect(productRepo.findOne).toHaveBeenCalledWith({
|
||||
expect(productRepo.findOneWithRelations).toHaveBeenCalledTimes(1)
|
||||
expect(productRepo.findOneWithRelations).toHaveBeenCalledWith(undefined, {
|
||||
where: { id: IdMap.getId("ironman") },
|
||||
})
|
||||
|
||||
@@ -42,7 +43,7 @@ describe("ProductService", () => {
|
||||
options: [],
|
||||
collection: { id: IdMap.getId("cat"), title: "Suits" },
|
||||
}),
|
||||
findOne: () => ({
|
||||
findOneWithRelations: () => ({
|
||||
id: IdMap.getId("ironman"),
|
||||
title: "Suit",
|
||||
options: [],
|
||||
@@ -137,7 +138,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("update", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query => {
|
||||
findOneWithRelations: (rels, query) => {
|
||||
if (query.where.id === IdMap.getId("ironman&co")) {
|
||||
return Promise.resolve({
|
||||
id: IdMap.getId("ironman&co"),
|
||||
@@ -322,7 +323,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("addOption", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query =>
|
||||
findOneWithRelations: query =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("ironman"),
|
||||
options: [{ title: "Color" }],
|
||||
@@ -395,7 +396,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("reorderVariants", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query =>
|
||||
findOneWithRelations: query =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("ironman"),
|
||||
variants: [{ id: IdMap.getId("green") }, { id: IdMap.getId("blue") }],
|
||||
@@ -453,7 +454,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("reorderOptions", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query =>
|
||||
findOneWithRelations: query =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("ironman"),
|
||||
options: [
|
||||
@@ -519,7 +520,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("updateOption", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query =>
|
||||
findOneWithRelations: query =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("ironman"),
|
||||
options: [
|
||||
@@ -594,7 +595,7 @@ describe("ProductService", () => {
|
||||
|
||||
describe("deleteOption", () => {
|
||||
const productRepository = MockRepository({
|
||||
findOne: query =>
|
||||
findOneWithRelations: query =>
|
||||
Promise.resolve({
|
||||
id: IdMap.getId("ironman"),
|
||||
variants: [
|
||||
|
||||
@@ -93,6 +93,17 @@ class ProductService extends BaseService {
|
||||
|
||||
const query = this.buildQuery_(selector, config)
|
||||
|
||||
if (config.relations && config.relations.length > 0) {
|
||||
query.relations = config.relations
|
||||
}
|
||||
|
||||
if (config.select && config.select.length > 0) {
|
||||
query.select = config.select
|
||||
}
|
||||
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
|
||||
if (q) {
|
||||
const where = query.where
|
||||
|
||||
@@ -122,7 +133,7 @@ class ProductService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
return productRepo.find(query)
|
||||
return productRepo.findWithRelations(rels, query)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,20 +154,33 @@ class ProductService extends BaseService {
|
||||
* @return {Promise<Product>} the result of the find one operation.
|
||||
*/
|
||||
async retrieve(productId, config = {}) {
|
||||
return this.atomicPhase_(async manager => {
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
const validatedId = this.validateId_(productId)
|
||||
const query = this.buildQuery_({ id: validatedId }, config)
|
||||
const product = await productRepo.findOne(query)
|
||||
if (!product) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product with id: ${productId} was not found`
|
||||
)
|
||||
}
|
||||
const productRepo = this.manager_.getCustomRepository(
|
||||
this.productRepository_
|
||||
)
|
||||
const validatedId = this.validateId_(productId)
|
||||
|
||||
return product
|
||||
})
|
||||
const query = { where: { id: validatedId } }
|
||||
|
||||
if (config.relations && config.relations.length > 0) {
|
||||
query.relations = config.relations
|
||||
}
|
||||
|
||||
if (config.select && config.select.length > 0) {
|
||||
query.select = config.select
|
||||
}
|
||||
|
||||
const rels = query.relations
|
||||
delete query.relations
|
||||
const product = await productRepo.findOneWithRelations(rels, query)
|
||||
|
||||
if (!product) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Product with id: ${productId} was not found`
|
||||
)
|
||||
}
|
||||
|
||||
return product
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user