|
1
|
import { Index } from "@medusajs/deps/mikro-orm/core" |
|
2
|
|
|
3
|
/** |
|
4
|
* Create a PSQL index statement |
|
5
|
* @param name The name of the index, if not provided it will be generated in the format IDX_tableName_columnName |
|
6
|
* @param tableName The name of the table |
|
7
|
* @param columns The columns to index |
|
8
|
* @param type The type of index (e.g GIN, GIST, BTREE, etc) |
|
9
|
* @param where The where clause |
|
10
|
* @param unique If the index should be a unique index |
|
11
|
* |
|
12
|
* @example |
|
13
|
* createPsqlIndexStatementHelper({ |
|
14
|
* name: "idx_user_email", |
|
15
|
* tableName: "user", |
|
16
|
* columns: "email", |
|
17
|
* type: "btree", |
|
18
|
* where: "email IS NOT NULL" |
|
19
|
* }); |
|
20
|
* |
|
21
|
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user USING btree (email) WHERE email IS NOT NULL; |
|
22
|
* |
|
23
|
* createPsqlIndexStatementHelper({ |
|
24
|
* name: "idx_user_email", |
|
25
|
* tableName: "user", |
|
26
|
* columns: "email" |
|
27
|
* }); |
|
28
|
* |
|
29
|
* // expression: CREATE INDEX IF NOT EXISTS idx_user_email ON user (email); |
|
30
|
* |
|
31
|
*/ |
|
32
|
export function createPsqlIndexStatementHelper({ |
|
33
|
name, |
|
34
|
tableName: qualifiedName, |
|
35
|
columns, |
|
36
|
type, |
|
37
|
where, |
|
38
|
unique, |
|
39
|
}: { |
|
40
|
name?: string |
|
41
|
tableName: string |
|
42
|
columns: string | string[] |
|
43
|
type?: string |
|
44
|
where?: string | null |
|
45
|
unique?: boolean |
|
46
|
}) { |
|
47
|
const columnsName = Array.isArray(columns) ? columns.join("_") : columns |
|
48
|
const tokens = qualifiedName.replace(/"/g, "").split(".") |
|
49
|
|
|
50
|
let pgSchemaName: string | undefined |
|
51
|
let tableName: string |
|
52
|
let tableReference: string |
|
53
|
|
|
54
|
if (tokens.length > 1) { |
|
55
|
pgSchemaName = tokens.shift() |
|
56
|
tableName = tokens.join(".") |
|
57
|
tableReference = `"${pgSchemaName}"."${tableName}"` |
|
58
|
} else { |
|
59
|
tableName = qualifiedName |
|
60
|
tableReference = `"${tableName}"` |
|
61
|
} |
|
62
|
|
|
63
|
columns = Array.isArray(columns) |
|
64
|
? columns.map((column) => `"${column}"`).join(", ") |
|
65
|
: columns |
|
66
|
name = name || `IDX_${tableName}_${columnsName}${unique ? "_unique" : ""}` |
|
67
|
|
|
68
|
const typeStr = type ? ` USING ${type}` : "" |
|
69
|
const optionsStr = where ? ` WHERE ${where}` : "" |
|
70
|
const uniqueStr = unique ? "UNIQUE " : "" |
|
71
|
|
|
72
|
const expression = `CREATE ${uniqueStr}INDEX IF NOT EXISTS "${name}" ON ${tableReference}${typeStr} (${columns})${optionsStr}` |
|
73
|
return { |
|
74
|
toString: () => { |
|
75
|
return expression |
|
76
|
}, |
|
77
|
valueOf: () => { |
|
78
|
return expression |
|
79
|
}, |
|
80
|
name, |
|
81
|
expression, |
|
82
|
MikroORMIndex: (options?: Parameters<typeof Index>[0]) => { |
|
83
|
return Index({ |
|
84
|
name, |
|
85
|
expression, |
|
86
|
...options, |
|
87
|
}) |
|
88
|
}, |
|
89
|
} |
|
90
|
} |