feat(modules-sdk): Remote Query (#4463)

* feat: Remote Query
This commit is contained in:
Carlos R. L. Rodrigues
2023-07-19 15:35:36 -03:00
committed by GitHub
parent 95c538c675
commit 5a8a889c6d
57 changed files with 1286 additions and 423 deletions
@@ -1,12 +1,12 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductCategoryService } from "@services"
import { ProductCategoryRepository } from "@repositories"
import { ProductCategory } from "@models"
import { ProductCategoryRepository } from "@repositories"
import { ProductCategoryService } from "@services"
import { TestDatabase } from "../../../utils"
import { createProductCategories } from "../../../__fixtures__/product-category"
import { productCategoriesData } from "../../../__fixtures__/product-category/data"
import { TestDatabase } from "../../../utils"
jest.setTimeout(30000)
@@ -1,11 +1,11 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductCollection } from "@models"
import { ProductCollectionService } from "@services"
import { ProductCollectionRepository } from "@repositories"
import { ProductCollectionService } from "@services"
import { TestDatabase } from "../../../utils"
import { createCollections } from "../../../__fixtures__/product"
import { TestDatabase } from "../../../utils"
jest.setTimeout(30000)
@@ -1,12 +1,12 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductTagService } from "@services"
import { ProductTagRepository } from "@repositories"
import { Product } from "@models"
import { ProductTagRepository } from "@repositories"
import { ProductTagService } from "@services"
import { TestDatabase } from "../../../utils"
import { createProductAndTags } from "../../../__fixtures__/product"
import { ProductTypes } from "@medusajs/types"
import { createProductAndTags } from "../../../__fixtures__/product"
import { TestDatabase } from "../../../utils"
jest.setTimeout(30000)
@@ -280,18 +280,15 @@ describe("ProductVariant Service", () => {
expect.objectContaining({
id: productVariantTestOne,
title: "variant 1",
}),
})
)
})
it("should return requested attributes when requested through config", async () => {
const result = await service.retrieve(
variantOne.id,
{
select: ["id", "title", "product.title"] as any,
relations: ["product"],
}
)
const result = await service.retrieve(variantOne.id, {
select: ["id", "title", "product.title"] as any,
relations: ["product"],
})
expect(result).toEqual(
expect.objectContaining({
@@ -302,7 +299,7 @@ describe("ProductVariant Service", () => {
id: "product-1",
title: "product 1",
}),
}),
})
)
})
@@ -315,7 +312,9 @@ describe("ProductVariant Service", () => {
error = e
}
expect(error.message).toEqual("ProductVariant with id: does-not-exist was not found")
expect(error.message).toEqual(
"ProductVariant with id: does-not-exist was not found"
)
})
it("should throw an error when an id is not provided", async () => {
@@ -1,11 +1,4 @@
import { TestDatabase } from "../../../utils"
import { ProductService } from "@services"
import { ProductRepository } from "@repositories"
import { Image, Product, ProductCategory, ProductVariant } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { ProductDTO } from "@medusajs/types"
import { createProductCategories } from "../../../__fixtures__/product-category"
import {
assignCategoriesToProduct,
createImages,
@@ -17,7 +10,14 @@ import {
productsData,
variantsData,
} from "../../../__fixtures__/product/data"
import { ProductDTO } from "@medusajs/types"
import { ProductRepository } from "@repositories"
import { ProductService } from "@services"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { TestDatabase } from "../../../utils"
import { buildProductOnlyData } from "../../../__fixtures__/product/data/create-product"
import { createProductCategories } from "../../../__fixtures__/product-category"
import { kebabCase } from "@medusajs/utils"
jest.setTimeout(30000)
@@ -0,0 +1,6 @@
import { ProductServiceInitializeOptions } from "../../src/types"
export const databaseOptions: ProductServiceInitializeOptions["database"] = {
schema: "public",
clientUrl: "medusa-products-test",
}
-1
View File
@@ -37,7 +37,6 @@
},
"devDependencies": {
"@mikro-orm/cli": "5.7.12",
"@mikro-orm/migrations": "5.7.12",
"cross-env": "^5.2.1",
"faker": "^6.6.6",
"jest": "^25.5.4",
+2 -2
View File
@@ -18,7 +18,7 @@ export const initialize = async (
): Promise<IProductModuleService> => {
const serviceKey = Modules.PRODUCT
const loaded = await MedusaModule.bootstrap(
const loaded = await MedusaModule.bootstrap<IProductModuleService>(
serviceKey,
MODULE_PACKAGE_NAMES[Modules.PRODUCT],
options as InternalModuleDeclaration | ExternalModuleDeclaration,
@@ -26,5 +26,5 @@ export const initialize = async (
injectedDependencies
)
return loaded[serviceKey] as IProductModuleService
return loaded[serviceKey]
}
+99
View File
@@ -0,0 +1,99 @@
import { Modules } from "@medusajs/modules-sdk"
import { JoinerServiceConfig } from "@medusajs/types"
export const joinerConfig: JoinerServiceConfig = {
serviceName: Modules.PRODUCT,
primaryKeys: ["id", "handle"],
alias: [
{
name: "product",
},
{
name: "products",
},
{
name: "variant",
args: {
methodSuffix: "Variants",
},
},
{
name: "variants",
args: {
methodSuffix: "Variants",
},
},
{
name: "product_option",
args: {
methodSuffix: "Options",
},
},
{
name: "product_options",
args: {
methodSuffix: "Options",
},
},
{
name: "product_type",
args: {
methodSuffix: "Types",
},
},
{
name: "product_types",
args: {
methodSuffix: "Types",
},
},
{
name: "product_image",
args: {
methodSuffix: "Images",
},
},
{
name: "product_images",
args: {
methodSuffix: "Images",
},
},
{
name: "product_tag",
args: {
methodSuffix: "Tags",
},
},
{
name: "product_tags",
args: {
methodSuffix: "Tags",
},
},
{
name: "product_collection",
args: {
methodSuffix: "Collections",
},
},
{
name: "product_collections",
args: {
methodSuffix: "Collections",
},
},
{
name: "product_category",
args: {
methodSuffix: "Categories",
},
},
{
name: "product_categories",
args: {
methodSuffix: "Categories",
},
},
],
}
@@ -54,7 +54,6 @@ async function loadDefault({ database, container }) {
}
const entities = Object.values(ProductModels) as unknown as EntitySchema[]
const orm = await createConnection(database, entities)
container.register({
+15 -14
View File
@@ -1,18 +1,5 @@
import { LoaderOptions } from "@medusajs/modules-sdk"
import { asClass } from "awilix"
import {
ProductCategoryService,
ProductCollectionService,
ProductImageService,
ProductModuleService,
ProductOptionService,
ProductService,
ProductTagService,
ProductTypeService,
ProductVariantService,
} from "@services"
import * as DefaultRepositories from "@repositories"
import {
BaseRepository,
ProductCategoryRepository,
@@ -25,6 +12,20 @@ import {
ProductVariantRepository,
} from "@repositories"
import { Constructor, DAL, ModulesSdkTypes } from "@medusajs/types"
import {
ProductCategoryService,
ProductCollectionService,
ProductImageService,
ProductModuleService,
ProductOptionService,
ProductService,
ProductTagService,
ProductTypeService,
ProductVariantService,
} from "@services"
import { LoaderOptions } from "@medusajs/modules-sdk"
import { asClass } from "awilix"
import { lowerCaseFirst } from "@medusajs/utils"
export default async ({
@@ -1,7 +1,5 @@
{
"namespaces": [
"public"
],
"namespaces": ["public"],
"name": "public",
"tables": [
{
@@ -117,27 +115,21 @@
"indexes": [
{
"keyName": "IDX_product_category_path",
"columnNames": [
"mpath"
],
"columnNames": ["mpath"],
"composite": false,
"primary": false,
"unique": false
},
{
"keyName": "IDX_product_category_handle",
"columnNames": [
"handle"
],
"columnNames": ["handle"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "product_category_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -147,13 +139,9 @@
"foreignKeys": {
"product_category_parent_category_id_foreign": {
"constraintName": "product_category_parent_category_id_foreign",
"columnNames": [
"parent_category_id"
],
"columnNames": ["parent_category_id"],
"localTableName": "public.product_category",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_category",
"deleteRule": "set null",
"updateRule": "cascade"
@@ -213,9 +201,7 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_collection_deleted_at",
"primary": false,
@@ -223,18 +209,14 @@
},
{
"keyName": "IDX_product_collection_handle_unique",
"columnNames": [
"handle"
],
"columnNames": ["handle"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "product_collection_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -287,18 +269,14 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"url"
],
"columnNames": ["url"],
"composite": false,
"keyName": "IDX_product_image_url",
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_image_deleted_at",
"primary": false,
@@ -306,9 +284,7 @@
},
{
"keyName": "image_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -361,9 +337,7 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_tag_deleted_at",
"primary": false,
@@ -371,9 +345,7 @@
},
{
"keyName": "product_tag_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -426,9 +398,7 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_type_deleted_at",
"primary": false,
@@ -436,9 +406,7 @@
},
{
"keyName": "product_type_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -511,12 +479,7 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"enumItems": [
"draft",
"proposed",
"published",
"rejected"
],
"enumItems": ["draft", "proposed", "published", "rejected"],
"mappedType": "enum"
},
"thumbnail": {
@@ -681,18 +644,14 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"type_id"
],
"columnNames": ["type_id"],
"composite": false,
"keyName": "IDX_product_type_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_deleted_at",
"primary": false,
@@ -700,18 +659,14 @@
},
{
"keyName": "IDX_product_handle_unique",
"columnNames": [
"handle"
],
"columnNames": ["handle"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "product_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -721,26 +676,18 @@
"foreignKeys": {
"product_collection_id_foreign": {
"constraintName": "product_collection_id_foreign",
"columnNames": [
"collection_id"
],
"columnNames": ["collection_id"],
"localTableName": "public.product",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_collection",
"deleteRule": "set null",
"updateRule": "cascade"
},
"product_type_id_foreign": {
"constraintName": "product_type_id_foreign",
"columnNames": [
"type_id"
],
"columnNames": ["type_id"],
"localTableName": "public.product",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_type",
"deleteRule": "set null",
"updateRule": "cascade"
@@ -800,18 +747,14 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"composite": false,
"keyName": "IDX_product_option_product_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_option_deleted_at",
"primary": false,
@@ -819,9 +762,7 @@
},
{
"keyName": "product_option_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -831,13 +772,9 @@
"foreignKeys": {
"product_option_product_id_foreign": {
"constraintName": "product_option_product_id_foreign",
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"localTableName": "public.product_option",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product",
"updateRule": "cascade"
}
@@ -869,10 +806,7 @@
"indexes": [
{
"keyName": "product_tags_pkey",
"columnNames": [
"product_id",
"product_tag_id"
],
"columnNames": ["product_id", "product_tag_id"],
"composite": true,
"primary": true,
"unique": true
@@ -882,26 +816,18 @@
"foreignKeys": {
"product_tags_product_id_foreign": {
"constraintName": "product_tags_product_id_foreign",
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"localTableName": "public.product_tags",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"product_tags_product_tag_id_foreign": {
"constraintName": "product_tags_product_tag_id_foreign",
"columnNames": [
"product_tag_id"
],
"columnNames": ["product_tag_id"],
"localTableName": "public.product_tags",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_tag",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -934,10 +860,7 @@
"indexes": [
{
"keyName": "product_images_pkey",
"columnNames": [
"product_id",
"product_image_id"
],
"columnNames": ["product_id", "product_image_id"],
"composite": true,
"primary": true,
"unique": true
@@ -947,26 +870,18 @@
"foreignKeys": {
"product_images_product_id_foreign": {
"constraintName": "product_images_product_id_foreign",
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"localTableName": "public.product_images",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"product_images_product_image_id_foreign": {
"constraintName": "product_images_product_image_id_foreign",
"columnNames": [
"product_image_id"
],
"columnNames": ["product_image_id"],
"localTableName": "public.product_images",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.image",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -999,10 +914,7 @@
"indexes": [
{
"keyName": "product_category_product_pkey",
"columnNames": [
"product_id",
"product_category_id"
],
"columnNames": ["product_id", "product_category_id"],
"composite": true,
"primary": true,
"unique": true
@@ -1012,26 +924,18 @@
"foreignKeys": {
"product_category_product_product_id_foreign": {
"constraintName": "product_category_product_product_id_foreign",
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"localTableName": "public.product_category_product",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product",
"deleteRule": "cascade",
"updateRule": "cascade"
},
"product_category_product_product_category_id_foreign": {
"constraintName": "product_category_product_product_category_id_foreign",
"columnNames": [
"product_category_id"
],
"columnNames": ["product_category_id"],
"localTableName": "public.product_category_product",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_category",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -1259,18 +1163,14 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_variant_deleted_at",
"primary": false,
"unique": false
},
{
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"composite": false,
"keyName": "IDX_product_variant_product_id",
"primary": false,
@@ -1278,45 +1178,35 @@
},
{
"keyName": "IDX_product_variant_sku_unique",
"columnNames": [
"sku"
],
"columnNames": ["sku"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "IDX_product_variant_barcode_unique",
"columnNames": [
"barcode"
],
"columnNames": ["barcode"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "IDX_product_variant_ean_unique",
"columnNames": [
"ean"
],
"columnNames": ["ean"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "IDX_product_variant_upc_unique",
"columnNames": [
"upc"
],
"columnNames": ["upc"],
"composite": false,
"primary": false,
"unique": true
},
{
"keyName": "product_variant_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -1326,13 +1216,9 @@
"foreignKeys": {
"product_variant_product_id_foreign": {
"constraintName": "product_variant_product_id_foreign",
"columnNames": [
"product_id"
],
"columnNames": ["product_id"],
"localTableName": "public.product_variant",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -1401,27 +1287,21 @@
"schema": "public",
"indexes": [
{
"columnNames": [
"option_id"
],
"columnNames": ["option_id"],
"composite": false,
"keyName": "IDX_product_option_value_option_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"variant_id"
],
"columnNames": ["variant_id"],
"composite": false,
"keyName": "IDX_product_option_value_variant_id",
"primary": false,
"unique": false
},
{
"columnNames": [
"deleted_at"
],
"columnNames": ["deleted_at"],
"composite": false,
"keyName": "IDX_product_option_value_deleted_at",
"primary": false,
@@ -1429,9 +1309,7 @@
},
{
"keyName": "product_option_value_pkey",
"columnNames": [
"id"
],
"columnNames": ["id"],
"composite": false,
"primary": true,
"unique": true
@@ -1441,25 +1319,17 @@
"foreignKeys": {
"product_option_value_option_id_foreign": {
"constraintName": "product_option_value_option_id_foreign",
"columnNames": [
"option_id"
],
"columnNames": ["option_id"],
"localTableName": "public.product_option_value",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_option",
"updateRule": "cascade"
},
"product_option_value_variant_id_foreign": {
"constraintName": "product_option_value_variant_id_foreign",
"columnNames": [
"variant_id"
],
"columnNames": ["variant_id"],
"localTableName": "public.product_option_value",
"referencedColumnNames": [
"id"
],
"referencedColumnNames": ["id"],
"referencedTableName": "public.product_variant",
"deleteRule": "cascade",
"updateRule": "cascade"
@@ -7,11 +7,10 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { generateEntityId } from "@medusajs/utils"
import { ProductOption, ProductVariant } from "./index"
import ProductOption from "./product-option"
import { ProductVariant } from "./index"
import { SoftDeletable } from "../utils"
import { generateEntityId } from "@medusajs/utils"
type OptionalFields =
| "created_at"
@@ -1,3 +1,4 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
@@ -10,7 +11,6 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { generateEntityId } from "@medusajs/utils"
import { Product } from "./index"
import ProductOptionValue from "./product-option-value"
import { SoftDeletable } from "../utils"
@@ -1,3 +1,4 @@
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,
@@ -11,7 +12,6 @@ import {
Property,
Unique,
} from "@mikro-orm/core"
import { generateEntityId } from "@medusajs/utils"
import { Product } from "@models"
import ProductOptionValue from "./product-option-value"
import { SoftDeletable } from "../utils"
+3 -2
View File
@@ -1,8 +1,9 @@
import * as ProductModels from "@models"
import { ModuleExports } from "@medusajs/types"
import { ProductModuleService } from "@services"
import loadContainer from "./loaders/container"
import loadConnection from "./loaders/connection"
import * as ProductModels from "@models"
import loadContainer from "./loaders/container"
const service = ProductModuleService
const loaders = [loadContainer, loadConnection] as any
@@ -1,8 +1,10 @@
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
import { createConnection } from "../utils"
import * as ProductModels from "@models"
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
import { EntitySchema } from "@mikro-orm/core"
import { ModulesSdkUtils } from "@medusajs/utils"
import { createConnection } from "../utils"
/**
* This script is only valid for mikro orm managers. If a user provide a custom manager
+3 -3
View File
@@ -1,9 +1,9 @@
export { default as ProductModuleService } from "./product-module-service"
export { default as ProductService } from "./product"
export { default as ProductCategoryService } from "./product-category"
export { default as ProductCollectionService } from "./product-collection"
export { default as ProductModuleService } from "./product-module-service"
export { default as ProductTagService } from "./product-tag"
export { default as ProductVariantService } from "./product-variant"
export { default as ProductCollectionService } from "./product-collection"
export { default as ProductCategoryService } from "./product-category"
export { default as ProductTypeService } from "./product-type"
export { default as ProductOptionService } from "./product-option"
export { default as ProductImageService } from "./product-image"
@@ -1,7 +1,8 @@
import { ProductCollection } from "@models"
import { Context, DAL, FindConfig, ProductTypes } from "@medusajs/types"
import { ModulesSdkUtils, retrieveEntity } from "@medusajs/utils"
import { ProductCollection } from "@models"
type InjectedDependencies = {
productCollectionRepository: DAL.RepositoryService
}
@@ -23,6 +23,7 @@ import {
DAL,
FindConfig,
InternalModuleDeclaration,
JoinerServiceConfig,
ProductTypes,
} from "@medusajs/types"
import ProductImageService from "./product-image"
@@ -34,6 +35,7 @@ import {
MedusaContext,
} from "@medusajs/utils"
import { shouldForceTransaction } from "../utils"
import { joinerConfig } from "./../joiner-config"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
@@ -96,6 +98,10 @@ export default class ProductModuleService<
this.productOptionService_ = productOptionService
}
__joinerConfig(): JoinerServiceConfig {
return joinerConfig
}
async list(
filters: ProductTypes.FilterableProductProps = {},
config: FindConfig<ProductTypes.ProductDTO> = {},