@remix-run/data-table 0.0.0 → 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.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +549 -2
  3. package/dist/index.d.ts +15 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +7 -0
  6. package/dist/lib/adapter.d.ts +550 -0
  7. package/dist/lib/adapter.d.ts.map +1 -0
  8. package/dist/lib/adapter.js +1 -0
  9. package/dist/lib/column.d.ts +193 -0
  10. package/dist/lib/column.d.ts.map +1 -0
  11. package/dist/lib/column.js +302 -0
  12. package/dist/lib/database/execution-context.d.ts +10 -0
  13. package/dist/lib/database/execution-context.d.ts.map +1 -0
  14. package/dist/lib/database/execution-context.js +1 -0
  15. package/dist/lib/database/helpers.d.ts +26 -0
  16. package/dist/lib/database/helpers.d.ts.map +1 -0
  17. package/dist/lib/database/helpers.js +116 -0
  18. package/dist/lib/database/query-execution.d.ts +7 -0
  19. package/dist/lib/database/query-execution.d.ts.map +1 -0
  20. package/dist/lib/database/query-execution.js +401 -0
  21. package/dist/lib/database/relations.d.ts +4 -0
  22. package/dist/lib/database/relations.d.ts.map +1 -0
  23. package/dist/lib/database/relations.js +207 -0
  24. package/dist/lib/database/write-lifecycle.d.ts +13 -0
  25. package/dist/lib/database/write-lifecycle.d.ts.map +1 -0
  26. package/dist/lib/database/write-lifecycle.js +279 -0
  27. package/dist/lib/database.d.ts +264 -0
  28. package/dist/lib/database.d.ts.map +1 -0
  29. package/dist/lib/database.js +319 -0
  30. package/dist/lib/errors.d.ts +59 -0
  31. package/dist/lib/errors.d.ts.map +1 -0
  32. package/dist/lib/errors.js +76 -0
  33. package/dist/lib/inflection.d.ts +3 -0
  34. package/dist/lib/inflection.d.ts.map +1 -0
  35. package/dist/lib/inflection.js +56 -0
  36. package/dist/lib/migrations/filename.d.ts +12 -0
  37. package/dist/lib/migrations/filename.d.ts.map +1 -0
  38. package/dist/lib/migrations/filename.js +20 -0
  39. package/dist/lib/migrations/helpers.d.ts +11 -0
  40. package/dist/lib/migrations/helpers.d.ts.map +1 -0
  41. package/dist/lib/migrations/helpers.js +77 -0
  42. package/dist/lib/migrations/journal-store.d.ts +15 -0
  43. package/dist/lib/migrations/journal-store.d.ts.map +1 -0
  44. package/dist/lib/migrations/journal-store.js +83 -0
  45. package/dist/lib/migrations/registry.d.ts +27 -0
  46. package/dist/lib/migrations/registry.d.ts.map +1 -0
  47. package/dist/lib/migrations/registry.js +51 -0
  48. package/dist/lib/migrations/runner.d.ts +20 -0
  49. package/dist/lib/migrations/runner.d.ts.map +1 -0
  50. package/dist/lib/migrations/runner.js +273 -0
  51. package/dist/lib/migrations/schema-api.d.ts +7 -0
  52. package/dist/lib/migrations/schema-api.d.ts.map +1 -0
  53. package/dist/lib/migrations/schema-api.js +326 -0
  54. package/dist/lib/migrations-node.d.ts +17 -0
  55. package/dist/lib/migrations-node.d.ts.map +1 -0
  56. package/dist/lib/migrations-node.js +65 -0
  57. package/dist/lib/migrations.d.ts +292 -0
  58. package/dist/lib/migrations.d.ts.map +1 -0
  59. package/dist/lib/migrations.js +38 -0
  60. package/dist/lib/operators.d.ts +154 -0
  61. package/dist/lib/operators.d.ts.map +1 -0
  62. package/dist/lib/operators.js +218 -0
  63. package/dist/lib/query.d.ts +159 -0
  64. package/dist/lib/query.d.ts.map +1 -0
  65. package/dist/lib/query.js +401 -0
  66. package/dist/lib/references.d.ts +41 -0
  67. package/dist/lib/references.d.ts.map +1 -0
  68. package/dist/lib/references.js +33 -0
  69. package/dist/lib/sql-helpers.d.ts +50 -0
  70. package/dist/lib/sql-helpers.d.ts.map +1 -0
  71. package/dist/lib/sql-helpers.js +111 -0
  72. package/dist/lib/sql.d.ts +45 -0
  73. package/dist/lib/sql.d.ts.map +1 -0
  74. package/dist/lib/sql.js +65 -0
  75. package/dist/lib/table.d.ts +569 -0
  76. package/dist/lib/table.d.ts.map +1 -0
  77. package/dist/lib/table.js +519 -0
  78. package/dist/lib/types.d.ts +4 -0
  79. package/dist/lib/types.d.ts.map +1 -0
  80. package/dist/lib/types.js +1 -0
  81. package/dist/migrations/node.d.ts +2 -0
  82. package/dist/migrations/node.d.ts.map +1 -0
  83. package/dist/migrations/node.js +1 -0
  84. package/dist/migrations.d.ts +8 -0
  85. package/dist/migrations.d.ts.map +1 -0
  86. package/dist/migrations.js +5 -0
  87. package/dist/operators.d.ts +3 -0
  88. package/dist/operators.d.ts.map +1 -0
  89. package/dist/operators.js +1 -0
  90. package/dist/sql-helpers.d.ts +3 -0
  91. package/dist/sql-helpers.d.ts.map +1 -0
  92. package/dist/sql-helpers.js +1 -0
  93. package/package.json +56 -7
  94. package/src/index.ts +198 -0
  95. package/src/lib/adapter.ts +653 -0
  96. package/src/lib/column.ts +384 -0
  97. package/src/lib/database/execution-context.ts +15 -0
  98. package/src/lib/database/helpers.ts +216 -0
  99. package/src/lib/database/query-execution.ts +638 -0
  100. package/src/lib/database/relations.ts +332 -0
  101. package/src/lib/database/write-lifecycle.ts +487 -0
  102. package/src/lib/database.ts +856 -0
  103. package/src/lib/errors.ts +119 -0
  104. package/src/lib/inflection.ts +69 -0
  105. package/src/lib/migrations/filename.ts +25 -0
  106. package/src/lib/migrations/helpers.ts +108 -0
  107. package/src/lib/migrations/journal-store.ts +122 -0
  108. package/src/lib/migrations/registry.ts +62 -0
  109. package/src/lib/migrations/runner.ts +374 -0
  110. package/src/lib/migrations/schema-api.ts +417 -0
  111. package/src/lib/migrations-node.ts +71 -0
  112. package/src/lib/migrations.ts +328 -0
  113. package/src/lib/operators.ts +436 -0
  114. package/src/lib/query.ts +958 -0
  115. package/src/lib/references.ts +78 -0
  116. package/src/lib/sql-helpers.ts +146 -0
  117. package/src/lib/sql.ts +84 -0
  118. package/src/lib/table.ts +1309 -0
  119. package/src/lib/types.ts +3 -0
  120. package/src/migrations/node.ts +1 -0
  121. package/src/migrations.ts +26 -0
  122. package/src/operators.ts +18 -0
  123. package/src/sql-helpers.ts +9 -0
