@medusajs/link-modules 0.2.12 → 1.0.0-rc-20241003153304

Sign up to get free protection for your applications and to get access to all the features.
Files changed (212) hide show
  1. package/dist/definitions/cart-payment-collection.d.ts +2 -1
  2. package/dist/definitions/cart-payment-collection.d.ts.map +1 -0
  3. package/dist/definitions/cart-payment-collection.js +15 -9
  4. package/dist/definitions/cart-payment-collection.js.map +1 -0
  5. package/dist/definitions/cart-promotion.d.ts +2 -1
  6. package/dist/definitions/cart-promotion.d.ts.map +1 -0
  7. package/dist/definitions/cart-promotion.js +22 -18
  8. package/dist/definitions/cart-promotion.js.map +1 -0
  9. package/dist/definitions/fulfillment-provider-location.d.ts +3 -0
  10. package/dist/definitions/fulfillment-provider-location.d.ts.map +1 -0
  11. package/dist/definitions/fulfillment-provider-location.js +72 -0
  12. package/dist/definitions/fulfillment-provider-location.js.map +1 -0
  13. package/dist/definitions/fulfillment-set-location.d.ts +3 -2
  14. package/dist/definitions/fulfillment-set-location.d.ts.map +1 -0
  15. package/dist/definitions/fulfillment-set-location.js +42 -33
  16. package/dist/definitions/fulfillment-set-location.js.map +1 -0
  17. package/dist/definitions/index.d.ts +7 -9
  18. package/dist/definitions/index.d.ts.map +1 -0
  19. package/dist/definitions/index.js +7 -9
  20. package/dist/definitions/index.js.map +1 -0
  21. package/dist/definitions/order-cart.d.ts +3 -0
  22. package/dist/definitions/order-cart.d.ts.map +1 -0
  23. package/dist/definitions/order-cart.js +68 -0
  24. package/dist/definitions/order-cart.js.map +1 -0
  25. package/dist/definitions/order-claim-payment-collection.d.ts +3 -0
  26. package/dist/definitions/order-claim-payment-collection.d.ts.map +1 -0
  27. package/dist/definitions/order-claim-payment-collection.js +74 -0
  28. package/dist/definitions/order-claim-payment-collection.js.map +1 -0
  29. package/dist/definitions/order-exchange-payment-collection.d.ts +3 -0
  30. package/dist/definitions/order-exchange-payment-collection.d.ts.map +1 -0
  31. package/dist/definitions/order-exchange-payment-collection.js +74 -0
  32. package/dist/definitions/order-exchange-payment-collection.js.map +1 -0
  33. package/dist/definitions/order-fulfillment.d.ts +3 -0
  34. package/dist/definitions/order-fulfillment.d.ts.map +1 -0
  35. package/dist/definitions/order-fulfillment.js +72 -0
  36. package/dist/definitions/order-fulfillment.js.map +1 -0
  37. package/dist/definitions/order-payment-collection.d.ts +3 -0
  38. package/dist/definitions/order-payment-collection.d.ts.map +1 -0
  39. package/dist/definitions/order-payment-collection.js +72 -0
  40. package/dist/definitions/order-payment-collection.js.map +1 -0
  41. package/dist/definitions/order-promotion.d.ts +2 -1
  42. package/dist/definitions/order-promotion.d.ts.map +1 -0
  43. package/dist/definitions/order-promotion.js +20 -11
  44. package/dist/definitions/order-promotion.js.map +1 -0
  45. package/dist/definitions/order-return-fulfillment.d.ts +3 -0
  46. package/dist/definitions/order-return-fulfillment.d.ts.map +1 -0
  47. package/dist/definitions/order-return-fulfillment.js +69 -0
  48. package/dist/definitions/order-return-fulfillment.js.map +1 -0
  49. package/dist/definitions/product-sales-channel.d.ts +2 -1
  50. package/dist/definitions/product-sales-channel.d.ts.map +1 -0
  51. package/dist/definitions/product-sales-channel.js +18 -9
  52. package/dist/definitions/product-sales-channel.js.map +1 -0
  53. package/dist/definitions/product-variant-inventory-item.d.ts +2 -1
  54. package/dist/definitions/product-variant-inventory-item.d.ts.map +1 -0
  55. package/dist/definitions/product-variant-inventory-item.js +21 -11
  56. package/dist/definitions/product-variant-inventory-item.js.map +1 -0
  57. package/dist/definitions/product-variant-price-set.d.ts +2 -1
  58. package/dist/definitions/product-variant-price-set.d.ts.map +1 -0
  59. package/dist/definitions/product-variant-price-set.js +18 -12
  60. package/dist/definitions/product-variant-price-set.js.map +1 -0
  61. package/dist/definitions/publishable-api-key-sales-channel.d.ts +2 -1
  62. package/dist/definitions/publishable-api-key-sales-channel.d.ts.map +1 -0
  63. package/dist/definitions/publishable-api-key-sales-channel.js +22 -8
  64. package/dist/definitions/publishable-api-key-sales-channel.js.map +1 -0
  65. package/dist/definitions/readonly/cart-customer.d.ts +3 -0
  66. package/dist/definitions/readonly/cart-customer.d.ts.map +1 -0
  67. package/dist/definitions/{cart-customer.js → readonly/cart-customer.js} +14 -5
  68. package/dist/definitions/readonly/cart-customer.js.map +1 -0
  69. package/dist/definitions/readonly/cart-product.d.ts +3 -0
  70. package/dist/definitions/readonly/cart-product.d.ts.map +1 -0
  71. package/dist/definitions/readonly/cart-product.js +50 -0
  72. package/dist/definitions/readonly/cart-product.js.map +1 -0
  73. package/dist/definitions/readonly/cart-region.d.ts +3 -0
  74. package/dist/definitions/readonly/cart-region.d.ts.map +1 -0
  75. package/dist/definitions/{cart-region.js → readonly/cart-region.js} +14 -5
  76. package/dist/definitions/readonly/cart-region.js.map +1 -0
  77. package/dist/definitions/readonly/cart-sales-channel.d.ts +3 -0
  78. package/dist/definitions/readonly/cart-sales-channel.d.ts.map +1 -0
  79. package/dist/definitions/{cart-sales-channel.js → readonly/cart-sales-channel.js} +14 -5
  80. package/dist/definitions/readonly/cart-sales-channel.js.map +1 -0
  81. package/dist/definitions/readonly/index.d.ts +12 -0
  82. package/dist/definitions/readonly/index.d.ts.map +1 -0
  83. package/dist/definitions/readonly/index.js +28 -0
  84. package/dist/definitions/readonly/index.js.map +1 -0
  85. package/dist/definitions/readonly/inventory-level-stock-location.d.ts +3 -0
  86. package/dist/definitions/readonly/inventory-level-stock-location.d.ts.map +1 -0
  87. package/dist/definitions/{inventory-level-stock-location.js → readonly/inventory-level-stock-location.js} +8 -3
  88. package/dist/definitions/readonly/inventory-level-stock-location.js.map +1 -0
  89. package/dist/definitions/readonly/line-item-adjustment-promotion.d.ts +3 -0
  90. package/dist/definitions/readonly/line-item-adjustment-promotion.d.ts.map +1 -0
  91. package/dist/definitions/readonly/line-item-adjustment-promotion.js +24 -0
  92. package/dist/definitions/readonly/line-item-adjustment-promotion.js.map +1 -0
  93. package/dist/definitions/readonly/order-customer.d.ts +3 -0
  94. package/dist/definitions/readonly/order-customer.d.ts.map +1 -0
  95. package/dist/definitions/{order-customer.js → readonly/order-customer.js} +14 -5
  96. package/dist/definitions/readonly/order-customer.js.map +1 -0
  97. package/dist/definitions/readonly/order-product.d.ts +3 -0
  98. package/dist/definitions/readonly/order-product.d.ts.map +1 -0
  99. package/dist/definitions/readonly/order-product.js +51 -0
  100. package/dist/definitions/readonly/order-product.js.map +1 -0
  101. package/dist/definitions/readonly/order-region.d.ts +3 -0
  102. package/dist/definitions/readonly/order-region.d.ts.map +1 -0
  103. package/dist/definitions/{order-region.js → readonly/order-region.js} +14 -5
  104. package/dist/definitions/readonly/order-region.js.map +1 -0
  105. package/dist/definitions/readonly/order-sales-channel.d.ts +3 -0
  106. package/dist/definitions/readonly/order-sales-channel.d.ts.map +1 -0
  107. package/dist/definitions/readonly/order-sales-channel.js +38 -0
  108. package/dist/definitions/readonly/order-sales-channel.js.map +1 -0
  109. package/dist/definitions/readonly/store-currency.d.ts +3 -0
  110. package/dist/definitions/readonly/store-currency.d.ts.map +1 -0
  111. package/dist/definitions/readonly/store-currency.js +24 -0
  112. package/dist/definitions/readonly/store-currency.js.map +1 -0
  113. package/dist/definitions/region-payment-provider.d.ts +2 -1
  114. package/dist/definitions/region-payment-provider.d.ts.map +1 -0
  115. package/dist/definitions/region-payment-provider.js +20 -11
  116. package/dist/definitions/region-payment-provider.js.map +1 -0
  117. package/dist/definitions/sales-channel-location.d.ts +2 -1
  118. package/dist/definitions/sales-channel-location.d.ts.map +1 -0
  119. package/dist/definitions/sales-channel-location.js +23 -11
  120. package/dist/definitions/sales-channel-location.js.map +1 -0
  121. package/dist/definitions/shipping-option-price-set.d.ts +2 -1
  122. package/dist/definitions/shipping-option-price-set.d.ts.map +1 -0
  123. package/dist/definitions/shipping-option-price-set.js +12 -9
  124. package/dist/definitions/shipping-option-price-set.js.map +1 -0
  125. package/dist/index.d.ts +3 -2
  126. package/dist/index.d.ts.map +1 -0
  127. package/dist/index.js +3 -2
  128. package/dist/index.js.map +1 -0
  129. package/dist/initialize/index.d.ts +13 -4
  130. package/dist/initialize/index.d.ts.map +1 -0
  131. package/dist/initialize/index.js +42 -28
  132. package/dist/initialize/index.js.map +1 -0
  133. package/dist/initialize/module-definition.d.ts +2 -1
  134. package/dist/initialize/module-definition.d.ts.map +1 -0
  135. package/dist/initialize/module-definition.js +2 -2
  136. package/dist/initialize/module-definition.js.map +1 -0
  137. package/dist/loaders/connection.d.ts +2 -1
  138. package/dist/loaders/connection.d.ts.map +1 -0
  139. package/dist/loaders/connection.js +3 -3
  140. package/dist/loaders/connection.js.map +1 -0
  141. package/dist/loaders/container.d.ts +2 -2
  142. package/dist/loaders/container.d.ts.map +1 -0
  143. package/dist/loaders/container.js +6 -7
  144. package/dist/loaders/container.js.map +1 -0
  145. package/dist/loaders/index.d.ts +2 -1
  146. package/dist/loaders/index.d.ts.map +1 -0
  147. package/dist/loaders/index.js +2 -2
  148. package/dist/loaders/index.js.map +1 -0
  149. package/dist/migration/index.d.ts +103 -2
  150. package/dist/migration/index.d.ts.map +1 -0
  151. package/dist/migration/index.js +341 -46
  152. package/dist/migration/index.js.map +1 -0
  153. package/dist/repositories/index.d.ts +2 -1
  154. package/dist/repositories/index.d.ts.map +1 -0
  155. package/dist/repositories/index.js +2 -1
  156. package/dist/repositories/index.js.map +1 -0
  157. package/dist/repositories/link.d.ts +20 -20
  158. package/dist/repositories/link.d.ts.map +1 -0
  159. package/dist/repositories/link.js +3 -3
  160. package/dist/repositories/link.js.map +1 -0
  161. package/dist/services/dynamic-service-class.d.ts +2 -1
  162. package/dist/services/dynamic-service-class.d.ts.map +1 -0
  163. package/dist/services/dynamic-service-class.js +10 -3
  164. package/dist/services/dynamic-service-class.js.map +1 -0
  165. package/dist/services/index.d.ts +1 -0
  166. package/dist/services/index.d.ts.map +1 -0
  167. package/dist/services/index.js +1 -0
  168. package/dist/services/index.js.map +1 -0
  169. package/dist/services/link-module-service.d.ts +9 -7
  170. package/dist/services/link-module-service.d.ts.map +1 -0
  171. package/dist/services/link-module-service.js +38 -45
  172. package/dist/services/link-module-service.js.map +1 -0
  173. package/dist/services/link.d.ts +2 -1
  174. package/dist/services/link.d.ts.map +1 -0
  175. package/dist/services/link.js +2 -1
  176. package/dist/services/link.js.map +1 -0
  177. package/dist/tsconfig.tsbuildinfo +1 -0
  178. package/dist/types/index.d.ts +2 -1
  179. package/dist/types/index.d.ts.map +1 -0
  180. package/dist/types/index.js +1 -0
  181. package/dist/types/index.js.map +1 -0
  182. package/dist/utils/compress-name.d.ts +2 -0
  183. package/dist/utils/compress-name.d.ts.map +1 -0
  184. package/dist/utils/compress-name.js +33 -0
  185. package/dist/utils/compress-name.js.map +1 -0
  186. package/dist/utils/generate-entity.d.ts +2 -1
  187. package/dist/utils/generate-entity.d.ts.map +1 -0
  188. package/dist/utils/generate-entity.js +6 -6
  189. package/dist/utils/generate-entity.js.map +1 -0
  190. package/dist/utils/generate-schema.d.ts +2 -1
  191. package/dist/utils/generate-schema.d.ts.map +1 -0
  192. package/dist/utils/generate-schema.js +76 -20
  193. package/dist/utils/generate-schema.js.map +1 -0
  194. package/dist/utils/index.d.ts +1 -1
  195. package/dist/utils/index.d.ts.map +1 -0
  196. package/dist/utils/index.js +5 -6
  197. package/dist/utils/index.js.map +1 -0
  198. package/package.json +22 -15
  199. package/dist/definitions/cart-customer.d.ts +0 -2
  200. package/dist/definitions/cart-region.d.ts +0 -2
  201. package/dist/definitions/cart-sales-channel.d.ts +0 -2
  202. package/dist/definitions/inventory-level-stock-location.d.ts +0 -2
  203. package/dist/definitions/order-customer.d.ts +0 -2
  204. package/dist/definitions/order-region.d.ts +0 -2
  205. package/dist/definitions/order-sales-channel.d.ts +0 -2
  206. package/dist/definitions/order-sales-channel.js +0 -67
  207. package/dist/definitions/product-shipping-profile.d.ts +0 -2
  208. package/dist/definitions/product-shipping-profile.js +0 -61
  209. package/dist/definitions/store-default-currency.d.ts +0 -2
  210. package/dist/definitions/store-default-currency.js +0 -19
  211. package/dist/utils/compose-link-name.d.ts +0 -2
  212. package/dist/utils/compose-link-name.js +0 -12
