1 import {
2 JoinerRelationship,
3 ModuleJoinerConfig,
4 } from "@medusajs/framework/types"
5 import {
6 composeTableName,
7 compressName,
8 mikroOrmSoftDeletableFilterOptions,
9 simpleHash,
10 SoftDeletableFilterKey,
11 } from "@medusajs/framework/utils"
12
13 import { EntitySchema } from "@medusajs/framework/mikro-orm/core"
14
15 function getClass(...properties) {
16 return class LinkModel {
17 constructor(...values) {
18 properties.forEach((name, idx) => {
19 this[name] = values[idx]
20 })
21 }
22 }
23 }
24
25 export function generateEntity(
26 joinerConfig: ModuleJoinerConfig,
27 primary: JoinerRelationship,
28 foreign: JoinerRelationship
29 ) {
30 const fieldNames = primary.foreignKey.split(",").concat(foreign.foreignKey)
31
32 const tableName =
33 joinerConfig.databaseConfig?.tableName ??
34 composeTableName(
35 primary.serviceName,
36 primary.foreignKey,
37 foreign.serviceName,
38 foreign.foreignKey
39 ).toLowerCase()
40
41 const fields = fieldNames.reduce((acc, curr) => {
42 acc[curr] = {
43 type: "string",
44 nullable: false,
45 primary: true,
46 }
47 return acc
48 }, {})
49
50 const extraFields = joinerConfig.databaseConfig?.extraFields ?? {}
51
52 for (const column in extraFields) {
53 fieldNames.push(column)
54
55 fields[column] = {
56 type: extraFields[column].type,
57 nullable: !!extraFields[column].nullable,
58 defaultRaw: extraFields[column].defaultValue,
59 ...(extraFields[column].options ?? {}),
60 }
61 }
62
63 const hashTableName = simpleHash(tableName)
64
65 return new EntitySchema({
66 class: getClass(
67 ...fieldNames.concat("created_at", "updated_at", "deleted_at")
68 ) as any,
69 tableName: compressName(tableName),
70 properties: {
71 id: {
72 type: "string",
73 nullable: false,
74 },
75 ...fields,
76 created_at: {
77 columnType: "timestamptz",
78 type: "date",
79 nullable: false,
80 defaultRaw: "CURRENT_TIMESTAMP",
81 },
82 updated_at: {
83 columnType: "timestamptz",
84 type: "date",
85 nullable: false,
86 defaultRaw: "CURRENT_TIMESTAMP",
87 },
88 deleted_at: {
89 columnType: "timestamptz",
90 type: "date",
91 nullable: true,
92 },
93 } as any,
94 filters: {
95 [SoftDeletableFilterKey]: mikroOrmSoftDeletableFilterOptions,
96 },
97 hooks: {
98 beforeUpdate: [
99 (args) => {
100 args.entity.updated_at = new Date()
101 },
102 ],
103 },
104 indexes: [
105 {
106 properties: ["id"],
107 name: "IDX_id_" + hashTableName,
108 },
109 {
110 properties: primary.foreignKey.split(",") as any,
111 name:
112 "IDX_" +
113 primary.foreignKey.split(",").join("_") +
114 "_" +
115 hashTableName,
116 expression:
117 "CREATE INDEX IF NOT EXISTS " +
118 '"IDX_' +
119 primary.foreignKey.split(",").join("_") +
120 "_" +
121 hashTableName +
122 '" ON "' +
123 compressName(tableName) +
124 '" ("' +
125 primary.foreignKey.split(",").join(",") +
126 '") WHERE deleted_at IS NULL',
127 },
128 {
129 properties: foreign.foreignKey as any,
130 name: "IDX_" + foreign.foreignKey + "_" + hashTableName,
131 expression:
132 "CREATE INDEX IF NOT EXISTS " +
133 '"IDX_' +
134 foreign.foreignKey +
135 "_" +
136 hashTableName +
137 '" ON "' +
138 compressName(tableName) +
139 '" ("' +
140 foreign.foreignKey +
141 '") WHERE deleted_at IS NULL',
142 },
143 {
144 properties: ["deleted_at"],
145 name: "IDX_deleted_at_" + hashTableName,
146 },
147 ],
148 })
149 }