feat: Migrate customer module to DML (#10499)
This commit is contained in:
@@ -1,133 +1,40 @@
|
||||
import { DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
Searchable,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Customer from "./customer"
|
||||
|
||||
type OptionalAddressProps = DAL.ModelDateColumns // TODO: To be revisited when more clear
|
||||
|
||||
const CustomerAddressUniqueCustomerShippingAddress =
|
||||
createPsqlIndexStatementHelper({
|
||||
name: "IDX_customer_address_unique_customer_shipping",
|
||||
tableName: "customer_address",
|
||||
columns: "customer_id",
|
||||
unique: true,
|
||||
where: '"is_default_shipping" = true',
|
||||
const CustomerAddress = model
|
||||
.define("CustomerAddress", {
|
||||
id: model.id({ prefix: "cuaddr" }).primaryKey(),
|
||||
address_name: model.text().searchable().nullable(),
|
||||
is_default_shipping: model.boolean().default(false),
|
||||
is_default_billing: model.boolean().default(false),
|
||||
company: model.text().searchable().nullable(),
|
||||
first_name: model.text().searchable().nullable(),
|
||||
last_name: model.text().searchable().nullable(),
|
||||
address_1: model.text().searchable().nullable(),
|
||||
address_2: model.text().searchable().nullable(),
|
||||
city: model.text().searchable().nullable(),
|
||||
country_code: model.text().nullable(),
|
||||
province: model.text().searchable().nullable(),
|
||||
postal_code: model.text().searchable().nullable(),
|
||||
phone: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
customer: model.belongsTo(() => Customer, {
|
||||
mappedBy: "addresses",
|
||||
}),
|
||||
})
|
||||
.indexes([
|
||||
{
|
||||
name: "IDX_customer_address_unique_customer_billing",
|
||||
on: ["customer_id"],
|
||||
unique: true,
|
||||
where: '"is_default_billing" = true',
|
||||
},
|
||||
{
|
||||
name: "IDX_customer_address_unique_customer_shipping",
|
||||
on: ["customer_id"],
|
||||
unique: true,
|
||||
where: '"is_default_shipping" = true',
|
||||
},
|
||||
])
|
||||
|
||||
const CustomerAddressUniqueCustomerBillingAddress =
|
||||
createPsqlIndexStatementHelper({
|
||||
name: "IDX_customer_address_unique_customer_billing",
|
||||
tableName: "customer_address",
|
||||
columns: "customer_id",
|
||||
unique: true,
|
||||
where: '"is_default_billing" = true',
|
||||
})
|
||||
|
||||
@Entity({ tableName: "customer_address" })
|
||||
@CustomerAddressUniqueCustomerShippingAddress.MikroORMIndex()
|
||||
@CustomerAddressUniqueCustomerBillingAddress.MikroORMIndex()
|
||||
export default class CustomerAddress {
|
||||
[OptionalProps]: OptionalAddressProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
address_name: string | null = null
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_default_shipping: boolean = false
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
is_default_billing: boolean = false
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
customer_id: string
|
||||
|
||||
@ManyToOne(() => Customer, {
|
||||
fieldName: "customer_id",
|
||||
index: "IDX_customer_address_customer_id",
|
||||
cascade: [Cascade.REMOVE, Cascade.PERSIST],
|
||||
})
|
||||
customer: Customer
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
company: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
first_name: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
last_name: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
address_1: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
address_2: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
city: string | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
country_code: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
province: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@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<string, unknown> | 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
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "cuaddr")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "cuaddr")
|
||||
}
|
||||
}
|
||||
export default CustomerAddress
|
||||
|
||||
@@ -1,78 +1,17 @@
|
||||
import { DAL } from "@medusajs/framework/types"
|
||||
import { generateEntityId } from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Customer from "./customer"
|
||||
import CustomerGroup from "./customer-group"
|
||||
|
||||
type OptionalGroupProps = "customer_group" | "customer" | DAL.ModelDateColumns // TODO: To be revisited when more clear
|
||||
const CustomerGroupCustomer = model.define("CustomerGroupCustomer", {
|
||||
id: model.id({ prefix: "cusgc" }).primaryKey(),
|
||||
created_by: model.text().nullable(),
|
||||
metadata: model.json().nullable(),
|
||||
customer: model.belongsTo(() => Customer, {
|
||||
mappedBy: "groups",
|
||||
}),
|
||||
customer_group: model.belongsTo(() => CustomerGroup, {
|
||||
mappedBy: "customers",
|
||||
}),
|
||||
})
|
||||
|
||||
@Entity({ tableName: "customer_group_customer" })
|
||||
export default class CustomerGroupCustomer {
|
||||
[OptionalProps]: OptionalGroupProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
customer_id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
customer_group_id: string
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => Customer,
|
||||
fieldName: "customer_id",
|
||||
index: "IDX_customer_group_customer_customer_id",
|
||||
cascade: [Cascade.REMOVE],
|
||||
})
|
||||
customer: Rel<Customer>
|
||||
|
||||
@ManyToOne({
|
||||
entity: () => CustomerGroup,
|
||||
fieldName: "customer_group_id",
|
||||
index: "IDX_customer_group_customer_group_id",
|
||||
cascade: [Cascade.REMOVE],
|
||||
})
|
||||
customer_group: Rel<CustomerGroup>
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | 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
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "cusgc")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "cusgc")
|
||||
}
|
||||
}
|
||||
export default CustomerGroupCustomer
|
||||
|
||||
@@ -1,84 +1,27 @@
|
||||
import { DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
DALUtils,
|
||||
Searchable,
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToMany,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import Customer from "./customer"
|
||||
import CustomerGroupCustomer from "./customer-group-customer"
|
||||
import { CustomerGroupCustomer } from "@models"
|
||||
|
||||
type OptionalGroupProps = DAL.SoftDeletableModelDateColumns // TODO: To be revisited when more clear
|
||||
|
||||
const CustomerGroupUniqueName = createPsqlIndexStatementHelper({
|
||||
tableName: "customer_group",
|
||||
columns: ["name"],
|
||||
unique: true,
|
||||
where: "deleted_at IS NULL",
|
||||
})
|
||||
|
||||
@Entity({ tableName: "customer_group" })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class CustomerGroup {
|
||||
[OptionalProps]: OptionalGroupProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id!: string
|
||||
|
||||
@Searchable()
|
||||
@CustomerGroupUniqueName.MikroORMIndex()
|
||||
@Property({ columnType: "text" })
|
||||
name: string
|
||||
|
||||
@ManyToMany({
|
||||
entity: () => Customer,
|
||||
pivotEntity: () => CustomerGroupCustomer,
|
||||
const CustomerGroup = model
|
||||
.define("CustomerGroup", {
|
||||
id: model.id({ prefix: "cusgroup" }).primaryKey(),
|
||||
name: model.text().searchable(),
|
||||
metadata: model.json().nullable(),
|
||||
created_by: model.text().nullable(),
|
||||
customers: model.manyToMany(() => Customer, {
|
||||
mappedBy: "groups",
|
||||
pivotEntity: () => CustomerGroupCustomer,
|
||||
}),
|
||||
})
|
||||
customers = new Collection<Rel<Customer>>(this)
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
.indexes([
|
||||
{
|
||||
on: ["name"],
|
||||
unique: true,
|
||||
where: "deleted_at IS NULL",
|
||||
},
|
||||
])
|
||||
.cascades({
|
||||
detach: ["customers"],
|
||||
})
|
||||
created_at: Date
|
||||
|
||||
@Property({
|
||||
onCreate: () => new Date(),
|
||||
onUpdate: () => new Date(),
|
||||
columnType: "timestamptz",
|
||||
defaultRaw: "now()",
|
||||
})
|
||||
updated_at: Date
|
||||
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "cusgroup")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "cusgroup")
|
||||
}
|
||||
}
|
||||
export default CustomerGroup
|
||||
|
||||
@@ -1,115 +1,37 @@
|
||||
import { DAL } from "@medusajs/framework/types"
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
DALUtils,
|
||||
generateEntityId,
|
||||
Searchable,
|
||||
} from "@medusajs/framework/utils"
|
||||
import {
|
||||
BeforeCreate,
|
||||
Cascade,
|
||||
Collection,
|
||||
Entity,
|
||||
Filter,
|
||||
ManyToMany,
|
||||
OneToMany,
|
||||
OnInit,
|
||||
OptionalProps,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
Rel,
|
||||
} from "@mikro-orm/core"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
import CustomerAddress from "./address"
|
||||
import CustomerGroup from "./customer-group"
|
||||
import CustomerGroupCustomer from "./customer-group-customer"
|
||||
|
||||
type OptionalCustomerProps =
|
||||
| "groups"
|
||||
| "addresses"
|
||||
| DAL.SoftDeletableModelDateColumns
|
||||
|
||||
const CustomerUniqueEmail = createPsqlIndexStatementHelper({
|
||||
tableName: "customer",
|
||||
columns: ["email", "has_account"],
|
||||
unique: true,
|
||||
where: "deleted_at IS NULL",
|
||||
})
|
||||
|
||||
@Entity({ tableName: "customer" })
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
@CustomerUniqueEmail.MikroORMIndex()
|
||||
export default class Customer {
|
||||
[OptionalProps]?: OptionalCustomerProps
|
||||
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
company_name: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
first_name: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
last_name: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
email: string | null = null
|
||||
|
||||
@Searchable()
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
phone: string | null = null
|
||||
|
||||
@Property({ columnType: "boolean", default: false })
|
||||
has_account: boolean = false
|
||||
|
||||
@Property({ columnType: "jsonb", nullable: true })
|
||||
metadata: Record<string, unknown> | null = null
|
||||
|
||||
@ManyToMany({
|
||||
mappedBy: "customers",
|
||||
entity: () => CustomerGroup,
|
||||
pivotEntity: () => CustomerGroupCustomer,
|
||||
const Customer = model
|
||||
.define("Customer", {
|
||||
id: model.id({ prefix: "cus" }).primaryKey(),
|
||||
company_name: model.text().searchable().nullable(),
|
||||
first_name: model.text().searchable().nullable(),
|
||||
last_name: model.text().searchable().nullable(),
|
||||
email: model.text().searchable().nullable(),
|
||||
phone: model.text().searchable().nullable(),
|
||||
has_account: model.boolean().default(false),
|
||||
metadata: model.json().nullable(),
|
||||
created_by: model.text().nullable(),
|
||||
groups: model.manyToMany(() => CustomerGroup, {
|
||||
mappedBy: "customers",
|
||||
pivotEntity: () => CustomerGroupCustomer,
|
||||
}),
|
||||
addresses: model.hasMany(() => CustomerAddress, {
|
||||
mappedBy: "customer",
|
||||
}),
|
||||
})
|
||||
groups = new Collection<Rel<CustomerGroup>>(this)
|
||||
|
||||
@OneToMany(() => CustomerAddress, (address) => address.customer, {
|
||||
cascade: [Cascade.REMOVE],
|
||||
.cascades({
|
||||
delete: ["addresses"],
|
||||
detach: ["groups"],
|
||||
})
|
||||
addresses = new Collection<Rel<CustomerAddress>>(this)
|
||||
.indexes([
|
||||
{
|
||||
on: ["email", "has_account"],
|
||||
unique: true,
|
||||
where: "deleted_at IS 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
|
||||
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
|
||||
@Property({ columnType: "text", nullable: true })
|
||||
created_by: string | null = null
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "cus")
|
||||
}
|
||||
|
||||
@OnInit()
|
||||
onInit() {
|
||||
this.id = generateEntityId(this.id, "cus")
|
||||
}
|
||||
}
|
||||
export default Customer
|
||||
|
||||
Reference in New Issue
Block a user