-
Notifications
You must be signed in to change notification settings - Fork 280
/
create-index-builder.ts
139 lines (126 loc) · 3.84 KB
/
create-index-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
CreateIndexNode,
IndexType,
} from '../operation-node/create-index-node.js'
import { ListNode } from '../operation-node/list-node.js'
import { OperationNodeSource } from '../operation-node/operation-node-source.js'
import { RawNode } from '../operation-node/raw-node.js'
import { parseColumnName } from '../parser/reference-parser.js'
import { parseTable } from '../parser/table-parser.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { Compilable } from '../util/compilable.js'
import { preventAwait } from '../util/prevent-await.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { QueryId } from '../util/query-id.js'
import { freeze } from '../util/object-utils.js'
export class CreateIndexBuilder implements OperationNodeSource, Compilable {
readonly #props: CreateIndexBuilderProps
constructor(props: CreateIndexBuilderProps) {
this.#props = freeze(props)
}
/**
* Makes the index unique.
*/
unique(): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
unique: true,
}),
})
}
/**
* Specifies the table for the index.
*/
on(table: string): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
table: parseTable(table),
}),
})
}
/**
* Specifies the column for the index.
*
* Also see the `expression` for specifying an arbitrary expression.
*/
column(column: string): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
expression: parseColumnName(column),
}),
})
}
/**
* Specifies a list of columns for the index.
*
* Also see the `expression` for specifying an arbitrary expression.
*/
columns(columns: string[]): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
expression: ListNode.create(columns.map(parseColumnName)),
}),
})
}
/**
* Specifies an arbitrary expression for the index.
*
* @example
* ```ts
* await db.schema
* .createIndex('person_first_name_index')
* .on('person')
* .expression('first_name COLLATE "fi_FI"')
* .execute()
* ```
*/
expression(expression: string): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
expression: RawNode.createWithSql(expression),
}),
})
}
/**
* Specifies the index type.
*/
using(indexType: IndexType): CreateIndexBuilder
using(indexType: string): CreateIndexBuilder
using(indexType: string): CreateIndexBuilder {
return new CreateIndexBuilder({
...this.#props,
createIndexNode: CreateIndexNode.cloneWith(this.#props.createIndexNode, {
using: RawNode.createWithSql(indexType),
}),
})
}
toOperationNode(): CreateIndexNode {
return this.#props.executor.transformQuery(
this.#props.createIndexNode,
this.#props.queryId
)
}
compile(): CompiledQuery {
return this.#props.executor.compileQuery(
this.toOperationNode(),
this.#props.queryId
)
}
async execute(): Promise<void> {
await this.#props.executor.executeQuery(this.compile(), this.#props.queryId)
}
}
preventAwait(
CreateIndexBuilder,
"don't await CreateIndexBuilder instances directly. To execute the query you need to call `execute`"
)
export interface CreateIndexBuilderProps {
readonly queryId: QueryId
readonly executor: QueryExecutor
readonly createIndexNode: CreateIndexNode
}