refactor: migration stock location module (#10471)

Fixes: FRMW-2831
This commit is contained in:
Harminder Virk
2024-12-10 21:38:48 +05:30
committed by GitHub
parent e41ab50f59
commit 4ad9ac1e5f
8 changed files with 171 additions and 230 deletions

View File

@@ -16,8 +16,8 @@ moduleIntegrationTestRunner<IStockLocationService>({
}).linkable
expect(Object.keys(linkable)).toEqual([
"stockLocationAddress",
"stockLocation",
"stockLocationAddress",
])
Object.keys(linkable).forEach((key) => {

View File

@@ -1,5 +1,7 @@
{
"namespaces": ["public"],
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
@@ -13,38 +15,6 @@
"nullable": false,
"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": {
"name": "address_1",
"type": "text",
@@ -125,6 +95,38 @@
"primary": false,
"nullable": true,
"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",
@@ -132,15 +134,17 @@
"indexes": [
{
"keyName": "IDX_stock_location_address_deleted_at",
"columnNames": ["deleted_at"],
"columnNames": [],
"composite": false,
"primary": 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",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -160,6 +164,33 @@
"nullable": false,
"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": {
"name": "created_at",
"type": "timestamptz",
@@ -191,49 +222,32 @@
"nullable": true,
"length": 6,
"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",
"schema": "public",
"indexes": [
{
"keyName": "IDX_stock_location_deleted_at",
"columnNames": ["deleted_at"],
"keyName": "IDX_stock_location_address_id",
"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 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",
"columnNames": ["id"],
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
@@ -243,11 +257,15 @@
"foreignKeys": {
"stock_location_address_id_foreign": {
"constraintName": "stock_location_address_id_foreign",
"columnNames": ["address_id"],
"columnNames": [
"address_id"
],
"localTableName": "public.stock_location",
"referencedColumnNames": ["id"],
"referencedColumnNames": [
"id"
],
"referencedTableName": "public.stock_location_address",
"deleteRule": "set null",
"deleteRule": "cascade",
"updateRule": "cascade"
}
}

View File

@@ -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;'
)
}
}

View File

@@ -1,2 +1,2 @@
export * from "./stock-location"
export * from "./stock-location-address"
export { default as StockLocation } from "./stock-location"
export { default as StockLocationAddress } from "./stock-location-address"

View File

@@ -1,79 +1,24 @@
import {
BeforeCreate,
Entity,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import {
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import { model } from "@medusajs/framework/utils"
import { StockLocation } from "@models"
const StockLocationAddressDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "stock_location_address",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
export class StockLocationAddress {
@PrimaryKey({ columnType: "text" })
id: string
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
const StockLocationAddress = model
.define("StockLocationAddress", {
id: model.id({ prefix: "laddr" }).primaryKey(),
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",
}),
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["stock_locations"],
})
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")
}
}
export default StockLocationAddress

View File

@@ -1,80 +1,15 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
Filter,
ManyToOne,
OnInit,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import StockLocationAddress from "./stock-location-address"
import { StockLocationAddress } from "./stock-location-address"
const StockLocationDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "stock_location",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
const StockLocation = model.define("StockLocation", {
id: model.id({ prefix: "sloc" }).primaryKey(),
name: model.text().searchable(),
metadata: model.json().nullable(),
address: model
.belongsTo(() => StockLocationAddress, {
mappedBy: "stock_locations",
})
.nullable(),
})
@Entity()
@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")
}
}
export default StockLocation

View File

@@ -4,6 +4,7 @@ import {
DAL,
FilterableStockLocationProps,
IEventBusService,
InferEntityType,
InternalModuleDeclaration,
IStockLocationService,
ModuleJoinerConfig,
@@ -44,8 +45,12 @@ export default class StockLocationModuleService
{
protected readonly eventBusModuleService_: IEventBusService
protected baseRepository_: DAL.RepositoryService
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<StockLocation>
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<StockLocationAddress>
protected readonly stockLocationService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof StockLocation>
>
protected readonly stockLocationAddressService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof StockLocationAddress>
>
constructor(
{
@@ -102,7 +107,7 @@ export default class StockLocationModuleService
async createStockLocations_(
data: CreateStockLocationInput[],
@MedusaContext() context: Context = {}
): Promise<StockLocation[]> {
): Promise<InferEntityType<typeof StockLocation>[]> {
return await this.stockLocationService_.create(data, context)
}
@@ -144,7 +149,10 @@ export default class StockLocationModuleService
(location) => !location.id
) as CreateStockLocationInput[]
const operations: Promise<StockLocation[] | StockLocation>[] = []
const operations: Promise<
| InferEntityType<typeof StockLocation>[]
| InferEntityType<typeof StockLocation>
>[] = []
if (toCreate.length) {
operations.push(this.createStockLocations_(toCreate, context))
@@ -207,7 +215,10 @@ export default class StockLocationModuleService
| UpdateStockLocationInput
| { data: any; selector: FilterableStockLocationProps },
@MedusaContext() context: Context = {}
): Promise<StockLocation[] | StockLocation> {
): Promise<
| InferEntityType<typeof StockLocation>[]
| InferEntityType<typeof StockLocation>
> {
return await this.stockLocationService_.update(data, context)
}