@c9up/atlas 0.1.18 → 0.2.0
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/README.md +55 -14
- package/db.darwin-arm64.node +0 -0
- package/db.darwin-x64.node +0 -0
- package/db.linux-arm64-gnu.node +0 -0
- package/db.linux-x64-gnu.node +0 -0
- package/db.win32-x64-msvc.node +0 -0
- package/dist/AtlasProvider.d.ts +6 -0
- package/dist/AtlasProvider.d.ts.map +1 -1
- package/dist/AtlasProvider.js +2 -2
- package/dist/AtlasProvider.js.map +1 -1
- package/dist/BaseEntity.d.ts +171 -7
- package/dist/BaseEntity.d.ts.map +1 -1
- package/dist/BaseEntity.js +339 -31
- package/dist/BaseEntity.js.map +1 -1
- package/dist/BaseModel.d.ts +91 -0
- package/dist/BaseModel.d.ts.map +1 -0
- package/dist/BaseModel.js +193 -0
- package/dist/BaseModel.js.map +1 -0
- package/dist/BaseRepository.d.ts +77 -15
- package/dist/BaseRepository.d.ts.map +1 -1
- package/dist/BaseRepository.js +1423 -354
- package/dist/BaseRepository.js.map +1 -1
- package/dist/ModelQuery.d.ts +429 -11
- package/dist/ModelQuery.d.ts.map +1 -1
- package/dist/ModelQuery.js +1733 -145
- package/dist/ModelQuery.js.map +1 -1
- package/dist/Transaction.d.ts +17 -0
- package/dist/Transaction.d.ts.map +1 -1
- package/dist/Transaction.js +57 -5
- package/dist/Transaction.js.map +1 -1
- package/dist/adapters/NapiDbAdapter.d.ts +33 -4
- package/dist/adapters/NapiDbAdapter.d.ts.map +1 -1
- package/dist/adapters/NapiDbAdapter.js +101 -11
- package/dist/adapters/NapiDbAdapter.js.map +1 -1
- package/dist/console/migrationCommands.d.ts +48 -0
- package/dist/console/migrationCommands.d.ts.map +1 -0
- package/dist/console/migrationCommands.js +220 -0
- package/dist/console/migrationCommands.js.map +1 -0
- package/dist/decorators/entity.d.ts +37 -6
- package/dist/decorators/entity.d.ts.map +1 -1
- package/dist/decorators/entity.js +32 -2
- package/dist/decorators/entity.js.map +1 -1
- package/dist/events.d.ts +64 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +82 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/metadata-keys.d.ts +3 -2
- package/dist/metadata-keys.d.ts.map +1 -1
- package/dist/naming/NamingStrategy.d.ts +7 -0
- package/dist/naming/NamingStrategy.d.ts.map +1 -1
- package/dist/naming/NamingStrategy.js +16 -0
- package/dist/naming/NamingStrategy.js.map +1 -1
- package/dist/schema/Migration.d.ts +26 -3
- package/dist/schema/Migration.d.ts.map +1 -1
- package/dist/schema/Migration.js +33 -24
- package/dist/schema/Migration.js.map +1 -1
- package/dist/schema/MigrationRunner.d.ts +43 -32
- package/dist/schema/MigrationRunner.d.ts.map +1 -1
- package/dist/schema/MigrationRunner.js +211 -26
- package/dist/schema/MigrationRunner.js.map +1 -1
- package/dist/schema/Schema.d.ts +57 -0
- package/dist/schema/Schema.d.ts.map +1 -1
- package/dist/schema/Schema.js +138 -3
- package/dist/schema/Schema.js.map +1 -1
- package/dist/schema/SchemaCheck.d.ts.map +1 -1
- package/dist/schema/SchemaCheck.js +3 -1
- package/dist/schema/SchemaCheck.js.map +1 -1
- package/dist/schema/TableBuilder.d.ts +247 -8
- package/dist/schema/TableBuilder.d.ts.map +1 -1
- package/dist/schema/TableBuilder.js +607 -41
- package/dist/schema/TableBuilder.js.map +1 -1
- package/dist/schema/catalog.d.ts +47 -0
- package/dist/schema/catalog.d.ts.map +1 -0
- package/dist/schema/catalog.js +111 -0
- package/dist/schema/catalog.js.map +1 -0
- package/dist/schema/introspect.js.map +1 -1
- package/dist/schema/types.d.ts +150 -1
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/schema/types.js +11 -0
- package/dist/schema/types.js.map +1 -1
- package/dist/services/db.d.ts +6 -0
- package/dist/services/db.d.ts.map +1 -1
- package/dist/services/db.js +17 -0
- package/dist/services/db.js.map +1 -1
- package/dist/testing/DatabaseCleanup.d.ts +7 -4
- package/dist/testing/DatabaseCleanup.d.ts.map +1 -1
- package/dist/testing/DatabaseCleanup.js +21 -18
- package/dist/testing/DatabaseCleanup.js.map +1 -1
- package/dist/testing/Factory.d.ts +70 -5
- package/dist/testing/Factory.d.ts.map +1 -1
- package/dist/testing/Factory.js +209 -10
- package/dist/testing/Factory.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +4 -1
- package/scripts/guard-publish.mjs +15 -0
- package/src/AtlasProvider.ts +8 -1
- package/src/BaseEntity.ts +449 -40
- package/src/BaseModel.ts +324 -0
- package/src/BaseRepository.ts +1659 -371
- package/src/ModelQuery.ts +2290 -203
- package/src/Transaction.ts +68 -5
- package/src/adapters/NapiDbAdapter.ts +159 -10
- package/src/console/migrationCommands.ts +258 -0
- package/src/decorators/entity.ts +53 -6
- package/src/events.ts +112 -0
- package/src/index.ts +19 -0
- package/src/metadata-keys.ts +3 -2
- package/src/naming/NamingStrategy.ts +23 -0
- package/src/schema/Migration.ts +42 -3
- package/src/schema/MigrationRunner.ts +270 -27
- package/src/schema/Schema.ts +210 -3
- package/src/schema/SchemaCheck.ts +7 -2
- package/src/schema/TableBuilder.ts +735 -41
- package/src/schema/catalog.ts +166 -0
- package/src/schema/introspect.ts +3 -4
- package/src/schema/types.ts +137 -2
- package/src/services/db.ts +28 -0
- package/src/testing/DatabaseCleanup.ts +23 -22
- package/src/testing/Factory.ts +332 -15
|
@@ -10,14 +10,65 @@ import { compileStatementNative, getAtlasDialect, } from "../query/native.js";
|
|
|
10
10
|
import { RawSql } from "../query/QueryBuilder.js";
|
|
11
11
|
import { renderDefaultValue } from "./raw.js";
|
|
12
12
|
import { TYPE_KIND_MAP, } from "./types.js";
|
|
13
|
+
/**
|
|
14
|
+
* The chainable returned by `table.foreign([...])`, so the target reads in
|
|
15
|
+
* Knex order: `.references([...]).inTable('other').onDelete('cascade')`. It
|
|
16
|
+
* mutates the already-recorded constraint in place.
|
|
17
|
+
*/
|
|
18
|
+
export class ForeignKeyBuilder {
|
|
19
|
+
#references;
|
|
20
|
+
constructor(references) {
|
|
21
|
+
this.#references = references;
|
|
22
|
+
}
|
|
23
|
+
/** Target column(s) on the referenced table. */
|
|
24
|
+
references(columns) {
|
|
25
|
+
this.#references.columns =
|
|
26
|
+
typeof columns === "string" ? [columns] : [...columns];
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Referenced table. Keeps returning this builder so `.onDelete()` /
|
|
31
|
+
* `.onUpdate()` can follow, as in Knex — the constraint was already
|
|
32
|
+
* recorded on the table when `foreign()` was called, so there is nothing to
|
|
33
|
+
* hand back to.
|
|
34
|
+
*/
|
|
35
|
+
inTable(table) {
|
|
36
|
+
this.#references.table = table;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
onDelete(action) {
|
|
40
|
+
this.#references.onDelete = action;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
onUpdate(action) {
|
|
44
|
+
this.#references.onUpdate = action;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
13
48
|
/** Table builder — used inside `schema.createTable(name, callback)`. */
|
|
14
49
|
export class TableBuilder {
|
|
15
50
|
tableName;
|
|
51
|
+
mode;
|
|
16
52
|
#columns = [];
|
|
17
53
|
#indexes = [];
|
|
18
54
|
#currentColumn;
|
|
19
|
-
|
|
55
|
+
/** Ordered ALTER TABLE ops. Empty (and unused) in `create` mode. */
|
|
56
|
+
#operations = [];
|
|
57
|
+
/** Table-level constraints. In `alter` mode these become `addConstraint` ops instead. */
|
|
58
|
+
#constraints = [];
|
|
59
|
+
#options = {};
|
|
60
|
+
/** The op the pending column modifiers apply to, in `alter` mode. */
|
|
61
|
+
#currentOp;
|
|
62
|
+
/**
|
|
63
|
+
* Whether `.nullable()` / `.notNullable()` was called on the current column.
|
|
64
|
+
* `ColumnDefinition.nullable` defaults to `true`, so without this flag
|
|
65
|
+
* `.alter()` could not tell "leave nullability alone" from "make it
|
|
66
|
+
* nullable" — see `AlterOperation.setNullable`.
|
|
67
|
+
*/
|
|
68
|
+
#nullabilityTouched = false;
|
|
69
|
+
constructor(tableName, mode = "create") {
|
|
20
70
|
this.tableName = tableName;
|
|
71
|
+
this.mode = mode;
|
|
21
72
|
}
|
|
22
73
|
// ─── Column types ─────────────────────────────────────────
|
|
23
74
|
uuid(name) {
|
|
@@ -42,12 +93,29 @@ export class TableBuilder {
|
|
|
42
93
|
this.#currentColumn.length = length;
|
|
43
94
|
return this;
|
|
44
95
|
}
|
|
45
|
-
|
|
46
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Text column (Lucid/Knex `text(name, textType)`). `textType` widens the
|
|
98
|
+
* MySQL type (`MEDIUMTEXT` / `LONGTEXT`); Postgres and SQLite have a single
|
|
99
|
+
* unbounded `TEXT` and ignore it.
|
|
100
|
+
*/
|
|
101
|
+
text(name, textType = "text") {
|
|
102
|
+
return this.#addColumn(name, textType);
|
|
47
103
|
}
|
|
48
104
|
integer(name) {
|
|
49
105
|
return this.#addColumn(name, "integer");
|
|
50
106
|
}
|
|
107
|
+
/** 24-bit integer (Lucid/Knex `mediumint`). MySQL `MEDIUMINT`; pg/SQLite widen to `INTEGER`. */
|
|
108
|
+
mediumint(name) {
|
|
109
|
+
return this.#addColumn(name, "mediumint");
|
|
110
|
+
}
|
|
111
|
+
/** 8-bit integer (Lucid `tinyint`). MySQL `TINYINT`; Postgres widens to `SMALLINT`; SQLite `INTEGER`. */
|
|
112
|
+
tinyint(name) {
|
|
113
|
+
return this.#addColumn(name, "tinyint");
|
|
114
|
+
}
|
|
115
|
+
/** 16-bit integer (Lucid `smallint`). `SMALLINT` on pg/mysql, `INTEGER` on SQLite. */
|
|
116
|
+
smallint(name) {
|
|
117
|
+
return this.#addColumn(name, "smallint");
|
|
118
|
+
}
|
|
51
119
|
bigInteger(name) {
|
|
52
120
|
return this.#addColumn(name, "bigInteger");
|
|
53
121
|
}
|
|
@@ -59,14 +127,52 @@ export class TableBuilder {
|
|
|
59
127
|
}
|
|
60
128
|
return this;
|
|
61
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Single-precision float (Lucid `float`). `REAL` on pg/sqlite, `FLOAT` on
|
|
132
|
+
* MySQL. `precision`/`scale` render `FLOAT(p, s)` on MySQL only — pg and
|
|
133
|
+
* SQLite have fixed-width floats and ignore them.
|
|
134
|
+
*/
|
|
135
|
+
float(name, precision, scale) {
|
|
136
|
+
return this.#addFloat(name, "float", precision, scale);
|
|
137
|
+
}
|
|
138
|
+
/** Double-precision float (Lucid `double`). `DOUBLE PRECISION` on pg, `REAL` on SQLite, `DOUBLE` on MySQL. See {@link float} for precision/scale. */
|
|
139
|
+
double(name, precision, scale) {
|
|
140
|
+
return this.#addFloat(name, "double", precision, scale);
|
|
141
|
+
}
|
|
62
142
|
boolean(name) {
|
|
63
143
|
return this.#addColumn(name, "boolean");
|
|
64
144
|
}
|
|
65
145
|
date(name) {
|
|
66
146
|
return this.#addColumn(name, "date");
|
|
67
147
|
}
|
|
68
|
-
|
|
69
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Time of day (Lucid `time`). `TIME` on pg/mysql, `TEXT` on SQLite.
|
|
150
|
+
* `precision` renders `TIME(p)` fractional seconds (ignored on SQLite,
|
|
151
|
+
* which has no time type to carry it).
|
|
152
|
+
*/
|
|
153
|
+
time(name, precision) {
|
|
154
|
+
this.#addColumn(name, "time");
|
|
155
|
+
if (this.#currentColumn)
|
|
156
|
+
this.#currentColumn.precision = precision;
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Timestamp column (Lucid `timestamp(name, options)`).
|
|
161
|
+
*
|
|
162
|
+
* `useTz: true` selects the tz-aware type — the same thing
|
|
163
|
+
* {@link timestamptz} does, exposed here for Lucid's option spelling.
|
|
164
|
+
* `precision` renders `TIMESTAMP(p)` (ignored on SQLite, which stores
|
|
165
|
+
* timestamps as TEXT).
|
|
166
|
+
*/
|
|
167
|
+
timestamp(name, options = {}) {
|
|
168
|
+
this.#addColumn(name, options.useTz ? "timestamptz" : "timestamp");
|
|
169
|
+
if (this.#currentColumn)
|
|
170
|
+
this.#currentColumn.precision = options.precision;
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
/** Alias of {@link timestamp} (Lucid `dateTime`). Use `{ useTz: true }` or {@link timestamptz} for a tz-aware column. */
|
|
174
|
+
dateTime(name, options = {}) {
|
|
175
|
+
return this.timestamp(name, options);
|
|
70
176
|
}
|
|
71
177
|
/**
|
|
72
178
|
* `timestamp WITH time zone` — Postgres normalises every writer (atlas,
|
|
@@ -83,8 +189,55 @@ export class TableBuilder {
|
|
|
83
189
|
json(name) {
|
|
84
190
|
return this.#addColumn(name, "json");
|
|
85
191
|
}
|
|
86
|
-
|
|
87
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Binary JSON (Lucid/Knex `jsonb`). `JSONB` on pg, `JSON` on MySQL, `TEXT`
|
|
194
|
+
* on SQLite.
|
|
195
|
+
*
|
|
196
|
+
* Deviation, named: atlas's {@link json} already maps to `JSONB` on
|
|
197
|
+
* Postgres (it predates this method), where Lucid's `json()` maps to
|
|
198
|
+
* `json`. Leaving `json()` alone avoids silently rewriting the physical
|
|
199
|
+
* type of existing columns and desyncing `SchemaCheck`, so on Postgres the
|
|
200
|
+
* two spellings coincide.
|
|
201
|
+
*/
|
|
202
|
+
jsonb(name) {
|
|
203
|
+
return this.#addColumn(name, "jsonb");
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Binary blob (Lucid/Knex `binary(name, length)`). `BYTEA` on pg, `BLOB` on
|
|
207
|
+
* SQLite; on MySQL `length` selects `VARBINARY(n)` over `BLOB`.
|
|
208
|
+
*/
|
|
209
|
+
binary(name, length) {
|
|
210
|
+
this.#addColumn(name, "binary");
|
|
211
|
+
if (this.#currentColumn)
|
|
212
|
+
this.#currentColumn.length = length;
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* A column typed with a verbatim dialect type (Lucid/Knex `specificType`) —
|
|
217
|
+
* the escape hatch for types atlas has no method for (`inet`, `tsvector`,
|
|
218
|
+
* `geometry(Point, 4326)`…).
|
|
219
|
+
*
|
|
220
|
+
* Deviation, named: Knex passes the string straight through. Atlas cannot —
|
|
221
|
+
* it lands verbatim in DDL, so the Rust compiler validates it against a
|
|
222
|
+
* narrow grammar (letters, digits, spaces, `_`, and one parenthesised
|
|
223
|
+
* argument list) and rejects anything else with `E_UNSAFE_SQL`.
|
|
224
|
+
*/
|
|
225
|
+
specificType(name, type) {
|
|
226
|
+
this.#addColumn(name, "specificType");
|
|
227
|
+
if (this.#currentColumn)
|
|
228
|
+
this.#currentColumn.rawType = type;
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Fixed value-set column (Lucid `enum`). MySQL renders a native `ENUM(...)`;
|
|
233
|
+
* Postgres and SQLite render `TEXT` plus a `CHECK (col IN (...))` that pins the
|
|
234
|
+
* value set. At least one value is required.
|
|
235
|
+
*/
|
|
236
|
+
enum(name, values) {
|
|
237
|
+
this.#addColumn(name, "enum");
|
|
238
|
+
if (this.#currentColumn)
|
|
239
|
+
this.#currentColumn.values = values;
|
|
240
|
+
return this;
|
|
88
241
|
}
|
|
89
242
|
// ─── Shortcuts ────────────────────────────────────────────
|
|
90
243
|
/**
|
|
@@ -140,24 +293,16 @@ export class TableBuilder {
|
|
|
140
293
|
return this;
|
|
141
294
|
}
|
|
142
295
|
// ─── Column modifiers ─────────────────────────────────────
|
|
143
|
-
primary() {
|
|
144
|
-
if (this.#currentColumn)
|
|
145
|
-
this.#currentColumn.primary = true;
|
|
146
|
-
return this;
|
|
147
|
-
}
|
|
148
296
|
notNullable() {
|
|
149
297
|
if (this.#currentColumn)
|
|
150
298
|
this.#currentColumn.nullable = false;
|
|
299
|
+
this.#nullabilityTouched = true;
|
|
151
300
|
return this;
|
|
152
301
|
}
|
|
153
302
|
nullable() {
|
|
154
303
|
if (this.#currentColumn)
|
|
155
304
|
this.#currentColumn.nullable = true;
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
unique() {
|
|
159
|
-
if (this.#currentColumn)
|
|
160
|
-
this.#currentColumn.unique = true;
|
|
305
|
+
this.#nullabilityTouched = true;
|
|
161
306
|
return this;
|
|
162
307
|
}
|
|
163
308
|
/**
|
|
@@ -171,11 +316,250 @@ export class TableBuilder {
|
|
|
171
316
|
}
|
|
172
317
|
return this;
|
|
173
318
|
}
|
|
319
|
+
/** MySQL `UNSIGNED` numeric modifier (Lucid `unsigned()`). No-op on pg/sqlite. */
|
|
320
|
+
unsigned() {
|
|
321
|
+
if (this.#currentColumn)
|
|
322
|
+
this.#currentColumn.unsigned = true;
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
174
325
|
references(table, column = "id") {
|
|
175
326
|
if (this.#currentColumn)
|
|
176
327
|
this.#currentColumn.references = { table, column };
|
|
177
328
|
return this;
|
|
178
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Referential action for the current column's foreign key `ON DELETE`
|
|
332
|
+
* (Lucid parity). Must follow {@link references}.
|
|
333
|
+
*/
|
|
334
|
+
onDelete(action) {
|
|
335
|
+
if (this.#currentColumn?.references) {
|
|
336
|
+
this.#currentColumn.references.onDelete = action;
|
|
337
|
+
}
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
/** Referential action for the current column's foreign key `ON UPDATE`. Must follow {@link references}. */
|
|
341
|
+
onUpdate(action) {
|
|
342
|
+
if (this.#currentColumn?.references) {
|
|
343
|
+
this.#currentColumn.references.onUpdate = action;
|
|
344
|
+
}
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Comment the current **column** (Lucid/Knex column `comment()`). Inline on
|
|
349
|
+
* MySQL, a separate `COMMENT ON COLUMN` on Postgres, dropped on SQLite.
|
|
350
|
+
*
|
|
351
|
+
* Deviation, named: Knex's `table.comment()` is the TABLE comment, because
|
|
352
|
+
* its column methods return a separate column builder. Atlas flattens the
|
|
353
|
+
* column modifiers onto the table builder (`.notNullable()`, `.unique()`,
|
|
354
|
+
* `.defaultTo()` all work this way), so `comment()` follows that same rule
|
|
355
|
+
* and the table comment is {@link tableComment}. Resolving it by "is a
|
|
356
|
+
* column pending?" would be exactly the kind of guessing that bites later.
|
|
357
|
+
*/
|
|
358
|
+
comment(text) {
|
|
359
|
+
if (this.#currentColumn)
|
|
360
|
+
this.#currentColumn.comment = text;
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
/** Collate the current **column** (Lucid/Knex column `collate()`). See {@link comment} for why the table form is {@link tableCollate}. */
|
|
364
|
+
collate(collation) {
|
|
365
|
+
if (this.#currentColumn)
|
|
366
|
+
this.#currentColumn.collate = collation;
|
|
367
|
+
return this;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Place an added column first (Lucid/Knex `first()`). MySQL-only —
|
|
371
|
+
* Postgres and SQLite always append, and the Rust compiler raises
|
|
372
|
+
* `E_UNSUPPORTED` rather than dropping the instruction silently.
|
|
373
|
+
*/
|
|
374
|
+
first() {
|
|
375
|
+
if (this.#currentColumn)
|
|
376
|
+
this.#currentColumn.position = { at: "first" };
|
|
377
|
+
return this;
|
|
378
|
+
}
|
|
379
|
+
/** Place an added column after `column` (Lucid/Knex `after()`). MySQL-only — see {@link first}. */
|
|
380
|
+
after(column) {
|
|
381
|
+
if (this.#currentColumn) {
|
|
382
|
+
this.#currentColumn.position = { at: "after", column };
|
|
383
|
+
}
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
// ─── CHECK constraints ────────────────────────────────────
|
|
387
|
+
/** `CHECK (col > 0)` on the current column (Lucid/Knex `checkPositive`). */
|
|
388
|
+
checkPositive(constraintName) {
|
|
389
|
+
return this.#addCheck((column) => ({ check: "positive", column }), constraintName);
|
|
390
|
+
}
|
|
391
|
+
/** `CHECK (col < 0)` on the current column (Lucid/Knex `checkNegative`). */
|
|
392
|
+
checkNegative(constraintName) {
|
|
393
|
+
return this.#addCheck((column) => ({ check: "negative", column }), constraintName);
|
|
394
|
+
}
|
|
395
|
+
/** `CHECK (col IN (…))` on the current column (Lucid/Knex `checkIn`). Values are quoted, never interpolated raw. */
|
|
396
|
+
checkIn(values, constraintName) {
|
|
397
|
+
return this.#addCheck((column) => ({ check: "in", column, values: [...values] }), constraintName);
|
|
398
|
+
}
|
|
399
|
+
/** `CHECK (col NOT IN (…))` on the current column (Lucid/Knex `checkNotIn`). */
|
|
400
|
+
checkNotIn(values, constraintName) {
|
|
401
|
+
return this.#addCheck((column) => ({ check: "notIn", column, values: [...values] }), constraintName);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* `CHECK (col BETWEEN lo AND hi)` on the current column (Lucid/Knex
|
|
405
|
+
* `checkBetween`). Accepts one `[min, max]` interval or a list of them —
|
|
406
|
+
* several intervals are OR'd together, as in Knex.
|
|
407
|
+
*/
|
|
408
|
+
checkBetween(range, constraintName) {
|
|
409
|
+
// A single [min, max] vs a list of intervals: the first element of a
|
|
410
|
+
// list-of-intervals is itself an array.
|
|
411
|
+
const ranges = Array.isArray(range[0])
|
|
412
|
+
? range.map((r) => [...r])
|
|
413
|
+
: [[...range]];
|
|
414
|
+
return this.#addCheck((column) => ({ check: "between", column, ranges }), constraintName);
|
|
415
|
+
}
|
|
416
|
+
/** `CHECK (LENGTH(col) <op> n)` on the current column (Lucid/Knex `checkLength`). The operator is allow-listed by the Rust compiler. */
|
|
417
|
+
checkLength(operator, length, constraintName) {
|
|
418
|
+
return this.#addCheck((column) => ({ check: "length", column, operator, length }), constraintName);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* `CHECK (col ~ 'pattern')` on the current column (Lucid/Knex `checkRegex`).
|
|
422
|
+
* Postgres spells it `~`; MySQL and SQLite use `REGEXP`.
|
|
423
|
+
*
|
|
424
|
+
* SQLite parses `REGEXP` but ships no implementation — the constraint only
|
|
425
|
+
* works if the connection registers a `regexp` function. Knex behaves the
|
|
426
|
+
* same way, so this is parity rather than a new trap, but it is worth
|
|
427
|
+
* knowing before you rely on it there.
|
|
428
|
+
*/
|
|
429
|
+
checkRegex(pattern, constraintName) {
|
|
430
|
+
return this.#addCheck((column) => ({ check: "regex", column, pattern }), constraintName);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* A free-form `CHECK (predicate)` (Lucid/Knex `check`). The predicate is
|
|
434
|
+
* emitted verbatim — exactly as trusted as {@link Schema.raw}, so never
|
|
435
|
+
* build it from user input. Prefer the typed `check*` helpers, which are
|
|
436
|
+
* safe by construction.
|
|
437
|
+
*/
|
|
438
|
+
check(predicate, constraintName) {
|
|
439
|
+
this.#pushConstraint({
|
|
440
|
+
constraint: "check",
|
|
441
|
+
name: constraintName,
|
|
442
|
+
expr: { check: "raw", predicate },
|
|
443
|
+
});
|
|
444
|
+
return this;
|
|
445
|
+
}
|
|
446
|
+
/** Drop named CHECK constraints (Lucid/Knex `dropChecks`). */
|
|
447
|
+
dropChecks(...constraintNames) {
|
|
448
|
+
this.#assertAlterMode("dropChecks()");
|
|
449
|
+
for (const name of constraintNames) {
|
|
450
|
+
this.#pushStandaloneOp({ op: "dropConstraint", name });
|
|
451
|
+
}
|
|
452
|
+
return this;
|
|
453
|
+
}
|
|
454
|
+
// ─── Table-level constraints ──────────────────────────────
|
|
455
|
+
/**
|
|
456
|
+
* With no argument, mark the current column as the primary key (the
|
|
457
|
+
* existing column modifier). With a column list, declare a composite
|
|
458
|
+
* `PRIMARY KEY (…)` table constraint (Lucid/Knex `primary([...])`).
|
|
459
|
+
*/
|
|
460
|
+
primary(columns, constraintName) {
|
|
461
|
+
if (columns === undefined) {
|
|
462
|
+
if (this.#currentColumn)
|
|
463
|
+
this.#currentColumn.primary = true;
|
|
464
|
+
return this;
|
|
465
|
+
}
|
|
466
|
+
this.#pushConstraint({
|
|
467
|
+
constraint: "primary",
|
|
468
|
+
name: constraintName,
|
|
469
|
+
columns: [...columns],
|
|
470
|
+
});
|
|
471
|
+
return this;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* With no argument, mark the current column `UNIQUE` (the existing column
|
|
475
|
+
* modifier). With a column list, declare a composite `UNIQUE (…)` table
|
|
476
|
+
* constraint (Lucid/Knex `unique([...])`).
|
|
477
|
+
*
|
|
478
|
+
* Note this is a real constraint, unlike {@link uniqueIndex}, which creates
|
|
479
|
+
* a separate `CREATE UNIQUE INDEX`.
|
|
480
|
+
*/
|
|
481
|
+
unique(columns, constraintName) {
|
|
482
|
+
if (columns === undefined) {
|
|
483
|
+
if (this.#currentColumn)
|
|
484
|
+
this.#currentColumn.unique = true;
|
|
485
|
+
return this;
|
|
486
|
+
}
|
|
487
|
+
this.#pushConstraint({
|
|
488
|
+
constraint: "unique",
|
|
489
|
+
name: constraintName ?? this.#constraintName(columns, "unique"),
|
|
490
|
+
columns: [...columns],
|
|
491
|
+
});
|
|
492
|
+
return this;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Declare a composite foreign key (Lucid/Knex
|
|
496
|
+
* `foreign([...]).references([...]).inTable(…)`). Returns a small chainable
|
|
497
|
+
* so the target reads in Knex order; the constraint is recorded up front
|
|
498
|
+
* and filled in as you chain.
|
|
499
|
+
*/
|
|
500
|
+
foreign(columns, constraintName) {
|
|
501
|
+
const cols = typeof columns === "string" ? [columns] : [...columns];
|
|
502
|
+
const references = { table: "", columns: [] };
|
|
503
|
+
this.#pushConstraint({
|
|
504
|
+
constraint: "foreign",
|
|
505
|
+
name: constraintName ?? this.#constraintName(cols, "foreign"),
|
|
506
|
+
columns: cols,
|
|
507
|
+
references,
|
|
508
|
+
});
|
|
509
|
+
// The constraint is already recorded; the builder fills `references` in
|
|
510
|
+
// place as the caller chains, so order of arrival doesn't matter.
|
|
511
|
+
return new ForeignKeyBuilder(references);
|
|
512
|
+
}
|
|
513
|
+
// ─── Dropping constraints ─────────────────────────────────
|
|
514
|
+
/** Drop the primary key (Lucid/Knex `dropPrimary`). MySQL drops it by keyword; Postgres by name (default `<table>_pkey`). */
|
|
515
|
+
dropPrimary(constraintName) {
|
|
516
|
+
this.#assertAlterMode("dropPrimary()");
|
|
517
|
+
this.#pushStandaloneOp({ op: "dropPrimary", name: constraintName });
|
|
518
|
+
return this;
|
|
519
|
+
}
|
|
520
|
+
/** Drop a unique constraint by columns (using the default name) or by explicit name (Lucid/Knex `dropUnique`). */
|
|
521
|
+
dropUnique(columns, constraintName) {
|
|
522
|
+
this.#assertAlterMode("dropUnique()");
|
|
523
|
+
this.#pushStandaloneOp({
|
|
524
|
+
op: "dropUnique",
|
|
525
|
+
name: constraintName ?? this.#constraintName(columns, "unique"),
|
|
526
|
+
});
|
|
527
|
+
return this;
|
|
528
|
+
}
|
|
529
|
+
/** Drop a foreign key by columns (using the default name) or by explicit name (Lucid/Knex `dropForeign`). */
|
|
530
|
+
dropForeign(columns, constraintName) {
|
|
531
|
+
this.#assertAlterMode("dropForeign()");
|
|
532
|
+
this.#pushStandaloneOp({
|
|
533
|
+
op: "dropForeign",
|
|
534
|
+
name: constraintName ?? this.#constraintName(columns, "foreign"),
|
|
535
|
+
});
|
|
536
|
+
return this;
|
|
537
|
+
}
|
|
538
|
+
/** Drop `created_at` + `updated_at` (Lucid/Knex `dropTimestamps`). */
|
|
539
|
+
dropTimestamps() {
|
|
540
|
+
return this.dropColumns("created_at", "updated_at");
|
|
541
|
+
}
|
|
542
|
+
// ─── Table options ────────────────────────────────────────
|
|
543
|
+
/** MySQL storage engine (Lucid/Knex `engine`). Ignored on pg/sqlite. */
|
|
544
|
+
engine(name) {
|
|
545
|
+
this.#options.engine = name;
|
|
546
|
+
return this;
|
|
547
|
+
}
|
|
548
|
+
/** MySQL default charset (Lucid/Knex `charset`). Ignored on pg/sqlite. */
|
|
549
|
+
charset(name) {
|
|
550
|
+
this.#options.charset = name;
|
|
551
|
+
return this;
|
|
552
|
+
}
|
|
553
|
+
/** MySQL default collation for the table. Named `tableCollate` because {@link collate} is the column modifier — see {@link comment}. */
|
|
554
|
+
tableCollate(name) {
|
|
555
|
+
this.#options.collate = name;
|
|
556
|
+
return this;
|
|
557
|
+
}
|
|
558
|
+
/** Table comment. Named `tableComment` because {@link comment} is the column modifier — see there for why. */
|
|
559
|
+
tableComment(text) {
|
|
560
|
+
this.#options.comment = text;
|
|
561
|
+
return this;
|
|
562
|
+
}
|
|
179
563
|
// ─── Indexes ──────────────────────────────────────────────
|
|
180
564
|
index(columns, name) {
|
|
181
565
|
const cols = Array.isArray(columns) ? columns : [columns];
|
|
@@ -195,6 +579,78 @@ export class TableBuilder {
|
|
|
195
579
|
});
|
|
196
580
|
return this;
|
|
197
581
|
}
|
|
582
|
+
// ─── ALTER TABLE operations ───────────────────────────────
|
|
583
|
+
/**
|
|
584
|
+
* Apply the pending column definition as a type change instead of an
|
|
585
|
+
* `ADD COLUMN` (Lucid/Knex `alter()`). Must follow a column-type method.
|
|
586
|
+
*
|
|
587
|
+
* Nullability moves only if `.nullable()` / `.notNullable()` was called
|
|
588
|
+
* before this — a bare `t.string('x').alter()` changes the type and leaves
|
|
589
|
+
* the NOT NULL constraint exactly as it is.
|
|
590
|
+
*
|
|
591
|
+
* SQLite cannot alter a column in place; the Rust compiler rejects it with
|
|
592
|
+
* `E_UNSUPPORTED` rather than emitting a table rebuild behind your back.
|
|
593
|
+
*/
|
|
594
|
+
alter() {
|
|
595
|
+
this.#assertAlterMode("alter()");
|
|
596
|
+
const pending = this.#currentOp;
|
|
597
|
+
if (!pending) {
|
|
598
|
+
throw new Error("E_ALTER_MISUSE: alter() must follow a column definition, e.g. table.string('email').alter()");
|
|
599
|
+
}
|
|
600
|
+
if (pending.op === "addColumn") {
|
|
601
|
+
const converted = {
|
|
602
|
+
op: "alterColumn",
|
|
603
|
+
column: pending.column,
|
|
604
|
+
setNullable: this.#nullabilityTouched
|
|
605
|
+
? pending.column.nullable
|
|
606
|
+
: undefined,
|
|
607
|
+
};
|
|
608
|
+
this.#operations[this.#operations.indexOf(pending)] = converted;
|
|
609
|
+
this.#currentOp = converted;
|
|
610
|
+
}
|
|
611
|
+
return this;
|
|
612
|
+
}
|
|
613
|
+
/** Drop a column (Lucid/Knex `dropColumn`). */
|
|
614
|
+
dropColumn(name) {
|
|
615
|
+
this.#assertAlterMode("dropColumn()");
|
|
616
|
+
this.#pushStandaloneOp({ op: "dropColumn", name });
|
|
617
|
+
return this;
|
|
618
|
+
}
|
|
619
|
+
/** Drop several columns in call order (Lucid/Knex `dropColumns`). */
|
|
620
|
+
dropColumns(...names) {
|
|
621
|
+
for (const name of names)
|
|
622
|
+
this.dropColumn(name);
|
|
623
|
+
return this;
|
|
624
|
+
}
|
|
625
|
+
/** Rename a column (Lucid/Knex `renameColumn`). */
|
|
626
|
+
renameColumn(from, to) {
|
|
627
|
+
this.#assertAlterMode("renameColumn()");
|
|
628
|
+
this.#pushStandaloneOp({ op: "renameColumn", from, to });
|
|
629
|
+
return this;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Make an existing column nullable — `DROP NOT NULL` (Lucid/Knex
|
|
633
|
+
* `setNullable`).
|
|
634
|
+
*
|
|
635
|
+
* **Deviation from Knex, named deliberately.** Knex supports this on every
|
|
636
|
+
* dialect by querying `columnInfo()` at runtime to recover the column's
|
|
637
|
+
* type. Atlas compiles SQL synchronously in Rust with no round-trip, so
|
|
638
|
+
* this is Postgres-only — Postgres is the one dialect whose syntax needs no
|
|
639
|
+
* type. On MySQL use `table.<type>('col').nullable().alter()`, which
|
|
640
|
+
* restates the type; SQLite cannot alter a column in place at all. Both
|
|
641
|
+
* raise `E_UNSUPPORTED` with the alternative spelled out.
|
|
642
|
+
*/
|
|
643
|
+
setNullable(name) {
|
|
644
|
+
this.#assertAlterMode("setNullable()");
|
|
645
|
+
this.#pushStandaloneOp({ op: "setNullable", name, nullable: true });
|
|
646
|
+
return this;
|
|
647
|
+
}
|
|
648
|
+
/** Make an existing column `NOT NULL` (Lucid/Knex `dropNullable`). Postgres-only — see {@link setNullable}. */
|
|
649
|
+
dropNullable(name) {
|
|
650
|
+
this.#assertAlterMode("dropNullable()");
|
|
651
|
+
this.#pushStandaloneOp({ op: "setNullable", name, nullable: false });
|
|
652
|
+
return this;
|
|
653
|
+
}
|
|
198
654
|
// ─── Accessors ────────────────────────────────────────────
|
|
199
655
|
getColumns() {
|
|
200
656
|
return [...this.#columns];
|
|
@@ -202,32 +658,125 @@ export class TableBuilder {
|
|
|
202
658
|
getIndexes() {
|
|
203
659
|
return [...this.#indexes];
|
|
204
660
|
}
|
|
661
|
+
/** Ordered ALTER TABLE operations. Empty in `create` mode. */
|
|
662
|
+
getOperations() {
|
|
663
|
+
return [...this.#operations];
|
|
664
|
+
}
|
|
205
665
|
/** Compile to SQL statements via the Rust compiler. */
|
|
206
|
-
toStatements(dialect = getAtlasDialect()) {
|
|
207
|
-
const spec =
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
666
|
+
toStatements(dialect = getAtlasDialect(), options = {}) {
|
|
667
|
+
const spec = this.mode === "alter"
|
|
668
|
+
? {
|
|
669
|
+
kind: "alterTable",
|
|
670
|
+
table: this.tableName,
|
|
671
|
+
operations: this.#operations.map((op) => op.op === "addColumn"
|
|
672
|
+
? { op: op.op, column: this.#serializeColumn(op.column) }
|
|
673
|
+
: op.op === "alterColumn"
|
|
674
|
+
? {
|
|
675
|
+
op: op.op,
|
|
676
|
+
column: this.#serializeColumn(op.column),
|
|
677
|
+
setNullable: op.setNullable ?? null,
|
|
678
|
+
}
|
|
679
|
+
: op),
|
|
680
|
+
}
|
|
681
|
+
: {
|
|
682
|
+
kind: "createTable",
|
|
683
|
+
table: this.tableName,
|
|
684
|
+
columns: this.#columns.map((c) => this.#serializeColumn(c)),
|
|
685
|
+
indexes: this.#indexes.map((i) => ({
|
|
686
|
+
name: i.name,
|
|
687
|
+
columns: i.columns,
|
|
688
|
+
unique: i.unique,
|
|
689
|
+
})),
|
|
690
|
+
ifNotExists: options.ifNotExists ?? false,
|
|
691
|
+
constraints: this.#constraints,
|
|
692
|
+
options: this.#options,
|
|
693
|
+
};
|
|
694
|
+
const statements = compileStatementNative(spec, dialect).statements;
|
|
695
|
+
// `alterTable` carries no index list — an index added alongside an
|
|
696
|
+
// ALTER compiles to its own CREATE INDEX, appended in declaration order.
|
|
697
|
+
if (this.mode === "alter" && this.#indexes.length > 0) {
|
|
698
|
+
for (const idx of this.#indexes) {
|
|
699
|
+
statements.push(...compileStatementNative({
|
|
700
|
+
kind: "createIndex",
|
|
701
|
+
table: this.tableName,
|
|
702
|
+
name: idx.name,
|
|
703
|
+
columns: idx.columns,
|
|
704
|
+
unique: idx.unique,
|
|
705
|
+
}, dialect).statements);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return statements;
|
|
709
|
+
}
|
|
710
|
+
/** Flatten a column into the wire shape the Rust `ColumnDef` deserialises. */
|
|
711
|
+
#serializeColumn(c) {
|
|
712
|
+
return {
|
|
713
|
+
name: c.name,
|
|
714
|
+
kind: TYPE_KIND_MAP[c.type],
|
|
715
|
+
length: c.length ?? null,
|
|
716
|
+
precision: c.precision ?? null,
|
|
717
|
+
scale: c.scale ?? null,
|
|
718
|
+
// Flattened into the Rust ColumnTypeSpec — each read for one kind only.
|
|
719
|
+
values: c.values ?? null,
|
|
720
|
+
rawType: c.rawType ?? null,
|
|
721
|
+
nullable: c.nullable,
|
|
722
|
+
primary: c.primary,
|
|
723
|
+
autoIncrement: c.autoIncrement ?? false,
|
|
724
|
+
unique: c.unique,
|
|
725
|
+
unsigned: c.unsigned ?? false,
|
|
726
|
+
default: c.defaultValue ?? null,
|
|
727
|
+
references: c.references ?? null,
|
|
728
|
+
comment: c.comment ?? null,
|
|
729
|
+
collate: c.collate ?? null,
|
|
730
|
+
position: c.position ?? null,
|
|
229
731
|
};
|
|
230
|
-
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Record a constraint. In `create` mode it renders inside the
|
|
735
|
+
* `CREATE TABLE`; in `alter` mode it becomes an `ADD CONSTRAINT`, kept in
|
|
736
|
+
* call order with the surrounding column operations.
|
|
737
|
+
*/
|
|
738
|
+
#pushConstraint(spec) {
|
|
739
|
+
if (this.mode === "alter") {
|
|
740
|
+
this.#operations.push({ op: "addConstraint", constraint: spec });
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
this.#constraints.push(spec);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/** Build a CHECK against the pending column. */
|
|
747
|
+
#addCheck(build, constraintName) {
|
|
748
|
+
const column = this.#currentColumn?.name;
|
|
749
|
+
if (!column) {
|
|
750
|
+
throw new Error("E_CHECK_MISUSE: a check* helper must follow a column definition, e.g. table.integer('qty').checkPositive()");
|
|
751
|
+
}
|
|
752
|
+
this.#pushConstraint({
|
|
753
|
+
constraint: "check",
|
|
754
|
+
name: constraintName,
|
|
755
|
+
expr: build(column),
|
|
756
|
+
});
|
|
757
|
+
return this;
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Default constraint name, following Knex's `<table>_<columns>_<suffix>`
|
|
761
|
+
* convention so `unique([...])` and `dropUnique([...])` agree without the
|
|
762
|
+
* caller naming anything. Distinct from {@link uniqueIndex}, which names a
|
|
763
|
+
* separate INDEX object `idx_…`.
|
|
764
|
+
*/
|
|
765
|
+
#constraintName(columns, suffix) {
|
|
766
|
+
const cols = typeof columns === "string" ? [columns] : columns;
|
|
767
|
+
return `${this.tableName}_${cols.join("_")}_${suffix}`;
|
|
768
|
+
}
|
|
769
|
+
#assertAlterMode(method) {
|
|
770
|
+
if (this.mode !== "alter") {
|
|
771
|
+
throw new Error(`E_ALTER_MISUSE: ${method} is only available inside schema.alterTable() — a new table has nothing to alter`);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
/** Record an op that takes no column modifiers, so `.nullable()` etc. can't silently attach to it. */
|
|
775
|
+
#pushStandaloneOp(op) {
|
|
776
|
+
this.#operations.push(op);
|
|
777
|
+
this.#currentColumn = undefined;
|
|
778
|
+
this.#currentOp = undefined;
|
|
779
|
+
this.#nullabilityTouched = false;
|
|
231
780
|
}
|
|
232
781
|
#addColumn(name, type) {
|
|
233
782
|
const col = {
|
|
@@ -239,6 +788,23 @@ export class TableBuilder {
|
|
|
239
788
|
};
|
|
240
789
|
this.#columns.push(col);
|
|
241
790
|
this.#currentColumn = col;
|
|
791
|
+
this.#nullabilityTouched = false;
|
|
792
|
+
if (this.mode === "alter") {
|
|
793
|
+
const op = {
|
|
794
|
+
op: "addColumn",
|
|
795
|
+
column: col,
|
|
796
|
+
};
|
|
797
|
+
this.#operations.push(op);
|
|
798
|
+
this.#currentOp = op;
|
|
799
|
+
}
|
|
800
|
+
return this;
|
|
801
|
+
}
|
|
802
|
+
#addFloat(name, type, precision, scale) {
|
|
803
|
+
this.#addColumn(name, type);
|
|
804
|
+
if (this.#currentColumn) {
|
|
805
|
+
this.#currentColumn.precision = precision;
|
|
806
|
+
this.#currentColumn.scale = scale;
|
|
807
|
+
}
|
|
242
808
|
return this;
|
|
243
809
|
}
|
|
244
810
|
#addIncrements(name, type) {
|