@@ -1,56 +1,351 @@
1
1
  "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _MigrationsExecutionPlanner_dbConfig, _MigrationsExecutionPlanner_unsafeSQLCommands, _MigrationsExecutionPlanner_linksEntities;
2
14
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMigration = void 0;
4
- const utils_1 = require("../utils");
5
- const utils_2 = require("@medusajs/utils");
6
- function getMigration(joinerConfig, serviceName, primary, foreign) {
7
- return async function runMigrations({ options, logger, } = {}) {
8
- logger ?? (logger = console);
9
- const dbData = utils_2.ModulesSdkUtils.loadDatabaseConfig("link_modules", options);
10
- const entity = (0, utils_1.generateEntity)(joinerConfig, primary, foreign);
11
- const pathToMigrations = __dirname + "/../migrations";
12
- const orm = await utils_2.DALUtils.mikroOrmCreateConnection(dbData, [entity], pathToMigrations);
15
+ exports.MigrationsExecutionPlanner = void 0;
16
+ const utils_1 = require("@medusajs/framework/utils");
17
+ const postgresql_1 = require("@mikro-orm/postgresql");
18
+ const utils_2 = require("../utils");
19
+ /**
20
+ * The migrations execution planner creates a plan of SQL queries
21
+ * to be executed to keep link modules database state in sync
22
+ * with the links defined inside the user application.
23
+ */
24
+ class MigrationsExecutionPlanner {
25
+ constructor(joinerConfig, options) {
26
+ /**
27
+ * Database options for the module service
28
+ */
29
+ _MigrationsExecutionPlanner_dbConfig.set(this, void 0);
30
+ /**
31
+ * The set of commands that are unsafe to execute automatically when
32
+ * performing "alter table"
33
+ */
34
+ _MigrationsExecutionPlanner_unsafeSQLCommands.set(this, ["alter column", "drop column"]
35
+ /**
36
+ * On-the-fly computed set of entities for the user provided joinerConfig and the link it is coming from
37
+ */
38
+ );
39
+ /**
40
+ * On-the-fly computed set of entities for the user provided joinerConfig and the link it is coming from
41
+ */
42
+ _MigrationsExecutionPlanner_linksEntities.set(this, void 0);
43
+ /**
44
+ * The table that keeps a track of tables generated by the link
45
+ * module.
46
+ */
47
+ this.tableName = "link_module_migrations";
48
+ __classPrivateFieldSet(this, _MigrationsExecutionPlanner_dbConfig, utils_1.ModulesSdkUtils.loadDatabaseConfig("link_modules", options), "f");
49
+ __classPrivateFieldSet(this, _MigrationsExecutionPlanner_linksEntities, joinerConfig
50
+ .map((config) => {
51
+ if (config.isReadOnlyLink) {
52
+ return;
53
+ }
54
+ const [primary, foreign] = config.relationships ?? [];
55
+ const linkDescriptor = {
56
+ fromModule: primary.serviceName,
57
+ toModule: foreign.serviceName,
58
+ fromModel: primary.alias,
59
+ toModel: foreign.alias,
60
+ };
61
+ return {
62
+ entity: (0, utils_2.generateEntity)(config, primary, foreign),
63
+ linkDescriptor,
64
+ };
65
+ })
66
+ .filter((item) => !!item), "f");
67
+ }
68
+ /**
69
+ * Initializes the ORM using the normalized dbConfig and set
70
+ * of provided entities
71
+ */
72
+ async createORM(entities = []) {
73
+ return await utils_1.DALUtils.mikroOrmCreateConnection(__classPrivateFieldGet(this, _MigrationsExecutionPlanner_dbConfig, "f"), entities, "");
74
+ }
75
+ /**
76
+ * Ensure the table to track link modules migrations
77
+ * exists.
78
+ *
79
+ * @param orm MikroORM
80
+ */
81
+ async ensureMigrationsTable(orm) {
82
+ await orm.em.getDriver().getConnection().execute(`
83
+ CREATE TABLE IF NOT EXISTS "${this.tableName}" (
84
+ id SERIAL PRIMARY KEY,
85
+ table_name VARCHAR(255) NOT NULL UNIQUE,
86
+ link_descriptor JSONB NOT NULL DEFAULT '{}'::jsonb,
87
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
88
+ )
89
+ `);
90
+ }
91
+ /**
92
+ * Ensure the migrations table is in sync
93
+ *
94
+ * @param orm
95
+ * @protected
96
+ */
97
+ async ensureMigrationsTableUpToDate(orm) {
98
+ const existingTables = (await orm.em
99
+ .getDriver()
100
+ .getConnection()
101
+ .execute(`
102
+ SELECT table_name
103
+ FROM information_schema.tables;
104
+ `))
105
+ .map(({ table_name }) => table_name)
106
+ .filter((tableName) => __classPrivateFieldGet(this, _MigrationsExecutionPlanner_linksEntities, "f").some(({ entity }) => entity.meta.collection === tableName));
107
+ if (!existingTables.length) {
108
+ return;
109
+ }
110
+ const orderedDescriptors = existingTables.map((tableName) => {
111
+ return __classPrivateFieldGet(this, _MigrationsExecutionPlanner_linksEntities, "f").find(({ entity }) => entity.meta.collection === tableName).linkDescriptor;
112
+ });
113
+ const positionalArgs = new Array(existingTables.length)
114
+ .fill("(?, ?)")
115
+ .join(", ");
116
+ await orm.em
117
+ .getDriver()
118
+ .getConnection()
119
+ .execute(`
120
+ INSERT INTO ${this.tableName} (table_name, link_descriptor) VALUES ${positionalArgs} ON CONFLICT DO NOTHING;
121
+ `, existingTables.flatMap((tableName, index) => [
122
+ tableName,
123
+ JSON.stringify(orderedDescriptors[index]),
124
+ ]));
125
+ }
126
+ /**
127
+ * Insert tuple to the migrations table and create the link table
128
+ *
129
+ * @param orm
130
+ * @param action
131
+ * @protected
132
+ */
133
+ async createLinkTable(orm, action) {
134
+ const { tableName, linkDescriptor, sql } = action;
135
+ await orm.em
136
+ .getDriver()
137
+ .getConnection()
138
+ .execute(`
139
+ INSERT INTO "${this.tableName}" (table_name, link_descriptor) VALUES (?, ?);
140
+ ${sql}
141
+ `, [tableName, linkDescriptor]);
142
+ }
143
+ /**
144
+ * Drops the link table and untracks it from the "link_modules_migrations"
145
+ * table.
146
+ */
147
+ async dropLinkTable(orm, tableName) {
148
+ await orm.em.getDriver().getConnection().execute(`
149
+ DROP TABLE IF EXISTS "${tableName}";
150
+ DELETE FROM "${this.tableName}" WHERE table_name = '${tableName}';
151
+ `);
152
+ }
153
+ /**
154
+ * Returns an array of table names that have been tracked during
155
+ * the last run. In short, these tables were created by the
156
+ * link modules migrations runner.
157
+ *
158
+ * @param orm MikroORM
159
+ */
160
+ async getTrackedLinksTables(orm) {
161
+ const results = await orm.em.getDriver().getConnection().execute(`
162
+ SELECT table_name, link_descriptor from "${this.tableName}"
163
+ `);
164
+ return results.map((tuple) => ({
165
+ table_name: tuple.table_name,
166
+ link_descriptor: tuple.link_descriptor,
167
+ }));
168
+ }
169
+ /**
170
+ * Returns the migration plan for a specific link entity.
171
+ */
172
+ async getEntityMigrationPlan(linkDescriptor, entity, trackedLinksTables) {
13
173
  const tableName = entity.meta.collection;
14
- let hasTable = false;
174
+ const orm = await this.createORM([entity]);
15
175
  try {
16
- await orm.em.getConnection().execute(`SELECT 1 FROM ${tableName} LIMIT 0`);
17
- hasTable = true;
176
+ const generator = orm.getSchemaGenerator();
177
+ const platform = orm.em.getPlatform();
178
+ const connection = orm.em.getConnection();
179
+ const schemaName = __classPrivateFieldGet(this, _MigrationsExecutionPlanner_dbConfig, "f").schema || "public";
180
+ /**
181
+ * If the table name for the entity has not been
182
+ * managed by us earlier, then we should create
183
+ * it.
184
+ */
185
+ if (!trackedLinksTables.includes(tableName)) {
186
+ return {
187
+ action: "create",
188
+ linkDescriptor,
189
+ tableName,
190
+ sql: await generator.getCreateSchemaSQL(),
191
+ };
192
+ }
193
+ /**
194
+ * Pre-fetching information schema from the database and using that
195
+ * as the way to compute the update diff.
196
+ *
197
+ * @note
198
+ * The "loadInformationSchema" mutates the "dbSchema" argument provided
199
+ * to it as the first argument.
200
+ */
201
+ const dbSchema = new postgresql_1.DatabaseSchema(platform, schemaName);
202
+ await platform
203
+ .getSchemaHelper?.()
204
+ ?.loadInformationSchema(dbSchema, connection, [
205
+ {
206
+ table_name: tableName,
207
+ schema_name: schemaName,
208
+ },
209
+ ]);
210
+ const updateSQL = await generator.getUpdateSchemaSQL({
211
+ fromSchema: dbSchema,
212
+ });
213
+ /**
214
+ * Entity is upto-date and hence we do not have to perform
215
+ * any updates on it.
216
+ */
217
+ if (!updateSQL.length) {
218
+ return {
219
+ action: "noop",
220
+ linkDescriptor,
221
+ tableName,
222
+ };
223
+ }
224
+ const usesUnsafeCommands = __classPrivateFieldGet(this, _MigrationsExecutionPlanner_unsafeSQLCommands, "f").some((fragment) => {
225
+ return updateSQL.match(new RegExp(`${fragment}`, "ig"));
226
+ });
227
+ return {
228
+ action: usesUnsafeCommands ? "notify" : "update",
229
+ linkDescriptor,
230
+ tableName,
231
+ sql: updateSQL,
232
+ };
18
233
  }
19
- catch { }
20
- const generator = orm.getSchemaGenerator();
21
- if (hasTable) {
22
- /* const updateSql = await generator.getUpdateSchemaSQL()
23
- const entityUpdates = updateSql
24
- .split(";")
25
- .map((sql) => sql.trim())
26
- .filter((sql) =>
27
- sql.toLowerCase().includes(`alter table "${tableName.toLowerCase()}"`)
28
- )
29
-
30
- if (entityUpdates.length > 0) {
31
- try {
32
- await generator.execute(entityUpdates.join(";"))
33
- logger.info(`Link module "${serviceName}" migration executed`)
34
- } catch (error) {
35
- logger.error(
36
- `Link module "${serviceName}" migration failed to run - Error: ${error}`
37
- )
38
- }
39
- } else {
40
- logger.info(`Skipping "${tableName}" migration.`)
41
- }*/
42
- logger.info(`Link module "${serviceName}" table update skipped because the table already exists. Please write your own migration if needed.`);
234
+ finally {
235
+ await orm.close(true);
43
236
  }
44
- else {
45
- try {
46
- await generator.createSchema();
47
- logger.info(`Link module "${serviceName}" migration executed`);
237
+ }
238
+ /**
239
+ * This method loops over the tables we have fetched from the
240
+ * "link_module_migrations" tables and checks if their new
241
+ * name is different from the tracked name and in that
242
+ * case it will rename the actual table and also the
243
+ * tracked entry.
244
+ */
245
+ async migrateOldTables(orm, trackedTables) {
246
+ const migratedTables = [];
247
+ for (let trackedTable of trackedTables) {
248
+ const newTableName = __classPrivateFieldGet(this, _MigrationsExecutionPlanner_linksEntities, "f").find((entity) => {
249
+ return (entity.linkDescriptor.fromModel ===
250
+ trackedTable.link_descriptor.fromModel &&
251
+ entity.linkDescriptor.toModel ===
252
+ trackedTable.link_descriptor.toModel &&
253
+ entity.linkDescriptor.fromModule ===
254
+ trackedTable.link_descriptor.fromModule &&
255
+ entity.linkDescriptor.toModule ===
256
+ trackedTable.link_descriptor.toModule);
257
+ })?.entity.meta.collection;
258
+ /**
259
+ * Perform rename
260
+ */
261
+ if (newTableName && trackedTable.table_name !== newTableName) {
262
+ await this.renameOldTable(orm, trackedTable.table_name, newTableName);
263
+ migratedTables.push({
264
+ ...trackedTable,
265
+ table_name: newTableName,
266
+ });
48
267
  }
49
- catch (error) {
50
- logger.error(`Link module "${serviceName}" migration failed to run - Error: ${error}`);
268
+ else {
269
+ migratedTables.push({
270
+ ...trackedTable,
271
+ });
51
272
  }
52
273
  }
53
- await orm.close();
54
- };
274
+ return migratedTables;
275
+ }
276
+ /**
277
+ * Renames existing table and also its tracked entry
278
+ */
279
+ async renameOldTable(orm, oldName, newName) {
280
+ await orm.em.getDriver().getConnection().execute(`
281
+ ALTER TABLE "${oldName}" RENAME TO "${newName}";
282
+ UPDATE "${this.tableName}" SET table_name = '${newName}' WHERE table_name = '${oldName}';
283
+ `);
284
+ }
285
+ /**
286
+ * Creates a plan to executed in order to keep the database state in
287
+ * sync with the user-defined links.
288
+ *
289
+ * This method only creates a plan and does not change the database
290
+ * state. You must call the "executePlan" method for that.
291
+ */
292
+ async createPlan() {
293
+ const orm = await this.createORM();
294
+ try {
295
+ await this.ensureMigrationsTable(orm);
296
+ const executionActions = [];
297
+ await this.ensureMigrationsTableUpToDate(orm);
298
+ const trackedTables = await this.migrateOldTables(orm, await this.getTrackedLinksTables(orm));
299
+ const trackedTablesNames = trackedTables.map(({ table_name }) => table_name);
300
+ /**
301
+ * Looping through the new set of entities and generating
302
+ * execution plan for them
303
+ */
304
+ for (let { entity, linkDescriptor } of __classPrivateFieldGet(this, _MigrationsExecutionPlanner_linksEntities, "f")) {
305
+ executionActions.push(await this.getEntityMigrationPlan(linkDescriptor, entity, trackedTablesNames));
306
+ }
307
+ const linksTableNames = __classPrivateFieldGet(this, _MigrationsExecutionPlanner_linksEntities, "f").map(({ entity }) => entity.meta.collection);
308
+ /**
309
+ * Finding the tables to be removed
310
+ */
311
+ const tablesToRemove = (0, utils_1.arrayDifference)(trackedTablesNames, linksTableNames);
312
+ tablesToRemove.forEach((tableToRemove) => {
313
+ executionActions.push({
314
+ action: "delete",
315
+ tableName: tableToRemove,
316
+ linkDescriptor: trackedTables.find(({ table_name }) => tableToRemove === table_name).link_descriptor,
317
+ });
318
+ });
319
+ return executionActions;
320
+ }
321
+ finally {
322
+ await orm.close(true);
323
+ }
324
+ }
325
+ /**
326
+ * Executes the actionsPlan actions where the action is one of 'create' | 'update' | 'delete'.
327
+ * 'noop' and 'notify' actions are implicitly ignored. If a notify action needs to be
328
+ * executed, you can mutate its action to 'update', in that scenario it means that an unsafe
329
+ * update sql (from our point of view) will be executed and some data could be lost.
330
+ *
331
+ * @param actionPlan
332
+ */
333
+ async executePlan(actionPlan) {
334
+ const orm = await this.createORM();
335
+ await (0, utils_1.promiseAll)(actionPlan.map(async (action) => {
336
+ switch (action.action) {
337
+ case "delete":
338
+ return await this.dropLinkTable(orm, action.tableName);
339
+ case "create":
340
+ return await this.createLinkTable(orm, action);
341
+ case "update":
342
+ return await orm.em.getDriver().getConnection().execute(action.sql);
343
+ default:
344
+ return;
345
+ }
346
+ })).finally(() => orm.close(true));
347
+ }
55
348
  }
56
- exports.getMigration = getMigration;
349
+ exports.MigrationsExecutionPlanner = MigrationsExecutionPlanner;
350
+ _MigrationsExecutionPlanner_dbConfig = new WeakMap(), _MigrationsExecutionPlanner_unsafeSQLCommands = new WeakMap(), _MigrationsExecutionPlanner_linksEntities = new WeakMap();
351
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/migration/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA,qDAKkC;AAElC,sDAAwE;AACxE,oCAAyC;AAEzC;;;;GAIG;AACH,MAAa,0BAA0B;IA0BrC,YACE,YAAkC,EAClC,OAAwC;QA3B1C;;WAEG;QACH,uDAAgE;QAEhE;;;WAGG;QACH,wDAAqB,CAAC,cAAc,EAAE,aAAa,CAAC;QAEpD;;WAEG;UAJiD;QAEpD;;WAEG;QACH,4DAGG;QAEH;;;WAGG;QACO,cAAS,GAAG,wBAAwB,CAAA;QAM5C,uBAAA,IAAI,wCAAa,uBAAe,CAAC,kBAAkB,CAAC,cAAc,EAAE,OAAO,CAAC,MAAA,CAAA;QAC5E,uBAAA,IAAI,6CAAkB,YAAY;aAC/B,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;YACd,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;gBAC1B,OAAM;YACR,CAAC;YAED,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,aAAa,IAAI,EAAE,CAAA;YACrD,MAAM,cAAc,GAAgC;gBAClD,UAAU,EAAE,OAAO,CAAC,WAAW;gBAC/B,QAAQ,EAAE,OAAO,CAAC,WAAW;gBAC7B,SAAS,EAAE,OAAO,CAAC,KAAK;gBACxB,OAAO,EAAE,OAAO,CAAC,KAAK;aACvB,CAAA;YAED,OAAO;gBACL,MAAM,EAAE,IAAA,sBAAc,EAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC;gBAChD,cAAc;aACf,CAAA;QACH,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,MAAA,CAAA;IAC7B,CAAC;IAED;;;OAGG;IACO,KAAK,CAA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;oBACX,OAAO,MAAM,GAAG,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACrE;oBACE,OAAM;YACV,CAAC;QACH,CAAC,CAAC,CACH,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;IAClC,CAAC;CACF;AAjcD,gEAicC"}
@@ -1,2 +1,3 @@
1
- export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils";
1
+ export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/framework/utils";
2
2
  export { getLinkRepository } from "./link";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/repositories/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,IAAI,cAAc,EAAE,MAAM,2BAA2B,CAAA;AACpF,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA"}
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getLinkRepository = exports.BaseRepository = void 0;
4
- var utils_1 = require("@medusajs/utils");
4
+ var utils_1 = require("@medusajs/framework/utils");
5
5
  Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return utils_1.MikroOrmBaseRepository; } });
6
6
  var link_1 = require("./link");
7
7
  Object.defineProperty(exports, "getLinkRepository", { enumerable: true, get: function () { return link_1.getLinkRepository; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/repositories/index.ts"],"names":[],"mappings":";;;AAAA,mDAAoF;AAA3E,uGAAA,sBAAsB,OAAkB;AACjD,+BAA0C;AAAjC,yGAAA,iBAAiB,OAAA"}
@@ -1,4 +1,4 @@
1
- import { Context, ModuleJoinerConfig } from "@medusajs/types";
1
+ import { Context, ModuleJoinerConfig } from "@medusajs/framework/types";
2
2
  import { EntitySchema } from "@mikro-orm/core";
3
3
  export declare function getLinkRepository(model: EntitySchema): {
4
4
  new ({ joinerConfig }: {
@@ -10,28 +10,28 @@ export declare function getLinkRepository(model: EntitySchema): {
10
10
  update(data: {
11
11
  entity: any;
12
12
  update: any;
13
- }[], context?: Context | undefined): Promise<object[]>;
14
- find(options?: import("@medusajs/types").FindOptions<object> | undefined, context?: Context | undefined): Promise<object[]>;
15
- findAndCount(options?: import("@medusajs/types").FindOptions<object> | undefined, context?: Context | undefined): Promise<[object[], number]>;
16
- upsert(data: unknown[], context?: Context | undefined): Promise<object[]>;
17
- upsertWithReplace(data: unknown[], config?: import("@medusajs/types").UpsertWithReplaceConfig<object> | undefined, context?: Context | undefined): Promise<object[]>;
18
- softDelete(idsOrFilter: string[] | {
19
- [x: string]: any;
20
- }, sharedContext?: Context | undefined): Promise<[object[], Record<string, unknown[]>]>;
21
- restore(idsOrFilter: string[] | {
22
- [x: string]: any;
23
- }, sharedContext?: Context | undefined): Promise<[object[], Record<string, unknown[]>]>;
24
- applyFreeTextSearchFilters<T>(findOptions: import("@medusajs/types").FindOptions<T & {
25
- q?: string | undefined;
13
+ }[], context?: Context): Promise<object[]>;
14
+ find(options?: import("@medusajs/framework/types").FindOptions<object> | undefined, context?: Context): Promise<object[]>;
15
+ findAndCount(options?: import("@medusajs/framework/types").FindOptions<object> | undefined, context?: Context): Promise<[object[], number]>;
16
+ upsert(data: unknown[], context?: Context): Promise<object[]>;
17
+ upsertWithReplace(data: unknown[], config?: import("@medusajs/framework/types").UpsertWithReplaceConfig<object> | undefined, context?: Context): Promise<{
18
+ entities: object[];
19
+ performedActions: import("@medusajs/framework/types").PerformedActions;
20
+ }>;
21
+ softDelete(filters: string | string[] | (object & import("@medusajs/framework/types").BaseFilterable<object>) | (object & import("@medusajs/framework/types").BaseFilterable<object>)[], sharedContext?: Context): Promise<[object[], Record<string, unknown[]>]>;
22
+ restore(idsOrFilter: string[] | import("@medusajs/framework/types").FilterQuery, sharedContext?: Context): Promise<[object[], Record<string, unknown[]>]>;
23
+ applyFreeTextSearchFilters<T>(findOptions: import("@medusajs/framework/types").FindOptions<T & {
24
+ q?: string;
26
25
  }>, retrieveConstraintsToApply: (q: string) => any[]): void;
27
26
  readonly manager_: any;
28
27
  getFreshManager<TManager = unknown>(): TManager;
29
- getActiveManager<TManager_1 = unknown>({ transactionManager, manager, }?: Context | undefined): TManager_1;
30
- transaction<TManager_2 = unknown>(task: (transactionManager: TManager_2) => Promise<any>, options?: {
31
- isolationLevel?: string | undefined;
32
- enableNestedTransactions?: boolean | undefined;
33
- transaction?: TManager_2 | undefined;
34
- } | undefined): Promise<any>;
28
+ getActiveManager<TManager = unknown>({ transactionManager, manager, }?: Context): TManager;
29
+ transaction<TManager = unknown>(task: (transactionManager: TManager) => Promise<any>, options?: {
30
+ isolationLevel?: string;
31
+ enableNestedTransactions?: boolean;
32
+ transaction?: TManager;
33
+ }): Promise<any>;
35
34
  serialize<TOutput extends object | object[]>(data: any, options?: any): Promise<TOutput>;
36
35
  };
37
36
  };
37
+ //# sourceMappingURL=link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../../src/repositories/link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAQ9C,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,YAAY;2BAInB;QAAE,YAAY,EAAE,kBAAkB,CAAA;KAAE;gCAF1C,kBAAkB;qBAQvB,GAAG,YAAW,OAAO,GAAQ,OAAO,CAAC,IAAI,CAAC;qBAY1C,MAAM,EAAE,YAAW,OAAO,GAAQ,OAAO,CAAC,MAAM,EAAE,CAAC;;;;;;;;;;;;;;;aAkBuqC,CAAC;;;;;;0BA5B5uC,CAAN;oCACU,CAAC;uBACM,CAAC;;;;EAyBjB"}
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getLinkRepository = void 0;
4
- const utils_1 = require("@medusajs/utils");
3
+ exports.getLinkRepository = getLinkRepository;
4
+ const utils_1 = require("@medusajs/framework/utils");
5
5
  function getLinkRepository(model) {
6
6
  return class LinkRepository extends (0, utils_1.mikroOrmBaseRepositoryFactory)(model) {
7
7
  constructor({ joinerConfig }) {
@@ -31,4 +31,4 @@ function getLinkRepository(model) {
31
31
  }
32
32
  };
33
33
  }
34
- exports.getLinkRepository = getLinkRepository;
34
+ //# sourceMappingURL=link.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link.js","sourceRoot":"","sources":["../../src/repositories/link.ts"],"names":[],"mappings":";;AASA,8CAuCC;AA7CD,qDAGkC;AAGlC,SAAgB,iBAAiB,CAAC,KAAmB;IACnD,OAAO,MAAM,cAAe,SAAQ,IAAA,qCAA6B,EAAC,KAAK,CAAC;QAGtE,YAAY,EAAE,YAAY,EAAwC;YAChE,aAAa;YACb,KAAK,CAAC,GAAG,SAAS,CAAC,CAAA;YACnB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAA;QACnC,CAAC;QAED,KAAK,CAAC,MAAM,CAAC,IAAS,EAAE,UAAmB,EAAE;YAC3C,MAAM,MAAM,GAAG,EAAE,CAAA;YACjB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;gBACvB,MAAM,CAAC,GAAG,CAAC,GAAG;oBACZ,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;iBACxD,CAAA;YACH,CAAC;YAED,MAAM,OAAO,GAAG,IAAI,CAAC,gBAAgB,CAAmB,OAAO,CAAC,CAAA;YAChE,MAAM,OAAO,CAAC,YAAY,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,CAAA;QAC7C,CAAC;QAED,KAAK,CAAC,MAAM,CAAC,IAAc,EAAE,UAAmB,EAAE;YAChD,MAAM,OAAO,GAAG,IAAI,CAAC,gBAAgB,CAAmB,OAAO,CAAC,CAAA;YAEhE,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,IAAS,EAAE,EAAE;gBACnC,IAAI,CAAC,EAAE,GAAG,IAAA,wBAAgB,EACxB,IAAI,CAAC,EAAE,EACP,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE,QAAQ,IAAI,MAAM,CACtD,CAAA;gBACD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;gBACtB,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;YACpC,CAAC,CAAC,CAAA;YAEF,MAAM,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;YAEtC,OAAO,KAAK,CAAA;QACd,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -1,7 +1,8 @@
1
- import { Constructor, ILinkModule, ModuleJoinerConfig } from "@medusajs/types";
1
+ import { Constructor, ILinkModule, ModuleJoinerConfig } from "@medusajs/framework/types";
2
2
  export declare function getModuleService(joinerConfig: ModuleJoinerConfig): Constructor<ILinkModule>;
3
3
  export declare function getReadOnlyModuleService(joinerConfig: ModuleJoinerConfig): {
4
4
  new (): {
5
5
  __joinerConfig(): ModuleJoinerConfig;
6
6
  };
7
7
  };
8
+ //# sourceMappingURL=dynamic-service-class.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamic-service-class.d.ts","sourceRoot":"","sources":["../../src/services/dynamic-service-class.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,WAAW,EACX,kBAAkB,EACnB,MAAM,2BAA2B,CAAA;AAIlC,wBAAgB,gBAAgB,CAC9B,YAAY,EAAE,kBAAkB,GAC/B,WAAW,CAAC,WAAW,CAAC,CAmB1B;AAED,wBAAgB,wBAAwB,CAAC,YAAY,EAAE,kBAAkB;;0BAEnD,kBAAkB;;EAIvC"}
@@ -1,17 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getReadOnlyModuleService = exports.getModuleService = void 0;
3
+ exports.getModuleService = getModuleService;
4
+ exports.getReadOnlyModuleService = getReadOnlyModuleService;
5
+ const utils_1 = require("@medusajs/framework/utils");
4
6
  const _services_1 = require(".");
5
7
  function getModuleService(joinerConfig) {
6
8
  const joinerConfig_ = JSON.parse(JSON.stringify(joinerConfig));
9
+ const databaseConfig = joinerConfig_.databaseConfig;
7
10
  delete joinerConfig_.databaseConfig;
11
+ // If extraDataFields is not defined, pick the fields to populate and validate from the
12
+ // database config if any fields are provided.
13
+ if (!(0, utils_1.isDefined)(joinerConfig_.extraDataFields)) {
14
+ joinerConfig_.extraDataFields = Object.keys(databaseConfig?.extraFields || {});
15
+ }
8
16
  return class LinkService extends _services_1.LinkModuleService {
9
17
  __joinerConfig() {
10
18
  return joinerConfig_;
11
19
  }
12
20
  };
13
21
  }
14
- exports.getModuleService = getModuleService;
15
22
  function getReadOnlyModuleService(joinerConfig) {
16
23
  return class ReadOnlyLinkService {
17
24
  __joinerConfig() {
@@ -19,4 +26,4 @@ function getReadOnlyModuleService(joinerConfig) {
19
26
  }
20
27
  };
21
28
  }
22
- exports.getReadOnlyModuleService = getReadOnlyModuleService;
29
+ //# sourceMappingURL=dynamic-service-class.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dynamic-service-class.js","sourceRoot":"","sources":["../../src/services/dynamic-service-class.ts"],"names":[],"mappings":";;AAQA,4CAqBC;AAED,4DAMC;AAhCD,qDAAqD;AACrD,yCAA6C;AAE7C,SAAgB,gBAAgB,CAC9B,YAAgC;IAEhC,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAA;IAC9D,MAAM,cAAc,GAAG,aAAa,CAAC,cAAc,CAAA;IAEnD,OAAO,aAAa,CAAC,cAAc,CAAA;IAEnC,uFAAuF;IACvF,8CAA8C;IAC9C,IAAI,CAAC,IAAA,iBAAS,EAAC,aAAa,CAAC,eAAe,CAAC,EAAE,CAAC;QAC9C,aAAa,CAAC,eAAe,GAAG,MAAM,CAAC,IAAI,CACzC,cAAc,EAAE,WAAW,IAAI,EAAE,CAClC,CAAA;IACH,CAAC;IAED,OAAO,MAAM,WAAY,SAAQ,6BAAiB;QACvC,cAAc;YACrB,OAAO,aAAmC,CAAA;QAC5C,CAAC;KACF,CAAA;AACH,CAAC;AAED,SAAgB,wBAAwB,CAAC,YAAgC;IACvE,OAAO,MAAM,mBAAmB;QAC9B,cAAc;YACZ,OAAO,YAAkC,CAAA;QAC3C,CAAC;KACF,CAAA;AACH,CAAC"}
@@ -1,3 +1,4 @@
1
1
  export * from "./dynamic-service-class";
2
2
  export { default as LinkService } from "./link";
3
3
  export { default as LinkModuleService } from "./link-module-service";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAA;AACvC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,QAAQ,CAAA;AAC/C,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,uBAAuB,CAAA"}
@@ -23,3 +23,4 @@ var link_1 = require("./link");
23
23
  Object.defineProperty(exports, "LinkService", { enumerable: true, get: function () { return __importDefault(link_1).default; } });
24
24
  var link_module_service_1 = require("./link-module-service");
25
25
  Object.defineProperty(exports, "LinkModuleService", { enumerable: true, get: function () { return __importDefault(link_module_service_1).default; } });
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,0DAAuC;AACvC,+BAA+C;AAAtC,oHAAA,OAAO,OAAe;AAC/B,6DAAoE;AAA3D,yIAAA,OAAO,OAAqB"}
@@ -1,26 +1,27 @@
1
- import { Context, DAL, FindConfig, IEventBusModuleService, ILinkModule, InternalModuleDeclaration, ModuleJoinerConfig, RestoreReturn, SoftDeleteReturn } from "@medusajs/types";
1
+ import { Context, DAL, FindConfig, IEventBusModuleService, ILinkModule, InternalModuleDeclaration, ModuleJoinerConfig, RestoreReturn, SoftDeleteReturn } from "@medusajs/framework/types";
2
+ import { Modules } from "@medusajs/framework/utils";
2
3
  import { LinkService } from ".";
3
4
  type InjectedDependencies = {
4
5
  baseRepository: DAL.RepositoryService;
5
6
  linkService: LinkService<any>;
6
- eventBusModuleService?: IEventBusModuleService;
7
7
  primaryKey: string | string[];
8
8
  foreignKey: string;
9
9
  extraFields: string[];
10
10
  entityName: string;
11
11
  serviceName: string;
12
+ [Modules.EVENT_BUS]?: IEventBusModuleService;
12
13
  };
13
- export default class LinkModuleService<TLink> implements ILinkModule {
14
+ export default class LinkModuleService implements ILinkModule {
14
15
  readonly moduleDeclaration: InternalModuleDeclaration;
15
16
  protected baseRepository_: DAL.RepositoryService;
16
- protected readonly linkService_: LinkService<TLink>;
17
+ protected readonly linkService_: LinkService<any>;
17
18
  protected readonly eventBusModuleService_?: IEventBusModuleService;
18
19
  protected readonly entityName_: string;
19
20
  protected readonly serviceName_: string;
20
21
  protected primaryKey_: string[];
21
22
  protected foreignKey_: string;
22
23
  protected extraFields_: string[];
23
- constructor({ baseRepository, linkService, eventBusModuleService, primaryKey, foreignKey, extraFields, entityName, serviceName, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
24
+ constructor({ baseRepository, linkService, primaryKey, foreignKey, extraFields, entityName, serviceName, [Modules.EVENT_BUS]: eventBusModuleService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
24
25
  __joinerConfig(): ModuleJoinerConfig;
25
26
  private buildData;
26
27
  private isValidKeyName;
@@ -28,8 +29,8 @@ export default class LinkModuleService<TLink> implements ILinkModule {
28
29
  retrieve(primaryKeyData: string | string[], foreignKeyData: string, sharedContext?: Context): Promise<unknown>;
29
30
  list(filters?: Record<string, unknown>, config?: FindConfig<unknown>, sharedContext?: Context): Promise<unknown[]>;
30
31
  listAndCount(filters?: Record<string, unknown>, config?: FindConfig<unknown>, sharedContext?: Context): Promise<[unknown[], number]>;
31
- create(primaryKeyOrBulkData: string | string[] | [string | string[], string, Record<string, unknown>][], foreignKeyData?: string, extraFields?: Record<string, unknown>, sharedContext?: Context): Promise<object[]>;
32
- dismiss(primaryKeyOrBulkData: string | string[] | [string | string[], string][], foreignKeyData?: string, sharedContext?: Context): Promise<object[]>;
32
+ create(primaryKeyOrBulkData: string | string[] | [string | string[], string, Record<string, unknown>][], foreignKeyData?: string, extraFields?: Record<string, unknown>, sharedContext?: Context): Promise<any[]>;
33
+ dismiss(primaryKeyOrBulkData: string | string[] | [string | string[], string][], foreignKeyData?: string, sharedContext?: Context): Promise<any[]>;
33
34
  delete(data: any, sharedContext?: Context): Promise<void>;
34
35
  softDelete(data: any, { returnLinkableKeys }?: SoftDeleteReturn, sharedContext?: Context): Promise<Record<string, unknown[]> | void>;
35
36
  protected softDelete_(data: any[], sharedContext?: Context): Promise<[object[], Record<string, string[]>]>;
@@ -37,3 +38,4 @@ export default class LinkModuleService<TLink> implements ILinkModule {
37
38
  restore_(data: any, sharedContext?: Context): Promise<[object[], Record<string, string[]>]>;
38
39
  }
39
40
  export {};
41
+ //# sourceMappingURL=link-module-service.d.ts.map