@@ -0,0 +1,326 @@
1
+ import { rawSql } from "../sql.js";
2
+ import { getTableColumnDefinitions, getTableName, getTablePrimaryKey } from "../table.js";
3
+ import { ColumnBuilder } from "../column.js";
4
+ import { createCheckName, createForeignKeyName, createIndexName, createPrimaryKeyName, createUniqueName, normalizeIndexColumns, normalizeKeyColumns, toTableRef, } from "./helpers.js";
5
+ function asColumnDefinition(definition) {
6
+ if (definition instanceof ColumnBuilder) {
7
+ return definition.build();
8
+ }
9
+ return definition;
10
+ }
11
+ function asTableRef(value) {
12
+ if (typeof value === 'string') {
13
+ return toTableRef(value);
14
+ }
15
+ return toTableRef(getTableName(value));
16
+ }
17
+ function lowerTableForCreate(table) {
18
+ let tableRef = toTableRef(getTableName(table));
19
+ let sourceColumnDefinitions = getTableColumnDefinitions(table);
20
+ let columns = {};
21
+ let uniques = [];
22
+ let checks = [];
23
+ let foreignKeys = [];
24
+ for (let columnName in sourceColumnDefinitions) {
25
+ if (!Object.prototype.hasOwnProperty.call(sourceColumnDefinitions, columnName)) {
26
+ continue;
27
+ }
28
+ let sourceDefinition = sourceColumnDefinitions[columnName];
29
+ let columnDefinition = {
30
+ ...sourceDefinition,
31
+ checks: undefined,
32
+ references: undefined,
33
+ primaryKey: undefined,
34
+ };
35
+ let unique = sourceDefinition.unique;
36
+ if (unique) {
37
+ let uniqueName = typeof unique === 'object' && unique.name
38
+ ? unique.name
39
+ : createUniqueName(tableRef, [columnName]);
40
+ uniques.push({
41
+ name: uniqueName,
42
+ columns: [columnName],
43
+ });
44
+ columnDefinition.unique = undefined;
45
+ }
46
+ if (sourceDefinition.checks) {
47
+ for (let check of sourceDefinition.checks) {
48
+ checks.push({
49
+ name: check.name || createCheckName(tableRef, check.expression),
50
+ expression: check.expression,
51
+ });
52
+ }
53
+ }
54
+ if (sourceDefinition.references) {
55
+ let referenceColumns = [...sourceDefinition.references.columns];
56
+ let referencesTable = { ...sourceDefinition.references.table };
57
+ foreignKeys.push({
58
+ name: sourceDefinition.references.name ||
59
+ createForeignKeyName(tableRef, [columnName], referencesTable, referenceColumns),
60
+ columns: [columnName],
61
+ references: {
62
+ table: referencesTable,
63
+ columns: referenceColumns,
64
+ },
65
+ onDelete: sourceDefinition.references.onDelete,
66
+ onUpdate: sourceDefinition.references.onUpdate,
67
+ });
68
+ }
69
+ columns[columnName] = columnDefinition;
70
+ }
71
+ let primaryKeyColumns = [...getTablePrimaryKey(table)];
72
+ let primaryKey;
73
+ if (primaryKeyColumns.length > 0) {
74
+ primaryKey = {
75
+ columns: primaryKeyColumns,
76
+ name: createPrimaryKeyName(tableRef),
77
+ };
78
+ }
79
+ return {
80
+ kind: 'createTable',
81
+ table: tableRef,
82
+ columns,
83
+ primaryKey,
84
+ uniques: uniques.length > 0 ? uniques : undefined,
85
+ checks: checks.length > 0 ? checks : undefined,
86
+ foreignKeys: foreignKeys.length > 0 ? foreignKeys : undefined,
87
+ };
88
+ }
89
+ class AlterTableBuilderRuntime {
90
+ alterChanges = [];
91
+ extraStatements = [];
92
+ table;
93
+ constructor(table) {
94
+ this.table = table;
95
+ }
96
+ addColumn(name, definition) {
97
+ this.alterChanges.push({
98
+ kind: 'addColumn',
99
+ column: name,
100
+ definition: asColumnDefinition(definition),
101
+ });
102
+ }
103
+ changeColumn(name, definition) {
104
+ this.alterChanges.push({
105
+ kind: 'changeColumn',
106
+ column: name,
107
+ definition: asColumnDefinition(definition),
108
+ });
109
+ }
110
+ renameColumn(from, to) {
111
+ this.alterChanges.push({ kind: 'renameColumn', from, to });
112
+ }
113
+ dropColumn(name, options) {
114
+ this.alterChanges.push({ kind: 'dropColumn', column: name, ifExists: options?.ifExists });
115
+ }
116
+ addPrimaryKey(columns, options) {
117
+ let normalizedColumns = normalizeKeyColumns(columns);
118
+ this.alterChanges.push({
119
+ kind: 'addPrimaryKey',
120
+ constraint: {
121
+ columns: normalizedColumns,
122
+ name: options?.name ?? createPrimaryKeyName(this.table),
123
+ },
124
+ });
125
+ }
126
+ dropPrimaryKey(name) {
127
+ this.alterChanges.push({ kind: 'dropPrimaryKey', name });
128
+ }
129
+ addUnique(columns, options) {
130
+ let normalizedColumns = normalizeKeyColumns(columns);
131
+ this.alterChanges.push({
132
+ kind: 'addUnique',
133
+ constraint: {
134
+ columns: normalizedColumns,
135
+ name: options?.name ?? createUniqueName(this.table, normalizedColumns),
136
+ },
137
+ });
138
+ }
139
+ dropUnique(name) {
140
+ this.alterChanges.push({ kind: 'dropUnique', name });
141
+ }
142
+ addForeignKey(columns, refTable, refColumns, options) {
143
+ let normalizedColumns = normalizeKeyColumns(columns);
144
+ let normalizedReferenceColumns = refColumns ? normalizeKeyColumns(refColumns) : ['id'];
145
+ let referenceTable = asTableRef(refTable);
146
+ this.alterChanges.push({
147
+ kind: 'addForeignKey',
148
+ constraint: {
149
+ columns: normalizedColumns,
150
+ references: {
151
+ table: referenceTable,
152
+ columns: normalizedReferenceColumns,
153
+ },
154
+ name: options?.name ??
155
+ createForeignKeyName(this.table, normalizedColumns, referenceTable, normalizedReferenceColumns),
156
+ onDelete: options?.onDelete,
157
+ onUpdate: options?.onUpdate,
158
+ },
159
+ });
160
+ }
161
+ dropForeignKey(name) {
162
+ this.alterChanges.push({ kind: 'dropForeignKey', name });
163
+ }
164
+ addCheck(expression, options) {
165
+ this.alterChanges.push({
166
+ kind: 'addCheck',
167
+ constraint: {
168
+ expression,
169
+ name: options?.name ?? createCheckName(this.table, expression),
170
+ },
171
+ });
172
+ }
173
+ dropCheck(name) {
174
+ this.alterChanges.push({ kind: 'dropCheck', name });
175
+ }
176
+ addIndex(columns, options) {
177
+ let normalizedColumns = normalizeIndexColumns(columns);
178
+ let { name, ifNotExists, ...indexOptions } = options ?? {};
179
+ this.extraStatements.push({
180
+ kind: 'createIndex',
181
+ index: {
182
+ table: this.table,
183
+ name: name ?? createIndexName(this.table, normalizedColumns),
184
+ columns: normalizedColumns,
185
+ ...indexOptions,
186
+ },
187
+ ifNotExists,
188
+ });
189
+ }
190
+ dropIndex(name) {
191
+ this.extraStatements.push({
192
+ kind: 'dropIndex',
193
+ table: this.table,
194
+ name,
195
+ });
196
+ }
197
+ comment(text) {
198
+ this.alterChanges.push({ kind: 'setTableComment', comment: text });
199
+ }
200
+ }
201
+ export function createMigrationSchema(db, emit, options) {
202
+ return {
203
+ async createTable(table, options) {
204
+ let operation = lowerTableForCreate(table);
205
+ operation.ifNotExists = options?.ifNotExists;
206
+ await emit(operation);
207
+ },
208
+ async alterTable(input, migrate, options) {
209
+ let tableRef = asTableRef(input);
210
+ let builder = new AlterTableBuilderRuntime(tableRef);
211
+ migrate(builder);
212
+ if (builder.alterChanges.length > 0) {
213
+ await emit({
214
+ kind: 'alterTable',
215
+ table: tableRef,
216
+ changes: builder.alterChanges,
217
+ ifExists: options?.ifExists,
218
+ });
219
+ }
220
+ for (let operation of builder.extraStatements) {
221
+ await emit(operation);
222
+ }
223
+ },
224
+ async renameTable(from, to) {
225
+ await emit({ kind: 'renameTable', from: asTableRef(from), to: toTableRef(to) });
226
+ },
227
+ async dropTable(table, options) {
228
+ await emit({
229
+ kind: 'dropTable',
230
+ table: asTableRef(table),
231
+ ifExists: options?.ifExists,
232
+ cascade: options?.cascade,
233
+ });
234
+ },
235
+ async createIndex(table, columns, options) {
236
+ let tableRef = asTableRef(table);
237
+ let normalizedColumns = normalizeIndexColumns(columns);
238
+ let { name, ifNotExists, ...indexOptions } = options ?? {};
239
+ await emit({
240
+ kind: 'createIndex',
241
+ index: {
242
+ table: tableRef,
243
+ name: name ?? createIndexName(tableRef, normalizedColumns),
244
+ columns: normalizedColumns,
245
+ ...indexOptions,
246
+ },
247
+ ifNotExists,
248
+ });
249
+ },
250
+ async dropIndex(table, name, options) {
251
+ await emit({
252
+ kind: 'dropIndex',
253
+ table: asTableRef(table),
254
+ name,
255
+ ifExists: options?.ifExists,
256
+ });
257
+ },
258
+ async renameIndex(table, from, to) {
259
+ await emit({
260
+ kind: 'renameIndex',
261
+ table: asTableRef(table),
262
+ from,
263
+ to,
264
+ });
265
+ },
266
+ async addForeignKey(table, columns, refTable, refColumns, options) {
267
+ let tableRef = asTableRef(table);
268
+ let normalizedColumns = normalizeKeyColumns(columns);
269
+ let referenceTable = asTableRef(refTable);
270
+ let normalizedReferenceColumns = refColumns ? normalizeKeyColumns(refColumns) : ['id'];
271
+ await emit({
272
+ kind: 'addForeignKey',
273
+ table: tableRef,
274
+ constraint: {
275
+ columns: normalizedColumns,
276
+ references: {
277
+ table: referenceTable,
278
+ columns: normalizedReferenceColumns,
279
+ },
280
+ name: options?.name ??
281
+ createForeignKeyName(tableRef, normalizedColumns, referenceTable, normalizedReferenceColumns),
282
+ onDelete: options?.onDelete,
283
+ onUpdate: options?.onUpdate,
284
+ },
285
+ });
286
+ },
287
+ async dropForeignKey(table, name) {
288
+ await emit({
289
+ kind: 'dropForeignKey',
290
+ table: asTableRef(table),
291
+ name,
292
+ });
293
+ },
294
+ async addCheck(table, expression, options) {
295
+ let tableRef = asTableRef(table);
296
+ await emit({
297
+ kind: 'addCheck',
298
+ table: tableRef,
299
+ constraint: {
300
+ expression,
301
+ name: options?.name ?? createCheckName(tableRef, expression),
302
+ },
303
+ });
304
+ },
305
+ async dropCheck(table, name) {
306
+ await emit({
307
+ kind: 'dropCheck',
308
+ table: asTableRef(table),
309
+ name,
310
+ });
311
+ },
312
+ async plan(sql) {
313
+ let statement = typeof sql === 'string' ? rawSql(sql) : sql;
314
+ await emit({
315
+ kind: 'raw',
316
+ sql: statement,
317
+ });
318
+ },
319
+ async hasTable(table) {
320
+ return db.adapter.hasTable(asTableRef(table), options?.transaction);
321
+ },
322
+ async hasColumn(table, columnName) {
323
+ return db.adapter.hasColumn(asTableRef(table), columnName, options?.transaction);
324
+ },
325
+ };
326
+ }
@@ -0,0 +1,17 @@
1
+ import type { MigrationDescriptor } from './migrations.ts';
2
+ /**
3
+ * Loads migration modules from a directory on Node.js.
4
+ *
5
+ * Filenames are used to infer migration `id` and `name`.
6
+ * Each file must default-export `createMigration(...)`.
7
+ * @param directory Absolute or relative directory containing migration files.
8
+ * @returns A sorted list of loaded migration descriptors.
9
+ * @example
10
+ * ```ts
11
+ * import { loadMigrations } from 'remix/data-table/migrations/node'
12
+ *
13
+ * let migrations = await loadMigrations('./app/db/migrations')
14
+ * ```
15
+ */
16
+ export declare function loadMigrations(directory: string): Promise<MigrationDescriptor[]>;
17
+ //# sourceMappingURL=migrations-node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations-node.d.ts","sourceRoot":"","sources":["../../src/lib/migrations-node.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAa,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAGrE;;;;;;;;;;;;;GAaG;AACH,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAiDtF"}
@@ -0,0 +1,65 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ import { createHash } from 'node:crypto';
10
+ import { promises as fs } from 'node:fs';
11
+ import path from 'node:path';
12
+ import { pathToFileURL } from 'node:url';
13
+ import { parseMigrationFilename } from "./migrations/filename.js";
14
+ /**
15
+ * Loads migration modules from a directory on Node.js.
16
+ *
17
+ * Filenames are used to infer migration `id` and `name`.
18
+ * Each file must default-export `createMigration(...)`.
19
+ * @param directory Absolute or relative directory containing migration files.
20
+ * @returns A sorted list of loaded migration descriptors.
21
+ * @example
22
+ * ```ts
23
+ * import { loadMigrations } from 'remix/data-table/migrations/node'
24
+ *
25
+ * let migrations = await loadMigrations('./app/db/migrations')
26
+ * ```
27
+ */
28
+ export async function loadMigrations(directory) {
29
+ let allFiles = (await fs.readdir(directory, { withFileTypes: true }))
30
+ .filter((entry) => entry.isFile())
31
+ .map((entry) => entry.name)
32
+ .sort((left, right) => left.localeCompare(right));
33
+ let files = [];
34
+ for (let file of allFiles) {
35
+ if (!/\.(?:m?ts|m?js|cts|cjs)$/.test(file)) {
36
+ continue;
37
+ }
38
+ let parsed = parseMigrationFilename(file);
39
+ files.push({ file, id: parsed.id, name: parsed.name });
40
+ }
41
+ let migrations = [];
42
+ let seenIds = new Set();
43
+ for (let entry of files) {
44
+ if (seenIds.has(entry.id)) {
45
+ throw new Error('Duplicate migration id "' + entry.id + '" inferred from filename "' + entry.file + '"');
46
+ }
47
+ seenIds.add(entry.id);
48
+ let fullPath = path.join(directory, entry.file);
49
+ let source = await fs.readFile(fullPath, 'utf8');
50
+ let checksum = createHash('sha256').update(source).digest('hex');
51
+ let module = (await import(__rewriteRelativeImportExtension(pathToFileURL(fullPath).href)));
52
+ let migration = module.default;
53
+ if (!migration || typeof migration.up !== 'function' || typeof migration.down !== 'function') {
54
+ throw new Error('Migration file "' + entry.file + '" must default-export createMigration(...)');
55
+ }
56
+ migrations.push({
57
+ id: entry.id,
58
+ name: entry.name,
59
+ path: fullPath,
60
+ checksum,
61
+ migration,
62
+ });
63
+ }
64
+ return migrations;
65
+ }
@@ -0,0 +1,292 @@
1
+ import type { Database } from './database.ts';
2
+ import type { ColumnDefinition, ForeignKeyAction, IndexDefinition } from './adapter.ts';
3
+ import type { ColumnBuilder } from './column.ts';
4
+ import type { SqlStatement } from './sql.ts';
5
+ import type { AnyTable } from './table.ts';
6
+ /**
7
+ * Controls how each migration is wrapped in transactions.
8
+ */
9
+ export type MigrationTransactionMode = 'auto' | 'required' | 'none';
10
+ /**
11
+ * Runtime context passed to migration `up`/`down` handlers.
12
+ */
13
+ export type MigrationContext = {
14
+ /**
15
+ * Immediate data runtime (`query/create/update/exec/transaction`).
16
+ */
17
+ db: Database;
18
+ /**
19
+ * Migration schema runtime (`createTable/alterTable/createIndex/...`).
20
+ */
21
+ schema: MigrationSchema;
22
+ };
23
+ /**
24
+ * Authoring shape for `createMigration(...)`.
25
+ */
26
+ export type CreateMigrationInput = {
27
+ up: (context: MigrationContext) => Promise<void> | void;
28
+ down: (context: MigrationContext) => Promise<void> | void;
29
+ transaction?: MigrationTransactionMode;
30
+ };
31
+ /**
32
+ * Normalized migration object consumed by the registry/runner.
33
+ */
34
+ export type Migration = {
35
+ up: CreateMigrationInput['up'];
36
+ down: CreateMigrationInput['down'];
37
+ transaction: MigrationTransactionMode;
38
+ };
39
+ /**
40
+ * Creates a migration descriptor with normalized defaults.
41
+ * @param input Migration handlers and transaction mode.
42
+ * @returns A normalized migration object.
43
+ * @example
44
+ * ```ts
45
+ * import { createMigration, column as c } from 'remix/data-table/migrations'
46
+ * import { table } from 'remix/data-table'
47
+ *
48
+ * let users = table({
49
+ * name: 'users',
50
+ * columns: {
51
+ * id: c.integer().primaryKey().autoIncrement(),
52
+ * email: c.varchar(255).notNull().unique(),
53
+ * },
54
+ * })
55
+ *
56
+ * export default createMigration({
57
+ * async up({ db, schema }) {
58
+ * await schema.createTable(users)
59
+ *
60
+ * if (db.adapter.dialect === 'sqlite') {
61
+ * await db.exec('pragma foreign_keys = on')
62
+ * }
63
+ * },
64
+ * async down({ schema }) {
65
+ * await schema.dropTable('users', { ifExists: true })
66
+ * },
67
+ * })
68
+ * ```
69
+ */
70
+ export declare function createMigration(input: CreateMigrationInput): Migration;
71
+ /**
72
+ * Migration metadata stored in registries and returned by loaders.
73
+ */
74
+ export type MigrationDescriptor = {
75
+ id: string;
76
+ name: string;
77
+ path?: string;
78
+ checksum?: string;
79
+ migration: Migration;
80
+ };
81
+ /**
82
+ * Direction used by migration runner operations.
83
+ */
84
+ export type MigrationDirection = 'up' | 'down';
85
+ /**
86
+ * Row shape persisted in the migration journal table.
87
+ */
88
+ export type MigrationJournalRow = {
89
+ id: string;
90
+ name: string;
91
+ checksum: string;
92
+ batch: number;
93
+ appliedAt: Date;
94
+ };
95
+ /**
96
+ * Effective status for a known migration.
97
+ */
98
+ export type MigrationStatus = 'applied' | 'pending' | 'drifted';
99
+ /**
100
+ * Status row returned by `runner.status()` and `runner.up/down(...)`.
101
+ */
102
+ export type MigrationStatusEntry = {
103
+ id: string;
104
+ name: string;
105
+ status: MigrationStatus;
106
+ appliedAt?: Date;
107
+ batch?: number;
108
+ checksum?: string;
109
+ };
110
+ /**
111
+ * Common options for `runner.up(...)` and `runner.down(...)`.
112
+ * `to` and `step` are mutually exclusive.
113
+ */
114
+ export type MigrateOptions = {
115
+ to: string;
116
+ step?: never;
117
+ dryRun?: boolean;
118
+ } | {
119
+ to?: never;
120
+ step: number;
121
+ dryRun?: boolean;
122
+ } | {
123
+ to?: undefined;
124
+ step?: undefined;
125
+ dryRun?: boolean;
126
+ };
127
+ /**
128
+ * Result shape returned by migration runner commands.
129
+ */
130
+ export type MigrateResult = {
131
+ applied: MigrationStatusEntry[];
132
+ reverted: MigrationStatusEntry[];
133
+ /**
134
+ * Compiled SQL statements for operations processed during this run.
135
+ * Includes planned SQL when running with `dryRun: true`.
136
+ */
137
+ sql: SqlStatement[];
138
+ };
139
+ /**
140
+ * Options for `schema.createTable(...)` migration operations.
141
+ */
142
+ export type CreateTableOptions = {
143
+ ifNotExists?: boolean;
144
+ };
145
+ /**
146
+ * Options for `schema.alterTable(...)` migration operations.
147
+ */
148
+ export type AlterTableOptions = {
149
+ ifExists?: boolean;
150
+ };
151
+ /**
152
+ * Options for `schema.dropTable(...)` migration operations.
153
+ */
154
+ export type DropTableOptions = {
155
+ ifExists?: boolean;
156
+ cascade?: boolean;
157
+ };
158
+ /**
159
+ * Accepts either one index column or multiple (compound index).
160
+ */
161
+ export type IndexColumns = string | string[];
162
+ /**
163
+ * Accepts either one key column or multiple (compound key).
164
+ */
165
+ export type KeyColumns = string | string[];
166
+ /**
167
+ * Accepts either a SQL table name or a `table(...)` object.
168
+ */
169
+ export type TableInput = string | AnyTable;
170
+ /**
171
+ * Optional name override for constraints and indexes.
172
+ */
173
+ export type NamedConstraintOptions = {
174
+ name?: string;
175
+ };
176
+ /**
177
+ * Foreign key options for migration APIs.
178
+ */
179
+ export type ForeignKeyOptions = NamedConstraintOptions & {
180
+ onDelete?: ForeignKeyAction;
181
+ onUpdate?: ForeignKeyAction;
182
+ };
183
+ /**
184
+ * Index options for migration APIs.
185
+ */
186
+ export type CreateIndexOptions = NamedConstraintOptions & Omit<IndexDefinition, 'table' | 'name' | 'columns'> & {
187
+ ifNotExists?: boolean;
188
+ };
189
+ /**
190
+ * Builder API available inside `schema.alterTable(name, table => ...)`.
191
+ */
192
+ export interface AlterTableBuilder {
193
+ /** Adds a column during an `alterTable` migration. */
194
+ addColumn(name: string, definition: ColumnDefinition | ColumnBuilder): void;
195
+ /** Changes an existing column during an `alterTable` migration. */
196
+ changeColumn(name: string, definition: ColumnDefinition | ColumnBuilder): void;
197
+ /** Renames a column during an `alterTable` migration. */
198
+ renameColumn(from: string, to: string): void;
199
+ /** Drops a column during an `alterTable` migration. */
200
+ dropColumn(name: string, options?: {
201
+ ifExists?: boolean;
202
+ }): void;
203
+ /** Adds a primary key during an `alterTable` migration. */
204
+ addPrimaryKey(columns: KeyColumns, options?: NamedConstraintOptions): void;
205
+ /** Drops a primary key during an `alterTable` migration. */
206
+ dropPrimaryKey(name: string): void;
207
+ /** Adds a unique constraint during an `alterTable` migration. */
208
+ addUnique(columns: KeyColumns, options?: NamedConstraintOptions): void;
209
+ /** Drops a unique constraint during an `alterTable` migration. */
210
+ dropUnique(name: string): void;
211
+ /** Adds a foreign key during an `alterTable` migration. */
212
+ addForeignKey(columns: KeyColumns, refTable: TableInput, refColumns?: KeyColumns, options?: ForeignKeyOptions): void;
213
+ /** Drops a foreign key during an `alterTable` migration. */
214
+ dropForeignKey(name: string): void;
215
+ /** Adds a check constraint during an `alterTable` migration. */
216
+ addCheck(expression: string, options?: NamedConstraintOptions): void;
217
+ /** Drops a check constraint during an `alterTable` migration. */
218
+ dropCheck(name: string): void;
219
+ /** Adds an index during an `alterTable` migration. */
220
+ addIndex(columns: IndexColumns, options?: CreateIndexOptions): void;
221
+ /** Drops an index during an `alterTable` migration. */
222
+ dropIndex(name: string): void;
223
+ /** Sets the table comment during an `alterTable` migration. */
224
+ comment(text: string): void;
225
+ }
226
+ /**
227
+ * DDL-focused operations mixed into the migration `db` object.
228
+ */
229
+ export interface MigrationSchema {
230
+ /** Creates a table in the migration schema. */
231
+ createTable<table extends AnyTable>(table: table, options?: CreateTableOptions): Promise<void>;
232
+ /** Alters an existing table in the migration schema. */
233
+ alterTable(table: TableInput, migrate: (table: AlterTableBuilder) => void, options?: AlterTableOptions): Promise<void>;
234
+ /** Renames a table in the migration schema. */
235
+ renameTable(from: TableInput, to: string): Promise<void>;
236
+ /** Drops a table from the migration schema. */
237
+ dropTable(table: TableInput, options?: DropTableOptions): Promise<void>;
238
+ /** Creates an index in the migration schema. */
239
+ createIndex(table: TableInput, columns: IndexColumns, options?: CreateIndexOptions): Promise<void>;
240
+ /** Drops an index from the migration schema. */
241
+ dropIndex(table: TableInput, name: string, options?: {
242
+ ifExists?: boolean;
243
+ }): Promise<void>;
244
+ /** Renames an index in the migration schema. */
245
+ renameIndex(table: TableInput, from: string, to: string): Promise<void>;
246
+ /** Adds a foreign key in the migration schema. */
247
+ addForeignKey(table: TableInput, columns: KeyColumns, refTable: TableInput, refColumns?: KeyColumns, options?: ForeignKeyOptions): Promise<void>;
248
+ /** Drops a foreign key in the migration schema. */
249
+ dropForeignKey(table: TableInput, name: string): Promise<void>;
250
+ /** Adds a check constraint in the migration schema. */
251
+ addCheck(table: TableInput, expression: string, options?: NamedConstraintOptions): Promise<void>;
252
+ /** Drops a check constraint in the migration schema. */
253
+ dropCheck(table: TableInput, name: string): Promise<void>;
254
+ /**
255
+ * Adds raw SQL to the migration plan as a migration operation.
256
+ */
257
+ plan(sql: string | SqlStatement): Promise<void>;
258
+ /**
259
+ * Returns `true` when the table exists in the current database.
260
+ */
261
+ hasTable(table: TableInput): Promise<boolean>;
262
+ /**
263
+ * Returns `true` when the column exists on the given table.
264
+ */
265
+ hasColumn(table: TableInput, column: string): Promise<boolean>;
266
+ }
267
+ /**
268
+ * Runtime-agnostic migration registry abstraction.
269
+ */
270
+ export type MigrationRegistry = {
271
+ register(migration: MigrationDescriptor): void;
272
+ list(): MigrationDescriptor[];
273
+ };
274
+ /**
275
+ * Options for creating a migration runner.
276
+ */
277
+ export type MigrationRunnerOptions = {
278
+ /**
279
+ * Journal table used to record applied migrations.
280
+ * Defaults to `data_table_migrations`.
281
+ */
282
+ journalTable?: string;
283
+ };
284
+ /**
285
+ * Migration runner API for applying, reverting, and inspecting migration state.
286
+ */
287
+ export type MigrationRunner = {
288
+ up(options?: MigrateOptions): Promise<MigrateResult>;
289
+ down(options?: MigrateOptions): Promise<MigrateResult>;
290
+ status(): Promise<MigrationStatusEntry[]>;
291
+ };
292
+ //# sourceMappingURL=migrations.d.ts.map