Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

chore: v2.0.0-canary.45

vholik66c6b4a4c31b
diff --git a/packages/admin/package.json b/packages/admin/package.json
index 83f780322..70167623c 100644
--- a/packages/admin/package.json
+++ b/packages/admin/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/admin",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/cli/package.json b/packages/cli/package.json
index dd6eefc85..b74b384d3 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/cli",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/client/package.json b/packages/client/package.json
index ca094e754..9a7ee8d4a 100644
--- a/packages/client/package.json
+++ b/packages/client/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/client",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/core-plugin/package.json b/packages/core-plugin/package.json
index ce3b25155..92c36e406 100644
--- a/packages/core-plugin/package.json
+++ b/packages/core-plugin/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/core-plugin",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/core-plugin/src/modules/custom-fields/services/custom-fields-module-service.ts b/packages/core-plugin/src/modules/custom-fields/services/custom-fields-module-service.ts
index 11845f419..36a488326 100644
--- a/packages/core-plugin/src/modules/custom-fields/services/custom-fields-module-service.ts
+++ b/packages/core-plugin/src/modules/custom-fields/services/custom-fields-module-service.ts
@@ -39,7 +39,7 @@ export default class CustomFieldsModuleService {
for (const entityName of entityNames) {
const entityAlias = joinerConfigAliases.find(
- (alias) => alias?.entity === entityName
+ (alias) => alias?.entity === entityName,
);
if (!entityAlias) {
@@ -64,16 +64,19 @@ export default class CustomFieldsModuleService {
config: FindConfig<any>,
) {
if (Object.keys(filter).length !== 1) {
- throw new MedusaError(MedusaError.Types.INVALID_ARGUMENT, 'Only single filter is allowed');
+ throw new MedusaError(
+ MedusaError.Types.INVALID_ARGUMENT,
+ "Only single filter is allowed",
+ );
}
const filterKey = Object.keys(filter)[0];
const filterValue = filter[filterKey];
- const alias = filterKey.split('_').slice(0, -1).join('_');
+ const alias = filterKey.split("_").slice(0, -1).join("_");
const tableName = compressName(`${alias}_custom_fields`);
const knex = this.pgConnection_;
- const query = knex(tableName).whereNull('deleted_at');
+ const query = knex(tableName).whereNull("deleted_at");
if (Array.isArray(filterValue)) {
query.whereIn(filterKey, filterValue);
@@ -94,51 +97,65 @@ export default class CustomFieldsModuleService {
if (!snakeEntity) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
- `Unknown custom fields alias: ${alias}`
+ `Unknown custom fields alias: ${alias}`,
);
}
return snakeEntity;
}
- async create(
+ async upsert(
alias: string,
- data: { id: string;[key: string]: unknown } | { id: string;[key: string]: unknown }[]
+ data:
+ | { id: string;[key: string]: unknown }
+ | { id: string;[key: string]: unknown }[],
) {
const items = Array.isArray(data) ? data : [data];
const snakeEntity = this.resolveAlias_(alias);
+ const foreignKey = snakeEntity + "_id";
const tableName = compressName(`${snakeEntity}_custom_fields`);
const knex = this.pgConnection_;
- const rows = items.map((entry) => {
- const { id, ...fields } = entry
- return {
- ...fields,
- [snakeEntity + '_id']: id,
- id: generateEntityId(undefined as any, "cf"),
+ const foreignKeyValues = items.map((entry) => entry.id);
+
+ const existing = await knex(tableName)
+ .whereIn(foreignKey, foreignKeyValues)
+ .whereNull("deleted_at");
+
+ const existingMap = new Map<
+ string,
+ { id: string; fields: Record<string, unknown> }
+ >(
+ existing.map((row) => [
+ row[foreignKey],
+ row,
+ ]),
+ );
+
+ const toInsert: Record<string, unknown>[] = [];
+ const toUpdate: { id: string; fields: Record<string, unknown> }[] = [];
+
+ for (const item of items) {
+ const { id, ...fields } = item;
+ const existingRow = existingMap.get(id);
+ if (existingRow) {
+ toUpdate.push({ id: existingRow.id as string, fields });
+ } else {
+ toInsert.push({
+ ...fields,
+ [foreignKey]: id,
+ id: generateEntityId(undefined as any, "cf"),
+ });
}
- });
-
- await knex.transaction(async (trx) => {
- await trx(tableName).insert(rows);
- });
-
- return rows;
- }
-
- async update(
- alias: string,
- data: { id: string;[key: string]: unknown } | { id: string;[key: string]: unknown }[],
- ) {
- const items = Array.isArray(data) ? data : [data];
- const snakeEntity = this.resolveAlias_(alias);
- const tableName = compressName(`${snakeEntity}_custom_fields`);
- const knex = this.pgConnection_;
+ }
await knex.transaction(async (trx) => {
- for (const { id, ...fields } of items) {
+ if (toInsert.length) {
+ await trx(tableName).insert(toInsert);
+ }
+ for (const { id, fields } of toUpdate) {
await trx(tableName)
- .where(`${snakeEntity + '_id'}`, id)
+ .where("id", id)
.update({ ...fields, updated_at: new Date() });
}
});
@@ -146,10 +163,7 @@ export default class CustomFieldsModuleService {
return items;
}
- async delete(
- alias: string,
- ids: string | string[],
- ) {
+ async delete(alias: string, ids: string | string[]) {
const items = Array.isArray(ids) ? ids : [ids];
const snakeEntity = this.resolveAlias_(alias);
const tableName = compressName(`${snakeEntity}_custom_fields`);
@@ -157,7 +171,7 @@ export default class CustomFieldsModuleService {
await knex.transaction(async (trx) => {
await trx(tableName)
- .whereIn('id', items)
+ .whereIn("id", items)
.update({ deleted_at: new Date() });
});
}
diff --git a/packages/core-plugin/src/workflows/custom-fields/steps/create-custom-fields.ts b/packages/core-plugin/src/workflows/custom-fields/steps/create-custom-fields.ts
deleted file mode 100644
index c3dab0111..000000000
--- a/packages/core-plugin/src/workflows/custom-fields/steps/create-custom-fields.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
-import { MercurModules } from "@mercurjs/types"
-
-import CustomFieldsModuleService from "../../../modules/custom-fields/services/custom-fields-module-service"
-
-type CreateCustomFieldsStepInput = {
- alias: string
- data: { id: string; [key: string]: unknown }[]
-}
-
-export const createCustomFieldsStep = createStep(
- "create-custom-fields",
- async (input: CreateCustomFieldsStepInput, { container }) => {
- const service = container.resolve<CustomFieldsModuleService>(MercurModules.CUSTOM_FIELDS)
-
- const created = await service.create(input.alias, input.data)
-
- return new StepResponse(created, {
- alias: input.alias,
- ids: created.map((row) => row.id),
- })
- },
- async (data, { container }) => {
- if (!data) return
-
- const service = container.resolve<CustomFieldsModuleService>(MercurModules.CUSTOM_FIELDS)
-
- await service.delete(data.alias, data.ids)
- }
-)
diff --git a/packages/core-plugin/src/workflows/custom-fields/steps/index.ts b/packages/core-plugin/src/workflows/custom-fields/steps/index.ts
index 6b3c2411b..3fd19f492 100644
--- a/packages/core-plugin/src/workflows/custom-fields/steps/index.ts
+++ b/packages/core-plugin/src/workflows/custom-fields/steps/index.ts
@@ -1,3 +1,2 @@
-export * from "./create-custom-fields"
-export * from "./update-custom-fields"
+export * from "./upsert-custom-fields"
export * from "./delete-custom-fields"
diff --git a/packages/core-plugin/src/workflows/custom-fields/steps/update-custom-fields.ts b/packages/core-plugin/src/workflows/custom-fields/steps/update-custom-fields.ts
deleted file mode 100644
index 6e20350c2..000000000
--- a/packages/core-plugin/src/workflows/custom-fields/steps/update-custom-fields.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
-import { MercurModules } from "@mercurjs/types"
-
-import CustomFieldsModuleService from "../../../modules/custom-fields/services/custom-fields-module-service"
-
-type UpdateCustomFieldsStepInput = {
- alias: string
- data: { id: string; [key: string]: unknown }[]
-}
-
-export const updateCustomFieldsStep = createStep(
- "update-custom-fields",
- async (input: UpdateCustomFieldsStepInput, { container }) => {
- const service = container.resolve<CustomFieldsModuleService>(MercurModules.CUSTOM_FIELDS)
-
- const previousData = await service.list(
- { [input.data[0] ? Object.keys(input.data[0]).find((k) => k.endsWith("_id")) || "id" : "id"]: input.data.map((d) => d.id) },
- {}
- )
-
- const updated = await service.update(input.alias, input.data)
-
- return new StepResponse(updated, {
- alias: input.alias,
- data: previousData,
- })
- },
- async (data, { container }) => {
- if (!data) return
-
- const service = container.resolve<CustomFieldsModuleService>(MercurModules.CUSTOM_FIELDS)
-
- await service.update(data.alias, data.data)
- }
-)
diff --git a/packages/core-plugin/src/workflows/custom-fields/steps/upsert-custom-fields.ts b/packages/core-plugin/src/workflows/custom-fields/steps/upsert-custom-fields.ts
new file mode 100644
index 000000000..9aacf2f33
--- /dev/null
+++ b/packages/core-plugin/src/workflows/custom-fields/steps/upsert-custom-fields.ts
@@ -0,0 +1,20 @@
+import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
+import { MercurModules } from "@mercurjs/types"
+
+import CustomFieldsModuleService from "../../../modules/custom-fields/services/custom-fields-module-service"
+
+type UpsertCustomFieldsStepInput = {
+ alias: string
+ data: { id: string; [key: string]: unknown } | { id: string; [key: string]: unknown }[]
+}
+
+export const upsertCustomFieldsStep = createStep(
+ "upsert-custom-fields",
+ async (input: UpsertCustomFieldsStepInput, { container }) => {
+ const service = container.resolve<CustomFieldsModuleService>(MercurModules.CUSTOM_FIELDS)
+
+ const result = await service.upsert(input.alias, input.data)
+
+ return new StepResponse(result, input)
+ }
+)
diff --git a/packages/dashboard-sdk/package.json b/packages/dashboard-sdk/package.json
index 37b5533cf..9fe97d44f 100644
--- a/packages/dashboard-sdk/package.json
+++ b/packages/dashboard-sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/dashboard-sdk",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/dashboard-shared/package.json b/packages/dashboard-shared/package.json
index e882f5a65..50bed62e5 100644
--- a/packages/dashboard-shared/package.json
+++ b/packages/dashboard-shared/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/dashboard-shared",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/types/package.json b/packages/types/package.json
index fc05d10ca..b80d9faeb 100644
--- a/packages/types/package.json
+++ b/packages/types/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/types",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",
diff --git a/packages/vendor/package.json b/packages/vendor/package.json
index 066bfcf17..a96f4cbd8 100644
--- a/packages/vendor/package.json
+++ b/packages/vendor/package.json
@@ -1,6 +1,6 @@
{
"name": "@mercurjs/vendor",
- "version": "2.0.0-canary.44",
+ "version": "2.0.0-canary.45",
"repository": {
"type": "git",
"url": "https://github.com/mercurjs/mercur",