-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathschema.ts
324 lines (308 loc) · 8.38 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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'
import { CreateViewBuilder } from './create-view-builder.js'
import { CreateViewNode } from '../operation-node/create-view-node.js'
import { DropViewBuilder } from './drop-view-builder.js'
import { DropViewNode } from '../operation-node/drop-view-node.js'
import { KyselyPlugin } from '../plugin/kysely-plugin.js'
import { CreateTypeBuilder } from './create-type-builder.js'
import { DropTypeBuilder } from './drop-type-builder.js'
import { CreateTypeNode } from '../operation-node/create-type-node.js'
import { DropTypeNode } from '../operation-node/drop-type-node.js'
import { parseSchemableIdentifier } from '../parser/identifier-parser.js'
/**
* Provides methods for building database schema.
*/
export class SchemaModule {
readonly #executor: QueryExecutor
constructor(executor: QueryExecutor) {
this.#executor = executor
}
/**
* Create a new table.
*
* ### Examples
*
* 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().autoIncrement())
* .addColumn('first_name', 'varchar', col => col.notNull())
* .addColumn('last_name', 'varchar', col => col.notNull())
* .addColumn('gender', 'varchar')
* .execute()
* ```
*
* 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().autoIncrement())
* .addColumn('owner_id', 'integer', col => col
* .references('person.id')
* .onDelete('cascade')
* )
* .execute()
* ```
*
* 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().autoIncrement())
* .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,
node: CreateTableNode.create(parseTable(table)),
})
}
/**
* Drop a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropTable('person')
* .execute()
* ```
*/
dropTable(table: string): DropTableBuilder {
return new DropTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: DropTableNode.create(parseTable(table)),
})
}
/**
* Create a new index.
*
* ### Examples
*
* ```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,
node: CreateIndexNode.create(indexName),
})
}
/**
* Drop an index.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropIndex('person_full_name_unique_index')
* .execute()
* ```
*/
dropIndex(indexName: string): DropIndexBuilder {
return new DropIndexBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: DropIndexNode.create(indexName),
})
}
/**
* Create a new schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .createSchema('some_schema')
* .execute()
* ```
*/
createSchema(schema: string): CreateSchemaBuilder {
return new CreateSchemaBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: CreateSchemaNode.create(schema),
})
}
/**
* Drop a schema.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropSchema('some_schema')
* .execute()
* ```
*/
dropSchema(schema: string): DropSchemaBuilder {
return new DropSchemaBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: DropSchemaNode.create(schema),
})
}
/**
* Alter a table.
*
* ### Examples
*
* ```ts
* await db.schema
* .alterTable('person')
* .alterColumn('first_name')
* .setDataType('text')
* .execute()
* ```
*/
alterTable(table: string): AlterTableBuilder {
return new AlterTableBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: AlterTableNode.create(table),
})
}
/**
* Create a new view.
*
* ### Examples
*
* ```ts
* await db.schema
* .createView('dogs')
* .orReplace()
* .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
* .execute()
* ```
*/
createView(viewName: string): CreateViewBuilder {
return new CreateViewBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: CreateViewNode.create(viewName),
})
}
/**
* Drop a view.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropView('dogs')
* .ifExists()
* .execute()
* ```
*/
dropView(viewName: string): DropViewBuilder {
return new DropViewBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: DropViewNode.create(viewName),
})
}
/**
* Create a new type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .createType('species')
* .asEnum(['dog', 'cat', 'frog'])
* .execute()
* ```
*/
createType(typeName: string): CreateTypeBuilder {
return new CreateTypeBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: CreateTypeNode.create(parseSchemableIdentifier(typeName)),
})
}
/**
* Drop a type.
*
* Only some dialects like PostgreSQL have user-defined types.
*
* ### Examples
*
* ```ts
* await db.schema
* .dropType('species')
* .ifExists()
* .execute()
* ```
*/
dropType(typeName: string): DropTypeBuilder {
return new DropTypeBuilder({
queryId: createQueryId(),
executor: this.#executor,
node: DropTypeNode.create(parseSchemableIdentifier(typeName)),
})
}
/**
* Returns a copy of this schema module with the given plugin installed.
*/
withPlugin(plugin: KyselyPlugin): SchemaModule {
return new SchemaModule(this.#executor.withPlugin(plugin))
}
/**
* Returns a copy of this schema module without any plugins.
*/
withoutPlugins(): SchemaModule {
return new SchemaModule(this.#executor.withoutPlugins())
}
/**
* See {@link QueryCreator.withSchema}
*/
withSchema(schema: string): SchemaModule {
return new SchemaModule(
this.#executor.withPluginAtFront(new WithSchemaPlugin(schema))
)
}
}