@warlock.js/cascade 4.0.131 → 4.0.133
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/cjs/contracts/migration-driver.contract.d.ts +70 -1
- package/cjs/contracts/migration-driver.contract.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-driver.d.ts +12 -0
- package/cjs/drivers/mongodb/mongodb-driver.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-driver.js +26 -4
- package/cjs/drivers/mongodb/mongodb-driver.js.map +1 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts +39 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.js +68 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.js.map +1 -1
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts +52 -1
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-migration-driver.js +226 -27
- package/cjs/drivers/postgres/postgres-migration-driver.js.map +1 -1
- package/cjs/migration/column-builder.d.ts +201 -1
- package/cjs/migration/column-builder.d.ts.map +1 -1
- package/cjs/migration/column-builder.js +295 -0
- package/cjs/migration/column-builder.js.map +1 -1
- package/cjs/migration/foreign-key-builder.d.ts +10 -17
- package/cjs/migration/foreign-key-builder.d.ts.map +1 -1
- package/cjs/migration/foreign-key-builder.js +11 -19
- package/cjs/migration/foreign-key-builder.js.map +1 -1
- package/cjs/migration/migration.d.ts +223 -15
- package/cjs/migration/migration.d.ts.map +1 -1
- package/cjs/migration/migration.js +249 -29
- package/cjs/migration/migration.js.map +1 -1
- package/esm/contracts/migration-driver.contract.d.ts +70 -1
- package/esm/contracts/migration-driver.contract.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-driver.d.ts +12 -0
- package/esm/drivers/mongodb/mongodb-driver.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-driver.js +26 -4
- package/esm/drivers/mongodb/mongodb-driver.js.map +1 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts +39 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.js +68 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.js.map +1 -1
- package/esm/drivers/postgres/postgres-migration-driver.d.ts +52 -1
- package/esm/drivers/postgres/postgres-migration-driver.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-migration-driver.js +226 -27
- package/esm/drivers/postgres/postgres-migration-driver.js.map +1 -1
- package/esm/migration/column-builder.d.ts +201 -1
- package/esm/migration/column-builder.d.ts.map +1 -1
- package/esm/migration/column-builder.js +295 -0
- package/esm/migration/column-builder.js.map +1 -1
- package/esm/migration/foreign-key-builder.d.ts +10 -17
- package/esm/migration/foreign-key-builder.d.ts.map +1 -1
- package/esm/migration/foreign-key-builder.js +11 -19
- package/esm/migration/foreign-key-builder.js.map +1 -1
- package/esm/migration/migration.d.ts +223 -15
- package/esm/migration/migration.d.ts.map +1 -1
- package/esm/migration/migration.js +249 -29
- package/esm/migration/migration.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ColumnDefinition, ColumnType } from "../contracts/migration-driver.contract";
|
|
1
|
+
import type { ColumnDefinition, ColumnType, ForeignKeyDefinition } from "../contracts/migration-driver.contract";
|
|
2
2
|
/**
|
|
3
3
|
* Reference to the Migration type to avoid circular imports.
|
|
4
4
|
* The actual type is injected at runtime.
|
|
@@ -8,6 +8,7 @@ type MigrationLike = {
|
|
|
8
8
|
columns: string[];
|
|
9
9
|
unique?: boolean;
|
|
10
10
|
}): void;
|
|
11
|
+
addForeignKeyOperation(fk: ForeignKeyDefinition): void;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
* Fluent builder for defining column properties.
|
|
@@ -35,6 +36,10 @@ export declare class ColumnBuilder {
|
|
|
35
36
|
private readonly migration;
|
|
36
37
|
/** Mutable column definition being accumulated */
|
|
37
38
|
private readonly definition;
|
|
39
|
+
/** Mutable foreign key definition, set when .references() is called */
|
|
40
|
+
private fkDefinition?;
|
|
41
|
+
/** Temporary storage for generated expression before .stored() or .virtual() is called */
|
|
42
|
+
private generatedExpression?;
|
|
38
43
|
/**
|
|
39
44
|
* Create a new column builder.
|
|
40
45
|
*
|
|
@@ -76,6 +81,32 @@ export declare class ColumnBuilder {
|
|
|
76
81
|
* ```
|
|
77
82
|
*/
|
|
78
83
|
default(value: unknown): this;
|
|
84
|
+
/**
|
|
85
|
+
* Set default value to the current timestamp.
|
|
86
|
+
*
|
|
87
|
+
* Database-agnostic. Generates NOW() / CURRENT_TIMESTAMP / GETDATE() based on driver.
|
|
88
|
+
*
|
|
89
|
+
* @returns This builder for chaining
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* this.timestamp("created_at").useCurrent();
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
useCurrent(): this;
|
|
97
|
+
/**
|
|
98
|
+
* Set column to update to current timestamp on row update.
|
|
99
|
+
*
|
|
100
|
+
* MySQL only. Other databases ignore this.
|
|
101
|
+
*
|
|
102
|
+
* @returns This builder for chaining
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* this.timestamp("updated_at").useCurrent().useCurrentOnUpdate();
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
useCurrentOnUpdate(): this;
|
|
79
110
|
/**
|
|
80
111
|
* Add unique constraint/index on this column.
|
|
81
112
|
*
|
|
@@ -154,6 +185,175 @@ export declare class ColumnBuilder {
|
|
|
154
185
|
* ```
|
|
155
186
|
*/
|
|
156
187
|
comment(text: string): this;
|
|
188
|
+
/**
|
|
189
|
+
* Add a CHECK constraint scoped to this column.
|
|
190
|
+
*
|
|
191
|
+
* @param expression - SQL CHECK expression (can reference the column by name)
|
|
192
|
+
* @param name - Constraint name (defaults to `check_<column>`)
|
|
193
|
+
* @returns This builder for chaining
|
|
194
|
+
*/
|
|
195
|
+
check(expression: string, name?: string): this;
|
|
196
|
+
/**
|
|
197
|
+
* Position this column after another column.
|
|
198
|
+
*
|
|
199
|
+
* MySQL/MariaDB only. Ignored by PostgreSQL and NoSQL drivers.
|
|
200
|
+
*
|
|
201
|
+
* @param columnName - Column to position after
|
|
202
|
+
* @returns This builder for chaining
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* this.string("middle_name").after("first_name");
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
after(columnName: string): this;
|
|
210
|
+
/**
|
|
211
|
+
* Position this column as the first column in the table.
|
|
212
|
+
*
|
|
213
|
+
* MySQL/MariaDB only. Ignored by PostgreSQL and NoSQL drivers.
|
|
214
|
+
*
|
|
215
|
+
* @returns This builder for chaining
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* this.integer("id").primary().first();
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
first(): this;
|
|
223
|
+
/**
|
|
224
|
+
* Declare a foreign key constraint on this column.
|
|
225
|
+
*
|
|
226
|
+
* Pushes an `addForeignKey` operation immediately using a mutable reference —
|
|
227
|
+
* subsequent `.on()`, `.onDelete()`, `.onUpdate()` calls mutate the same
|
|
228
|
+
* definition that is already queued, so no `.add()` terminator is needed.
|
|
229
|
+
*
|
|
230
|
+
* Referenced column defaults to `"id"` — use `.on()` to override.
|
|
231
|
+
*
|
|
232
|
+
* @param table - Referenced table name
|
|
233
|
+
* @returns This builder for chaining
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* this.integer("user_id").references("users");
|
|
238
|
+
* this.integer("user_id").references("users").on("custom_id").onDelete("cascade");
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
references(table: string): this;
|
|
242
|
+
/**
|
|
243
|
+
* Set the referenced column for the foreign key.
|
|
244
|
+
*
|
|
245
|
+
* Only meaningful after `.references()`. Defaults to `"id"` if omitted.
|
|
246
|
+
*
|
|
247
|
+
* @param column - Referenced column name
|
|
248
|
+
* @returns This builder for chaining
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* this.integer("user_id").references("users").on("custom_id");
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
on(column: string): this;
|
|
256
|
+
/**
|
|
257
|
+
* Set the ON DELETE action for the foreign key.
|
|
258
|
+
*
|
|
259
|
+
* Only meaningful after `.references()`.
|
|
260
|
+
*
|
|
261
|
+
* @param action - Action when the referenced row is deleted
|
|
262
|
+
* @returns This builder for chaining
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```typescript
|
|
266
|
+
* this.integer("user_id").references("users").onDelete("cascade");
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
onDelete(action: ForeignKeyDefinition["onDelete"]): this;
|
|
270
|
+
/**
|
|
271
|
+
* Set the ON UPDATE action for the foreign key.
|
|
272
|
+
*
|
|
273
|
+
* Only meaningful after `.references()`.
|
|
274
|
+
*
|
|
275
|
+
* @param action - Action when the referenced row's key is updated
|
|
276
|
+
* @returns This builder for chaining
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* this.integer("user_id").references("users").onUpdate("cascade");
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
onUpdate(action: ForeignKeyDefinition["onUpdate"]): this;
|
|
284
|
+
/**
|
|
285
|
+
* Shorthand for `.onDelete("cascade").onUpdate("cascade")`.
|
|
286
|
+
*
|
|
287
|
+
* Only meaningful after `.references()`.
|
|
288
|
+
*
|
|
289
|
+
* @returns This builder for chaining
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```typescript
|
|
293
|
+
* this.integer("user_id").references("users").cascadeAll();
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
cascadeAll(): this;
|
|
297
|
+
/**
|
|
298
|
+
* Mark this column definition as a modification of an existing column.
|
|
299
|
+
*
|
|
300
|
+
* Pushes a `modifyColumn` operation instead of `addColumn`.
|
|
301
|
+
* This method breaks the builder chain and returns the parent migration.
|
|
302
|
+
*
|
|
303
|
+
* @returns The parent migration instance (breaks builder chain)
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* ```typescript
|
|
307
|
+
* // Make email nullable
|
|
308
|
+
* this.string("email").nullable().change();
|
|
309
|
+
*
|
|
310
|
+
* // Increase length
|
|
311
|
+
* this.string("name", 255).change();
|
|
312
|
+
*
|
|
313
|
+
* // Change type
|
|
314
|
+
* this.text("description").change();
|
|
315
|
+
* ```
|
|
316
|
+
*/
|
|
317
|
+
change(): unknown;
|
|
318
|
+
/**
|
|
319
|
+
* Mark this column as a generated column with the given SQL expression.
|
|
320
|
+
*
|
|
321
|
+
* Must be followed by `.stored()` or `.virtual()` to specify storage type.
|
|
322
|
+
*
|
|
323
|
+
* PostgreSQL: GENERATED ALWAYS AS (...) STORED
|
|
324
|
+
* MySQL: GENERATED ALWAYS AS (...) STORED | VIRTUAL
|
|
325
|
+
*
|
|
326
|
+
* @param expression - SQL expression to compute the value
|
|
327
|
+
* @returns This builder for chaining
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* this.string("full_name")
|
|
332
|
+
* .generatedAs("CONCAT(first_name, ' ', last_name)")
|
|
333
|
+
* .stored();
|
|
334
|
+
*
|
|
335
|
+
* this.decimal("price_with_tax")
|
|
336
|
+
* .generatedAs("price * 1.2")
|
|
337
|
+
* .virtual();
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
generatedAs(expression: string): this;
|
|
341
|
+
/**
|
|
342
|
+
* Mark the generated column as stored (computed and persisted to disk).
|
|
343
|
+
*
|
|
344
|
+
* Must be called after `.generatedAs()`.
|
|
345
|
+
*
|
|
346
|
+
* @returns This builder for chaining
|
|
347
|
+
*/
|
|
348
|
+
stored(): this;
|
|
349
|
+
/**
|
|
350
|
+
* Mark the generated column as virtual (computed on read, not stored).
|
|
351
|
+
*
|
|
352
|
+
* Must be called after `.generatedAs()`. Not supported by PostgreSQL.
|
|
353
|
+
*
|
|
354
|
+
* @returns This builder for chaining
|
|
355
|
+
*/
|
|
356
|
+
virtual(): this;
|
|
157
357
|
/**
|
|
158
358
|
* Get the built column definition.
|
|
159
359
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-builder.d.ts","sourceRoot":"","sources":["../../src/migration/column-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,wCAAwC,CAAC;
|
|
1
|
+
{"version":3,"file":"column-builder.d.ts","sourceRoot":"","sources":["../../src/migration/column-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAEjH;;;GAGG;AACH,KAAK,aAAa,GAAG;IACnB,eAAe,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,IAAI,CAAC;IACtE,sBAAsB,CAAC,EAAE,EAAE,oBAAoB,GAAG,IAAI,CAAC;CACxD,CAAC;AAcF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,aAAa;IAmBtB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAlB5B,kDAAkD;IAClD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;IAE9C,uEAAuE;IACvE,OAAO,CAAC,YAAY,CAAC,CAA8B;IAEnD,0FAA0F;IAC1F,OAAO,CAAC,mBAAmB,CAAC,CAAS;IAErC;;;;;;;OAOG;gBAEgB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;;;;;;;;;;;OAYG;IACI,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAC,UAAU,CAAC,GAAG,IAAI;IAO/D;;;;;;;;;;;OAWG;IACI,UAAU,IAAI,IAAI;IAYzB;;;;;;;;;;;;;;;;;;;OAmBG;IACI,MAAM,IAAI,OAAO;IAsBxB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACI,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAK5C;;;;;;OAMG;IACI,MAAM,IAAI,IAAI;IAUrB;;;;;;OAMG;IACI,OAAO,IAAI,IAAI;IActB;;;;;;OAMG;IACI,aAAa,IAAI,gBAAgB;CAGzC"}
|
|
@@ -24,6 +24,10 @@ class ColumnBuilder {
|
|
|
24
24
|
migration;
|
|
25
25
|
/** Mutable column definition being accumulated */
|
|
26
26
|
definition;
|
|
27
|
+
/** Mutable foreign key definition, set when .references() is called */
|
|
28
|
+
fkDefinition;
|
|
29
|
+
/** Temporary storage for generated expression before .stored() or .virtual() is called */
|
|
30
|
+
generatedExpression;
|
|
27
31
|
/**
|
|
28
32
|
* Create a new column builder.
|
|
29
33
|
*
|
|
@@ -88,6 +92,38 @@ class ColumnBuilder {
|
|
|
88
92
|
this.definition.defaultValue = value;
|
|
89
93
|
return this;
|
|
90
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Set default value to the current timestamp.
|
|
97
|
+
*
|
|
98
|
+
* Database-agnostic. Generates NOW() / CURRENT_TIMESTAMP / GETDATE() based on driver.
|
|
99
|
+
*
|
|
100
|
+
* @returns This builder for chaining
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* this.timestamp("created_at").useCurrent();
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
useCurrent() {
|
|
108
|
+
this.definition.defaultValue = { __type: "CURRENT_TIMESTAMP" };
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Set column to update to current timestamp on row update.
|
|
113
|
+
*
|
|
114
|
+
* MySQL only. Other databases ignore this.
|
|
115
|
+
*
|
|
116
|
+
* @returns This builder for chaining
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* this.timestamp("updated_at").useCurrent().useCurrentOnUpdate();
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
useCurrentOnUpdate() {
|
|
124
|
+
this.definition.onUpdateCurrent = true;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
91
127
|
// ============================================================================
|
|
92
128
|
// INDEXES
|
|
93
129
|
// ============================================================================
|
|
@@ -202,6 +238,265 @@ class ColumnBuilder {
|
|
|
202
238
|
return this;
|
|
203
239
|
}
|
|
204
240
|
// ============================================================================
|
|
241
|
+
// CONSTRAINTS
|
|
242
|
+
// ============================================================================
|
|
243
|
+
/**
|
|
244
|
+
* Add a CHECK constraint scoped to this column.
|
|
245
|
+
*
|
|
246
|
+
* @param expression - SQL CHECK expression (can reference the column by name)
|
|
247
|
+
* @param name - Constraint name (defaults to `check_<column>`)
|
|
248
|
+
* @returns This builder for chaining
|
|
249
|
+
*/
|
|
250
|
+
check(expression, name) {
|
|
251
|
+
this.definition.checkConstraint = {
|
|
252
|
+
expression,
|
|
253
|
+
name: name ?? `check_${this.definition.name}`,
|
|
254
|
+
};
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
// ============================================================================
|
|
258
|
+
// COLUMN POSITIONING (MySQL/MariaDB only)
|
|
259
|
+
// ============================================================================
|
|
260
|
+
/**
|
|
261
|
+
* Position this column after another column.
|
|
262
|
+
*
|
|
263
|
+
* MySQL/MariaDB only. Ignored by PostgreSQL and NoSQL drivers.
|
|
264
|
+
*
|
|
265
|
+
* @param columnName - Column to position after
|
|
266
|
+
* @returns This builder for chaining
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* this.string("middle_name").after("first_name");
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
after(columnName) {
|
|
274
|
+
this.definition.after = columnName;
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Position this column as the first column in the table.
|
|
279
|
+
*
|
|
280
|
+
* MySQL/MariaDB only. Ignored by PostgreSQL and NoSQL drivers.
|
|
281
|
+
*
|
|
282
|
+
* @returns This builder for chaining
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* this.integer("id").primary().first();
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
first() {
|
|
290
|
+
this.definition.first = true;
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
// ============================================================================
|
|
294
|
+
// FOREIGN KEY
|
|
295
|
+
// ============================================================================
|
|
296
|
+
/**
|
|
297
|
+
* Declare a foreign key constraint on this column.
|
|
298
|
+
*
|
|
299
|
+
* Pushes an `addForeignKey` operation immediately using a mutable reference —
|
|
300
|
+
* subsequent `.on()`, `.onDelete()`, `.onUpdate()` calls mutate the same
|
|
301
|
+
* definition that is already queued, so no `.add()` terminator is needed.
|
|
302
|
+
*
|
|
303
|
+
* Referenced column defaults to `"id"` — use `.on()` to override.
|
|
304
|
+
*
|
|
305
|
+
* @param table - Referenced table name
|
|
306
|
+
* @returns This builder for chaining
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* this.integer("user_id").references("users");
|
|
311
|
+
* this.integer("user_id").references("users").on("custom_id").onDelete("cascade");
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
references(table) {
|
|
315
|
+
this.fkDefinition = {
|
|
316
|
+
column: this.definition.name,
|
|
317
|
+
referencesTable: table,
|
|
318
|
+
referencesColumn: "id",
|
|
319
|
+
onDelete: "restrict",
|
|
320
|
+
onUpdate: "restrict",
|
|
321
|
+
};
|
|
322
|
+
this.migration.addForeignKeyOperation(this.fkDefinition);
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Set the referenced column for the foreign key.
|
|
327
|
+
*
|
|
328
|
+
* Only meaningful after `.references()`. Defaults to `"id"` if omitted.
|
|
329
|
+
*
|
|
330
|
+
* @param column - Referenced column name
|
|
331
|
+
* @returns This builder for chaining
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```typescript
|
|
335
|
+
* this.integer("user_id").references("users").on("custom_id");
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
on(column) {
|
|
339
|
+
if (this.fkDefinition) {
|
|
340
|
+
this.fkDefinition.referencesColumn = column;
|
|
341
|
+
}
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Set the ON DELETE action for the foreign key.
|
|
346
|
+
*
|
|
347
|
+
* Only meaningful after `.references()`.
|
|
348
|
+
*
|
|
349
|
+
* @param action - Action when the referenced row is deleted
|
|
350
|
+
* @returns This builder for chaining
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* ```typescript
|
|
354
|
+
* this.integer("user_id").references("users").onDelete("cascade");
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
onDelete(action) {
|
|
358
|
+
if (this.fkDefinition) {
|
|
359
|
+
this.fkDefinition.onDelete = action;
|
|
360
|
+
}
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Set the ON UPDATE action for the foreign key.
|
|
365
|
+
*
|
|
366
|
+
* Only meaningful after `.references()`.
|
|
367
|
+
*
|
|
368
|
+
* @param action - Action when the referenced row's key is updated
|
|
369
|
+
* @returns This builder for chaining
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```typescript
|
|
373
|
+
* this.integer("user_id").references("users").onUpdate("cascade");
|
|
374
|
+
* ```
|
|
375
|
+
*/
|
|
376
|
+
onUpdate(action) {
|
|
377
|
+
if (this.fkDefinition) {
|
|
378
|
+
this.fkDefinition.onUpdate = action;
|
|
379
|
+
}
|
|
380
|
+
return this;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Shorthand for `.onDelete("cascade").onUpdate("cascade")`.
|
|
384
|
+
*
|
|
385
|
+
* Only meaningful after `.references()`.
|
|
386
|
+
*
|
|
387
|
+
* @returns This builder for chaining
|
|
388
|
+
*
|
|
389
|
+
* @example
|
|
390
|
+
* ```typescript
|
|
391
|
+
* this.integer("user_id").references("users").cascadeAll();
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
cascadeAll() {
|
|
395
|
+
if (this.fkDefinition) {
|
|
396
|
+
this.fkDefinition.onDelete = "cascade";
|
|
397
|
+
this.fkDefinition.onUpdate = "cascade";
|
|
398
|
+
}
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
401
|
+
// ============================================================================
|
|
402
|
+
// COLUMN MODIFICATION
|
|
403
|
+
// ============================================================================
|
|
404
|
+
/**
|
|
405
|
+
* Mark this column definition as a modification of an existing column.
|
|
406
|
+
*
|
|
407
|
+
* Pushes a `modifyColumn` operation instead of `addColumn`.
|
|
408
|
+
* This method breaks the builder chain and returns the parent migration.
|
|
409
|
+
*
|
|
410
|
+
* @returns The parent migration instance (breaks builder chain)
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```typescript
|
|
414
|
+
* // Make email nullable
|
|
415
|
+
* this.string("email").nullable().change();
|
|
416
|
+
*
|
|
417
|
+
* // Increase length
|
|
418
|
+
* this.string("name", 255).change();
|
|
419
|
+
*
|
|
420
|
+
* // Change type
|
|
421
|
+
* this.text("description").change();
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
change() {
|
|
425
|
+
// Remove the existing addColumn operation that was pushed in the constructor
|
|
426
|
+
const operations = this.migration.pendingOperations;
|
|
427
|
+
const lastOp = operations[operations.length - 1];
|
|
428
|
+
if (lastOp?.type === "addColumn" && lastOp.payload === this.definition) {
|
|
429
|
+
operations.pop();
|
|
430
|
+
}
|
|
431
|
+
// Push modifyColumn instead
|
|
432
|
+
this.migration.pendingOperations.push({
|
|
433
|
+
type: "modifyColumn",
|
|
434
|
+
payload: this.definition,
|
|
435
|
+
});
|
|
436
|
+
return this.migration;
|
|
437
|
+
}
|
|
438
|
+
// ============================================================================
|
|
439
|
+
// GENERATED COLUMNS
|
|
440
|
+
// ============================================================================
|
|
441
|
+
/**
|
|
442
|
+
* Mark this column as a generated column with the given SQL expression.
|
|
443
|
+
*
|
|
444
|
+
* Must be followed by `.stored()` or `.virtual()` to specify storage type.
|
|
445
|
+
*
|
|
446
|
+
* PostgreSQL: GENERATED ALWAYS AS (...) STORED
|
|
447
|
+
* MySQL: GENERATED ALWAYS AS (...) STORED | VIRTUAL
|
|
448
|
+
*
|
|
449
|
+
* @param expression - SQL expression to compute the value
|
|
450
|
+
* @returns This builder for chaining
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```typescript
|
|
454
|
+
* this.string("full_name")
|
|
455
|
+
* .generatedAs("CONCAT(first_name, ' ', last_name)")
|
|
456
|
+
* .stored();
|
|
457
|
+
*
|
|
458
|
+
* this.decimal("price_with_tax")
|
|
459
|
+
* .generatedAs("price * 1.2")
|
|
460
|
+
* .virtual();
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
generatedAs(expression) {
|
|
464
|
+
this.generatedExpression = expression;
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Mark the generated column as stored (computed and persisted to disk).
|
|
469
|
+
*
|
|
470
|
+
* Must be called after `.generatedAs()`.
|
|
471
|
+
*
|
|
472
|
+
* @returns This builder for chaining
|
|
473
|
+
*/
|
|
474
|
+
stored() {
|
|
475
|
+
if (this.generatedExpression) {
|
|
476
|
+
this.definition.generated = {
|
|
477
|
+
expression: this.generatedExpression,
|
|
478
|
+
stored: true,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
return this;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Mark the generated column as virtual (computed on read, not stored).
|
|
485
|
+
*
|
|
486
|
+
* Must be called after `.generatedAs()`. Not supported by PostgreSQL.
|
|
487
|
+
*
|
|
488
|
+
* @returns This builder for chaining
|
|
489
|
+
*/
|
|
490
|
+
virtual() {
|
|
491
|
+
if (this.generatedExpression) {
|
|
492
|
+
this.definition.generated = {
|
|
493
|
+
expression: this.generatedExpression,
|
|
494
|
+
stored: false,
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
return this;
|
|
498
|
+
}
|
|
499
|
+
// ============================================================================
|
|
205
500
|
// ACCESSOR
|
|
206
501
|
// ============================================================================
|
|
207
502
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"column-builder.js","sources":["../../src/migration/column-builder.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"column-builder.js","sources":["../../src/migration/column-builder.ts"],"sourcesContent":[null],"names":[],"mappings":"aAuBA;;;;;;;;;;;;;;;;;;;;;AAqBG;MACU,aAAa,CAAA;AAmBL,IAAA,SAAA,CAAA;;AAjBF,IAAA,UAAU,CAAmB;;AAGtC,IAAA,YAAY,CAA+B;;AAG3C,IAAA,mBAAmB,CAAU;AAErC;;;;;;;AAOG;AACH,IAAA,WAAA,CACmB,SAAwB,EACzC,IAAY,EACZ,IAAgB,EAChB,UAEI,EAAE,EAAA;QALW,IAAS,CAAA,SAAA,GAAT,SAAS,CAAe;QAOzC,IAAI,CAAC,UAAU,GAAG;YAChB,IAAI;YACJ,IAAI;AACJ,YAAA,QAAQ,EAAE,KAAK;AACf,YAAA,GAAG,OAAO;SACX,CAAC;KACH;;;;AAMD;;;;;;;;;AASG;IACI,QAAQ,GAAA;AACb,QAAA,IAAI,CAAC,UAAU,CAAC,QAAQ,GAAG,IAAI,CAAC;AAChC,QAAA,OAAO,IAAI,CAAC;KACb;AAED;;;;;;AAMG;IACI,WAAW,GAAA;AA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U,EAAE;YACtE,UAAU,CAAC,GAAG,EAAE,CAAC;AAClB,SAAA;;AAGA,QAAA,IAAI,CAAC,SAAiB,CAAC,iBAAiB,CAAC,IAAI,CAAC;AAC7C,YAAA,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,IAAI,CAAC,UAAU;AACzB,SAAA,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,SAAS,CAAC;KACvB;;;;AAMD;;;;;;;;;;;;;;;;;;;;;AAqBG;AACI,IAAA,WAAW,CAAC,UAAkB,EAAA;AACnC,QAAA,IAAI,CAAC,mBAAmB,GAAG,UAAU,CAAC;AACtC,QAAA,OAAO,IAAI,CAAC;KACb;AAED;;;;;;AAMG;IACI,MAAM,GAAA;QACX,IAAI,IAAI,CAAC,mBAAmB,EAAE;AAC5B,YAAA,IAAI,CAAC,UAAU,CAAC,SAAS,GAAG;gBAC1B,UAAU,EAAE,IAAI,CAAC,mBAAmB;AACpC,gBAAA,MAAM,EAAE,IAAI;aACb,CAAC;AACH,SAAA;AACD,QAAA,OAAO,IAAI,CAAC;KACb;AAED;;;;;;AAMG;IACI,OAAO,GAAA;QACZ,IAAI,IAAI,CAAC,mBAAmB,EAAE;AAC5B,YAAA,IAAI,CAAC,UAAU,CAAC,SAAS,GAAG;gBAC1B,UAAU,EAAE,IAAI,CAAC,mBAAmB;AACpC,gBAAA,MAAM,EAAE,KAAK;aACd,CAAC;AACH,SAAA;AACD,QAAA,OAAO,IAAI,CAAC;KACb;;;;AAMD;;;;;;AAMG;IACI,aAAa,GAAA;QAClB,OAAO,IAAI,CAAC,UAAU,CAAC;KACxB;AACF"}
|
|
@@ -11,13 +11,16 @@ type MigrationLike = {
|
|
|
11
11
|
* Allows building foreign key definitions with a chainable API.
|
|
12
12
|
* SQL-only feature; NoSQL drivers ignore foreign keys.
|
|
13
13
|
*
|
|
14
|
+
* The operation is pushed when `.references()` is called using a mutable
|
|
15
|
+
* reference — subsequent `.onDelete()` / `.onUpdate()` calls mutate the
|
|
16
|
+
* same definition already queued in pendingOperations.
|
|
17
|
+
*
|
|
14
18
|
* @example
|
|
15
19
|
* ```typescript
|
|
16
20
|
* this.foreign("user_id")
|
|
17
21
|
* .references("users", "id")
|
|
18
22
|
* .onDelete("cascade")
|
|
19
|
-
* .onUpdate("cascade")
|
|
20
|
-
* .add();
|
|
23
|
+
* .onUpdate("cascade");
|
|
21
24
|
* ```
|
|
22
25
|
*/
|
|
23
26
|
export declare class ForeignKeyBuilder {
|
|
@@ -39,7 +42,11 @@ export declare class ForeignKeyBuilder {
|
|
|
39
42
|
*/
|
|
40
43
|
name(name: string): this;
|
|
41
44
|
/**
|
|
42
|
-
* Set the referenced table and column.
|
|
45
|
+
* Set the referenced table and column, and register the foreign key operation.
|
|
46
|
+
*
|
|
47
|
+
* Pushes the operation immediately using a mutable reference — any
|
|
48
|
+
* `.onDelete()` / `.onUpdate()` calls after this will mutate the same
|
|
49
|
+
* definition already queued in pendingOperations.
|
|
43
50
|
*
|
|
44
51
|
* @param table - Referenced table name
|
|
45
52
|
* @param column - Referenced column name (default: "id")
|
|
@@ -91,20 +98,6 @@ export declare class ForeignKeyBuilder {
|
|
|
91
98
|
* ```
|
|
92
99
|
*/
|
|
93
100
|
cascadeAll(): this;
|
|
94
|
-
/**
|
|
95
|
-
* Finalize and add the foreign key constraint to the migration.
|
|
96
|
-
*
|
|
97
|
-
* This must be called to register the foreign key.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* this.foreign("user_id")
|
|
102
|
-
* .references("users")
|
|
103
|
-
* .onDelete("cascade")
|
|
104
|
-
* .add(); // Required!
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
add(): void;
|
|
108
101
|
}
|
|
109
102
|
export {};
|
|
110
103
|
//# sourceMappingURL=foreign-key-builder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"foreign-key-builder.d.ts","sourceRoot":"","sources":["../../src/migration/foreign-key-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAEnF;;GAEG;AACH,KAAK,aAAa,GAAG;IACnB,sBAAsB,CAAC,EAAE,EAAE,oBAAoB,GAAG,IAAI,CAAC;CACxD,CAAC;AAcF
|
|
1
|
+
{"version":3,"file":"foreign-key-builder.d.ts","sourceRoot":"","sources":["../../src/migration/foreign-key-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAEnF;;GAEG;AACH,KAAK,aAAa,GAAG;IACnB,sBAAsB,CAAC,EAAE,EAAE,oBAAoB,GAAG,IAAI,CAAC;CACxD,CAAC;AAcF;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,iBAAiB;IAW1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAV5B,uDAAuD;IACvD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA8B;IAEzD;;;;;OAKG;gBAEgB,SAAS,EAAE,aAAa,EACzC,MAAM,EAAE,MAAM;IAWhB;;;;;OAKG;IACI,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK/B;;;;;;;;;;;;;;;;OAgBG;IACI,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,SAAO,GAAG,IAAI;IAOrD;;;;;;;;;;;;OAYG;IACI,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAC,UAAU,CAAC,GAAG,IAAI;IAK/D;;;;;;;;;;;;OAYG;IACI,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAC,UAAU,CAAC,GAAG,IAAI;IAK/D;;;;;;;;;OASG;IACI,UAAU,IAAI,IAAI;CAM1B"}
|
|
@@ -4,13 +4,16 @@
|
|
|
4
4
|
* Allows building foreign key definitions with a chainable API.
|
|
5
5
|
* SQL-only feature; NoSQL drivers ignore foreign keys.
|
|
6
6
|
*
|
|
7
|
+
* The operation is pushed when `.references()` is called using a mutable
|
|
8
|
+
* reference — subsequent `.onDelete()` / `.onUpdate()` calls mutate the
|
|
9
|
+
* same definition already queued in pendingOperations.
|
|
10
|
+
*
|
|
7
11
|
* @example
|
|
8
12
|
* ```typescript
|
|
9
13
|
* this.foreign("user_id")
|
|
10
14
|
* .references("users", "id")
|
|
11
15
|
* .onDelete("cascade")
|
|
12
|
-
* .onUpdate("cascade")
|
|
13
|
-
* .add();
|
|
16
|
+
* .onUpdate("cascade");
|
|
14
17
|
* ```
|
|
15
18
|
*/
|
|
16
19
|
class ForeignKeyBuilder {
|
|
@@ -44,7 +47,11 @@ class ForeignKeyBuilder {
|
|
|
44
47
|
return this;
|
|
45
48
|
}
|
|
46
49
|
/**
|
|
47
|
-
* Set the referenced table and column.
|
|
50
|
+
* Set the referenced table and column, and register the foreign key operation.
|
|
51
|
+
*
|
|
52
|
+
* Pushes the operation immediately using a mutable reference — any
|
|
53
|
+
* `.onDelete()` / `.onUpdate()` calls after this will mutate the same
|
|
54
|
+
* definition already queued in pendingOperations.
|
|
48
55
|
*
|
|
49
56
|
* @param table - Referenced table name
|
|
50
57
|
* @param column - Referenced column name (default: "id")
|
|
@@ -59,6 +66,7 @@ class ForeignKeyBuilder {
|
|
|
59
66
|
references(table, column = "id") {
|
|
60
67
|
this.definition.referencesTable = table;
|
|
61
68
|
this.definition.referencesColumn = column;
|
|
69
|
+
this.migration.addForeignKeyOperation(this.definition);
|
|
62
70
|
return this;
|
|
63
71
|
}
|
|
64
72
|
/**
|
|
@@ -110,20 +118,4 @@ class ForeignKeyBuilder {
|
|
|
110
118
|
this.definition.onUpdate = "cascade";
|
|
111
119
|
return this;
|
|
112
120
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Finalize and add the foreign key constraint to the migration.
|
|
115
|
-
*
|
|
116
|
-
* This must be called to register the foreign key.
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```typescript
|
|
120
|
-
* this.foreign("user_id")
|
|
121
|
-
* .references("users")
|
|
122
|
-
* .onDelete("cascade")
|
|
123
|
-
* .add(); // Required!
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
add() {
|
|
127
|
-
this.migration.addForeignKeyOperation(this.definition);
|
|
128
|
-
}
|
|
129
121
|
}exports.ForeignKeyBuilder=ForeignKeyBuilder;//# sourceMappingURL=foreign-key-builder.js.map
|