rake-db 2.8.48 → 2.8.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +680 -9
- package/dist/index.js +563 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +564 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -65,7 +65,7 @@ type SilentQueries = {
|
|
|
65
65
|
silentQuery: Adapter['query'];
|
|
66
66
|
silentArrays: Adapter['arrays'];
|
|
67
67
|
};
|
|
68
|
-
type
|
|
68
|
+
type DbMigration<CT extends ColumnTypesBase = DefaultColumnTypes> = DbResult<CT> & Migration<CT> & {
|
|
69
69
|
adapter: SilentQueries;
|
|
70
70
|
};
|
|
71
71
|
type ConstraintArg = {
|
|
@@ -79,56 +79,711 @@ type ConstraintArg = {
|
|
|
79
79
|
check?: RawExpression;
|
|
80
80
|
dropMode?: DropMode;
|
|
81
81
|
};
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new `db` instance that is an instance of `pqb` with mixed in migration methods from the `Migration` class.
|
|
84
|
+
* It overrides `query` and `array` db adapter methods to intercept SQL for the logging.
|
|
85
|
+
*
|
|
86
|
+
* @param tx - database adapter that executes inside a transaction
|
|
87
|
+
* @param up - migrate or rollback
|
|
88
|
+
* @param config - config of `rakeDb`
|
|
89
|
+
*/
|
|
90
|
+
declare const createMigrationInterface: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(tx: TransactionAdapter, up: boolean, config: RakeDbConfig<CT>) => DbMigration;
|
|
91
|
+
declare class Migration<CT extends ColumnTypesBase> {
|
|
84
92
|
adapter: TransactionAdapter;
|
|
85
93
|
log?: QueryLogObject;
|
|
86
94
|
up: boolean;
|
|
87
95
|
options: RakeDbConfig;
|
|
88
96
|
migratedAsts: RakeDbAst[];
|
|
89
97
|
columnTypes: CT;
|
|
90
|
-
|
|
98
|
+
/**
|
|
99
|
+
* `createTable` accepts a string for a table name, optional options, and a callback to specify columns.
|
|
100
|
+
*
|
|
101
|
+
* `dropTable` accepts the same arguments, it will drop the table when migrating and create a table when rolling back.
|
|
102
|
+
*
|
|
103
|
+
* When creating a table within a specific schema, write the table name with schema name: `'schemaName.tableName'`.
|
|
104
|
+
*
|
|
105
|
+
* Returns object `{ table: TableInterface }` that allows to insert records right after creating a table.
|
|
106
|
+
*
|
|
107
|
+
* Options are:
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* type TableOptions = {
|
|
111
|
+
* // used when reverting a `createTable`
|
|
112
|
+
* dropMode?: 'CASCADE' | 'RESTRICT';
|
|
113
|
+
*
|
|
114
|
+
* // add a database comment on the table
|
|
115
|
+
* comment?: string;
|
|
116
|
+
*
|
|
117
|
+
* // by default, it will throw an error when the table has no primary key
|
|
118
|
+
* // set `noPrimaryKey` to `true` to bypass it
|
|
119
|
+
* noPrimaryKey?: boolean;
|
|
120
|
+
*
|
|
121
|
+
* // override rakeDb `snakeCase` option for only this table
|
|
122
|
+
* snakeCase?: boolean;
|
|
123
|
+
* };
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* Example:
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* import { change } from '../dbScript';
|
|
130
|
+
*
|
|
131
|
+
* change(async (db, up) => {
|
|
132
|
+
* // call `createTable` with options
|
|
133
|
+
* await db.createTable(
|
|
134
|
+
* 'table',
|
|
135
|
+
* {
|
|
136
|
+
* comment: 'Table comment',
|
|
137
|
+
* dropMode: 'CASCADE',
|
|
138
|
+
* noPrimaryKey: true,
|
|
139
|
+
* },
|
|
140
|
+
* (t) => ({
|
|
141
|
+
* // ...
|
|
142
|
+
* }),
|
|
143
|
+
* );
|
|
144
|
+
*
|
|
145
|
+
* // call without options
|
|
146
|
+
* const { table } = await db.createTable('user', (t) => ({
|
|
147
|
+
* id: t.identity().primaryKey(),
|
|
148
|
+
* email: t.text().unique(),
|
|
149
|
+
* name: t.text(),
|
|
150
|
+
* active: t.boolean().nullable(),
|
|
151
|
+
* ...t.timestamps(),
|
|
152
|
+
* }));
|
|
153
|
+
*
|
|
154
|
+
* // create records only when migrating up
|
|
155
|
+
* if (up) {
|
|
156
|
+
* // table is a db table interface, all query methods are available
|
|
157
|
+
* await table.createMany([...data]);
|
|
158
|
+
* }
|
|
159
|
+
* });
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @param tableName - name of the table to create
|
|
163
|
+
* @param fn - create table callback
|
|
164
|
+
*/
|
|
91
165
|
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
92
|
-
|
|
166
|
+
/**
|
|
167
|
+
* See {@link createTable}
|
|
168
|
+
*
|
|
169
|
+
* @param tableName - name of the table to create
|
|
170
|
+
* @param options - create table options
|
|
171
|
+
* @param fn - create table callback
|
|
172
|
+
*/
|
|
173
|
+
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
174
|
+
/**
|
|
175
|
+
* Drop the table, create it on rollback. See {@link createTable}.
|
|
176
|
+
*
|
|
177
|
+
* @param tableName - name of the table to drop
|
|
178
|
+
* @param fn - create table callback
|
|
179
|
+
*/
|
|
93
180
|
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
94
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Drop the table, create it on rollback. See {@link createTable}.
|
|
183
|
+
*
|
|
184
|
+
* @param tableName - name of the table to drop
|
|
185
|
+
* @param options - create table options
|
|
186
|
+
* @param fn - create table callback
|
|
187
|
+
*/
|
|
188
|
+
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
189
|
+
/**
|
|
190
|
+
* `changeTable` accepts a table name, optional options, and a special callback with column changes.
|
|
191
|
+
*
|
|
192
|
+
* When changing a table within a specific schema, write the table name with schema name: `'schemaName.tableName'`.
|
|
193
|
+
*
|
|
194
|
+
* Options are:
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* type ChangeTableOptions = {
|
|
198
|
+
* comment?:
|
|
199
|
+
* | // add a comment to the table on migrating, remove a comment on rollback
|
|
200
|
+
* string // change comment from first to second on migrating, from second to first on rollback
|
|
201
|
+
* | [string, string] // remove a comment on both migrate and rollback
|
|
202
|
+
* | null;
|
|
203
|
+
*
|
|
204
|
+
* // override rakeDb `snakeCase` option for only this table
|
|
205
|
+
* snakeCase?: boolean;
|
|
206
|
+
* };
|
|
207
|
+
* ```
|
|
208
|
+
*
|
|
209
|
+
* The callback of the `changeTable` is different from `createTable` in the way that it expects columns to be wrapped in change methods such as `add`, `drop`, and `change`.
|
|
210
|
+
*
|
|
211
|
+
* @param tableName - name of the table to change (ALTER)
|
|
212
|
+
* @param fn - change table callback
|
|
213
|
+
*/
|
|
95
214
|
changeTable(tableName: string, fn: ChangeTableCallback<CT>): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* See {@link changeTable}
|
|
217
|
+
*
|
|
218
|
+
* @param tableName - name of the table to change (ALTER)
|
|
219
|
+
* @param options - change table options
|
|
220
|
+
* @param fn - change table callback
|
|
221
|
+
*/
|
|
222
|
+
changeTable(tableName: string, options: ChangeTableOptions, fn?: ChangeTableCallback<CT>): Promise<void>;
|
|
223
|
+
/**
|
|
224
|
+
* Rename a table:
|
|
225
|
+
*
|
|
226
|
+
* ```ts
|
|
227
|
+
* import { change } from '../dbScript';
|
|
228
|
+
*
|
|
229
|
+
* change(async (db) => {
|
|
230
|
+
* await db.renameTable('oldTableName', 'newTableName');
|
|
231
|
+
* });
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @param from - rename the table from
|
|
235
|
+
* @param to - rename the table to
|
|
236
|
+
*/
|
|
96
237
|
renameTable(from: string, to: string): Promise<void>;
|
|
238
|
+
/**
|
|
239
|
+
* Add a column to the table on migrating, and remove it on rollback.
|
|
240
|
+
*
|
|
241
|
+
* `dropColumn` takes the same arguments, removes a column on migrate, and adds it on rollback.
|
|
242
|
+
*
|
|
243
|
+
* ```ts
|
|
244
|
+
* import { change } from '../dbScript';
|
|
245
|
+
*
|
|
246
|
+
* change(async (db) => {
|
|
247
|
+
* await db.addColumn('tableName', 'columnName', (t) =>
|
|
248
|
+
* t.integer().index().nullable(),
|
|
249
|
+
* );
|
|
250
|
+
* });
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @param tableName - name of the table to add the column to
|
|
254
|
+
* @param columnName - name of the column to add
|
|
255
|
+
* @param fn - function returning a type of the column
|
|
256
|
+
*/
|
|
97
257
|
addColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
258
|
+
/**
|
|
259
|
+
* Drop the schema, create it on rollback. See {@link addIndex}.
|
|
260
|
+
*
|
|
261
|
+
* @param tableName - name of the table to add the column to
|
|
262
|
+
* @param columnName - name of the column to add
|
|
263
|
+
* @param fn - function returning a type of the column
|
|
264
|
+
*/
|
|
98
265
|
dropColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
266
|
+
/**
|
|
267
|
+
* Add an index to the table on migrating, and remove it on rollback.
|
|
268
|
+
*
|
|
269
|
+
* `dropIndex` takes the same arguments, removes the index on migrate, and adds it on rollback.
|
|
270
|
+
*
|
|
271
|
+
* The first argument is the table name, other arguments are the same as in [composite index](#composite-index).
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* import { change } from '../dbScript';
|
|
275
|
+
*
|
|
276
|
+
* change(async (db) => {
|
|
277
|
+
* await db.addIndex(
|
|
278
|
+
* 'tableName',
|
|
279
|
+
* ['column1', { column: 'column2', order: 'DESC' }],
|
|
280
|
+
* {
|
|
281
|
+
* name: 'indexName',
|
|
282
|
+
* },
|
|
283
|
+
* );
|
|
284
|
+
* });
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @param tableName - name of the table to add the index for
|
|
288
|
+
* @param columns - indexed columns
|
|
289
|
+
* @param options - index options
|
|
290
|
+
*/
|
|
99
291
|
addIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Drop the schema, create it on rollback. See {@link addIndex}.
|
|
294
|
+
*
|
|
295
|
+
* @param tableName - name of the table to add the index for
|
|
296
|
+
* @param columns - indexed columns
|
|
297
|
+
* @param options - index options
|
|
298
|
+
*/
|
|
100
299
|
dropIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
300
|
+
/**
|
|
301
|
+
* Add a foreign key to a table on migrating, and remove it on rollback.
|
|
302
|
+
*
|
|
303
|
+
* `dropForeignKey` takes the same arguments, removes the foreign key on migrate, and adds it on rollback.
|
|
304
|
+
*
|
|
305
|
+
* Arguments:
|
|
306
|
+
*
|
|
307
|
+
* - table name
|
|
308
|
+
* - column names in the table
|
|
309
|
+
* - other table name
|
|
310
|
+
* - column names in the other table
|
|
311
|
+
* - options:
|
|
312
|
+
* - `name`: constraint name
|
|
313
|
+
* - `match`: 'FULL', 'PARTIAL', or 'SIMPLE'
|
|
314
|
+
* - `onUpdate` and `onDelete`: 'NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', or 'SET DEFAULT'
|
|
315
|
+
*
|
|
316
|
+
* The first argument is the table name, other arguments are the same as in [composite foreign key](#composite-foreign-key).
|
|
317
|
+
*
|
|
318
|
+
* ```ts
|
|
319
|
+
* import { change } from '../dbScript';
|
|
320
|
+
*
|
|
321
|
+
* change(async (db) => {
|
|
322
|
+
* await db.addForeignKey(
|
|
323
|
+
* 'tableName',
|
|
324
|
+
* ['id', 'name'],
|
|
325
|
+
* 'otherTable',
|
|
326
|
+
* ['foreignId', 'foreignName'],
|
|
327
|
+
* {
|
|
328
|
+
* name: 'constraintName',
|
|
329
|
+
* match: 'FULL',
|
|
330
|
+
* onUpdate: 'RESTRICT',
|
|
331
|
+
* onDelete: 'CASCADE',
|
|
332
|
+
* },
|
|
333
|
+
* );
|
|
334
|
+
* });
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* @param tableName - table name
|
|
338
|
+
* @param columns - column names in the table
|
|
339
|
+
* @param foreignTable - other table name
|
|
340
|
+
* @param foreignColumns - column names in the other table
|
|
341
|
+
* @param options - foreign key options
|
|
342
|
+
*/
|
|
101
343
|
addForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): Promise<void>;
|
|
344
|
+
/**
|
|
345
|
+
* Drop the schema, create it on rollback. See {@link addForeignKey}.
|
|
346
|
+
*
|
|
347
|
+
* @param tableName - table name
|
|
348
|
+
* @param columns - column names in the table
|
|
349
|
+
* @param foreignTable - other table name
|
|
350
|
+
* @param foreignColumns - column names in the other table
|
|
351
|
+
* @param options - foreign key options
|
|
352
|
+
*/
|
|
102
353
|
dropForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): Promise<void>;
|
|
354
|
+
/**
|
|
355
|
+
* Add a primary key to a table on migrate, and remove it on rollback.
|
|
356
|
+
*
|
|
357
|
+
* `dropPrimaryKey` takes the same arguments, removes the primary key on migrate, and adds it on rollback.
|
|
358
|
+
*
|
|
359
|
+
* First argument is a table name, second argument is an array of columns.
|
|
360
|
+
* The optional third argument may have a name for the primary key constraint.
|
|
361
|
+
*
|
|
362
|
+
* ```ts
|
|
363
|
+
* import { change } from '../dbScript';
|
|
364
|
+
*
|
|
365
|
+
* change(async (db) => {
|
|
366
|
+
* await db.addPrimaryKey('tableName', ['id', 'name'], {
|
|
367
|
+
* name: 'tablePkeyName',
|
|
368
|
+
* });
|
|
369
|
+
* });
|
|
370
|
+
* ```
|
|
371
|
+
*
|
|
372
|
+
* @param tableName - name of the table
|
|
373
|
+
* @param columns - array of the columns
|
|
374
|
+
* @param options - object with a constraint name
|
|
375
|
+
*/
|
|
103
376
|
addPrimaryKey(tableName: string, columns: string[], options?: {
|
|
104
377
|
name?: string;
|
|
105
378
|
}): Promise<void>;
|
|
379
|
+
/**
|
|
380
|
+
* Drop the schema, create it on rollback. See {@link addPrimaryKey}.
|
|
381
|
+
*
|
|
382
|
+
* @param tableName - name of the table
|
|
383
|
+
* @param columns - array of the columns
|
|
384
|
+
* @param options - object with a constraint name
|
|
385
|
+
*/
|
|
106
386
|
dropPrimaryKey(tableName: string, columns: string[], options?: {
|
|
107
387
|
name?: string;
|
|
108
388
|
}): Promise<void>;
|
|
389
|
+
/**
|
|
390
|
+
* Add or drop a check for multiple columns.
|
|
391
|
+
*
|
|
392
|
+
* ```ts
|
|
393
|
+
* import { change } from '../dbScript';
|
|
394
|
+
*
|
|
395
|
+
* change(async (db) => {
|
|
396
|
+
* await db.addCheck('tableName', t.sql`column > 123`);
|
|
397
|
+
* });
|
|
398
|
+
* ```
|
|
399
|
+
*
|
|
400
|
+
* @param tableName - name of the table to add the check into
|
|
401
|
+
* @param check - raw SQL for the check
|
|
402
|
+
*/
|
|
109
403
|
addCheck(tableName: string, check: RawExpression): Promise<void>;
|
|
404
|
+
/**
|
|
405
|
+
* Drop the schema, create it on rollback. See {@link addConstraint}.
|
|
406
|
+
*
|
|
407
|
+
* @param tableName - name of the table to add the check into
|
|
408
|
+
* @param check - raw SQL for the check
|
|
409
|
+
*/
|
|
110
410
|
dropCheck(tableName: string, check: RawExpression): Promise<void>;
|
|
411
|
+
/**
|
|
412
|
+
* Add or drop a constraint with check and a foreign key references.
|
|
413
|
+
*
|
|
414
|
+
* See foreign key details in [foreign key](/guide/migration-column-methods.html#composite-foreign-key).
|
|
415
|
+
*
|
|
416
|
+
* ```ts
|
|
417
|
+
* import { change } from '../dbScript';
|
|
418
|
+
*
|
|
419
|
+
* change(async (db) => {
|
|
420
|
+
* await db.addConstraint('tableName', {
|
|
421
|
+
* name: 'constraintName',
|
|
422
|
+
* check: db.sql`column > 123`,
|
|
423
|
+
* references: [['id', 'name'], 'otherTable', ['otherId', 'otherName']],
|
|
424
|
+
* });
|
|
425
|
+
* });
|
|
426
|
+
* ```
|
|
427
|
+
*
|
|
428
|
+
* @param tableName - name of the table to add the constraint to
|
|
429
|
+
* @param constraint - constraint config object
|
|
430
|
+
*/
|
|
111
431
|
addConstraint(tableName: string, constraint: ConstraintArg): Promise<void>;
|
|
432
|
+
/**
|
|
433
|
+
* Drop the schema, create it on rollback. See {@link addConstraint}.
|
|
434
|
+
*
|
|
435
|
+
* @param tableName - name of the table to add the constraint to
|
|
436
|
+
* @param constraint - constraint config object
|
|
437
|
+
*/
|
|
112
438
|
dropConstraint(tableName: string, constraint: ConstraintArg): Promise<void>;
|
|
439
|
+
/**
|
|
440
|
+
* Rename a column:
|
|
441
|
+
*
|
|
442
|
+
* ```ts
|
|
443
|
+
* import { change } from '../dbScript';
|
|
444
|
+
*
|
|
445
|
+
* change(async (db) => {
|
|
446
|
+
* await db.renameColumn('tableName', 'oldColumnName', 'newColumnName');
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*
|
|
450
|
+
* @param tableName - name of the table to rename the column in
|
|
451
|
+
* @param from - rename column from
|
|
452
|
+
* @param to - rename column to
|
|
453
|
+
*/
|
|
113
454
|
renameColumn(tableName: string, from: string, to: string): Promise<void>;
|
|
455
|
+
/**
|
|
456
|
+
* `createSchema` creates a database schema, and removes it on rollback.
|
|
457
|
+
*
|
|
458
|
+
* `dropSchema` takes the same arguments, removes schema on migration, and adds it on rollback.
|
|
459
|
+
*
|
|
460
|
+
* ```ts
|
|
461
|
+
* import { change } from '../dbScript';
|
|
462
|
+
*
|
|
463
|
+
* change(async (db) => {
|
|
464
|
+
* await db.createSchema('schemaName');
|
|
465
|
+
* });
|
|
466
|
+
* ```
|
|
467
|
+
*
|
|
468
|
+
* @param schemaName - name of the schema
|
|
469
|
+
*/
|
|
114
470
|
createSchema(schemaName: string): Promise<void>;
|
|
471
|
+
/**
|
|
472
|
+
* Drop the schema, create it on rollback. See {@link createSchema}.
|
|
473
|
+
*
|
|
474
|
+
* @param schemaName - name of the schema
|
|
475
|
+
*/
|
|
115
476
|
dropSchema(schemaName: string): Promise<void>;
|
|
477
|
+
/**
|
|
478
|
+
* `createExtension` creates a database extension, and removes it on rollback.
|
|
479
|
+
*
|
|
480
|
+
* `dropExtension` takes the same arguments, removes the extension on migrate, and adds it on rollback.
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* import { change } from '../dbScript';
|
|
484
|
+
*
|
|
485
|
+
* change(async (db) => {
|
|
486
|
+
* await db.createExtension('pg_trgm');
|
|
487
|
+
* });
|
|
488
|
+
* ```
|
|
489
|
+
*
|
|
490
|
+
* @param name - name of the extension
|
|
491
|
+
* @param options - extension options
|
|
492
|
+
*/
|
|
116
493
|
createExtension(name: string, options?: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name'>): Promise<void>;
|
|
494
|
+
/**
|
|
495
|
+
* Drop the extension, create it on rollback. See {@link createExtension}.
|
|
496
|
+
*
|
|
497
|
+
* @param name - name of the extension
|
|
498
|
+
* @param options - extension options
|
|
499
|
+
*/
|
|
117
500
|
dropExtension(name: string, options?: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name' | 'values'>): Promise<void>;
|
|
501
|
+
/**
|
|
502
|
+
* `createEnum` creates an enum on migrate, drops it on rollback.
|
|
503
|
+
*
|
|
504
|
+
* `dropEnum` does the opposite.
|
|
505
|
+
*
|
|
506
|
+
* Third argument for options is optional.
|
|
507
|
+
*
|
|
508
|
+
* ```ts
|
|
509
|
+
* import { change } from '../dbScript';
|
|
510
|
+
*
|
|
511
|
+
* change(async (db) => {
|
|
512
|
+
* await db.createEnum('number', ['one', 'two', 'three']);
|
|
513
|
+
*
|
|
514
|
+
* // use `schemaName.enumName` format to specify a schema
|
|
515
|
+
* await db.createEnum('customSchema.mood', ['sad', 'ok', 'happy'], {
|
|
516
|
+
* // following options are used when dropping enum
|
|
517
|
+
* dropIfExists: true,
|
|
518
|
+
* cascade: true,
|
|
519
|
+
* });
|
|
520
|
+
* });
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @param name - name of the enum
|
|
524
|
+
* @param values - possible enum values
|
|
525
|
+
* @param options - enum options
|
|
526
|
+
*/
|
|
118
527
|
createEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
528
|
+
/**
|
|
529
|
+
* Drop the enum, create it on rollback. See {@link createEnum}.
|
|
530
|
+
*
|
|
531
|
+
* @param name - name of the enum
|
|
532
|
+
* @param values - possible enum values
|
|
533
|
+
* @param options - enum options
|
|
534
|
+
*/
|
|
119
535
|
dropEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
536
|
+
/**
|
|
537
|
+
* Domain is a custom database type that allows to predefine a `NOT NULL` and a `CHECK` (see [postgres tutorial](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-user-defined-data-types/)).
|
|
538
|
+
*
|
|
539
|
+
* `createDomain` and `dropDomain` take a domain name as first argument, callback returning inner column type as a second, and optional object with parameters as third.
|
|
540
|
+
*
|
|
541
|
+
* ```ts
|
|
542
|
+
* import { change } from '../dbScript';
|
|
543
|
+
*
|
|
544
|
+
* change(async (db) => {
|
|
545
|
+
* await db.createDomain('domainName', (t) => t.integer(), {
|
|
546
|
+
* check: db.sql`value = 42`,
|
|
547
|
+
* });
|
|
548
|
+
*
|
|
549
|
+
* // use `schemaName.domainName` format to specify a schema
|
|
550
|
+
* await db.createDomain('schemaName.domainName', (t) => t.text(), {
|
|
551
|
+
* // unlike columns, domain is nullable by default, use notNull when needed:
|
|
552
|
+
* notNull: true,
|
|
553
|
+
* collation: 'C',
|
|
554
|
+
* default: db.sql`'default text'`,
|
|
555
|
+
* check: db.sql`length(value) > 10`,
|
|
556
|
+
*
|
|
557
|
+
* // cascade is used when dropping domain
|
|
558
|
+
* cascade: true,
|
|
559
|
+
* });
|
|
560
|
+
* });
|
|
561
|
+
* ```
|
|
562
|
+
*
|
|
563
|
+
* @param name - name of the domain
|
|
564
|
+
* @param fn - function returning a column type for the domain
|
|
565
|
+
* @param options - domain options
|
|
566
|
+
*/
|
|
120
567
|
createDomain(name: string, fn: (t: CT) => ColumnType, options?: Omit<RakeDbAst.Domain, 'type' | 'action' | 'schema' | 'name' | 'baseType'>): Promise<void>;
|
|
568
|
+
/**
|
|
569
|
+
* Drop the domain, create it on rollback. See {@link dropDomain}.
|
|
570
|
+
*
|
|
571
|
+
* @param name - name of the domain
|
|
572
|
+
* @param fn - function returning a column type for the domain
|
|
573
|
+
* @param options - domain options
|
|
574
|
+
*/
|
|
121
575
|
dropDomain(name: string, fn: (t: CT) => ColumnType, options?: Omit<RakeDbAst.Domain, 'type' | 'action' | 'schema' | 'name' | 'baseType'>): Promise<void>;
|
|
576
|
+
/**
|
|
577
|
+
* Create and drop a database collation, (see [Postgres docs](https://www.postgresql.org/docs/current/sql-createcollation.html)).
|
|
578
|
+
*
|
|
579
|
+
* ```ts
|
|
580
|
+
* import { change } from '../dbScript';
|
|
581
|
+
*
|
|
582
|
+
* change(async (db) => {
|
|
583
|
+
* await db.createCollation('myCollation', {
|
|
584
|
+
* // This is a shortcut for setting lcCollate and lcCType at once.
|
|
585
|
+
* locale: 'en-u-kn-true',
|
|
586
|
+
*
|
|
587
|
+
* // set `lcType` and `lcCType` only if the `locale` is not set.
|
|
588
|
+
* // lcType: 'C',
|
|
589
|
+
* // lcCType: 'C',
|
|
590
|
+
*
|
|
591
|
+
* // provider can be 'icu' or 'libc'. 'libc' is a default.
|
|
592
|
+
* provider: 'icu',
|
|
593
|
+
*
|
|
594
|
+
* // true by default, false is only supported with 'icu' provider.
|
|
595
|
+
* deterministic: true,
|
|
596
|
+
*
|
|
597
|
+
* // Is intended to by used by `pg_upgrade`. Normally, it should be omitted.
|
|
598
|
+
* version: '1.2.3',
|
|
599
|
+
*
|
|
600
|
+
* // For `CREATE IF NOT EXISTS` when creating.
|
|
601
|
+
* createIfNotExists: true,
|
|
602
|
+
*
|
|
603
|
+
* // For `DROP IF EXISTS` when dropping.
|
|
604
|
+
* dropIfExists: true,
|
|
605
|
+
*
|
|
606
|
+
* // For `DROP ... CASCADE` when dropping.
|
|
607
|
+
* cascase: true,
|
|
608
|
+
* });
|
|
609
|
+
* });
|
|
610
|
+
* ```
|
|
611
|
+
*
|
|
612
|
+
* Instead of specifying the collation options, you can specify a collation to copy options from.
|
|
613
|
+
*
|
|
614
|
+
* ```ts
|
|
615
|
+
* import { change } from '../dbScript';
|
|
616
|
+
*
|
|
617
|
+
* change(async (db) => {
|
|
618
|
+
* await db.createCollation('myCollation', {
|
|
619
|
+
* fromExisting: 'otherCollation',
|
|
620
|
+
* });
|
|
621
|
+
* });
|
|
622
|
+
* ```
|
|
623
|
+
*
|
|
624
|
+
* To create a collation withing a specific database schema, prepend it to the collation name:
|
|
625
|
+
*
|
|
626
|
+
* ```ts
|
|
627
|
+
* import { change } from '../dbScript';
|
|
628
|
+
*
|
|
629
|
+
* change(async (db) => {
|
|
630
|
+
* await db.createCollation('schemaName.myCollation', {
|
|
631
|
+
* // `fromExisting` also can accept a collation name with a schema.
|
|
632
|
+
* fromExisting: 'schemaName.otherCollation',
|
|
633
|
+
* });
|
|
634
|
+
* });
|
|
635
|
+
* ```
|
|
636
|
+
*
|
|
637
|
+
* @param name - name of the collation, can contain a name of schema separated with a dot.
|
|
638
|
+
* @param options - options to create and drop the collation.
|
|
639
|
+
*/
|
|
640
|
+
createCollation(name: string, options: Omit<RakeDbAst.Collation, 'type' | 'action' | 'schema' | 'name'>): Promise<void>;
|
|
641
|
+
/**
|
|
642
|
+
* Drop the collation, create it on rollback. See {@link createCollation}.
|
|
643
|
+
*
|
|
644
|
+
* @param name - name of the collation, can contain a name of schema separated with a dot.
|
|
645
|
+
* @param options - options to create and drop the collation.
|
|
646
|
+
*/
|
|
647
|
+
dropCollation(name: string, options: Omit<RakeDbAst.Collation, 'type' | 'action' | 'schema' | 'name'>): Promise<void>;
|
|
648
|
+
/**
|
|
649
|
+
* Create and drop database views.
|
|
650
|
+
*
|
|
651
|
+
* Provide SQL as a string or via `db.sql` that can accept variables.
|
|
652
|
+
*
|
|
653
|
+
* ```ts
|
|
654
|
+
* import { change } from '../dbScript';
|
|
655
|
+
*
|
|
656
|
+
* change(async (db) => {
|
|
657
|
+
* await db.createView(
|
|
658
|
+
* 'simpleView',
|
|
659
|
+
* `
|
|
660
|
+
* SELECT a.one, b.two
|
|
661
|
+
* FROM a
|
|
662
|
+
* JOIN b ON b."aId" = a.id
|
|
663
|
+
* `,
|
|
664
|
+
* );
|
|
665
|
+
*
|
|
666
|
+
* // view can accept db.sql with variables in such way:
|
|
667
|
+
* const value = 'some value';
|
|
668
|
+
* await db.createView(
|
|
669
|
+
* 'viewWithVariables',
|
|
670
|
+
* db.sql`
|
|
671
|
+
* SELECT * FROM a WHERE key = ${value}
|
|
672
|
+
* `,
|
|
673
|
+
* );
|
|
674
|
+
*
|
|
675
|
+
* // view with options
|
|
676
|
+
* await db.createView(
|
|
677
|
+
* 'schemaName.recursiveView',
|
|
678
|
+
* {
|
|
679
|
+
* // createOrReplace has effect when creating the view
|
|
680
|
+
* createOrReplace: true,
|
|
681
|
+
*
|
|
682
|
+
* // dropIfExists and dropMode have effect when dropping the view
|
|
683
|
+
* dropIfExists: true,
|
|
684
|
+
* dropMode: 'CASCADE',
|
|
685
|
+
*
|
|
686
|
+
* // for details, check Postgres docs for CREATE VIEW,
|
|
687
|
+
* // these options are matching CREATE VIEW options
|
|
688
|
+
* temporary: true,
|
|
689
|
+
* recursive: true,
|
|
690
|
+
* columns: ['n'],
|
|
691
|
+
* with: {
|
|
692
|
+
* checkOption: 'LOCAL', // or 'CASCADED'
|
|
693
|
+
* securityBarrier: true,
|
|
694
|
+
* securityInvoker: true,
|
|
695
|
+
* },
|
|
696
|
+
* },
|
|
697
|
+
* `
|
|
698
|
+
* VALUES (1)
|
|
699
|
+
* UNION ALL
|
|
700
|
+
* SELECT n + 1 FROM "schemaName"."recursiveView" WHERE n < 100;
|
|
701
|
+
* `,
|
|
702
|
+
* );
|
|
703
|
+
* });
|
|
704
|
+
* ```
|
|
705
|
+
*
|
|
706
|
+
* @param name - name of the view
|
|
707
|
+
* @param options - view options
|
|
708
|
+
* @param sql - SQL to create the view with
|
|
709
|
+
*/
|
|
122
710
|
createView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawExpression): Promise<void>;
|
|
711
|
+
/**
|
|
712
|
+
* See {@link createView}
|
|
713
|
+
*
|
|
714
|
+
* @param name - name of the view
|
|
715
|
+
* @param sql - SQL to create the view with
|
|
716
|
+
*/
|
|
123
717
|
createView(name: string, sql: string | RawExpression): Promise<void>;
|
|
718
|
+
/**
|
|
719
|
+
* Drop the view, create it on rollback. See {@link createView}.
|
|
720
|
+
*
|
|
721
|
+
* @param name - name of the view
|
|
722
|
+
* @param options - view options
|
|
723
|
+
* @param sql - SQL to create the view with
|
|
724
|
+
*/
|
|
124
725
|
dropView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawExpression): Promise<void>;
|
|
726
|
+
/**
|
|
727
|
+
* Drop the view, create it on rollback. See {@link createView}.
|
|
728
|
+
*
|
|
729
|
+
* @param name - name of the view
|
|
730
|
+
* @param sql - SQL to create the view with
|
|
731
|
+
*/
|
|
125
732
|
dropView(name: string, sql: string | RawExpression): Promise<void>;
|
|
733
|
+
/**
|
|
734
|
+
* Returns boolean to know if table exists:
|
|
735
|
+
*
|
|
736
|
+
* ```ts
|
|
737
|
+
* import { change } from '../dbScript';
|
|
738
|
+
*
|
|
739
|
+
* change(async (db) => {
|
|
740
|
+
* if (await db.tableExists('tableName')) {
|
|
741
|
+
* // ...do something
|
|
742
|
+
* }
|
|
743
|
+
* });
|
|
744
|
+
* ```
|
|
745
|
+
*
|
|
746
|
+
* @param tableName - name of the table
|
|
747
|
+
*/
|
|
126
748
|
tableExists(tableName: string): Promise<boolean>;
|
|
749
|
+
/**
|
|
750
|
+
* Returns boolean to know if a column exists:
|
|
751
|
+
*
|
|
752
|
+
* Note that when `snakeCase` option is set to true, this method won't translate column to snake case, unlike other parts.
|
|
753
|
+
*
|
|
754
|
+
* ```ts
|
|
755
|
+
* import { change } from '../dbScript';
|
|
756
|
+
*
|
|
757
|
+
* change(async (db) => {
|
|
758
|
+
* if (await db.columnExists('tableName', 'columnName')) {
|
|
759
|
+
* // ...do something
|
|
760
|
+
* }
|
|
761
|
+
* });
|
|
762
|
+
* ```
|
|
763
|
+
*
|
|
764
|
+
* @param tableName - name of the table to check for the column in
|
|
765
|
+
* @param columnName - name of the column
|
|
766
|
+
*/
|
|
127
767
|
columnExists(tableName: string, columnName: string): Promise<boolean>;
|
|
768
|
+
/**
|
|
769
|
+
* Returns boolean to know if constraint exists:
|
|
770
|
+
*
|
|
771
|
+
* ```ts
|
|
772
|
+
* import { change } from '../dbScript';
|
|
773
|
+
*
|
|
774
|
+
* change(async (db) => {
|
|
775
|
+
* if (await db.constraintExists('constraintName')) {
|
|
776
|
+
* // ...do something
|
|
777
|
+
* }
|
|
778
|
+
* });
|
|
779
|
+
* ```
|
|
780
|
+
*
|
|
781
|
+
* @param constraintName - name of the constraint
|
|
782
|
+
*/
|
|
128
783
|
constraintExists(constraintName: string): Promise<boolean>;
|
|
129
784
|
}
|
|
130
785
|
|
|
131
|
-
type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.Domain | RakeDbAst.Constraint | RakeDbAst.View;
|
|
786
|
+
type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.View;
|
|
132
787
|
declare namespace RakeDbAst {
|
|
133
788
|
type Table = {
|
|
134
789
|
type: 'table';
|
|
@@ -228,6 +883,22 @@ declare namespace RakeDbAst {
|
|
|
228
883
|
check?: RawExpression;
|
|
229
884
|
cascade?: boolean;
|
|
230
885
|
};
|
|
886
|
+
type Collation = {
|
|
887
|
+
type: 'collation';
|
|
888
|
+
action: 'create' | 'drop';
|
|
889
|
+
schema?: string;
|
|
890
|
+
name: string;
|
|
891
|
+
locale?: string;
|
|
892
|
+
lcCollate?: string;
|
|
893
|
+
lcCType?: string;
|
|
894
|
+
provider?: string;
|
|
895
|
+
deterministic?: boolean;
|
|
896
|
+
version?: string;
|
|
897
|
+
fromExisting?: string;
|
|
898
|
+
createIfNotExists?: boolean;
|
|
899
|
+
dropIfExists?: boolean;
|
|
900
|
+
cascade?: boolean;
|
|
901
|
+
};
|
|
231
902
|
type EnumOptions = {
|
|
232
903
|
createIfNotExists?: boolean;
|
|
233
904
|
dropIfExists?: boolean;
|
|
@@ -322,7 +993,7 @@ declare const writeMigrationFile: <CT extends Record<string, orchid_core.AnyColu
|
|
|
322
993
|
declare const generate: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(config: RakeDbConfig<CT>, [name]: string[]) => Promise<void>;
|
|
323
994
|
declare const makeFileTimeStamp: () => string;
|
|
324
995
|
|
|
325
|
-
type ChangeCallback<CT extends ColumnTypesBase = ColumnTypesBase> = (db:
|
|
996
|
+
type ChangeCallback<CT extends ColumnTypesBase = ColumnTypesBase> = (db: DbMigration<CT>, up: boolean) => Promise<void>;
|
|
326
997
|
|
|
327
998
|
declare const migrateOrRollback: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(options: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>, args: string[], up: boolean) => Promise<void>;
|
|
328
999
|
declare const changeCache: Record<string, ChangeCallback[] | undefined>;
|
|
@@ -485,4 +1156,4 @@ declare const saveMigratedVersion: <CT extends Record<string, orchid_core.AnyCol
|
|
|
485
1156
|
declare const removeMigratedVersion: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(db: SilentQueries, version: string, config: RakeDbConfig<CT>) => Promise<void>;
|
|
486
1157
|
declare const getMigratedVersionsMap: <CT extends Record<string, orchid_core.AnyColumnTypeCreator>>(db: Adapter, config: RakeDbConfig<CT>) => Promise<Record<string, boolean>>;
|
|
487
1158
|
|
|
488
|
-
export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, Migration,
|
|
1159
|
+
export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DbMigration, DropMode, Migration, MigrationColumnTypes, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|