diff --git a/.changeset/sour-cats-sort.md b/.changeset/sour-cats-sort.md new file mode 100644 index 0000000000000..bcdb2842123c0 --- /dev/null +++ b/.changeset/sour-cats-sort.md @@ -0,0 +1,6 @@ +--- +"@medusajs/cart": patch +"@medusajs/utils": patch +--- + +refactor: migrate cart module to DML diff --git a/packages/core/utils/src/dml/relations/index.ts b/packages/core/utils/src/dml/relations/index.ts index 8e94644d4c58a..cdf7a4a955ecf 100644 --- a/packages/core/utils/src/dml/relations/index.ts +++ b/packages/core/utils/src/dml/relations/index.ts @@ -4,3 +4,4 @@ export * from "./has-many" export * from "./has-one" export * from "./many-to-many" export * from "./nullable" +export * from "./has-one-fk" diff --git a/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts b/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts index d143925d6bd81..1bbac62243bc7 100644 --- a/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts +++ b/packages/modules/cart/integration-tests/__fixtures__/cart/index.ts @@ -1,18 +1,19 @@ -import { CreateCartDTO } from "@medusajs/framework/types" +import { CreateCartDTO, InferEntityType } from "@medusajs/framework/types" import { SqlEntityManager } from "@mikro-orm/postgresql" import { Cart } from "../../../src/models" import { defaultCartsData } from "./data" +import { toMikroORMEntity } from "@medusajs/framework/utils" export * from "./data" export async function createCarts( manager: SqlEntityManager, cartsData: CreateCartDTO[] = defaultCartsData -): Promise { - const carts: Cart[] = [] +): Promise[]> { + const carts: InferEntityType[] = [] for (let cartData of cartsData) { - let cart = manager.create(Cart, cartData) + let cart = manager.create(toMikroORMEntity(Cart), cartData) await manager.persistAndFlush(cart) } diff --git a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json index a48b7e87a0894..af920bf4f77a9 100644 --- a/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json +++ b/packages/modules/cart/src/migrations/.snapshot-medusa-cart.json @@ -161,13 +161,11 @@ "indexes": [ { "keyName": "IDX_cart_address_deleted_at", - "columnNames": [ - "deleted_at" - ], + "columnNames": [], "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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_address_deleted_at\" ON \"cart_address\" (deleted_at) WHERE deleted_at IS NULL" }, { "keyName": "cart_address_pkey", @@ -238,42 +236,42 @@ "nullable": false, "mappedType": "text" }, - "shipping_address_id": { - "name": "shipping_address_id", - "type": "text", + "metadata": { + "name": "metadata", + "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "text" + "mappedType": "json" }, - "billing_address_id": { - "name": "billing_address_id", - "type": "text", + "completed_at": { + "name": "completed_at", + "type": "timestamptz", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "text" + "length": 6, + "mappedType": "datetime" }, - "metadata": { - "name": "metadata", - "type": "jsonb", + "shipping_address_id": { + "name": "shipping_address_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "json" + "mappedType": "text" }, - "completed_at": { - "name": "completed_at", - "type": "timestamptz", + "billing_address_id": { + "name": "billing_address_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "length": 6, - "mappedType": "datetime" + "mappedType": "text" }, "created_at": { "name": "created_at", @@ -312,20 +310,42 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_cart_region_id", "columnNames": [ - "region_id" + "shipping_address_id" ], "composite": false, + "keyName": "cart_shipping_address_id_unique", + "primary": false, + "unique": true + }, + { + "columnNames": [ + "billing_address_id" + ], + "composite": false, + "keyName": "cart_billing_address_id_unique", + "primary": false, + "unique": true + }, + { + "keyName": "IDX_cart_deleted_at", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_deleted_at\" ON \"cart\" (deleted_at) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_region_id", + "columnNames": [], + "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" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -333,9 +353,7 @@ }, { "keyName": "IDX_cart_sales_channel_id", - "columnNames": [ - "sales_channel_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -343,9 +361,7 @@ }, { "keyName": "IDX_cart_curency_code", - "columnNames": [ - "currency_code" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -353,9 +369,7 @@ }, { "keyName": "IDX_cart_shipping_address_id", - "columnNames": [ - "shipping_address_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -363,24 +377,12 @@ }, { "keyName": "IDX_cart_billing_address_id", - "columnNames": [ - "billing_address_id" - ], + "columnNames": [], "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": [ @@ -432,15 +434,6 @@ "nullable": false, "mappedType": "text" }, - "cart_id": { - "name": "cart_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, "title": { "name": "title", "type": "text", @@ -601,6 +594,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "true", "mappedType": "boolean" }, "is_discountable": { @@ -610,6 +604,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "true", "mappedType": "boolean" }, "is_tax_inclusive": { @@ -619,6 +614,7 @@ "autoincrement": false, "primary": false, "nullable": false, + "default": "false", "mappedType": "boolean" }, "compare_at_unit_price": { @@ -630,8 +626,17 @@ "nullable": true, "mappedType": "decimal" }, - "raw_compare_at_unit_price": { - "name": "raw_compare_at_unit_price", + "unit_price": { + "name": "unit_price", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "decimal" + }, + "metadata": { + "name": "metadata", "type": "jsonb", "unsigned": false, "autoincrement": false, @@ -639,31 +644,31 @@ "nullable": true, "mappedType": "json" }, - "unit_price": { - "name": "unit_price", - "type": "numeric", + "cart_id": { + "name": "cart_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "text" }, - "raw_unit_price": { - "name": "raw_unit_price", + "raw_compare_at_unit_price": { + "name": "raw_compare_at_unit_price", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "mappedType": "json" }, - "metadata": { - "name": "metadata", + "raw_unit_price": { + "name": "raw_unit_price", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "json" }, "created_at": { @@ -702,11 +707,25 @@ "name": "cart_line_item", "schema": "public", "indexes": [ + { + "keyName": "IDX_cart_line_item_cart_id", + "columnNames": [], + "composite": false, + "primary": false, + "unique": false, + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_cart_id\" ON \"cart_line_item\" (cart_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_deleted_at", + "columnNames": [], + "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 NULL" + }, { "keyName": "IDX_line_item_cart_id", - "columnNames": [ - "cart_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -714,9 +733,7 @@ }, { "keyName": "IDX_line_item_variant_id", - "columnNames": [ - "variant_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -724,9 +741,7 @@ }, { "keyName": "IDX_line_item_product_id", - "columnNames": [ - "product_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, @@ -734,24 +749,12 @@ }, { "keyName": "IDX_line_item_product_type_id", - "columnNames": [ - "product_type_id" - ], + "columnNames": [], "composite": false, "primary": false, "unique": false, "expression": "CREATE INDEX IF NOT EXISTS \"IDX_line_item_product_type_id\" ON \"cart_line_item\" (product_type_id) WHERE deleted_at IS NULL AND product_type_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", "columnNames": [ @@ -774,6 +777,7 @@ "id" ], "referencedTableName": "public.cart", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -816,17 +820,17 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", - "type": "jsonb", + "provider_id": { + "name": "provider_id", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, - "mappedType": "json" + "nullable": true, + "mappedType": "text" }, - "provider_id": { - "name": "provider_id", + "promotion_id": { + "name": "promotion_id", "type": "text", "unsigned": false, "autoincrement": false, @@ -843,6 +847,24 @@ "nullable": true, "mappedType": "json" }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -865,24 +887,6 @@ "default": "now()", "mappedType": "datetime" }, - "item_id": { - "name": "item_id", - "type": "text", - "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", @@ -898,34 +902,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_adjustment_item_id", - "columnNames": [ - "item_id" - ], + "keyName": "IDX_cart_line_item_adjustment_item_id", + "columnNames": [], "composite": false, "primary": 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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_adjustment_deleted_at", + "columnNames": [], + "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 NULL" }, { "keyName": "IDX_line_item_adjustment_promotion_id", - "columnNames": [ - "promotion_id" - ], + "columnNames": [], "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" - ], + "keyName": "IDX_adjustment_item_id", + "columnNames": [], "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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_adjustment_item_id\" ON \"cart_line_item_adjustment\" (item_id) WHERE deleted_at IS NULL" }, { "keyName": "cart_line_item_adjustment_pkey", @@ -955,6 +961,7 @@ "id" ], "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -990,12 +997,12 @@ }, "rate": { "name": "rate", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "integer" }, "provider_id": { "name": "provider_id", @@ -1015,6 +1022,24 @@ "nullable": true, "mappedType": "json" }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "item_id": { + "name": "item_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -1037,24 +1062,6 @@ "default": "now()", "mappedType": "datetime" }, - "item_id": { - "name": "item_id", - "type": "text", - "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", @@ -1070,34 +1077,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_tax_line_item_id", - "columnNames": [ - "item_id" - ], + "keyName": "IDX_cart_line_item_tax_line_item_id", + "columnNames": [], "composite": false, "primary": 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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_line_item_tax_line_item_id\" ON \"cart_line_item_tax_line\" (item_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_line_item_tax_line_deleted_at", + "columnNames": [], + "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 NULL" }, { "keyName": "IDX_line_item_tax_line_tax_rate_id", - "columnNames": [ - "tax_rate_id" - ], + "columnNames": [], "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" - ], + "keyName": "IDX_tax_line_item_id", + "columnNames": [], "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" + "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": "cart_line_item_tax_line_pkey", @@ -1121,6 +1130,7 @@ "id" ], "referencedTableName": "public.cart_line_item", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -1136,15 +1146,6 @@ "nullable": false, "mappedType": "text" }, - "cart_id": { - "name": "cart_id", - "type": "text", - "unsigned": false, - "autoincrement": false, - "primary": false, - "nullable": false, - "mappedType": "text" - }, "name": { "name": "name", "type": "text", @@ -1172,15 +1173,6 @@ "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", @@ -1218,6 +1210,24 @@ "nullable": true, "mappedType": "json" }, + "cart_id": { + "name": "cart_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -1255,34 +1265,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_shipping_method_cart_id", - "columnNames": [ - "cart_id" - ], + "keyName": "IDX_cart_shipping_method_cart_id", + "columnNames": [], "composite": false, "primary": 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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_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" - ], + "keyName": "IDX_cart_shipping_method_deleted_at", + "columnNames": [], "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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_deleted_at\" ON \"cart_shipping_method\" (deleted_at) WHERE deleted_at IS NULL" }, { - "keyName": "IDX_cart_shipping_method_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_shipping_method_cart_id", + "columnNames": [], "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" + "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": [], + "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": "cart_shipping_method_pkey", @@ -1312,6 +1324,7 @@ "id" ], "referencedTableName": "public.cart", + "deleteRule": "cascade", "updateRule": "cascade" } } @@ -1354,17 +1367,26 @@ "nullable": false, "mappedType": "decimal" }, - "raw_amount": { - "name": "raw_amount", + "provider_id": { + "name": "provider_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, + "metadata": { + "name": "metadata", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": false, + "nullable": true, "mappedType": "json" }, - "provider_id": { - "name": "provider_id", + "promotion_id": { + "name": "promotion_id", "type": "text", "unsigned": false, "autoincrement": false, @@ -1372,13 +1394,22 @@ "nullable": true, "mappedType": "text" }, - "metadata": { - "name": "metadata", + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, + "raw_amount": { + "name": "raw_amount", "type": "jsonb", "unsigned": false, "autoincrement": false, "primary": false, - "nullable": true, + "nullable": false, "mappedType": "json" }, "created_at": { @@ -1403,24 +1434,6 @@ "default": "now()", "mappedType": "datetime" }, - "shipping_method_id": { - "name": "shipping_method_id", - "type": "text", - "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", @@ -1436,34 +1449,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_adjustment_shipping_method_id", - "columnNames": [ - "shipping_method_id" - ], + "keyName": "IDX_cart_shipping_method_adjustment_shipping_method_id", + "columnNames": [], "composite": false, "primary": 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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_adjustment_shipping_method_id\" ON \"cart_shipping_method_adjustment\" (shipping_method_id) WHERE deleted_at IS NULL" + }, + { + "keyName": "IDX_cart_shipping_method_adjustment_deleted_at", + "columnNames": [], + "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 NULL" }, { "keyName": "IDX_shipping_method_adjustment_promotion_id", - "columnNames": [ - "promotion_id" - ], + "columnNames": [], "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" - ], + "keyName": "IDX_adjustment_shipping_method_id", + "columnNames": [], "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" + "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": "cart_shipping_method_adjustment_pkey", @@ -1522,12 +1537,12 @@ }, "rate": { "name": "rate", - "type": "numeric", + "type": "integer", "unsigned": false, "autoincrement": false, "primary": false, "nullable": false, - "mappedType": "decimal" + "mappedType": "integer" }, "provider_id": { "name": "provider_id", @@ -1538,6 +1553,15 @@ "nullable": true, "mappedType": "text" }, + "tax_rate_id": { + "name": "tax_rate_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": true, + "mappedType": "text" + }, "metadata": { "name": "metadata", "type": "jsonb", @@ -1547,6 +1571,15 @@ "nullable": true, "mappedType": "json" }, + "shipping_method_id": { + "name": "shipping_method_id", + "type": "text", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "text" + }, "created_at": { "name": "created_at", "type": "timestamptz", @@ -1569,24 +1602,6 @@ "default": "now()", "mappedType": "datetime" }, - "shipping_method_id": { - "name": "shipping_method_id", - "type": "text", - "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", @@ -1602,34 +1617,36 @@ "schema": "public", "indexes": [ { - "keyName": "IDX_tax_line_shipping_method_id", - "columnNames": [ - "shipping_method_id" - ], + "keyName": "IDX_cart_shipping_method_tax_line_shipping_method_id", + "columnNames": [], "composite": false, "primary": 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" + "expression": "CREATE INDEX IF NOT EXISTS \"IDX_cart_shipping_method_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" - ], + "keyName": "IDX_cart_shipping_method_tax_line_deleted_at", + "columnNames": [], "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" + "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 NULL" }, { - "keyName": "IDX_cart_shipping_method_tax_line_deleted_at", - "columnNames": [ - "deleted_at" - ], + "keyName": "IDX_tax_line_shipping_method_id", + "columnNames": [], + "composite": false, + "primary": 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": [], "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" + "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": "cart_shipping_method_tax_line_pkey", diff --git a/packages/modules/cart/src/migrations/Migration20241205095237.ts b/packages/modules/cart/src/migrations/Migration20241205095237.ts new file mode 100644 index 0000000000000..2cf70fdfee223 --- /dev/null +++ b/packages/modules/cart/src/migrations/Migration20241205095237.ts @@ -0,0 +1,139 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20241205095237 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" type boolean using ("requires_shipping"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "requires_shipping" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" type boolean using ("is_discountable"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_discountable" set default true;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" set default false;' + ) + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_cart_id" ON "cart_line_item" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "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;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_adjustment_item_id" ON "cart_line_item_adjustment" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_line_item_tax_line_item_id" ON "cart_line_item_tax_line" (item_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade on delete cascade;' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_cart_id" ON "cart_shipping_method" (cart_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_adjustment_shipping_method_id" ON "cart_shipping_method_adjustment" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type integer using ("rate"::integer);' + ) + this.addSql( + 'CREATE INDEX IF NOT EXISTS "IDX_cart_shipping_method_tax_line_shipping_method_id" ON "cart_shipping_method_tax_line" (shipping_method_id) WHERE deleted_at IS NULL;' + ) + } + + async down(): Promise { + this.addSql( + 'alter table if exists "cart_line_item" drop constraint if exists "cart_line_item_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_adjustment" drop constraint if exists "cart_line_item_adjustment_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" drop constraint if exists "cart_line_item_tax_line_item_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method" drop constraint if exists "cart_shipping_method_cart_id_foreign";' + ) + + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" drop default;' + ) + this.addSql( + 'alter table if exists "cart_line_item" alter column "is_tax_inclusive" type boolean using ("is_tax_inclusive"::boolean);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_cart_id";') + this.addSql( + 'alter table if exists "cart_line_item" add constraint "cart_line_item_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_line_item_adjustment_item_id";') + this.addSql( + 'alter table if exists "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;' + ) + + this.addSql( + 'alter table if exists "cart_line_item_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql('drop index if exists "IDX_cart_line_item_tax_line_item_id";') + this.addSql( + 'alter table if exists "cart_line_item_tax_line" add constraint "cart_line_item_tax_line_item_id_foreign" foreign key ("item_id") references "cart_line_item" ("id") on update cascade;' + ) + + this.addSql('drop index if exists "IDX_cart_shipping_method_cart_id";') + this.addSql( + 'alter table if exists "cart_shipping_method" add constraint "cart_shipping_method_cart_id_foreign" foreign key ("cart_id") references "cart" ("id") on update cascade;' + ) + + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_adjustment_shipping_method_id";' + ) + + this.addSql( + 'alter table if exists "cart_shipping_method_tax_line" alter column "rate" type numeric using ("rate"::numeric);' + ) + this.addSql( + 'drop index if exists "IDX_cart_shipping_method_tax_line_shipping_method_id";' + ) + } +} diff --git a/packages/modules/cart/src/models/address.ts b/packages/modules/cart/src/models/address.ts index 270ba6dbf3bde..34fa8885a009f 100644 --- a/packages/modules/cart/src/models/address.ts +++ b/packages/modules/cart/src/models/address.ts @@ -1,97 +1,22 @@ -import { DAL } from "@medusajs/framework/types" -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - OnInit, - OptionalProps, - PrimaryKey, - Property, -} from "@mikro-orm/core" - -type OptionalAddressProps = DAL.SoftDeletableModelDateColumns - -const PgIndex = createPsqlIndexStatementHelper({ - tableName: "cart_address", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}) - -@Entity({ tableName: "cart_address" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class Address { - [OptionalProps]: OptionalAddressProps - - @PrimaryKey({ columnType: "text" }) - id!: string - - @Property({ columnType: "text", nullable: true }) - customer_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - company: string | null = null - - @Property({ columnType: "text", nullable: true }) - first_name: string | null = null - - @Property({ columnType: "text", nullable: true }) - last_name: string | null = null - - @Property({ columnType: "text", nullable: true }) - address_1: string | null = null - - @Property({ columnType: "text", nullable: true }) - address_2: string | null = null - - @Property({ columnType: "text", nullable: true }) - city: string | null = null - - @Property({ columnType: "text", nullable: true }) - country_code: string | null = null - - @Property({ columnType: "text", nullable: true }) - province: string | null = null - - @Property({ columnType: "text", nullable: true }) - postal_code: string | null = null - - @Property({ columnType: "text", nullable: true }) - phone: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @PgIndex.MikroORMIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "caaddr") +import { model } from "@medusajs/framework/utils" + +const Address = model.define( + { tableName: "cart_address", name: "Address" }, + { + id: model.id({ prefix: "caaddr" }).primaryKey(), + customer_id: model.text().nullable(), + company: model.text().nullable(), + first_name: model.text().nullable(), + last_name: model.text().nullable(), + address_1: model.text().nullable(), + address_2: model.text().nullable(), + city: model.text().nullable(), + country_code: model.text().nullable(), + province: model.text().nullable(), + postal_code: model.text().nullable(), + phone: model.text().nullable(), + metadata: model.json().nullable(), } +) - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "caaddr") - } -} +export default Address diff --git a/packages/modules/cart/src/models/adjustment-line.ts b/packages/modules/cart/src/models/adjustment-line.ts deleted file mode 100644 index 55b913c79404e..0000000000000 --- a/packages/modules/cart/src/models/adjustment-line.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { DAL } from "@medusajs/framework/types" -import { BigNumber, MikroOrmBigNumberProperty } from "@medusajs/framework/utils" -import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" - -type OptionalAdjustmentLineProps = DAL.SoftDeletableModelDateColumns - -/** - * 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 AdjustmentLine { - [OptionalProps]: OptionalAdjustmentLineProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @Property({ columnType: "text", nullable: true }) - description: string | null = null - - @Property({ columnType: "text", nullable: true }) - code: string | null = null - - @MikroOrmBigNumberProperty() - amount: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_amount: Record - - @Property({ columnType: "text", nullable: true }) - provider_id: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date -} diff --git a/packages/modules/cart/src/models/cart.ts b/packages/modules/cart/src/models/cart.ts index 9df5ba729675a..0f40460cf39fa 100644 --- a/packages/modules/cart/src/models/cart.ts +++ b/packages/modules/cart/src/models/cart.ts @@ -1,181 +1,76 @@ -import { DAL } from "@medusajs/framework/types" -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Address from "./address" import LineItem from "./line-item" import ShippingMethod from "./shipping-method" -type OptionalCartProps = - | "shipping_address" - | "billing_address" - | DAL.SoftDeletableModelDateColumns - -const RegionIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_region_id", - tableName: "cart", - columns: "region_id", - where: "deleted_at IS NULL AND region_id IS NOT NULL", -}).MikroORMIndex - -const CustomerIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_customer_id", - tableName: "cart", - columns: "customer_id", - where: "deleted_at IS NULL AND customer_id IS NOT NULL", -}).MikroORMIndex - -const SalesChannelIdIndex = 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 - -const CurrencyCodeIndex = createPsqlIndexStatementHelper({ - name: "IDX_cart_curency_code", - tableName: "cart", - columns: "currency_code", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const ShippingAddressIdIndex = 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 - -const BillingAddressIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class Cart { - [OptionalProps]?: OptionalCartProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @RegionIdIndex() - @Property({ columnType: "text", nullable: true }) - region_id: string | null = null - - @CustomerIdIndex() - @Property({ columnType: "text", nullable: true }) - customer_id: string | null = null - - @SalesChannelIdIndex() - @Property({ columnType: "text", nullable: true }) - sales_channel_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - email: string | null = null - - @CurrencyCodeIndex() - @Property({ columnType: "text" }) - currency_code: string - - @ShippingAddressIdIndex() - @ManyToOne({ - entity: () => Address, - columnType: "text", - fieldName: "shipping_address_id", - mapToPk: true, - nullable: true, +const Cart = model + .define("Cart", { + id: model.id({ prefix: "cart" }).primaryKey(), + region_id: model.text().nullable(), + customer_id: model.text().nullable(), + sales_channel_id: model.text().nullable(), + email: model.text().nullable(), + currency_code: model.text(), + metadata: model.json().nullable(), + completed_at: model.dateTime().nullable(), + shipping_address: model + .hasOne(() => Address, { + mappedBy: undefined, + foreignKey: true, + }) + .nullable(), + billing_address: model + .hasOne(() => Address, { + mappedBy: undefined, + foreignKey: true, + }) + .nullable(), + items: model.hasMany(() => LineItem, { + mappedBy: "cart", + }), + shipping_methods: model.hasMany(() => ShippingMethod, { + mappedBy: "cart", + }), }) - shipping_address_id: string | null - - @ManyToOne(() => Address, { - cascade: [Cascade.PERSIST], - nullable: true, + .cascades({ + delete: [ + "items", + "shipping_methods", + "shipping_address", + "billing_address", + ], }) - shipping_address: Rel
| null - - @BillingAddressIdIndex() - @ManyToOne({ - entity: () => Address, - columnType: "text", - fieldName: "billing_address_id", - mapToPk: true, - nullable: true, - }) - billing_address_id: string | null - - @ManyToOne(() => Address, { - cascade: [Cascade.PERSIST], - nullable: true, - }) - billing_address: Rel
| null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - items = new Collection>(this) - - @OneToMany(() => ShippingMethod, (shippingMethod) => shippingMethod.cart, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - shipping_methods = new Collection>(this) - - @Property({ columnType: "timestamptz", nullable: true }) - completed_at: Date | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "cart") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "cart") - } -} + .indexes([ + { + name: "IDX_cart_region_id", + on: ["region_id"], + where: "deleted_at IS NULL AND region_id IS NOT NULL", + }, + { + name: "IDX_cart_customer_id", + on: ["customer_id"], + where: "deleted_at IS NULL AND customer_id IS NOT NULL", + }, + { + name: "IDX_cart_sales_channel_id", + on: ["sales_channel_id"], + where: "deleted_at IS NULL AND sales_channel_id IS NOT NULL", + }, + { + name: "IDX_cart_curency_code", + on: ["currency_code"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_cart_shipping_address_id", + on: ["shipping_address_id"], + where: "deleted_at IS NULL AND shipping_address_id IS NOT NULL", + }, + { + name: "IDX_cart_billing_address_id", + on: ["billing_address_id"], + where: "deleted_at IS NULL AND billing_address_id IS NOT NULL", + }, + ]) + +export default Cart diff --git a/packages/modules/cart/src/models/line-item-adjustment.ts b/packages/modules/cart/src/models/line-item-adjustment.ts index f0c816d67d51f..2a1cc8edc26fd 100644 --- a/packages/modules/cart/src/models/line-item-adjustment.ts +++ b/packages/modules/cart/src/models/line-item-adjustment.ts @@ -1,74 +1,34 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Check, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" -import AdjustmentLine from "./adjustment-line" +import { model } from "@medusajs/framework/utils" import LineItem from "./line-item" -const LineItemIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_adjustment_item_id", - tableName: "cart_line_item_adjustment", - columns: "item_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const PromotionIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item_adjustment", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_line_item_adjustment" }) -@Check({ - expression: (columns) => `${columns.amount} >= 0`, -}) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItemAdjustment extends AdjustmentLine { - @ManyToOne({ entity: () => LineItem, persist: false }) - item: Rel - - @LineItemIdIndex() - @ManyToOne({ - entity: () => LineItem, - columnType: "text", - fieldName: "item_id", - mapToPk: true, - }) - item_id: string - - @PromotionIdIndex() - @Property({ columnType: "text", nullable: true }) - promotion_id: string | null = null - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "caliadj") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "caliadj") - } -} +const LineItemAdjustment = model + .define( + { name: "LineItemAdjustment", tableName: "cart_line_item_adjustment" }, + { + id: model.id({ prefix: "caliadj" }).primaryKey(), + description: model.text().nullable(), + code: model.text().nullable(), + amount: model.bigNumber(), + provider_id: model.text().nullable(), + promotion_id: model.text().nullable(), + metadata: model.json().nullable(), + item: model.belongsTo(() => LineItem, { + mappedBy: "adjustments", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_adjustment_promotion_id", + on: ["promotion_id"], + where: "deleted_at IS NULL AND promotion_id IS NOT NULL", + }, + { + name: "IDX_adjustment_item_id", + on: ["item_id"], + where: "deleted_at IS NULL", + }, + ]) + .checks([(columns) => `${columns.amount} >= 0`]) + +export default LineItemAdjustment diff --git a/packages/modules/cart/src/models/line-item-tax-line.ts b/packages/modules/cart/src/models/line-item-tax-line.ts index 4a773c07c9495..bc6e0f9911339 100644 --- a/packages/modules/cart/src/models/line-item-tax-line.ts +++ b/packages/modules/cart/src/models/line-item-tax-line.ts @@ -1,70 +1,36 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import LineItem from "./line-item" -import TaxLine from "./tax-line" -const LineItemIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_tax_line_item_id", - tableName: "cart_line_item_tax_line", - columns: "item_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const TaxRateIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item_tax_line", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_line_item_tax_line" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItemTaxLine extends TaxLine { - @ManyToOne({ entity: () => LineItem, persist: false }) - item: Rel - - @LineItemIdIndex() - @ManyToOne({ - entity: () => LineItem, - columnType: "text", - fieldName: "item_id", - mapToPk: true, - }) - item_id: string - - @TaxRateIdIndex() - @Property({ columnType: "text", nullable: true }) - tax_rate_id: string | null = null - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "calitxl") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "calitxl") - } -} +const LineItemTaxLine = model + .define( + { + name: "LineItemTaxLine", + tableName: "cart_line_item_tax_line", + }, + { + id: model.id({ prefix: "calitxl" }).primaryKey(), + description: model.text().nullable(), + code: model.text(), + rate: model.number(), + provider_id: model.text().nullable(), + metadata: model.json().nullable(), + tax_rate_id: model.text().nullable(), + item: model.belongsTo(() => LineItem, { + mappedBy: "tax_lines", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_tax_line_tax_rate_id", + on: ["tax_rate_id"], + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }, + { + name: "IDX_tax_line_item_id", + on: ["item_id"], + where: "deleted_at IS NULL", + }, + ]) + +export default LineItemTaxLine diff --git a/packages/modules/cart/src/models/line-item.ts b/packages/modules/cart/src/models/line-item.ts index 86d9792a0c391..b37a13211d148 100644 --- a/packages/modules/cart/src/models/line-item.ts +++ b/packages/modules/cart/src/models/line-item.ts @@ -1,205 +1,71 @@ -import { BigNumberRawValue, DAL } from "@medusajs/framework/types" -import { - BigNumber, - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, - MikroOrmBigNumberProperty, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Cart from "./cart" -import LineItemAdjustment from "./line-item-adjustment" import LineItemTaxLine from "./line-item-tax-line" +import LineItemAdjustment from "./line-item-adjustment" -type OptionalLineItemProps = - | "is_discoutable" - | "is_tax_inclusive" - | "compare_at_unit_price" - | "requires_shipping" - | "cart" - | DAL.SoftDeletableModelDateColumns - -const CartIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_cart_id", - tableName: "cart_line_item", - columns: "cart_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const VariantIdIndex = 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 - -const ProductIdIndex = 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 - -const ProductTypeIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_line_item_product_type_id", - tableName: "cart_line_item", - columns: "product_type_id", - where: "deleted_at IS NULL AND product_type_id IS NOT NULL", -}).MikroORMIndex - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_line_item", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_line_item" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class LineItem { - [OptionalProps]?: OptionalLineItemProps - - @PrimaryKey({ columnType: "text" }) - id: string - - @CartIdIndex() - @ManyToOne({ - entity: () => Cart, - columnType: "text", - fieldName: "cart_id", - mapToPk: true, - }) - cart_id: string - - @ManyToOne({ entity: () => Cart, persist: false }) - cart: Rel - - @Property({ columnType: "text" }) - title: string - - @Property({ columnType: "text", nullable: true }) - subtitle: string | null = null - - @Property({ columnType: "text", nullable: true }) - thumbnail: string | null = null - - @Property({ columnType: "integer" }) - quantity: number - - @VariantIdIndex() - @Property({ columnType: "text", nullable: true }) - variant_id: string | null = null - - @ProductIdIndex() - @Property({ columnType: "text", nullable: true }) - product_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_title: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_description: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_subtitle: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_type: string | null = null - - @ProductTypeIdIndex() - @Property({ columnType: "text", nullable: true }) - product_type_id: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_collection: string | null = null - - @Property({ columnType: "text", nullable: true }) - product_handle: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_sku: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_barcode: string | null = null - - @Property({ columnType: "text", nullable: true }) - variant_title: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - variant_option_values: Record | null = null - - @Property({ columnType: "boolean" }) - requires_shipping: boolean = true - - @Property({ columnType: "boolean" }) - is_discountable: boolean = true - - @Property({ columnType: "boolean" }) - is_tax_inclusive: boolean = false - - @MikroOrmBigNumberProperty({ nullable: true }) - compare_at_unit_price?: BigNumber | number | null = null - - @Property({ columnType: "jsonb", nullable: true }) - raw_compare_at_unit_price: BigNumberRawValue | null = null - - @MikroOrmBigNumberProperty() - unit_price: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_unit_price: BigNumberRawValue - - @OneToMany(() => LineItemTaxLine, (taxLine) => taxLine.item, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - tax_lines = new Collection>(this) - - @OneToMany(() => LineItemAdjustment, (adjustment) => adjustment.item, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - }) - adjustments = new Collection>(this) - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", +const LineItem = model + .define( + { name: "LineItem", tableName: "cart_line_item" }, + { + id: model.id({ prefix: "cali" }).primaryKey(), + title: model.text(), + subtitle: model.text().nullable(), + thumbnail: model.text().nullable(), + quantity: model.number(), + variant_id: model.text().nullable(), + product_id: model.text().nullable(), + product_title: model.text().nullable(), + product_description: model.text().nullable(), + product_subtitle: model.text().nullable(), + product_type: model.text().nullable(), + product_type_id: model.text().nullable(), + product_collection: model.text().nullable(), + product_handle: model.text().nullable(), + variant_sku: model.text().nullable(), + variant_barcode: model.text().nullable(), + variant_title: model.text().nullable(), + variant_option_values: model.json().nullable(), + requires_shipping: model.boolean().default(true), + is_discountable: model.boolean().default(true), + is_tax_inclusive: model.boolean().default(false), + compare_at_unit_price: model.bigNumber().nullable(), + unit_price: model.bigNumber(), + metadata: model.json().nullable(), + adjustments: model.hasMany(() => LineItemAdjustment, { + mappedBy: "item", + }), + tax_lines: model.hasMany(() => LineItemTaxLine, { + mappedBy: "item", + }), + cart: model.belongsTo(() => Cart, { + mappedBy: "items", + }), + } + ) + .indexes([ + { + name: "IDX_line_item_cart_id", + on: ["cart_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_line_item_variant_id", + on: ["variant_id"], + where: "deleted_at IS NULL AND variant_id IS NOT NULL", + }, + { + name: "IDX_line_item_product_id", + on: ["product_id"], + where: "deleted_at IS NULL AND product_id IS NOT NULL", + }, + { + name: "IDX_line_item_product_type_id", + on: ["product_type_id"], + where: "deleted_at IS NULL AND product_type_id IS NOT NULL", + }, + ]) + .cascades({ + delete: ["adjustments", "tax_lines"], }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "cali") - } - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "cali") - } -} +export default LineItem diff --git a/packages/modules/cart/src/models/shipping-method-adjustment.ts b/packages/modules/cart/src/models/shipping-method-adjustment.ts index 8e9702741d8e5..935c791cc2b6b 100644 --- a/packages/modules/cart/src/models/shipping-method-adjustment.ts +++ b/packages/modules/cart/src/models/shipping-method-adjustment.ts @@ -1,70 +1,36 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" -import AdjustmentLine from "./adjustment-line" +import { model } from "@medusajs/framework/utils" import ShippingMethod from "./shipping-method" -const ShippingMethodIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_adjustment_shipping_method_id", - tableName: "cart_shipping_method_adjustment", - columns: "shipping_method_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const PromotionIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method_adjustment", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_shipping_method_adjustment" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class ShippingMethodAdjustment extends AdjustmentLine { - @ManyToOne({ entity: () => ShippingMethod, persist: false }) - shipping_method: Rel - - @ShippingMethodIdIndex() - @ManyToOne({ - entity: () => ShippingMethod, - columnType: "text", - fieldName: "shipping_method_id", - mapToPk: true, - }) - shipping_method_id: string - - @PromotionIdIndex() - @Property({ columnType: "text", nullable: true }) - promotion_id: string | null = null - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casmadj") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casmadj") - } -} +const ShippingMethodAdjustment = model + .define( + { + name: "ShippingMethodAdjustment", + tableName: "cart_shipping_method_adjustment", + }, + { + id: model.id({ prefix: "casmadj" }).primaryKey(), + description: model.text().nullable(), + code: model.text().nullable(), + amount: model.bigNumber(), + provider_id: model.text().nullable(), + metadata: model.json().nullable(), + promotion_id: model.text().nullable(), + shipping_method: model.belongsTo(() => ShippingMethod, { + mappedBy: "adjustments", + }), + } + ) + .indexes([ + { + name: "IDX_shipping_method_adjustment_promotion_id", + on: ["promotion_id"], + where: "deleted_at IS NULL AND promotion_id IS NOT NULL", + }, + { + name: "IDX_adjustment_shipping_method_id", + on: ["shipping_method_id"], + where: "deleted_at IS NULL", + }, + ]) + +export default ShippingMethodAdjustment diff --git a/packages/modules/cart/src/models/shipping-method-tax-line.ts b/packages/modules/cart/src/models/shipping-method-tax-line.ts index 697dd156b707f..80c1aa53c57dd 100644 --- a/packages/modules/cart/src/models/shipping-method-tax-line.ts +++ b/packages/modules/cart/src/models/shipping-method-tax-line.ts @@ -1,70 +1,36 @@ -import { - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Entity, - Filter, - ManyToOne, - OnInit, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import ShippingMethod from "./shipping-method" -import TaxLine from "./tax-line" -const ShippingMethodIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_tax_line_shipping_method_id", - tableName: "cart_shipping_method_tax_line", - columns: "shipping_method_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const TaxRateIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method_tax_line", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@Entity({ tableName: "cart_shipping_method_tax_line" }) -@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions) -export default class ShippingMethodTaxLine extends TaxLine { - @ManyToOne({ entity: () => ShippingMethod, persist: false }) - shipping_method: Rel - - @ShippingMethodIdIndex() - @ManyToOne({ - entity: () => ShippingMethod, - columnType: "text", - fieldName: "shipping_method_id", - mapToPk: true, - }) - shipping_method_id: string - - @TaxRateIdIndex() - @Property({ columnType: "text", nullable: true }) - tax_rate_id: string | null = null - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casmtxl") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casmtxl") - } -} +const ShippingMethodTaxLine = model + .define( + { + name: "ShippingMethodTaxLine", + tableName: "cart_shipping_method_tax_line", + }, + { + id: model.id({ prefix: "casmtxl" }).primaryKey(), + description: model.text().nullable(), + code: model.text(), + rate: model.number(), + provider_id: model.text().nullable(), + tax_rate_id: model.text().nullable(), + metadata: model.json().nullable(), + shipping_method: model.belongsTo(() => ShippingMethod, { + mappedBy: "tax_lines", + }), + } + ) + .indexes([ + { + name: "IDX_tax_line_shipping_method_id", + on: ["shipping_method_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_shipping_method_tax_line_tax_rate_id", + on: ["tax_rate_id"], + where: "deleted_at IS NULL AND tax_rate_id IS NOT NULL", + }, + ]) + +export default ShippingMethodTaxLine diff --git a/packages/modules/cart/src/models/shipping-method.ts b/packages/modules/cart/src/models/shipping-method.ts index 6da5721656166..b3a303c2c6d73 100644 --- a/packages/modules/cart/src/models/shipping-method.ts +++ b/packages/modules/cart/src/models/shipping-method.ts @@ -1,145 +1,46 @@ -import { BigNumberRawValue, DAL } from "@medusajs/framework/types" -import { - BigNumber, - createPsqlIndexStatementHelper, - DALUtils, - generateEntityId, - MikroOrmBigNumberProperty, -} from "@medusajs/framework/utils" -import { - BeforeCreate, - Cascade, - Check, - Collection, - Entity, - Filter, - ManyToOne, - OneToMany, - OnInit, - OptionalProps, - PrimaryKey, - Property, - Rel, -} from "@mikro-orm/core" +import { model } from "@medusajs/framework/utils" import Cart from "./cart" import ShippingMethodAdjustment from "./shipping-method-adjustment" import ShippingMethodTaxLine from "./shipping-method-tax-line" -type OptionalShippingMethodProps = - | "cart" - | "is_tax_inclusive" - | DAL.SoftDeletableModelDateColumns - -const CartIdIndex = createPsqlIndexStatementHelper({ - name: "IDX_shipping_method_cart_id", - tableName: "cart_shipping_method", - columns: "cart_id", - where: "deleted_at IS NULL", -}).MikroORMIndex - -const ShippingOptionIdIndex = 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 - -const DeletedAtIndex = createPsqlIndexStatementHelper({ - tableName: "cart_shipping_method", - columns: "deleted_at", - where: "deleted_at IS NOT NULL", -}).MikroORMIndex - -@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 - - @CartIdIndex() - @ManyToOne({ - entity: () => Cart, - columnType: "text", - fieldName: "cart_id", - mapToPk: true, - }) - cart_id: string - - @ManyToOne({ entity: () => Cart, persist: false }) - cart: Rel - - @Property({ columnType: "text" }) - name: string - - @Property({ columnType: "jsonb", nullable: true }) - description: string | null = null - - @MikroOrmBigNumberProperty() - amount: BigNumber | number - - @Property({ columnType: "jsonb" }) - raw_amount: BigNumberRawValue - - @Property({ columnType: "boolean" }) - is_tax_inclusive: boolean = false - - @ShippingOptionIdIndex() - @Property({ columnType: "text", nullable: true }) - shipping_option_id: string | null = null - - @Property({ columnType: "jsonb", nullable: true }) - data: Record | null = null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @OneToMany( - () => ShippingMethodTaxLine, - (taxLine) => taxLine.shipping_method, +const ShippingMethod = model + .define( { - cascade: [Cascade.PERSIST, "soft-remove"] as any, - } - ) - tax_lines = new Collection>(this) - - @OneToMany( - () => ShippingMethodAdjustment, - (adjustment) => adjustment.shipping_method, + name: "ShippingMethod", + tableName: "cart_shipping_method", + }, { - cascade: [Cascade.PERSIST, "soft-remove"] as any, + id: model.id({ prefix: "casm" }).primaryKey(), + name: model.text(), + description: model.json().nullable(), + amount: model.bigNumber(), + is_tax_inclusive: model.boolean().default(false), + shipping_option_id: model.text().nullable(), + data: model.json().nullable(), + metadata: model.json().nullable(), + cart: model.belongsTo(() => Cart, { + mappedBy: "shipping_methods", + }), + tax_lines: model.hasMany(() => ShippingMethodTaxLine, { + mappedBy: "shipping_method", + }), + adjustments: model.hasMany(() => ShippingMethodAdjustment, { + mappedBy: "shipping_method", + }), } ) - adjustments = new Collection>(this) - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date - - @DeletedAtIndex() - @Property({ columnType: "timestamptz", nullable: true }) - deleted_at: Date | null = null - - @BeforeCreate() - onCreate() { - this.id = generateEntityId(this.id, "casm") - } - - @OnInit() - onInit() { - this.id = generateEntityId(this.id, "casm") - } -} + .indexes([ + { + name: "IDX_shipping_method_cart_id", + on: ["cart_id"], + where: "deleted_at IS NULL", + }, + { + name: "IDX_shipping_method_option_id", + on: ["shipping_option_id"], + where: "deleted_at IS NULL AND shipping_option_id IS NOT NULL", + }, + ]) + .checks([(columns) => `${columns.amount} >= 0`]) + +export default ShippingMethod diff --git a/packages/modules/cart/src/models/tax-line.ts b/packages/modules/cart/src/models/tax-line.ts deleted file mode 100644 index b99ab74cb88ab..0000000000000 --- a/packages/modules/cart/src/models/tax-line.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { DAL } from "@medusajs/framework/types" -import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" - -type OptionalTaxLineProps = DAL.SoftDeletableModelDateColumns - -/** - * 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" }) - code: string - - @Property({ columnType: "numeric", serializer: Number }) - rate: number - - @Property({ columnType: "text", nullable: true }) - provider_id?: string | null - - @Property({ columnType: "jsonb", nullable: true }) - metadata: Record | null = null - - @Property({ - onCreate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - created_at: Date - - @Property({ - onCreate: () => new Date(), - onUpdate: () => new Date(), - columnType: "timestamptz", - defaultRaw: "now()", - }) - updated_at: Date -} diff --git a/packages/modules/cart/src/services/cart-module.ts b/packages/modules/cart/src/services/cart-module.ts index d727707d77cb8..25f155e0e87c7 100644 --- a/packages/modules/cart/src/services/cart-module.ts +++ b/packages/modules/cart/src/services/cart-module.ts @@ -5,6 +5,7 @@ import { DAL, FindConfig, ICartModuleService, + InferEntityType, InternalModuleDeclaration, ModulesSdkTypes, } from "@medusajs/framework/types" @@ -76,14 +77,30 @@ export default class CartModuleService implements ICartModuleService { protected baseRepository_: DAL.RepositoryService - protected cartService_: ModulesSdkTypes.IMedusaInternalService - protected addressService_: ModulesSdkTypes.IMedusaInternalService
- protected lineItemService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService - protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService - protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService - protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService + protected cartService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected addressService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodAdjustmentService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemAdjustmentService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected lineItemTaxLineService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > + protected shippingMethodTaxLineService_: ModulesSdkTypes.IMedusaInternalService< + InferEntityType + > constructor( { @@ -274,7 +291,7 @@ export default class CartModuleService @MedusaContext() sharedContext: Context = {} ) { const lineItemsToCreate: CreateLineItemDTO[] = [] - const createdCarts: Cart[] = [] + const createdCarts: InferEntityType[] = [] for (const { items, ...cart } of data) { const [created] = await this.cartService_.create([cart], sharedContext) @@ -397,7 +414,7 @@ export default class CartModuleService data?: CartTypes.CreateLineItemDTO[] | CartTypes.CreateLineItemDTO, @MedusaContext() sharedContext: Context = {} ): Promise { - let items: LineItem[] = [] + let items: InferEntityType[] = [] if (isString(cartIdOrData)) { items = await this.addLineItems_( cartIdOrData, @@ -422,7 +439,7 @@ export default class CartModuleService cartId: string, items: CartTypes.CreateLineItemDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { const cart = await this.retrieveCart( cartId, { select: ["id"] }, @@ -443,7 +460,7 @@ export default class CartModuleService protected async addLineItemsBulk_( data: CreateLineItemDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { return await this.lineItemService_.create(data, sharedContext) } @@ -471,7 +488,7 @@ export default class CartModuleService data?: CartTypes.UpdateLineItemDTO | Partial, @MedusaContext() sharedContext: Context = {} ): Promise { - let items: LineItem[] = [] + let items: InferEntityType[] = [] if (isString(lineItemIdOrDataOrSelector)) { const item = await this.updateLineItem_( lineItemIdOrDataOrSelector, @@ -511,7 +528,7 @@ export default class CartModuleService lineItemId: string, data: Partial, @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise> { const [item] = await this.lineItemService_.update( [{ id: lineItemId, ...data }], sharedContext @@ -524,7 +541,7 @@ export default class CartModuleService protected async updateLineItemsWithSelector_( updates: CartTypes.UpdateLineItemWithSelectorDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { let toUpdate: UpdateLineItemDTO[] = [] for (const { selector, data } of updates) { const items = await this.listLineItems({ ...selector }, {}, sharedContext) @@ -639,7 +656,7 @@ export default class CartModuleService ): Promise< CartTypes.CartShippingMethodDTO[] | CartTypes.CartShippingMethodDTO > { - let methods: ShippingMethod[] + let methods: InferEntityType[] if (isString(cartIdOrData)) { methods = await this.addShippingMethods_( cartIdOrData, @@ -664,7 +681,7 @@ export default class CartModuleService cartId: string, data: CartTypes.CreateShippingMethodForSingleCartDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { const cart = await this.retrieveCart( cartId, { select: ["id"] }, @@ -685,7 +702,7 @@ export default class CartModuleService protected async addShippingMethodsBulk_( data: CartTypes.CreateShippingMethodDTO[], @MedusaContext() sharedContext: Context = {} - ): Promise { + ): Promise[]> { return await this.shippingMethodService_.create( data as unknown as CreateShippingMethodDTO[], sharedContext @@ -713,7 +730,7 @@ export default class CartModuleService adjustments?: CartTypes.CreateLineItemAdjustmentDTO[], @MedusaContext() sharedContext: Context = {} ): Promise { - let addedAdjustments: LineItemAdjustment[] = [] + let addedAdjustments: InferEntityType[] = [] if (isString(cartIdOrData)) { const cart = await this.retrieveCart( cartIdOrData, @@ -888,7 +905,8 @@ export default class CartModuleService | CartTypes.ShippingMethodAdjustmentDTO[] | CartTypes.ShippingMethodAdjustmentDTO > { - let addedAdjustments: ShippingMethodAdjustment[] = [] + let addedAdjustments: InferEntityType[] = + [] if (isString(cartIdOrData)) { const cart = await this.retrieveCart( cartIdOrData, @@ -961,7 +979,7 @@ export default class CartModuleService | CartTypes.CreateLineItemTaxLineDTO, @MedusaContext() sharedContext: Context = {} ): Promise { - let addedTaxLines: LineItemTaxLine[] + let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext) @@ -1077,7 +1095,7 @@ export default class CartModuleService ): Promise< CartTypes.ShippingMethodTaxLineDTO[] | CartTypes.ShippingMethodTaxLineDTO > { - let addedTaxLines: ShippingMethodTaxLine[] + let addedTaxLines: InferEntityType[] if (isString(cartIdOrData)) { // existence check await this.retrieveCart(cartIdOrData, { select: ["id"] }, sharedContext)