1 import { model } from "@medusajs/framework/utils"
2 import CustomerAddress from "./address"
3 import CustomerGroup from "./customer-group"
4 import CustomerGroupCustomer from "./customer-group-customer"
5
6 const Customer = model
7 .define("Customer", {
8 id: model.id({ prefix: "cus" }).primaryKey(),
9 company_name: model.text().searchable().nullable(),
10 first_name: model.text().searchable().nullable(),
11 last_name: model.text().searchable().nullable(),
12 email: model.text().searchable().nullable(),
13 phone: model.text().searchable().nullable(),
14 has_account: model.boolean().default(false),
15 metadata: model.json().nullable(),
16 created_by: model.text().nullable(),
17 groups: model.manyToMany(() => CustomerGroup, {
18 mappedBy: "customers",
19 pivotEntity: () => CustomerGroupCustomer,
20 }),
21 addresses: model.hasMany(() => CustomerAddress, {
22 mappedBy: "customer",
23 }),
24 })
25 .cascades({
26 delete: ["addresses"],
27 detach: ["groups"],
28 })
29 .indexes([
30 {
31 on: ["email", "has_account"],
32 unique: true,
33 where: "deleted_at IS NULL",
34 },
35 ])
36
37 export default Customer