refactor: migration stock location module (#10471)

Fixes: FRMW-2831
This commit is contained in:
Harminder Virk authored and GitHub committed 2024-12-10 16:08:48 +00:00
1 parent e41ab50f59
commit 4ad9ac1e5f
8 files changed
+172 -231

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/stock-location": patch
---
refactor: migration stock location module
@@ -16,8 +16,8 @@ moduleIntegrationTestRunner<IStockLocationService>({
}).linkable }).linkable
expect(Object.keys(linkable)).toEqual([ expect(Object.keys(linkable)).toEqual([
"stockLocationAddress",
"stockLocation", "stockLocation",
"stockLocationAddress",
]) ])
Object.keys(linkable).forEach((key) => { Object.keys(linkable).forEach((key) => {
@@ -1,5 +1,7 @@
{ {
"namespaces": ["public"], "namespaces": [
"public"
],
"name": "public", "name": "public",
"tables": [ "tables": [
{ {
@@ -13,38 +15,6 @@
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
}, },
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"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"
},
"address_1": { "address_1": {
"name": "address_1", "name": "address_1",
"type": "text", "type": "text",
@@ -125,6 +95,38 @@
"primary": false, "primary": false,
"nullable": true, "nullable": true,
"mappedType": "json" "mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
},
"updated_at": {
"name": "updated_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"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": "stock_location_address", "name": "stock_location_address",
@@ -132,15 +134,17 @@
"indexes": [ "indexes": [
{ {
"keyName": "IDX_stock_location_address_deleted_at", "keyName": "IDX_stock_location_address_deleted_at",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_deleted_at\" ON \"stock_location_address\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_deleted_at\" ON \"stock_location_address\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "stock_location_address_pkey", "keyName": "stock_location_address_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -160,6 +164,33 @@
"nullable": false, "nullable": false,
"mappedType": "text" "mappedType": "text"
}, },
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"address_id": {
"name": "address_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"created_at": { "created_at": {
"name": "created_at", "name": "created_at",
"type": "timestamptz", "type": "timestamptz",
@@ -191,49 +222,32 @@
"nullable": true, "nullable": true,
"length": 6, "length": 6,
"mappedType": "datetime" "mappedType": "datetime"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"address_id": {
"name": "address_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": true,
"mappedType": "json"
} }
}, },
"name": "stock_location", "name": "stock_location",
"schema": "public", "schema": "public",
"indexes": [ "indexes": [
{ {
"keyName": "IDX_stock_location_deleted_at", "keyName": "IDX_stock_location_address_id",
"columnNames": ["deleted_at"], "columnNames": [],
"composite": false, "composite": false,
"primary": false, "primary": false,
"unique": false, "unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_deleted_at\" ON \"stock_location\" (deleted_at) WHERE deleted_at IS NOT NULL" "expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_address_id\" ON \"stock_location\" (address_id) WHERE deleted_at IS NULL"
},
{
"keyName": "IDX_stock_location_deleted_at",
"columnNames": [],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_stock_location_deleted_at\" ON \"stock_location\" (deleted_at) WHERE deleted_at IS NULL"
}, },
{ {
"keyName": "stock_location_pkey", "keyName": "stock_location_pkey",
"columnNames": ["id"], "columnNames": [
"id"
],
"composite": false, "composite": false,
"primary": true, "primary": true,
"unique": true "unique": true
@@ -243,11 +257,15 @@
"foreignKeys": { "foreignKeys": {
"stock_location_address_id_foreign": { "stock_location_address_id_foreign": {
"constraintName": "stock_location_address_id_foreign", "constraintName": "stock_location_address_id_foreign",
"columnNames": ["address_id"], "columnNames": [
"address_id"
],
"localTableName": "public.stock_location", "localTableName": "public.stock_location",
"referencedColumnNames": ["id"], "referencedColumnNames": [
"id"
],
"referencedTableName": "public.stock_location_address", "referencedTableName": "public.stock_location_address",
"deleteRule": "set null", "deleteRule": "cascade",
"updateRule": "cascade" "updateRule": "cascade"
} }
} }
@@ -0,0 +1,27 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20241210073813 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_foreign";'
)
this.addSql(
'alter table if exists "stock_location" add constraint "stock_location_address_id_foreign" foreign key ("address_id") references "stock_location_address" ("id") on update cascade on delete cascade;'
)
this.addSql(
'CREATE INDEX IF NOT EXISTS "IDX_stock_location_address_id" ON "stock_location" (address_id) WHERE deleted_at IS NULL;'
)
}
async down(): Promise<void> {
this.addSql(
'alter table if exists "stock_location" drop constraint if exists "stock_location_address_id_foreign";'
)
this.addSql('drop index if exists "IDX_stock_location_address_id";')
this.addSql(
'alter table if exists "stock_location" add constraint "stock_location_address_id_foreign" foreign key ("address_id") references "stock_location_address" ("id") on update cascade on delete set null;'
)
}
}
@@ -1,2 +1,2 @@
export * from "./stock-location" export { default as StockLocation } from "./stock-location"
export * from "./stock-location-address" export { default as StockLocationAddress } from "./stock-location-address"
@@ -1,79 +1,24 @@
import { import { model } from "@medusajs/framework/utils"
BeforeCreate, import { StockLocation } from "@models"
Entity,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
const StockLocationAddressDeletedAtIndex = createPsqlIndexStatementHelper({ const StockLocationAddress = model
tableName: "stock_location_address", .define("StockLocationAddress", {
columns: "deleted_at", id: model.id({ prefix: "laddr" }).primaryKey(),
where: "deleted_at IS NOT NULL", address_1: model.text(),
address_2: model.text().nullable(),
company: model.text().nullable(),
city: model.text().nullable(),
country_code: model.text(),
phone: model.text().nullable(),
province: model.text().nullable(),
postal_code: model.text().nullable(),
metadata: model.json().nullable(),
stock_locations: model.hasMany(() => StockLocation, {
mappedBy: "address",
}),
})
.cascades({
delete: ["stock_locations"],
}) })
@Entity() export default StockLocationAddress
export class StockLocationAddress {
@PrimaryKey({ columnType: "text" })
id: string
@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
@StockLocationAddressDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@Property({ columnType: "text" })
address_1: string
@Property({ columnType: "text", nullable: true })
address_2: string | null
@Property({ columnType: "text", nullable: true })
company: string | null
@Property({ columnType: "text", nullable: true })
city: string | null
@Property({ columnType: "text" })
country_code: string
@Property({ columnType: "text", nullable: true })
phone: string | null
@Property({ columnType: "text", nullable: true })
province: string | null
@Property({ columnType: "text", nullable: true })
postal_code: string | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null
@BeforeCreate()
beforeCreate(): void {
this.id = generateEntityId(this.id, "laddr")
}
@OnInit()
onInit(): void {
this.id = generateEntityId(this.id, "laddr")
}
}
@@ -1,80 +1,15 @@
import { import { model } from "@medusajs/framework/utils"
DALUtils, import StockLocationAddress from "./stock-location-address"
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { StockLocationAddress } from "./stock-location-address" const StockLocation = model.define("StockLocation", {
id: model.id({ prefix: "sloc" }).primaryKey(),
const StockLocationDeletedAtIndex = createPsqlIndexStatementHelper({ name: model.text().searchable(),
tableName: "stock_location", metadata: model.json().nullable(),
columns: "deleted_at", address: model
where: "deleted_at IS NOT NULL", .belongsTo(() => StockLocationAddress, {
mappedBy: "stock_locations",
})
.nullable(),
}) })
@Entity() export default StockLocation
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export class StockLocation {
@PrimaryKey({ columnType: "text" })
id: string
@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
@StockLocationDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@Searchable()
@Property({ columnType: "text" })
name: string
@ManyToOne(() => StockLocationAddress, {
fieldName: "address_id",
type: "text",
mapToPk: true,
nullable: true,
onDelete: "cascade",
})
address_id: string | null
@ManyToOne(() => StockLocationAddress, {
nullable: true,
})
address: StockLocationAddress | null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null
@BeforeCreate()
beforeCreate(): void {
this.id = generateEntityId(this.id, "sloc")
}
@OnInit()
onInit(): void {
this.id = generateEntityId(this.id, "sloc")
}
}
@@ -4,6 +4,7 @@ import {
DAL, DAL,
FilterableStockLocationProps, FilterableStockLocationProps,
IEventBusService, IEventBusService,
InferEntityType,
InternalModuleDeclaration, InternalModuleDeclaration,
IStockLocationService, IStockLocationService,
ModuleJoinerConfig, ModuleJoinerConfig,
@@ -44,8 +45,12 @@ export default class StockLocationModuleService
{ {
protected readonly eventBusModuleService_: IEventBusService protected readonly eventBusModuleService_: IEventBusService
protected baseRepository_: DAL.RepositoryService protected baseRepository_: DAL.RepositoryService
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<StockLocation> protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<StockLocationAddress> InferEntityType<typeof StockLocation>
>
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof StockLocationAddress>
>
constructor( constructor(
{ {
@@ -102,7 +107,7 @@ export default class StockLocationModuleService
async createStockLocations_( async createStockLocations_(
data: CreateStockLocationInput[], data: CreateStockLocationInput[],
@MedusaContext() context: Context = {} @MedusaContext() context: Context = {}
): Promise<StockLocation[]> { ): Promise<InferEntityType<typeof StockLocation>[]> {
return await this.stockLocationService_.create(data, context) return await this.stockLocationService_.create(data, context)
} }
@@ -144,7 +149,10 @@ export default class StockLocationModuleService
(location) => !location.id (location) => !location.id
) as CreateStockLocationInput[] ) as CreateStockLocationInput[]
const operations: Promise<StockLocation[] | StockLocation>[] = [] const operations: Promise<
| InferEntityType<typeof StockLocation>[]
| InferEntityType<typeof StockLocation>
>[] = []
if (toCreate.length) { if (toCreate.length) {
operations.push(this.createStockLocations_(toCreate, context)) operations.push(this.createStockLocations_(toCreate, context))
@@ -207,7 +215,10 @@ export default class StockLocationModuleService
| UpdateStockLocationInput | UpdateStockLocationInput
| { data: any; selector: FilterableStockLocationProps }, | { data: any; selector: FilterableStockLocationProps },
@MedusaContext() context: Context = {} @MedusaContext() context: Context = {}
): Promise<StockLocation[] | StockLocation> { ): Promise<
| InferEntityType<typeof StockLocation>[]
| InferEntityType<typeof StockLocation>
> {
return await this.stockLocationService_.update(data, context) return await this.stockLocationService_.update(data, context)
} }