feat: convert MikroORM entities to DML entities (#10043)

* feat: convert MikroORM entities to DML entities

* feat: wip on repository changes

* continue repositories and types rework

* fix order repository usage

* continue to update product category repository

* Add foreign key as part of the inferred DML type

* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* cleanup

* re add foerign key indexes

* chore: remove unused types

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* fix ordering

* fix ordering remove logs

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2024-11-26 20:01:02 +05:30
1 parent d6fa912b22
commit 9f204817b0
61 files changed
+2315 -1939

No files matched your search

@@ -1,3 +1,5 @@
import { kebabCase } from "@medusajs/framework/utils"
export const productCategoriesData = [
{
id: "category-0",
@@ -25,7 +27,12 @@ export const productCategoriesData = [
name: "category 1 b 1",
parent_category_id: "category-1-b",
},
]
].map((entry) => {
return {
handle: kebabCase(entry.name),
...entry,
}
})
export const productCategoriesRankData = [
{
@@ -64,7 +71,12 @@ export const productCategoriesRankData = [
parent_category_id: "category-0-0",
rank: 2,
},
]
].map((entry) => {
return {
handle: kebabCase(entry.name),
...entry,
}
})
export const eletronicsCategoriesData = [
{
@@ -188,4 +200,9 @@ export const eletronicsCategoriesData = [
name: "Mini Models",
parent_category_id: "iphones",
},
]
].map((entry) => {
return {
handle: kebabCase(entry.name),
...entry,
}
})
@@ -2,16 +2,19 @@ export const categoriesData = [
{
id: "category-0",
name: "category 0",
handle: "category-0",
parent_category_id: null,
},
{
id: "category-1",
name: "category 1",
handle: "category-1",
parent_category_id: "category-0",
},
{
id: "category-1-a",
name: "category 1 a",
handle: "category-1-a",
parent_category_id: "category-1",
},
]
@@ -1,10 +1,11 @@
import { ProductTypes } from "@medusajs/framework/types"
import { ProductStatus } from "@medusajs/framework/utils"
import { Image } from "@models"
import { ProductStatus, toHandle } from "@medusajs/framework/utils"
import { ProductImage } from "@models"
import faker from "faker"
export const buildProductOnlyData = ({
title,
handle,
description,
subtitle,
is_giftcard,
@@ -14,6 +15,7 @@ export const buildProductOnlyData = ({
status,
}: {
title?: string
handle?: string
description?: string
subtitle?: string
is_giftcard?: boolean
@@ -22,15 +24,17 @@ export const buildProductOnlyData = ({
images?: { id?: string; url: string }[]
status?: ProductStatus
} = {}) => {
title ??= faker.commerce.productName()
return {
title: title ?? faker.commerce.productName(),
title: title as string,
handle: handle ?? toHandle(title!),
description: description ?? faker.commerce.productName(),
subtitle: subtitle ?? faker.commerce.productName(),
is_giftcard: is_giftcard ?? false,
discountable: discountable ?? true,
thumbnail: thumbnail as string,
status: status ?? ProductStatus.PUBLISHED,
images: (images ?? []) as Image[],
images: (images ?? []) as ProductImage[],
}
}
@@ -48,7 +52,7 @@ export const buildProductAndRelationsData = ({
options,
variants,
collection_id,
}: Partial<ProductTypes.CreateProductDTO>) => {
}: Partial<ProductTypes.CreateProductDTO> & { tags: { value: string }[] }) => {
const defaultOptionTitle = "test-option"
const defaultOptionValue = "test-value"
@@ -60,7 +64,7 @@ export const buildProductAndRelationsData = ({
discountable: discountable ?? true,
thumbnail: thumbnail as string,
status: status ?? ProductStatus.PUBLISHED,
images: (images ?? []) as Image[],
images: (images ?? []) as ProductImage[],
type_id,
tags: tags ?? [{ value: "tag-1" }],
collection_id,
@@ -4,6 +4,7 @@ export const productsData = [
{
id: "test-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
tags: [
{
@@ -15,6 +16,7 @@ export const productsData = [
{
id: "test-2",
title: "product",
handle: "product",
status: ProductStatus.PUBLISHED,
tags: [
{
@@ -26,6 +28,7 @@ export const productsData = [
{
id: "test-3",
title: "product 3",
handle: "product-3",
status: ProductStatus.PUBLISHED,
tags: [
{
@@ -1,4 +1,8 @@
import { ProductStatus } from "@medusajs/framework/utils"
import {
kebabCase,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import {
Image,
@@ -10,6 +14,7 @@ import {
} from "@models"
import ProductOption from "../../../src/models/product-option"
import { InferEntityType } from "@medusajs/types"
export * from "./data/create-product"
@@ -24,7 +29,7 @@ export async function createProductAndTags(
}[]
) {
const products: any[] = data.map((productData) => {
return manager.create(Product, productData)
return manager.create(toMikroORMEntity(Product), productData)
})
await manager.persistAndFlush(products)
@@ -42,7 +47,7 @@ export async function createProductAndTypes(
}[]
) {
const products: any[] = data.map((productData) => {
return manager.create(Product, productData)
return manager.create(toMikroORMEntity(Product), productData)
})
await manager.persistAndFlush(products)
@@ -55,7 +60,7 @@ export async function createProductVariants(
data: any[]
) {
const variants: any[] = data.map((variantsData) => {
return manager.create(ProductVariant, variantsData)
return manager.create(toMikroORMEntity(ProductVariant), variantsData)
})
await manager.persistAndFlush(variants)
@@ -72,7 +77,10 @@ export async function createCollections(
}[]
) {
const collections: any[] = collectionData.map((collectionData) => {
return manager.create(ProductCollection, collectionData)
if (!collectionData.handle && collectionData.title) {
collectionData.handle = kebabCase(collectionData.title)
}
return manager.create(toMikroORMEntity(ProductCollection), collectionData)
})
await manager.persistAndFlush(collections)
@@ -88,7 +96,7 @@ export async function createTypes(
}[]
) {
const types: any[] = typesData.map((typesData) => {
return manager.create(ProductType, typesData)
return manager.create(toMikroORMEntity(ProductType), typesData)
})
await manager.persistAndFlush(types)
@@ -112,7 +120,7 @@ export async function createOptions(
}[]
) {
const options: any[] = optionsData.map((option) => {
return manager.create(ProductOption, option)
return manager.create(toMikroORMEntity(ProductOption), option)
})
await manager.persistAndFlush(options)
@@ -125,7 +133,7 @@ export async function createImages(
imagesData: string[]
) {
const images: any[] = imagesData.map((img) => {
return manager.create(Image, { url: img })
return manager.create(toMikroORMEntity(Image), { url: img })
})
await manager.persistAndFlush(images)
@@ -135,8 +143,8 @@ export async function createImages(
export async function assignCategoriesToProduct(
manager: SqlEntityManager,
product: Product,
categories: ProductCategory[]
product: InferEntityType<Product>,
categories: InferEntityType<ProductCategory>[]
) {
product.categories.add(categories)
@@ -175,7 +175,7 @@ moduleIntegrationTestRunner<Service>({
include_descendants_tree: true,
},
{
select: ["id", "handle"],
select: ["id", "handle", "rank"],
}
)
@@ -190,12 +190,14 @@ moduleIntegrationTestRunner<Service>({
mpath: "category-0.category-1.category-1-a",
parent_category_id: "category-1",
category_children: [],
rank: 0,
},
{
id: "category-1-b",
handle: "category-1-b",
mpath: "category-0.category-1.category-1-b",
parent_category_id: "category-1",
rank: 1,
category_children: [
expect.objectContaining({
id: "category-1-b-1",
@@ -220,7 +222,7 @@ moduleIntegrationTestRunner<Service>({
include_ancestors_tree: true,
},
{
select: ["id", "handle"],
select: ["id", "handle", "rank"],
}
)
@@ -235,8 +237,10 @@ moduleIntegrationTestRunner<Service>({
mpath:
"electronics.computers.laptops.gaming-laptops.high-performance.4k-gaming",
parent_category_id: "high-performance",
rank: 1,
parent_category: expect.objectContaining({
id: "high-performance",
rank: 1,
parent_category_id: "gaming-laptops",
handle: "high-performance-gaming-laptops",
mpath:
@@ -245,6 +249,7 @@ moduleIntegrationTestRunner<Service>({
id: "gaming-laptops",
handle: "gaming-laptops",
mpath: "electronics.computers.laptops.gaming-laptops",
rank: 0,
parent_category_id: "laptops",
parent_category: expect.objectContaining({
id: "laptops",
@@ -282,7 +287,7 @@ moduleIntegrationTestRunner<Service>({
include_descendants_tree: true,
},
{
select: ["id", "handle"],
select: ["id", "handle", "rank"],
}
)
@@ -296,6 +301,7 @@ moduleIntegrationTestRunner<Service>({
handle: "gaming-laptops",
mpath: "electronics.computers.laptops.gaming-laptops",
parent_category_id: "laptops",
rank: 0,
category_children: [
expect.objectContaining({
id: "budget-gaming",
@@ -304,6 +310,7 @@ moduleIntegrationTestRunner<Service>({
"electronics.computers.laptops.gaming-laptops.budget-gaming",
parent_category_id: "gaming-laptops",
category_children: [],
rank: 0,
}),
expect.objectContaining({
id: "high-performance",
@@ -311,6 +318,7 @@ moduleIntegrationTestRunner<Service>({
mpath:
"electronics.computers.laptops.gaming-laptops.high-performance",
parent_category_id: "gaming-laptops",
rank: 1,
category_children: expect.arrayContaining([
expect.objectContaining({
id: "vr-ready",
@@ -319,6 +327,7 @@ moduleIntegrationTestRunner<Service>({
"electronics.computers.laptops.gaming-laptops.high-performance.vr-ready",
parent_category_id: "high-performance",
category_children: [],
rank: 0,
}),
expect.objectContaining({
id: "4k-gaming",
@@ -327,6 +336,7 @@ moduleIntegrationTestRunner<Service>({
"electronics.computers.laptops.gaming-laptops.high-performance.4k-gaming",
parent_category_id: "high-performance",
category_children: [],
rank: 1,
}),
]),
}),
@@ -407,7 +417,7 @@ moduleIntegrationTestRunner<Service>({
include_ancestors_tree: true,
},
{
select: ["id", "handle"],
select: ["id", "handle", "rank"],
}
)
@@ -421,12 +431,15 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-a",
mpath: "category-0.category-1.category-1-a",
parent_category_id: "category-1",
rank: 0,
parent_category: expect.objectContaining({
id: "category-1",
handle: "category-1",
mpath: "category-0.category-1",
parent_category_id: "category-0",
rank: 0,
parent_category: expect.objectContaining({
rank: 0,
id: "category-0",
handle: "category-0",
mpath: "category-0",
@@ -440,17 +453,20 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-b",
mpath: "category-0.category-1.category-1-b",
parent_category_id: "category-1",
rank: 1,
parent_category: expect.objectContaining({
id: "category-1",
handle: "category-1",
mpath: "category-0.category-1",
parent_category_id: "category-0",
rank: 0,
parent_category: expect.objectContaining({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
parent_category: null,
rank: 0,
}),
}),
},
@@ -465,7 +481,7 @@ moduleIntegrationTestRunner<Service>({
include_ancestors_tree: true,
},
{
select: ["id", "handle"],
select: ["id", "handle", "rank"],
}
)
@@ -475,17 +491,20 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-a",
mpath: "category-0.category-1.category-1-a",
parent_category_id: "category-1",
rank: 0,
parent_category: expect.objectContaining({
id: "category-1",
handle: "category-1",
mpath: "category-0.category-1",
parent_category_id: "category-0",
rank: 0,
parent_category: expect.objectContaining({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
parent_category: null,
rank: 0,
}),
}),
category_children: [],
@@ -495,17 +514,20 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-b",
mpath: "category-0.category-1.category-1-b",
parent_category_id: "category-1",
rank: 1,
parent_category: expect.objectContaining({
id: "category-1",
handle: "category-1",
mpath: "category-0.category-1",
parent_category_id: "category-0",
rank: 0,
parent_category: expect.objectContaining({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
parent_category: null,
rank: 0,
}),
}),
category_children: [
@@ -514,6 +536,7 @@ moduleIntegrationTestRunner<Service>({
handle: "category-1-b-1",
mpath: "category-0.category-1.category-1-b.category-1-b-1",
parent_category_id: "category-1-b",
rank: 0,
}),
],
},
@@ -879,6 +902,7 @@ moduleIntegrationTestRunner<Service>({
await service.create([
{
name: "New Category",
handle: "new-category",
parent_category_id: null,
},
])
@@ -904,11 +928,13 @@ moduleIntegrationTestRunner<Service>({
await service.create([
{
name: "New Category",
handle: "new-category",
parent_category_id: null,
rank: 0,
},
{
name: "New Category 2",
handle: "new-category-2",
parent_category_id: null,
},
])
@@ -932,6 +958,7 @@ moduleIntegrationTestRunner<Service>({
await service.create([
{
name: "New Category 2.1",
handle: "new-category-2-1",
parent_category_id: productCategoryNew.id,
},
])
@@ -5,6 +5,7 @@ import {
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductCategory } from "@models"
import {
@@ -31,15 +32,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
productTwo = testManager.create(Product, {
productTwo = testManager.create(toMikroORMEntity(Product), {
id: "product-2",
title: "product 2",
handle: "product-2",
status: ProductStatus.PUBLISHED,
})
@@ -5,6 +5,7 @@ import {
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductCollection } from "@models"
import {
@@ -31,15 +32,17 @@ moduleIntegrationTestRunner<IProductModuleService>({
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
productTwo = testManager.create(Product, {
productTwo = testManager.create(toMikroORMEntity(Product), {
id: "product-2",
title: "product 2",
handle: "product-2",
status: ProductStatus.PUBLISHED,
})
@@ -47,11 +50,13 @@ moduleIntegrationTestRunner<IProductModuleService>({
{
id: "test-1",
title: "collection 1",
handle: "collection-1",
products: [productOne],
},
{
id: "test-2",
title: "collection",
handle: "collection",
products: [productTwo],
},
]
@@ -1,5 +1,9 @@
import { IProductModuleService } from "@medusajs/framework/types"
import { Modules, ProductStatus } from "@medusajs/framework/utils"
import {
Modules,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductOption } from "@models"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
@@ -16,25 +20,27 @@ moduleIntegrationTestRunner<IProductModuleService>({
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
productTwo = testManager.create(Product, {
productTwo = testManager.create(toMikroORMEntity(Product), {
id: "product-2",
title: "product 2",
handle: "product-2",
status: ProductStatus.PUBLISHED,
})
optionOne = testManager.create(ProductOption, {
optionOne = testManager.create(toMikroORMEntity(ProductOption), {
id: "option-1",
title: "option 1",
product: productOne,
})
optionTwo = testManager.create(ProductOption, {
optionTwo = testManager.create(toMikroORMEntity(ProductOption), {
id: "option-2",
title: "option 1",
product: productTwo,
@@ -198,7 +204,7 @@ moduleIntegrationTestRunner<IProductModuleService>({
it("should return requested attributes when requested through config", async () => {
const option = await service.retrieveProductOption(optionOne.id, {
select: ["id", "product.title"],
select: ["id", "product.handle", "product.title"],
relations: ["product"],
})
@@ -5,6 +5,7 @@ import {
Modules,
ProductEvents,
ProductStatus,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { Product, ProductTag } from "@models"
import {
@@ -35,25 +36,27 @@ moduleIntegrationTestRunner<IProductModuleService>({
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
productTwo = testManager.create(Product, {
productTwo = testManager.create(toMikroORMEntity(Product), {
id: "product-2",
title: "product 2",
handle: "product-2",
status: ProductStatus.PUBLISHED,
})
tagOne = testManager.create(ProductTag, {
tagOne = testManager.create(toMikroORMEntity(ProductTag), {
id: "tag-1",
value: "tag 1",
products: [productOne],
})
tagTwo = testManager.create(ProductTag, {
tagTwo = testManager.create(toMikroORMEntity(ProductTag), {
id: "tag-2",
value: "tag",
products: [productTwo],
@@ -1,7 +1,7 @@
import { IProductModuleService } from "@medusajs/framework/types"
import { ProductType } from "@models"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { Modules } from "@medusajs/framework/utils"
import { Modules, toMikroORMEntity } from "@medusajs/framework/utils"
jest.setTimeout(30000)
@@ -15,12 +15,12 @@ moduleIntegrationTestRunner<IProductModuleService>({
beforeEach(async () => {
const testManager = await MikroOrmWrapper.forkManager()
typeOne = testManager.create(ProductType, {
typeOne = testManager.create(toMikroORMEntity(ProductType), {
id: "type-1",
value: "type 1",
})
typeTwo = testManager.create(ProductType, {
typeTwo = testManager.create(toMikroORMEntity(ProductType), {
id: "type-2",
value: "type",
})
@@ -125,7 +125,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
productCategoryTwo = categories[1]
productOne = service.createProducts({
id: "product-1",
title: "product 1",
status: ProductStatus.PUBLISHED,
options: [
@@ -136,7 +135,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
],
variants: [
{
id: "variant-1",
title: "variant 1",
options: { "opt-title": "val-1" },
},
@@ -144,12 +142,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
})
productTwo = service.createProducts({
id: "product-2",
title: "product 2",
status: ProductStatus.PUBLISHED,
categories: [{ id: productCategoryOne.id }],
collection_id: productCollectionOne.id,
tags: [{ id: tags[0].id }],
category_ids: [productCategoryOne.id],
tag_ids: [tags[0].id],
options: [
{
title: "size",
@@ -162,7 +159,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
],
variants: [
{
id: "variant-2",
title: "variant 2",
options: {
size: "large",
@@ -170,7 +166,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
},
},
{
id: "variant-3",
title: "variant 3",
options: {
size: "small",
@@ -389,11 +384,11 @@ moduleIntegrationTestRunner<IProductModuleService>({
const existingVariant1 = product.variants.find(
(v) => v.title === "new variant 1"
)
)!
const existingVariant2 = product.variants.find(
(v) => v.title === "new variant 2"
)
)!
await service.upsertProductVariants([
{
@@ -793,10 +788,14 @@ moduleIntegrationTestRunner<IProductModuleService>({
})
it("should do a partial update on the options of a variant successfully", async () => {
const variantToUpdate = productTwo.variants.find(
(variant) => variant.title === "variant 3"
)!
await service.updateProducts(productTwo.id, {
variants: [
{
id: "variant-3",
id: variantToUpdate.id,
options: { size: "small", color: "blue" },
},
],
@@ -7,12 +7,17 @@ import {
createProductVariants,
} from "../__fixtures__/product"
import { IProductModuleService, ProductDTO } from "@medusajs/framework/types"
import {
InferEntityType,
IProductModuleService,
ProductDTO,
} from "@medusajs/framework/types"
import {
Module,
Modules,
ProductStatus,
kebabCase,
toMikroORMEntity,
} from "@medusajs/framework/utils"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
@@ -27,7 +32,7 @@ import {
variantsData,
} from "../__fixtures__/product/data"
jest.setTimeout(30000)
jest.setTimeout(300000)
type Service = IProductModuleService & {
productService_: ProductService
@@ -50,17 +55,19 @@ moduleIntegrationTestRunner<Service>({
service: ProductModuleService,
}).linkable
expect(Object.keys(linkable)).toEqual([
"product",
"productVariant",
"productOption",
"productOptionValue",
"productType",
"productImage",
"productTag",
"productCollection",
"productCategory",
])
expect(Object.keys(linkable)).toHaveLength(8)
expect(Object.keys(linkable)).toEqual(
expect.arrayContaining([
"product",
"productVariant",
"productOption",
"productOptionValue",
"productType",
"productTag",
"productCollection",
"productCategory",
])
)
Object.keys(linkable).forEach((key) => {
delete linkable[key].toJSON
@@ -119,15 +126,6 @@ moduleIntegrationTestRunner<Service>({
field: "productType",
},
},
productImage: {
id: {
linkable: "product_image_id",
entity: "ProductImage",
primaryKey: "id",
serviceName: "product",
field: "productImage",
},
},
productTag: {
id: {
linkable: "product_tag_id",
@@ -160,16 +158,17 @@ moduleIntegrationTestRunner<Service>({
describe("Product Service", () => {
let testManager: SqlEntityManager
let products!: Product[]
let productOne: Product
let categories!: ProductCategory[]
let products!: InferEntityType<typeof Product>[]
let productOne: InferEntityType<typeof Product>
let categories!: InferEntityType<typeof ProductCategory>[]
describe("retrieve", () => {
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
@@ -243,9 +242,10 @@ moduleIntegrationTestRunner<Service>({
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
productOne = testManager.create(Product, {
productOne = testManager.create(toMikroORMEntity(Product), {
id: "product-1",
title: "product 1",
handle: "product-1",
status: ProductStatus.PUBLISHED,
})
@@ -435,23 +435,23 @@ moduleIntegrationTestRunner<Service>({
})
describe("relation: categories", () => {
let workingProduct: Product
let workingCategory: ProductCategory
let workingProduct: InferEntityType<typeof Product>
let workingCategory: InferEntityType<typeof ProductCategory>
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
products = await createProductAndTags(testManager, productsData)
workingProduct = products.find((p) => p.id === "test-1") as Product
workingProduct = products.find((p) => p.id === "test-1")!
categories = []
for (const entry of categoriesData) {
categories.push((await categoryService.create([entry]))[0])
}
workingCategory = (await testManager.findOne(
ProductCategory,
toMikroORMEntity(ProductCategory),
"category-1"
)) as ProductCategory
))!
workingProduct = await assignCategoriesToProduct(
testManager,
@@ -477,9 +477,7 @@ moduleIntegrationTestRunner<Service>({
}
)
const product = products.find(
(p) => p.id === workingProduct.id
) as unknown as Product
const product = products.find((p) => p.id === workingProduct.id)!
expect(product).toEqual(
expect.objectContaining({
@@ -534,32 +532,34 @@ moduleIntegrationTestRunner<Service>({
})
describe("relation: collections", () => {
let workingProduct: Product
let workingProductTwo: Product
let workingCollection: ProductCollection
let workingCollectionTwo: ProductCollection
let workingProduct: InferEntityType<typeof Product>
let workingProductTwo: InferEntityType<typeof Product>
let workingCollection: InferEntityType<typeof ProductCollection>
let workingCollectionTwo: InferEntityType<typeof ProductCollection>
const collectionData = [
{
id: "test-1",
title: "col 1",
handle: "col-1",
},
{
id: "test-2",
title: "col 2",
handle: "col-2",
},
]
beforeEach(async () => {
testManager = await MikroOrmWrapper.forkManager()
await createCollections(testManager, collectionData)
workingCollection = (await testManager.findOne(
ProductCollection,
workingCollection = await testManager.findOne(
toMikroORMEntity(ProductCollection),
"test-1"
)) as ProductCollection
)
workingCollectionTwo = (await testManager.findOne(
ProductCollection,
toMikroORMEntity(ProductCollection),
"test-2"
)) as ProductCollection
))!
products = await createProductAndTags(testManager, [
{
@@ -575,10 +575,8 @@ moduleIntegrationTestRunner<Service>({
},
])
workingProduct = products.find((p) => p.id === "test-1") as Product
workingProductTwo = products.find(
(p) => p.id === "test-2"
) as Product
workingProduct = products.find((p) => p.id === "test-1")!
workingProductTwo = products.find((p) => p.id === "test-2")!
})
it("should filter by collection relation and scope fields", async () => {
@@ -588,7 +586,12 @@ moduleIntegrationTestRunner<Service>({
collection_id: workingCollection.id,
},
{
select: ["title", "collection.title"],
select: [
"title",
"handle",
"collection.title",
"collection.handle",
],
relations: ["collection"],
}
)
@@ -618,7 +621,12 @@ moduleIntegrationTestRunner<Service>({
collection_id: [workingCollection.id, workingCollectionTwo.id],
},
{
select: ["title", "collection.title"],
select: [
"title",
"handle",
"collection.title",
"collection.handle",
],
relations: ["collection"],
}
)