1 import { ModuleJoinerConfig } from "@medusajs/framework/types"
2
3 /**
4 * Joiner configs describing the fake "remote" modules the index module
5 * indexes data from. The index module is generic over the modules
6 * registered in the application — it only consumes their joiner configs
7 * (schema, aliases, linkable keys) and their events/query data. These
8 * fixtures model a product-like and a pricing-like module joined by a
9 * link module, without depending on the real commerce modules.
10 */
11
12 const PRODUCT_SERVICE = "product"
13 const PRICING_SERVICE = "pricing"
14 const LINK_SERVICE = "productVariantPriceSet"
15
16 export const productJoinerConfig: ModuleJoinerConfig = {
17 serviceName: PRODUCT_SERVICE,
18 primaryKeys: ["id"],
19 linkableKeys: {
20 product_id: "Product",
21 variant_id: "ProductVariant",
22 },
23 alias: [
24 {
25 name: ["product", "products"],
26 entity: "Product",
27 },
28 {
29 name: ["product_variant", "product_variants", "variant", "variants"],
30 entity: "ProductVariant",
31 },
32 ],
33 schema: `
34 scalar DateTime
35
36 type Product {
37 id: ID!
38 title: String
39 handle: String
40 created_at: DateTime
41 updated_at: DateTime
42 variants: [ProductVariant]
43 }
44
45 type ProductVariant {
46 id: ID!
47 product_id: String
48 sku: String
49 description: String
50 created_at: DateTime
51 updated_at: DateTime
52 product: Product
53 }
54 `,
55 }
56
57 export const pricingJoinerConfig: ModuleJoinerConfig = {
58 serviceName: PRICING_SERVICE,
59 primaryKeys: ["id"],
60 linkableKeys: {
61 price_set_id: "PriceSet",
62 price_id: "Price",
63 },
64 alias: [
65 {
66 name: ["price_set", "price_sets"],
67 entity: "PriceSet",
68 },
69 {
70 name: ["price", "prices"],
71 entity: "Price",
72 },
73 ],
74 schema: `
75 scalar DateTime
76
77 type PriceSet {
78 id: ID!
79 created_at: DateTime
80 updated_at: DateTime
81 prices: [Price]
82 }
83
84 type Price {
85 id: ID!
86 amount: Float
87 currency_code: String
88 created_at: DateTime
89 updated_at: DateTime
90 price_set: PriceSet
91 }
92 `,
93 }
94
95 export const productVariantPriceSetLinkConfig: ModuleJoinerConfig = {
96 serviceName: LINK_SERVICE,
97 isLink: true,
98 databaseConfig: {
99 tableName: "product_variant_price_set",
100 idPrefix: "pvps",
101 },
102 alias: [
103 {
104 name: ["product_variant_price_set", "product_variant_price_sets"],
105 entity: "LinkProductVariantPriceSet",
106 },
107 ],
108 primaryKeys: ["id", "variant_id", "price_set_id"],
109 relationships: [
110 {
111 serviceName: PRODUCT_SERVICE,
112 entity: "ProductVariant",
113 primaryKey: "id",
114 foreignKey: "variant_id",
115 alias: "variant",
116 },
117 {
118 serviceName: PRICING_SERVICE,
119 entity: "PriceSet",
120 primaryKey: "id",
121 foreignKey: "price_set_id",
122 alias: "price_set",
123 deleteCascade: true,
124 },
125 ],
126 extends: [
127 {
128 serviceName: PRODUCT_SERVICE,
129 entity: "ProductVariant",
130 fieldAlias: {
131 price_set: "price_set_link.price_set",
132 },
133 relationship: {
134 serviceName: LINK_SERVICE,
135 primaryKey: "variant_id",
136 foreignKey: "id",
137 alias: "price_set_link",
138 },
139 },
140 {
141 serviceName: PRICING_SERVICE,
142 entity: "PriceSet",
143 fieldAlias: {
144 variant: "variant_link.variant",
145 },
146 relationship: {
147 serviceName: LINK_SERVICE,
148 primaryKey: "price_set_id",
149 foreignKey: "id",
150 alias: "variant_link",
151 },
152 },
153 ],
154 schema: `
155 extend type ProductVariant {
156 price_set_link: LinkProductVariantPriceSet
157 price_set: PriceSet
158 }
159
160 extend type PriceSet {
161 variant_link: LinkProductVariantPriceSet
162 variant: ProductVariant
163 }
164
165 type LinkProductVariantPriceSet {
166 id: ID!
167 variant_id: String!
168 price_set_id: String!
169 variant: ProductVariant
170 price_set: PriceSet
171 }
172 `,
173 }
174
175 /**
176 * Registration order matters: entities are resolved against the configs in
177 * order, and the link config's cleaned schema also declares the entities it
178 * extends, so the owning modules must come first.
179 */
180 export const remoteModulesJoinerConfigs: ModuleJoinerConfig[] = [
181 productJoinerConfig,
182 pricingJoinerConfig,
183 productVariantPriceSetLinkConfig,
184 ]