From d9636f46313cd0e514b36e2b4eddd2993b930d8b Mon Sep 17 00:00:00 2001 From: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Fri, 23 Feb 2024 08:59:02 +0100 Subject: [PATCH] chore(cart): Make all cart entities soft-deletable (#6475) --- .../src/migrations/.snapshot-medusa-cart.json | 477 +++++++++++++----- ...22122952.ts => Migration20240222170223.ts} | 55 +- packages/cart/src/models/address.ts | 18 +- packages/cart/src/models/adjustment-line.ts | 8 +- packages/cart/src/models/cart.ts | 62 ++- .../cart/src/models/line-item-adjustment.ts | 34 +- .../cart/src/models/line-item-tax-line.ts | 34 +- packages/cart/src/models/line-item.ts | 57 ++- .../src/models/shipping-method-adjustment.ts | 34 +- .../src/models/shipping-method-tax-line.ts | 34 +- packages/cart/src/models/shipping-method.ts | 52 +- packages/cart/src/models/tax-line.ts | 13 +- packages/types/src/cart/service.ts | 99 ++++ 13 files changed, 763 insertions(+), 214 deletions(-) rename packages/cart/src/migrations/{CartModuleSetup20240122122952.ts => Migration20240222170223.ts} (74%) diff --git a/packages/cart/src/migrations/.snapshot-medusa-cart.json b/packages/cart/src/migrations/.snapshot-medusa-cart.json index 1a4401ec62..d6d42245e3 100644 --- a/packages/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/cart/src/migrations/.snapshot-medusa-cart.json @@ -144,11 +144,31 @@ "length": 6, "default": "now()", "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_address", "schema": "public", "indexes": [ + { + "keyName": "IDX_cart_address_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_address_deleted_at\" ON \"cart_address\" (deleted_at) WHERE deleted_at IS NOT NULL" + }, { "keyName": "cart_address_pkey", "columnNames": [ @@ -283,19 +303,19 @@ "indexes": [ { "columnNames": [ - "customer_id" + "sales_channel_id" ], "composite": false, - "keyName": "IDX_cart_customer_id", + "keyName": "IDX_cart_sales_channel_id", "primary": false, "unique": false }, { "columnNames": [ - "shipping_address_id" + "currency_code" ], "composite": false, - "keyName": "IDX_cart_shipping_address_id", + "keyName": "IDX_cart_curency_code", "primary": false, "unique": false }, @@ -308,6 +328,66 @@ "primary": false, "unique": false }, + { + "keyName": "IDX_cart_region_id", + "columnNames": [ + "region_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_region_id\" ON \"cart\" (region_id) WHERE deleted_at IS NULL AND region_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_customer_id", + "columnNames": [ + "customer_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_customer_id\" ON \"cart\" (customer_id) WHERE deleted_at IS NULL AND customer_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_sales_channel_id", + "columnNames": [ + "sales_channel_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_sales_channel_id\" ON \"cart\" (sales_channel_id) WHERE deleted_at IS NULL AND sales_channel_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_shipping_address_id", + "columnNames": [ + "shipping_address_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_address_id\" ON \"cart\" (shipping_address_id) WHERE deleted_at IS NULL AND shipping_address_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_billing_address_id", + "columnNames": [ + "billing_address_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_billing_address_id\" ON \"cart\" (billing_address_id) WHERE deleted_at IS NULL AND billing_address_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_deleted_at\" ON \"cart\" (deleted_at) WHERE deleted_at IS NOT NULL" + }, { "keyName": "cart_pkey", "columnNames": [ @@ -551,6 +631,15 @@ "nullable": true, "mappedType": "decimal" }, + "raw_compare_at_unit_price": { + "name": "raw_compare_at_unit_price", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "json" + }, "unit_price": { "name": "unit_price", "type": "numeric", @@ -560,6 +649,15 @@ "nullable": false, "mappedType": "decimal" }, + "raw_unit_price": { + "name": "raw_unit_price", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -581,28 +679,60 @@ "length": 6, "default": "now()", "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_line_item", "schema": "public", "indexes": [ { + "keyName": "IDX_line_item_cart_id", "columnNames": [ "cart_id" ], "composite": false, - "keyName": "IDX_line_item_cart_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL" }, { + "keyName": "IDX_line_item_variant_id", "columnNames": [ "variant_id" ], "composite": false, - "keyName": "IDX_line_item_variant_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_variant_id\" ON \"cart_line_item\" (variant_id) WHERE deleted_at IS NULL AND variant_id IS NOT NULL" + }, + { + "keyName": "IDX_line_item_product_id", + "columnNames": [ + "product_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_product_id\" ON \"cart_line_item\" (product_id) WHERE deleted_at IS NULL AND product_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_line_item_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_deleted_at\" ON \"cart_line_item\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_line_item_pkey", @@ -614,14 +744,7 @@ "unique": true } ], - "checks": [ - { - "name": "cart_line_item_unit_price_check", - "expression": "unit_price >= 0", - "definition": "check ((unit_price >= 0))", - "columnName": "unit_price" - } - ], + "checks": [], "foreignKeys": { "cart_line_item_cart_id_foreign": { "constraintName": "cart_line_item_cart_id_foreign", @@ -633,7 +756,6 @@ "id" ], "referencedTableName": "public.cart", - "deleteRule": "cascade", "updateRule": "cascade" } } @@ -658,15 +780,6 @@ "nullable": true, "mappedType": "text" }, - "promotion_id": { - "name": "promotion_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "code": { "name": "code", "type": "text", @@ -722,21 +835,61 @@ "unsigned": false, "autoincrement": false, "primary": false, + "nullable": false, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, "nullable": true, "mappedType": "text" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_line_item_adjustment", "schema": "public", "indexes": [ { + "keyName": "IDX_adjustment_item_id", "columnNames": [ "item_id" ], "composite": false, - "keyName": "IDX_adjustment_item_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_line_item_adjustment_promotion_id", + "columnNames": [ + "promotion_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_adjustment_promotion_id\" ON \"cart_line_item_adjustment\" (promotion_id) WHERE deleted_at IS NULL and promotion_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_line_item_adjustment_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_deleted_at\" ON \"cart_line_item_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_line_item_adjustment_pkey", @@ -755,21 +908,7 @@ "definition": "check ((amount >= 0))" } ], - "foreignKeys": { - "cart_line_item_adjustment_item_id_foreign": { - "constraintName": "cart_line_item_adjustment_item_id_foreign", - "columnNames": [ - "item_id" - ], - "localTableName": "public.cart_line_item_adjustment", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.cart_line_item", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } + "foreignKeys": {} }, { "columns": { @@ -791,15 +930,6 @@ "nullable": true, "mappedType": "text" }, - "tax_rate_id": { - "name": "tax_rate_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "code": { "name": "code", "type": "text", @@ -855,21 +985,61 @@ "unsigned": false, "autoincrement": false, "primary": false, + "nullable": false, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, "nullable": true, "mappedType": "text" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_line_item_tax_line", "schema": "public", "indexes": [ { + "keyName": "IDX_tax_line_item_id", "columnNames": [ "item_id" ], "composite": false, - "keyName": "IDX_tax_line_item_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_line_item_tax_line_tax_rate_id", + "columnNames": [ + "tax_rate_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_tax_line_tax_rate_id\" ON \"cart_line_item_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_line_item_tax_line_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_deleted_at\" ON \"cart_line_item_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_line_item_tax_line_pkey", @@ -882,21 +1052,7 @@ } ], "checks": [], - "foreignKeys": { - "cart_line_item_tax_line_item_id_foreign": { - "constraintName": "cart_line_item_tax_line_item_id_foreign", - "columnNames": [ - "item_id" - ], - "localTableName": "public.cart_line_item_tax_line", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.cart_line_item", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } + "foreignKeys": {} }, { "columns": { @@ -945,6 +1101,15 @@ "nullable": false, "mappedType": "decimal" }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "is_tax_inclusive": { "name": "is_tax_inclusive", "type": "boolean", @@ -1003,19 +1168,50 @@ "length": 6, "default": "now()", "mappedType": "datetime" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_shipping_method", "schema": "public", "indexes": [ { + "keyName": "IDX_shipping_method_cart_id", "columnNames": [ "cart_id" ], "composite": false, - "keyName": "IDX_shipping_method_cart_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_cart_id\" ON \"cart_shipping_method\" (cart_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_shipping_method_option_id", + "columnNames": [ + "shipping_option_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_option_id\" ON \"cart_shipping_method\" (shipping_option_id) WHERE deleted_at IS NULL AND shipping_option_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_shipping_method_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_shipping_method_pkey", @@ -1045,7 +1241,6 @@ "id" ], "referencedTableName": "public.cart", - "deleteRule": "cascade", "updateRule": "cascade" } } @@ -1070,15 +1265,6 @@ "nullable": true, "mappedType": "text" }, - "promotion_id": { - "name": "promotion_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "code": { "name": "code", "type": "text", @@ -1134,21 +1320,61 @@ "unsigned": false, "autoincrement": false, "primary": false, + "nullable": false, + "mappedType": "text" + }, + "promotion_id": { + "name": "promotion_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, "nullable": true, "mappedType": "text" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_shipping_method_adjustment", "schema": "public", "indexes": [ { + "keyName": "IDX_adjustment_shipping_method_id", "columnNames": [ "shipping_method_id" ], "composite": false, - "keyName": "IDX_adjustment_shipping_method_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_shipping_method_adjustment_promotion_id", + "columnNames": [ + "promotion_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_adjustment_promotion_id\" ON \"cart_shipping_method_adjustment\" (promotion_id) WHERE deleted_at IS NULL and promotion_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_shipping_method_adjustment_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_deleted_at\" ON \"cart_shipping_method_adjustment\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_shipping_method_adjustment_pkey", @@ -1161,21 +1387,7 @@ } ], "checks": [], - "foreignKeys": { - "cart_shipping_method_adjustment_shipping_method_id_foreign": { - "constraintName": "cart_shipping_method_adjustment_shipping_method_id_foreign", - "columnNames": [ - "shipping_method_id" - ], - "localTableName": "public.cart_shipping_method_adjustment", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.cart_shipping_method", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } + "foreignKeys": {} }, { "columns": { @@ -1197,15 +1409,6 @@ "nullable": true, "mappedType": "text" }, - "tax_rate_id": { - "name": "tax_rate_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": true, - "mappedType": "text" - }, "code": { "name": "code", "type": "text", @@ -1261,21 +1464,61 @@ "unsigned": false, "autoincrement": false, "primary": false, + "nullable": false, + "mappedType": "text" + }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, "nullable": true, "mappedType": "text" + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamptz", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "length": 6, + "mappedType": "datetime" } }, "name": "cart_shipping_method_tax_line", "schema": "public", "indexes": [ { + "keyName": "IDX_tax_line_shipping_method_id", "columnNames": [ "shipping_method_id" ], "composite": false, - "keyName": "IDX_tax_line_shipping_method_id", "primary": false, - "unique": false + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_tax_line_shipping_method_id\" ON \"cart_shipping_method_tax_line\" (shipping_method_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_shipping_method_tax_line_tax_rate_id", + "columnNames": [ + "tax_rate_id" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_shipping_method_tax_line_tax_rate_id\" ON \"cart_shipping_method_tax_line\" (tax_rate_id) WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL" + }, + { + "keyName": "IDX_cart_shipping_method_tax_line_deleted_at", + "columnNames": [ + "deleted_at" + ], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_tax_line_deleted_at\" ON \"cart_shipping_method_tax_line\" (deleted_at) WHERE deleted_at IS NOT NULL" }, { "keyName": "cart_shipping_method_tax_line_pkey", @@ -1288,21 +1531,7 @@ } ], "checks": [], - "foreignKeys": { - "cart_shipping_method_tax_line_shipping_method_id_foreign": { - "constraintName": "cart_shipping_method_tax_line_shipping_method_id_foreign", - "columnNames": [ - "shipping_method_id" - ], - "localTableName": "public.cart_shipping_method_tax_line", - "referencedColumnNames": [ - "id" - ], - "referencedTableName": "public.cart_shipping_method", - "deleteRule": "cascade", - "updateRule": "cascade" - } - } + "foreignKeys": {} } ] } diff --git a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts b/packages/cart/src/migrations/Migration20240222170223.ts similarity index 74% rename from packages/cart/src/migrations/CartModuleSetup20240122122952.ts rename to packages/cart/src/migrations/Migration20240222170223.ts index c178fb50c1..db2592f6f3 100644 --- a/packages/cart/src/migrations/CartModuleSetup20240122122952.ts +++ b/packages/cart/src/migrations/Migration20240222170223.ts @@ -1,6 +1,6 @@ import { Migration } from "@mikro-orm/migrations" -export class CartModuleSetup20240122122952 extends Migration { +export class Migration20240222170223 extends Migration { async up(): Promise { this.addSql( ` @@ -21,6 +21,7 @@ export class CartModuleSetup20240122122952 extends Migration { ); ALTER TABLE "cart" ADD COLUMN IF NOT EXISTS "currency_code" TEXT NOT NULL; + ALTER TABLE "cart" ADD COLUMN IF NOT EXISTS "deleted_at" TIMESTAMPTZ NULL; ALTER TABLE "cart" ALTER COLUMN "region_id" DROP NOT NULL; ALTER TABLE "cart" ALTER COLUMN "email" DROP NOT NULL; @@ -31,11 +32,11 @@ export class CartModuleSetup20240122122952 extends Migration { ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_a2bd3c26f42e754b9249ba78fd6"; ALTER TABLE "cart" DROP CONSTRAINT IF EXISTS "FK_ced15a9a695d2b5db9dabce763d"; - CREATE INDEX IF NOT EXISTS "IDX_cart_customer_id" ON "cart" ("customer_id"); - CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_address_id" ON "cart" ("shipping_address_id"); - CREATE INDEX IF NOT EXISTS "IDX_cart_billing_address_id" ON "cart" ("billing_address_id"); - CREATE INDEX IF NOT EXISTS "IDX_cart_region_id" ON "cart" ("region_id"); - CREATE INDEX IF NOT EXISTS "IDX_cart_sales_channel_id" ON "cart" ("sales_channel_id"); + CREATE INDEX IF NOT EXISTS "IDX_cart_customer_id" ON "cart" ("customer_id") WHERE deleted_at IS NULL AND customer_id IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_address_id" ON "cart" ("shipping_address_id") WHERE deleted_at IS NULL AND shipping_address_id IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_billing_address_id" ON "cart" ("billing_address_id") WHERE deleted_at IS NULL AND billing_address_id IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_region_id" ON "cart" ("region_id") WHERE deleted_at IS NULL AND region_id IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_sales_channel_id" ON "cart" ("sales_channel_id") WHERE deleted_at IS NULL AND sales_channel_id IS NOT NULL; CREATE INDEX IF NOT EXISTS "IDX_cart_currency_code" ON "cart" ("currency_code"); CREATE TABLE IF NOT EXISTS "cart_address" ( @@ -54,6 +55,7 @@ export class CartModuleSetup20240122122952 extends Migration { "metadata" JSONB NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, CONSTRAINT "cart_address_pkey" PRIMARY KEY ("id") ); @@ -85,6 +87,7 @@ export class CartModuleSetup20240122122952 extends Migration { "raw_unit_price" JSONB NOT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, CONSTRAINT "cart_line_item_pkey" PRIMARY KEY ("id"), CONSTRAINT cart_line_item_unit_price_check CHECK (unit_price >= 0) ); @@ -92,9 +95,9 @@ export class CartModuleSetup20240122122952 extends Migration { ALTER TABLE "cart" ADD CONSTRAINT "cart_shipping_address_id_foreign" FOREIGN KEY ("shipping_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL; ALTER TABLE "cart" ADD CONSTRAINT "cart_billing_address_id_foreign" FOREIGN KEY ("billing_address_id") REFERENCES "cart_address" ("id") ON UPDATE CASCADE ON DELETE SET NULL; - CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" ("cart_id"); - CREATE INDEX IF NOT EXISTS "IDX_line_item_product_id" ON "cart_line_item" ("product_id"); - CREATE INDEX IF NOT EXISTS "IDX_line_item_variant_id" ON "cart_line_item" ("variant_id"); + CREATE INDEX IF NOT EXISTS "IDX_line_item_cart_id" ON "cart_line_item" ("cart_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_line_item_product_id" ON "cart_line_item" ("product_id") WHERE deleted_at IS NULL AND product_id IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_line_item_variant_id" ON "cart_line_item" ("variant_id") WHERE deleted_at IS NULL AND variant_id IS NOT NULL; CREATE TABLE IF NOT EXISTS "cart_line_item_adjustment" ( @@ -106,13 +109,14 @@ export class CartModuleSetup20240122122952 extends Migration { "provider_id" TEXT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, "item_id" TEXT NULL, CONSTRAINT "cart_line_item_adjustment_pkey" PRIMARY KEY ("id"), CONSTRAINT cart_line_item_adjustment_check CHECK (amount >= 0) ); - CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" ("item_id"); - CREATE INDEX IF NOT EXISTS "IDX_line_item_adjustment_promotion_id" ON "cart_line_item_adjustment" ("promotion_id"); + CREATE INDEX IF NOT EXISTS "IDX_adjustment_item_id" ON "cart_line_item_adjustment" ("item_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_line_item_adjustment_promotion_id" ON "cart_line_item_adjustment" ("promotion_id") WHERE deleted_at IS NULL AND promotion_id IS NOT NULL; CREATE TABLE IF NOT EXISTS "cart_line_item_tax_line" ( "id" TEXT NOT NULL, @@ -123,12 +127,13 @@ export class CartModuleSetup20240122122952 extends Migration { "provider_id" TEXT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, "item_id" TEXT NULL, CONSTRAINT "cart_line_item_tax_line_pkey" PRIMARY KEY ("id") ); - CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" ("item_id"); - CREATE INDEX IF NOT EXISTS "IDX_line_item_tax_line_tax_rate_id" ON "cart_line_item_tax_line" ("tax_rate_id"); + CREATE INDEX IF NOT EXISTS "IDX_tax_line_item_id" ON "cart_line_item_tax_line" ("item_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_line_item_tax_line_tax_rate_id" ON "cart_line_item_tax_line" ("tax_rate_id") WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL; CREATE TABLE IF NOT EXISTS "cart_shipping_method" ( "id" TEXT NOT NULL, @@ -143,12 +148,13 @@ export class CartModuleSetup20240122122952 extends Migration { "metadata" JSONB NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, CONSTRAINT "cart_shipping_method_pkey" PRIMARY KEY ("id"), CONSTRAINT cart_shipping_method_check CHECK (amount >= 0) ); - CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" ("cart_id"); - CREATE INDEX IF NOT EXISTS "IDX_shipping_method_option_id" ON "cart_shipping_method" ("shipping_option_id"); + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_cart_id" ON "cart_shipping_method" ("cart_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_option_id" ON "cart_shipping_method" ("shipping_option_id") WHERE deleted_at IS NULL AND shipping_option_id IS NOT NULL; CREATE TABLE IF NOT EXISTS "cart_shipping_method_adjustment" ( "id" TEXT NOT NULL, @@ -159,12 +165,13 @@ export class CartModuleSetup20240122122952 extends Migration { "provider_id" TEXT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, "shipping_method_id" TEXT NULL, CONSTRAINT "cart_shipping_method_adjustment_pkey" PRIMARY KEY ("id") ); - CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" ("shipping_method_id"); - CREATE INDEX IF NOT EXISTS "IDX_shipping_method_adjustment_promotion_id" ON "cart_shipping_method_adjustment" ("promotion_id"); + CREATE INDEX IF NOT EXISTS "IDX_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" ("shipping_method_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_adjustment_promotion_id" ON "cart_shipping_method_adjustment" ("promotion_id") WHERE deleted_at IS NULL AND promotion_id IS NOT NULL; CREATE TABLE IF NOT EXISTS "cart_shipping_method_tax_line" ( "id" TEXT NOT NULL, @@ -175,12 +182,13 @@ export class CartModuleSetup20240122122952 extends Migration { "provider_id" TEXT NULL, "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(), + "deleted_at" TIMESTAMPTZ NULL, "shipping_method_id" TEXT NULL, CONSTRAINT "cart_shipping_method_tax_line_pkey" PRIMARY KEY ("id") ); - CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" ("shipping_method_id"); - CREATE INDEX IF NOT EXISTS "IDX_shipping_method_tax_line_tax_rate_id" ON "cart_shipping_method_tax_line" ("tax_rate_id"); + CREATE INDEX IF NOT EXISTS "IDX_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" ("shipping_method_id") WHERE deleted_at IS NULL; + CREATE INDEX IF NOT EXISTS "IDX_shipping_method_tax_line_tax_rate_id" ON "cart_shipping_method_tax_line" ("tax_rate_id") WHERE deleted_at IS NULL AND tax_rate_id IS NOT NULL; ALTER TABLE "cart_line_item" ADD CONSTRAINT "cart_line_item_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE "cart_line_item_adjustment" ADD CONSTRAINT "cart_line_item_adjustment_item_id_foreign" FOREIGN KEY ("item_id") REFERENCES "cart_line_item" ("id") ON UPDATE CASCADE ON DELETE CASCADE; @@ -188,6 +196,15 @@ export class CartModuleSetup20240122122952 extends Migration { ALTER TABLE "cart_shipping_method" ADD CONSTRAINT "cart_shipping_method_cart_id_foreign" FOREIGN KEY ("cart_id") REFERENCES "cart" ("id") ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE "cart_shipping_method_adjustment" ADD CONSTRAINT "cart_shipping_method_adjustment_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE; ALTER TABLE "cart_shipping_method_tax_line" ADD CONSTRAINT "cart_shipping_method_tax_line_shipping_method_id_foreign" FOREIGN KEY ("shipping_method_id") REFERENCES "cart_shipping_method" ("id") ON UPDATE CASCADE ON DELETE CASCADE; + + CREATE INDEX IF NOT EXISTS "IDX_cart_deleted_at" ON "cart" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_address_deleted_at" ON "cart_address" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_deleted_at" ON "cart_line_item_adjustment" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_deleted_at" ON "cart_shipping_method_adjustment" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_deleted_at" ON "cart_line_item_tax_line" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_deleted_at" ON "cart_shipping_method_tax_line" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_deleted_at" ON "cart_shipping_method" (deleted_at) WHERE deleted_at IS NOT NULL; + CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_deleted_at" ON "cart_line_item" (deleted_at) WHERE deleted_at IS NOT NULL; ` ) } diff --git a/packages/cart/src/models/address.ts b/packages/cart/src/models/address.ts index c26baa66b3..0aabf6badb 100644 --- a/packages/cart/src/models/address.ts +++ b/packages/cart/src/models/address.ts @@ -1,17 +1,23 @@ import { DAL } from "@medusajs/types" -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Entity, + Filter, OnInit, OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" -type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear +type OptionalAddressProps = DAL.SoftDeletableEntityDateColumns @Entity({ tableName: "cart_address" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class Address { [OptionalProps]: OptionalAddressProps @@ -69,6 +75,14 @@ export default class Address { }) updated_at: Date + @createPsqlIndexStatementHelper({ + tableName: "cart_address", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "caaddr") diff --git a/packages/cart/src/models/adjustment-line.ts b/packages/cart/src/models/adjustment-line.ts index 02401fef94..e3091f8531 100644 --- a/packages/cart/src/models/adjustment-line.ts +++ b/packages/cart/src/models/adjustment-line.ts @@ -1,7 +1,7 @@ import { DAL } from "@medusajs/types" import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" -type OptionalAdjustmentLineProps = DAL.EntityDateColumns // TODO: To be revisited when more clear +type OptionalAdjustmentLineProps = DAL.SoftDeletableEntityDateColumns /** * As per the Mikro ORM docs, superclasses should use the abstract class definition @@ -16,12 +16,6 @@ export default abstract class AdjustmentLine { @Property({ columnType: "text", nullable: true }) description: string | null = null - @Property({ - columnType: "text", - nullable: true, - }) - promotion_id: string | null = null - @Property({ columnType: "text", nullable: true }) code: string | null = null diff --git a/packages/cart/src/models/cart.ts b/packages/cart/src/models/cart.ts index 42b5db19fc..ba9942dfe2 100644 --- a/packages/cart/src/models/cart.ts +++ b/packages/cart/src/models/cart.ts @@ -1,10 +1,15 @@ import { DAL } from "@medusajs/types" -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Collection, Entity, + Filter, ManyToOne, OnInit, OneToMany, @@ -19,29 +24,40 @@ import ShippingMethod from "./shipping-method" type OptionalCartProps = | "shipping_address" | "billing_address" - | DAL.EntityDateColumns + | DAL.SoftDeletableEntityDateColumns @Entity({ tableName: "cart" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class Cart { [OptionalProps]?: OptionalCartProps @PrimaryKey({ columnType: "text" }) id: string - @Property({ - columnType: "text", - nullable: true, - index: "IDX_cart_region_id", - }) + @createPsqlIndexStatementHelper({ + name: "IDX_cart_region_id", + tableName: "cart", + columns: "region_id", + where: "deleted_at IS NULL AND region_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) region_id: string | null = null - @Property({ - columnType: "text", - nullable: true, - index: "IDX_cart_customer_id", - }) + @createPsqlIndexStatementHelper({ + name: "IDX_cart_customer_id", + tableName: "cart", + columns: "customer_id", + where: "deleted_at IS NULL AND customer_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) customer_id: string | null = null + @createPsqlIndexStatementHelper({ + name: "IDX_cart_sales_channel_id", + tableName: "cart", + columns: "sales_channel_id", + where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL", + }).MikroORMIndex() @Property({ columnType: "text", nullable: true, @@ -55,6 +71,12 @@ export default class Cart { @Property({ columnType: "text", index: "IDX_cart_curency_code" }) currency_code: string + @createPsqlIndexStatementHelper({ + name: "IDX_cart_shipping_address_id", + tableName: "cart", + columns: "shipping_address_id", + where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", + }).MikroORMIndex() @Property({ columnType: "text", nullable: true }) shipping_address_id?: string | null @@ -62,11 +84,16 @@ export default class Cart { entity: () => Address, fieldName: "shipping_address_id", nullable: true, - index: "IDX_cart_shipping_address_id", cascade: [Cascade.PERSIST], }) shipping_address?: Address | null + @createPsqlIndexStatementHelper({ + name: "IDX_cart_billing_address_id", + tableName: "cart", + columns: "billing_address_id", + where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", + }).MikroORMIndex() @Property({ columnType: "text", nullable: true }) billing_address_id?: string | null @@ -83,12 +110,12 @@ export default class Cart { metadata: Record | null = null @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, }) items = new Collection(this) @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, }) shipping_methods = new Collection(this) @@ -107,6 +134,11 @@ export default class Cart { }) updated_at: Date + @createPsqlIndexStatementHelper({ + tableName: "cart", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() @Property({ columnType: "timestamptz", nullable: true }) deleted_at: Date | null = null diff --git a/packages/cart/src/models/line-item-adjustment.ts b/packages/cart/src/models/line-item-adjustment.ts index a1134a1393..5bf86a9a17 100644 --- a/packages/cart/src/models/line-item-adjustment.ts +++ b/packages/cart/src/models/line-item-adjustment.ts @@ -1,9 +1,14 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Check, Entity, + Filter, ManyToOne, OnInit, Property, @@ -15,17 +20,40 @@ import LineItem from "./line-item" @Check({ expression: (columns) => `${columns.amount} >= 0`, }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class LineItemAdjustment extends AdjustmentLine { @ManyToOne({ entity: () => LineItem, - index: "IDX_adjustment_item_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) item: LineItem + @createPsqlIndexStatementHelper({ + name: "IDX_adjustment_item_id", + tableName: "cart_line_item_adjustment", + columns: "item_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) item_id: string + @createPsqlIndexStatementHelper({ + name: "IDX_line_item_adjustment_promotion_id", + tableName: "cart_line_item_adjustment", + columns: "promotion_id", + where: "deleted_at IS NULL and promotion_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) + promotion_id: string | null = null + + @createPsqlIndexStatementHelper({ + tableName: "cart_line_item_adjustment", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "caliadj") diff --git a/packages/cart/src/models/line-item-tax-line.ts b/packages/cart/src/models/line-item-tax-line.ts index 59a37dad20..1b3ceffa21 100644 --- a/packages/cart/src/models/line-item-tax-line.ts +++ b/packages/cart/src/models/line-item-tax-line.ts @@ -1,8 +1,13 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Entity, + Filter, ManyToOne, OnInit, Property, @@ -11,17 +16,40 @@ import LineItem from "./line-item" import TaxLine from "./tax-line" @Entity({ tableName: "cart_line_item_tax_line" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class LineItemTaxLine extends TaxLine { @ManyToOne({ entity: () => LineItem, - index: "IDX_tax_line_item_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST] + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) item: LineItem + @createPsqlIndexStatementHelper({ + name: "IDX_tax_line_item_id", + tableName: "cart_line_item_tax_line", + columns: "item_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) item_id: string + @createPsqlIndexStatementHelper({ + name: "IDX_line_item_tax_line_tax_rate_id", + tableName: "cart_line_item_tax_line", + columns: "tax_rate_id", + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) + tax_rate_id: string | null = null + + @createPsqlIndexStatementHelper({ + tableName: "cart_line_item_tax_line", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "calitxl") diff --git a/packages/cart/src/models/line-item.ts b/packages/cart/src/models/line-item.ts index 2cc4dc87ac..b86057b59a 100644 --- a/packages/cart/src/models/line-item.ts +++ b/packages/cart/src/models/line-item.ts @@ -1,11 +1,17 @@ import { BigNumberRawValue, DAL } from "@medusajs/types" -import { BigNumber, generateEntityId } from "@medusajs/utils" +import { + BigNumber, + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, BeforeUpdate, Cascade, Collection, Entity, + Filter, ManyToOne, OnInit, OneToMany, @@ -23,23 +29,28 @@ type OptionalLineItemProps = | "compare_at_unit_price" | "requires_shipping" | "cart" - | DAL.EntityDateColumns + | DAL.SoftDeletableEntityDateColumns @Entity({ tableName: "cart_line_item" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class LineItem { [OptionalProps]?: OptionalLineItemProps @PrimaryKey({ columnType: "text" }) id: string + @createPsqlIndexStatementHelper({ + name: "IDX_line_item_cart_id", + tableName: "cart_line_item", + columns: "cart_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) cart_id: string @ManyToOne({ entity: () => Cart, - onDelete: "cascade", - index: "IDX_line_item_cart_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) cart: Cart @@ -55,18 +66,22 @@ export default class LineItem { @Property({ columnType: "integer" }) quantity: number - @Property({ - columnType: "text", - nullable: true, - index: "IDX_line_item_variant_id", - }) + @createPsqlIndexStatementHelper({ + name: "IDX_line_item_variant_id", + tableName: "cart_line_item", + columns: "variant_id", + where: "deleted_at IS NULL AND variant_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) variant_id: string | null = null - @Property({ - columnType: "text", - nullable: true, - index: "IDX_line_item_product_id", - }) + @createPsqlIndexStatementHelper({ + name: "IDX_line_item_product_id", + tableName: "cart_line_item", + columns: "product_id", + where: "deleted_at IS NULL AND product_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) product_id: string | null = null @Property({ columnType: "text", nullable: true }) @@ -121,12 +136,12 @@ export default class LineItem { raw_unit_price: BigNumberRawValue @OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, }) tax_lines = new Collection(this) @OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, }) adjustments = new Collection(this) @@ -145,6 +160,14 @@ export default class LineItem { }) updated_at: Date + @createPsqlIndexStatementHelper({ + tableName: "cart_line_item", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "cali") diff --git a/packages/cart/src/models/shipping-method-adjustment.ts b/packages/cart/src/models/shipping-method-adjustment.ts index 16a49fdb43..2d97ea3b05 100644 --- a/packages/cart/src/models/shipping-method-adjustment.ts +++ b/packages/cart/src/models/shipping-method-adjustment.ts @@ -1,8 +1,13 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Entity, + Filter, ManyToOne, OnInit, Property, @@ -11,17 +16,40 @@ import AdjustmentLine from "./adjustment-line" import ShippingMethod from "./shipping-method" @Entity({ tableName: "cart_shipping_method_adjustment" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class ShippingMethodAdjustment extends AdjustmentLine { @ManyToOne({ entity: () => ShippingMethod, - index: "IDX_adjustment_shipping_method_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) shipping_method: ShippingMethod + @createPsqlIndexStatementHelper({ + name: "IDX_adjustment_shipping_method_id", + tableName: "cart_shipping_method_adjustment", + columns: "shipping_method_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) shipping_method_id: string + @createPsqlIndexStatementHelper({ + name: "IDX_shipping_method_adjustment_promotion_id", + tableName: "cart_shipping_method_adjustment", + columns: "promotion_id", + where: "deleted_at IS NULL and promotion_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) + promotion_id: string | null = null + + @createPsqlIndexStatementHelper({ + tableName: "cart_shipping_method_adjustment", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "casmadj") diff --git a/packages/cart/src/models/shipping-method-tax-line.ts b/packages/cart/src/models/shipping-method-tax-line.ts index 4673d19a43..ad4c30b9a9 100644 --- a/packages/cart/src/models/shipping-method-tax-line.ts +++ b/packages/cart/src/models/shipping-method-tax-line.ts @@ -1,8 +1,13 @@ -import { generateEntityId } from "@medusajs/utils" +import { + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Entity, + Filter, ManyToOne, OnInit, Property, @@ -11,17 +16,40 @@ import ShippingMethod from "./shipping-method" import TaxLine from "./tax-line" @Entity({ tableName: "cart_shipping_method_tax_line" }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class ShippingMethodTaxLine extends TaxLine { @ManyToOne({ entity: () => ShippingMethod, - index: "IDX_tax_line_shipping_method_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) shipping_method: ShippingMethod + @createPsqlIndexStatementHelper({ + name: "IDX_tax_line_shipping_method_id", + tableName: "cart_shipping_method_tax_line", + columns: "shipping_method_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) shipping_method_id: string + @createPsqlIndexStatementHelper({ + name: "IDX_shipping_method_tax_line_tax_rate_id", + tableName: "cart_shipping_method_tax_line", + columns: "tax_rate_id", + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) + tax_rate_id: string | null = null + + @createPsqlIndexStatementHelper({ + tableName: "cart_shipping_method_tax_line", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "casmtxl") diff --git a/packages/cart/src/models/shipping-method.ts b/packages/cart/src/models/shipping-method.ts index 9daa2a1580..72157c4fde 100644 --- a/packages/cart/src/models/shipping-method.ts +++ b/packages/cart/src/models/shipping-method.ts @@ -1,14 +1,21 @@ -import { BigNumberRawValue } from "@medusajs/types" -import { BigNumber, generateEntityId } from "@medusajs/utils" +import { BigNumberRawValue, DAL } from "@medusajs/types" +import { + BigNumber, + DALUtils, + createPsqlIndexStatementHelper, + generateEntityId, +} from "@medusajs/utils" import { BeforeCreate, Cascade, Check, Collection, Entity, + Filter, ManyToOne, OnInit, OneToMany, + OptionalProps, PrimaryKey, Property, } from "@mikro-orm/core" @@ -17,19 +24,32 @@ import Cart from "./cart" import ShippingMethodAdjustment from "./shipping-method-adjustment" import ShippingMethodTaxLine from "./shipping-method-tax-line" +type OptionalShippingMethodProps = + | "cart" + | "is_tax_inclusive" + | DAL.SoftDeletableEntityDateColumns + @Entity({ tableName: "cart_shipping_method" }) @Check({ expression: (columns) => `${columns.amount} >= 0` }) +@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) export default class ShippingMethod { + [OptionalProps]?: OptionalShippingMethodProps + @PrimaryKey({ columnType: "text" }) id: string + @createPsqlIndexStatementHelper({ + name: "IDX_shipping_method_cart_id", + tableName: "cart_shipping_method", + columns: "cart_id", + where: "deleted_at IS NULL", + }).MikroORMIndex() @Property({ columnType: "text" }) cart_id: string @ManyToOne({ entity: () => Cart, - index: "IDX_shipping_method_cart_id", - cascade: [Cascade.REMOVE, Cascade.PERSIST], + cascade: [Cascade.REMOVE, Cascade.PERSIST, "soft-remove"] as any, }) cart: Cart @@ -48,11 +68,13 @@ export default class ShippingMethod { @Property({ columnType: "boolean" }) is_tax_inclusive = false - @Property({ - columnType: "text", - nullable: true, - index: "IDX_shipping_method_option_id", - }) + @createPsqlIndexStatementHelper({ + name: "IDX_shipping_method_option_id", + tableName: "cart_shipping_method", + columns: "shipping_option_id", + where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "text", nullable: true }) shipping_option_id: string | null = null @Property({ columnType: "jsonb", nullable: true }) @@ -65,7 +87,7 @@ export default class ShippingMethod { () => ShippingMethodTaxLine, (taxLine) => taxLine.shipping_method, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, } ) tax_lines = new Collection(this) @@ -74,7 +96,7 @@ export default class ShippingMethod { () => ShippingMethodAdjustment, (adjustment) => adjustment.shipping_method, { - cascade: [Cascade.REMOVE], + cascade: [Cascade.PERSIST, "soft-remove"] as any, } ) adjustments = new Collection(this) @@ -94,6 +116,14 @@ export default class ShippingMethod { }) updated_at: Date + @createPsqlIndexStatementHelper({ + tableName: "cart_shipping_method", + columns: "deleted_at", + where: "deleted_at IS NOT NULL", + }).MikroORMIndex() + @Property({ columnType: "timestamptz", nullable: true }) + deleted_at: Date | null = null + @BeforeCreate() onCreate() { this.id = generateEntityId(this.id, "casm") diff --git a/packages/cart/src/models/tax-line.ts b/packages/cart/src/models/tax-line.ts index 5ba302e305..149634d8ce 100644 --- a/packages/cart/src/models/tax-line.ts +++ b/packages/cart/src/models/tax-line.ts @@ -1,22 +1,21 @@ -import { PrimaryKey, Property } from "@mikro-orm/core" +import { DAL } from "@medusajs/types" +import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" + +type OptionalTaxLineProps = DAL.SoftDeletableEntityDateColumns /** * As per the Mikro ORM docs, superclasses should use the abstract class definition * Source: https://mikro-orm.io/docs/inheritance-mapping */ export default abstract class TaxLine { + [OptionalProps]?: OptionalTaxLineProps + @PrimaryKey({ columnType: "text" }) id: string @Property({ columnType: "text", nullable: true }) description?: string | null - @Property({ - columnType: "text", - nullable: true, - }) - tax_rate_id?: string | null - @Property({ columnType: "text" }) code: string diff --git a/packages/types/src/cart/service.ts b/packages/types/src/cart/service.ts index 8e00c2a06a..e78316e963 100644 --- a/packages/types/src/cart/service.ts +++ b/packages/types/src/cart/service.ts @@ -1,4 +1,5 @@ import { FindConfig } from "../common" +import { RestoreReturn, SoftDeleteReturn } from "../dal" import { IModuleService } from "../modules-sdk" import { Context } from "../shared-context" import { @@ -329,4 +330,102 @@ export interface ICartModuleService extends IModuleService { selector: FilterableShippingMethodTaxLineProps, sharedContext?: Context ): Promise + + softDelete( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restore( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteAddresses( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreAddresses( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteLineItems( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreLineItems( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteShippingMethods( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreShippingMethods( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteLineItemAdjustments< + TReturnableLinkableKeys extends string = string + >( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreLineItemAdjustments( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteShippingMethodAdjustments< + TReturnableLinkableKeys extends string = string + >( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreShippingMethodAdjustments< + TReturnableLinkableKeys extends string = string + >( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteLineItemTaxLines( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreLineItemTaxLines( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> + + softDeleteShippingMethodTaxLines< + TReturnableLinkableKeys extends string = string + >( + ids: string[], + config?: SoftDeleteReturn, + sharedContext?: Context + ): Promise | void> + restoreShippingMethodTaxLines< + TReturnableLinkableKeys extends string = string + >( + ids: string[], + config?: RestoreReturn, + sharedContext?: Context + ): Promise | void> }