-
Notifications
You must be signed in to change notification settings - Fork 280
/
schema.ts
210 lines (200 loc) · 5.68 KB
/
schema.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { AlterTableNode } from '../operation-node/alter-table-node.js'
import { CreateIndexNode } from '../operation-node/create-index-node.js'
import { CreateSchemaNode } from '../operation-node/create-schema-node.js'
import { CreateTableNode } from '../operation-node/create-table-node.js'
import { DropIndexNode } from '../operation-node/drop-index-node.js'
import { DropSchemaNode } from '../operation-node/drop-schema-node.js'
import { DropTableNode } from '../operation-node/drop-table-node.js'
import { parseTable } from '../parser/table-parser.js'
import { QueryExecutor } from '../query-executor/query-executor.js'
import { AlterTableBuilder } from './alter-table-builder.js'
import { CreateIndexBuilder } from './create-index-builder.js'
import { CreateSchemaBuilder } from './create-schema-builder.js'
import { CreateTableBuilder } from './create-table-builder.js'
import { DropIndexBuilder } from './drop-index-builder.js'
import { DropSchemaBuilder } from './drop-schema-builder.js'
import { DropTableBuilder } from './drop-table-builder.js'
import { createQueryId } from '../util/query-id.js'
import { WithSchemaPlugin } from '../plugin/with-schema/with-schema-plugin.js'
/**
* Provides methods for building database schema.
*/
export class SchemaModule {
readonly #executor: QueryExecutor
constructor(executor: QueryExecutor) {
this.#executor = executor
}
/**
* Create a new table.
*
* @example
* This example creates a new table with columns `id`, `first_name`,
* `last_name` and `gender`:
*
* ```ts
* await db.schema
* .createTable('person')
* .addColumn('id', 'integer', col => col.primaryKey().increments())
* .addColumn('first_name', 'varchar', col => col.notNull())
* .addColumn('last_name', 'varchar', col => col.notNull())
* .addColumn('gender', 'varchar')
* .execute()
* ```
*
* @example
* This example creates a table with a foreign key. Not all database
* engines support column-level foreign key constraint definitions.
* For example if you are using MySQL 5.X see the next example after
* this one.
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().increments())
* .addColumn('owner_id', 'integer', col => col
* .references('person.id')
* .onDelete('cascade')
* )
* .execute()
* ```
*
* @example
* This example adds a foreign key constraint for a columns just
* like the previous example, but using a table-level statement.
* On MySQL 5.X you need to define foreign key constraints like
* this:
*
* ```ts
* await db.schema
* .createTable('pet')
* .addColumn('id', 'integer', col => col.primaryKey().increments())
* .addColumn('owner_id', 'integer')
* .addForeignKeyConstraint(
* 'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
* (constraint) => constraint.onDelete('cascade')
* )
* .execute()
* ```
*/
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never> {
return new CreateTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
createTableNode: CreateTableNode.create(parseTable(table)),
})
}
/**
* Drop a table.
*
* @example
* ```ts
* await db.schema
* .dropTable('person')
* .execute()
* ```
*/
dropTable(table: string): DropTableBuilder {
return new DropTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
dropTableNode: DropTableNode.create(parseTable(table)),
})
}
/**
* Create a new index.
*
* @example
* ```ts
* await db.schema
* .createIndex('person_full_name_unique_index')
* .on('person')
* .columns(['first_name', 'last_name'])
* .execute()
* ```
*/
createIndex(indexName: string): CreateIndexBuilder {
return new CreateIndexBuilder({
queryId: createQueryId(),
executor: this.#executor,
createIndexNode: CreateIndexNode.create(indexName),
})
}
/**
* Drop an index.
*
* @example
* ```ts
* await db.schema
* .dropIndex('person_full_name_unique_index')
* .execute()
* ```
*/
dropIndex(indexName: string): DropIndexBuilder {
return new DropIndexBuilder({
queryId: createQueryId(),
executor: this.#executor,
dropIndexNode: DropIndexNode.create(indexName),
})
}
/**
* Create a new schema.
*
* @example
* ```ts
* await db.schema
* .createSchema('some_schema')
* .execute()
* ```
*/
createSchema(schema: string): CreateSchemaBuilder {
return new CreateSchemaBuilder({
queryId: createQueryId(),
executor: this.#executor,
createSchemaNode: CreateSchemaNode.create(schema),
})
}
/**
* Drop a schema.
*
* @example
* ```ts
* await db.schema
* .dropSchema('some_schema')
* .execute()
* ```
*/
dropSchema(schema: string): DropSchemaBuilder {
return new DropSchemaBuilder({
queryId: createQueryId(),
executor: this.#executor,
dropSchemaNode: DropSchemaNode.create(schema),
})
}
/**
* Alter a table.
*
* @example
* ```ts
* await db.schema
* .alterTable('person')
* .alterColumn('first_name')
* .setDataType('text')
* .execute()
* ```
*/
alterTable(table: string): AlterTableBuilder {
return new AlterTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
alterTableNode: AlterTableNode.create(table),
})
}
/**
* See {@link QueryCreator.withSchema}
*/
withSchema(schema: string): SchemaModule {
return new SchemaModule(
this.#executor.withPluginAtFront(new WithSchemaPlugin(schema))
)
}
}