@remix-run/data-table 0.2.1 → 0.4.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 (113) hide show
  1. package/README.md +251 -123
  2. package/dist/cli.d.ts +54 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +53 -0
  5. package/dist/index.d.ts +5 -4
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +7 -7
  8. package/dist/lib/column.d.ts +1 -1
  9. package/dist/lib/column.d.ts.map +1 -1
  10. package/dist/lib/column.js +10 -1
  11. package/dist/lib/database/execution-context.d.ts +5 -5
  12. package/dist/lib/database/execution-context.d.ts.map +1 -1
  13. package/dist/lib/database/execution-context.js +1 -0
  14. package/dist/lib/database/helpers.js +6 -6
  15. package/dist/lib/database/query-execution.d.ts.map +1 -1
  16. package/dist/lib/database/query-execution.js +17 -17
  17. package/dist/lib/database/relations.js +5 -5
  18. package/dist/lib/database/write-lifecycle.d.ts +2 -2
  19. package/dist/lib/database/write-lifecycle.d.ts.map +1 -1
  20. package/dist/lib/database/write-lifecycle.js +5 -5
  21. package/dist/lib/database.d.ts +74 -59
  22. package/dist/lib/database.d.ts.map +1 -1
  23. package/dist/lib/database.js +176 -83
  24. package/dist/lib/driver.d.ts +287 -0
  25. package/dist/lib/driver.d.ts.map +1 -0
  26. package/dist/lib/errors.d.ts +2 -2
  27. package/dist/lib/errors.d.ts.map +1 -1
  28. package/dist/lib/errors.js +4 -4
  29. package/dist/lib/migrations/directive.d.ts +12 -0
  30. package/dist/lib/migrations/directive.d.ts.map +1 -0
  31. package/dist/lib/migrations/directive.js +28 -0
  32. package/dist/lib/migrations/directory-name.d.ts +12 -0
  33. package/dist/lib/migrations/directory-name.d.ts.map +1 -0
  34. package/dist/lib/migrations/directory-name.js +18 -0
  35. package/dist/lib/migrations/journal-store.d.ts +7 -7
  36. package/dist/lib/migrations/journal-store.d.ts.map +1 -1
  37. package/dist/lib/migrations/journal-store.js +37 -28
  38. package/dist/lib/migrations/registry.d.ts +1 -1
  39. package/dist/lib/migrations/registry.js +1 -1
  40. package/dist/lib/migrations/runner.d.ts +12 -19
  41. package/dist/lib/migrations/runner.d.ts.map +1 -1
  42. package/dist/lib/migrations/runner.js +170 -175
  43. package/dist/lib/migrations-node.d.ts +26 -5
  44. package/dist/lib/migrations-node.d.ts.map +1 -1
  45. package/dist/lib/migrations-node.js +73 -41
  46. package/dist/lib/migrations.d.ts +63 -209
  47. package/dist/lib/migrations.d.ts.map +1 -1
  48. package/dist/lib/migrations.js +1 -38
  49. package/dist/lib/operators.d.ts +1 -1
  50. package/dist/lib/operators.js +1 -1
  51. package/dist/lib/query.d.ts +34 -1
  52. package/dist/lib/query.d.ts.map +1 -1
  53. package/dist/lib/query.js +32 -4
  54. package/dist/lib/sql-helpers.d.ts +1 -7
  55. package/dist/lib/sql-helpers.d.ts.map +1 -1
  56. package/dist/lib/sql-helpers.js +0 -16
  57. package/dist/lib/sql.d.ts +1 -1
  58. package/dist/lib/sql.js +1 -1
  59. package/dist/lib/table.d.ts +1 -1
  60. package/dist/lib/table.d.ts.map +1 -1
  61. package/dist/lib/table.js +5 -5
  62. package/dist/migrations/node.d.ts +1 -1
  63. package/dist/migrations/node.d.ts.map +1 -1
  64. package/dist/migrations/node.js +1 -1
  65. package/dist/migrations.d.ts +2 -6
  66. package/dist/migrations.d.ts.map +1 -1
  67. package/dist/migrations.js +2 -5
  68. package/dist/operators.js +1 -1
  69. package/dist/sql-helpers.d.ts +1 -1
  70. package/dist/sql-helpers.d.ts.map +1 -1
  71. package/dist/sql-helpers.js +1 -1
  72. package/package.json +13 -9
  73. package/src/cli.ts +120 -0
  74. package/src/index.ts +20 -54
  75. package/src/lib/column.ts +14 -2
  76. package/src/lib/database/execution-context.ts +10 -6
  77. package/src/lib/database/helpers.ts +1 -1
  78. package/src/lib/database/query-execution.ts +15 -11
  79. package/src/lib/database/write-lifecycle.ts +4 -4
  80. package/src/lib/database.ts +223 -96
  81. package/src/lib/driver.ts +333 -0
  82. package/src/lib/errors.ts +4 -4
  83. package/src/lib/migrations/directive.ts +38 -0
  84. package/src/lib/migrations/directory-name.ts +23 -0
  85. package/src/lib/migrations/journal-store.ts +42 -28
  86. package/src/lib/migrations/registry.ts +1 -1
  87. package/src/lib/migrations/runner.ts +224 -205
  88. package/src/lib/migrations-node.ts +80 -38
  89. package/src/lib/migrations.ts +62 -228
  90. package/src/lib/operators.ts +1 -1
  91. package/src/lib/query.ts +34 -1
  92. package/src/lib/sql-helpers.ts +1 -22
  93. package/src/lib/sql.ts +1 -1
  94. package/src/lib/table.ts +1 -1
  95. package/src/migrations/node.ts +1 -1
  96. package/src/migrations.ts +5 -16
  97. package/src/sql-helpers.ts +0 -1
  98. package/dist/lib/adapter.d.ts +0 -550
  99. package/dist/lib/adapter.d.ts.map +0 -1
  100. package/dist/lib/migrations/filename.d.ts +0 -12
  101. package/dist/lib/migrations/filename.d.ts.map +0 -1
  102. package/dist/lib/migrations/filename.js +0 -20
  103. package/dist/lib/migrations/helpers.d.ts +0 -11
  104. package/dist/lib/migrations/helpers.d.ts.map +0 -1
  105. package/dist/lib/migrations/helpers.js +0 -77
  106. package/dist/lib/migrations/schema-api.d.ts +0 -7
  107. package/dist/lib/migrations/schema-api.d.ts.map +0 -1
  108. package/dist/lib/migrations/schema-api.js +0 -326
  109. package/src/lib/adapter.ts +0 -653
  110. package/src/lib/migrations/filename.ts +0 -25
  111. package/src/lib/migrations/helpers.ts +0 -108
  112. package/src/lib/migrations/schema-api.ts +0 -417
  113. /package/dist/lib/{adapter.js → driver.js} +0 -0
@@ -1,16 +1,18 @@
1
- import { createHash } from 'node:crypto'
2
1
  import { promises as fs } from 'node:fs'
3
2
  import path from 'node:path'
4
- import { pathToFileURL } from 'node:url'
5
- import type { Migration, MigrationDescriptor } from './migrations.ts'
6
- import { parseMigrationFilename } from './migrations/filename.ts'
3
+
4
+ import type { MigrationDescriptor, Seed } from './migrations.ts'
5
+ import { parseMigrationDirectoryName } from './migrations/directory-name.ts'
7
6
 
8
7
  /**
9
- * Loads migration modules from a directory on Node.js.
8
+ * Loads SQL-file migrations from a directory on Node.js.
9
+ *
10
+ * Each migration is a directory named `YYYYMMDDHHmmss_<slug>` containing:
11
+ * - `up.sql` (required)
12
+ * - `down.sql` (optional; omit for irreversible migrations)
10
13
  *
11
- * Filenames are used to infer migration `id` and `name`.
12
- * Each file must default-export `createMigration(...)`.
13
- * @param directory Absolute or relative directory containing migration files.
14
+ * `id` and `name` are inferred from the directory name.
15
+ * @param directory Absolute or relative directory containing migration directories.
14
16
  * @returns A sorted list of loaded migration descriptors.
15
17
  * @example
16
18
  * ```ts
@@ -20,52 +22,92 @@ import { parseMigrationFilename } from './migrations/filename.ts'
20
22
  * ```
21
23
  */
22
24
  export async function loadMigrations(directory: string): Promise<MigrationDescriptor[]> {
23
- let allFiles = (await fs.readdir(directory, { withFileTypes: true }))
24
- .filter((entry) => entry.isFile())
25
+ let entries = await fs.readdir(directory, { withFileTypes: true })
26
+ let directories = entries
27
+ .filter((entry) => entry.isDirectory())
25
28
  .map((entry) => entry.name)
26
29
  .sort((left, right) => left.localeCompare(right))
27
- let files: Array<{ file: string; id: string; name: string }> = []
28
-
29
- for (let file of allFiles) {
30
- if (!/\.(?:m?ts|m?js|cts|cjs)$/.test(file)) {
31
- continue
32
- }
33
-
34
- let parsed = parseMigrationFilename(file)
35
- files.push({ file, id: parsed.id, name: parsed.name })
36
- }
37
30
 
38
31
  let migrations: MigrationDescriptor[] = []
39
32
  let seenIds = new Set<string>()
40
33
 
41
- for (let entry of files) {
42
- if (seenIds.has(entry.id)) {
34
+ for (let directoryName of directories) {
35
+ let parsed = parseMigrationDirectoryName(directoryName)
36
+
37
+ if (seenIds.has(parsed.id)) {
43
38
  throw new Error(
44
- 'Duplicate migration id "' + entry.id + '" inferred from filename "' + entry.file + '"',
39
+ 'Duplicate migration id "' +
40
+ parsed.id +
41
+ '" inferred from directory "' +
42
+ directoryName +
43
+ '"',
45
44
  )
46
45
  }
47
46
 
48
- seenIds.add(entry.id)
49
- let fullPath = path.join(directory, entry.file)
50
- let source = await fs.readFile(fullPath, 'utf8')
51
- let checksum = createHash('sha256').update(source).digest('hex')
52
- let module = (await import(pathToFileURL(fullPath).href)) as { default?: Migration }
53
- let migration = module.default
47
+ seenIds.add(parsed.id)
54
48
 
55
- if (!migration || typeof migration.up !== 'function' || typeof migration.down !== 'function') {
56
- throw new Error(
57
- 'Migration file "' + entry.file + '" must default-export createMigration(...)',
58
- )
49
+ let directoryPath = path.join(directory, directoryName)
50
+ let upPath = path.join(directoryPath, 'up.sql')
51
+ let downPath = path.join(directoryPath, 'down.sql')
52
+
53
+ let up: string
54
+ try {
55
+ up = await fs.readFile(upPath, 'utf8')
56
+ } catch (error) {
57
+ if (isNodeFileNotFoundError(error)) {
58
+ throw new Error('Migration directory "' + directoryName + '" is missing up.sql')
59
+ }
60
+ throw error
61
+ }
62
+
63
+ let down: string | undefined
64
+ try {
65
+ down = await fs.readFile(downPath, 'utf8')
66
+ } catch (error) {
67
+ if (!isNodeFileNotFoundError(error)) {
68
+ throw error
69
+ }
59
70
  }
60
71
 
61
72
  migrations.push({
62
- id: entry.id,
63
- name: entry.name,
64
- path: fullPath,
65
- checksum,
66
- migration,
73
+ id: parsed.id,
74
+ name: parsed.name,
75
+ up,
76
+ down,
77
+ path: directoryPath,
67
78
  })
68
79
  }
69
80
 
70
81
  return migrations
71
82
  }
83
+
84
+ /**
85
+ * Loads a SQL seed file on Node.js.
86
+ *
87
+ * The file may contain multiple SQL statements. Seeds that must be safe to
88
+ * run against an already-seeded database should use idempotent statements
89
+ * (for example, `insert or ignore` on SQLite).
90
+ *
91
+ * @param filename Absolute or relative path to a SQL seed file.
92
+ * @returns A seed function that executes the file's SQL script.
93
+ * @example
94
+ * ```ts
95
+ * import { loadSeed } from 'remix/data-table/migrations/node'
96
+ *
97
+ * let seed = await loadSeed('./app/data/seed.sql')
98
+ * await db.reset({ migrations, seed })
99
+ * ```
100
+ */
101
+ export async function loadSeed(filename: string): Promise<Seed> {
102
+ let sql = await fs.readFile(filename, 'utf8')
103
+ return (db) => db.executeScript(sql)
104
+ }
105
+
106
+ function isNodeFileNotFoundError(error: unknown): boolean {
107
+ return (
108
+ typeof error === 'object' &&
109
+ error !== null &&
110
+ 'code' in error &&
111
+ (error as { code?: unknown }).code === 'ENOENT'
112
+ )
113
+ }
@@ -1,98 +1,37 @@
1
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
2
 
7
3
  /**
8
4
  * Controls how each migration is wrapped in transactions.
9
- */
10
- export type MigrationTransactionMode = 'auto' | 'required' | 'none'
11
-
12
- /**
13
- * Runtime context passed to migration `up`/`down` handlers.
14
- */
15
- export type MigrationContext = {
16
- /**
17
- * Immediate data runtime (`query/create/update/exec/transaction`).
18
- */
19
- db: Database
20
- /**
21
- * Migration schema runtime (`createTable/alterTable/createIndex/...`).
22
- */
23
- schema: MigrationSchema
24
- }
25
-
26
- /**
27
- * Authoring shape for `createMigration(...)`.
28
- */
29
- export type CreateMigrationInput = {
30
- up: (context: MigrationContext) => Promise<void> | void
31
- down: (context: MigrationContext) => Promise<void> | void
32
- transaction?: MigrationTransactionMode
33
- }
34
-
35
- /**
36
- * Normalized migration object consumed by the registry/runner.
37
- */
38
- export type Migration = {
39
- up: CreateMigrationInput['up']
40
- down: CreateMigrationInput['down']
41
- transaction: MigrationTransactionMode
42
- }
43
-
44
- /**
45
- * Creates a migration descriptor with normalized defaults.
46
- * @param input Migration handlers and transaction mode.
47
- * @returns A normalized migration object.
48
- * @example
49
- * ```ts
50
- * import { createMigration, column as c } from 'remix/data-table/migrations'
51
- * import { table } from 'remix/data-table'
52
- *
53
- * let users = table({
54
- * name: 'users',
55
- * columns: {
56
- * id: c.integer().primaryKey().autoIncrement(),
57
- * email: c.varchar(255).notNull().unique(),
58
- * },
59
- * })
60
5
  *
61
- * export default createMigration({
62
- * async up({ db, schema }) {
63
- * await schema.createTable(users)
64
- *
65
- * if (db.adapter.dialect === 'sqlite') {
66
- * await db.exec('pragma foreign_keys = on')
67
- * }
68
- * },
69
- * async down({ schema }) {
70
- * await schema.dropTable('users', { ifExists: true })
71
- * },
72
- * })
73
- * ```
6
+ * - `auto` (default): wrap when the database supports transactional DDL.
7
+ * - `required`: wrap; throws when the database does not support transactional DDL.
8
+ * - `none`: never wrap.
74
9
  */
75
- export function createMigration(input: CreateMigrationInput): Migration {
76
- return {
77
- up: input.up,
78
- down: input.down,
79
- transaction: input.transaction ?? 'auto',
80
- }
81
- }
10
+ export type MigrationTransactionMode = 'auto' | 'required' | 'none'
82
11
 
83
12
  /**
84
- * Migration metadata stored in registries and returned by loaders.
13
+ * Migration metadata and SQL consumed by `Database.migrate()`.
85
14
  */
86
15
  export type MigrationDescriptor = {
16
+ /** Migration id (typically a `YYYYMMDDHHmmss` timestamp). */
87
17
  id: string
18
+ /** Human-readable migration slug. */
88
19
  name: string
20
+ /** SQL executed when applying the migration. May contain multiple statements. */
21
+ up: string
22
+ /**
23
+ * SQL executed when reverting the migration. May contain multiple statements.
24
+ * Omit (or pass `undefined`) for irreversible migrations.
25
+ */
26
+ down?: string
27
+ /** Transaction wrapping mode. Defaults to `auto`. */
28
+ transaction?: MigrationTransactionMode
29
+ /** Optional source path used in error messages. */
89
30
  path?: string
90
- checksum?: string
91
- migration: Migration
92
31
  }
93
32
 
94
33
  /**
95
- * Direction used by migration runner operations.
34
+ * Direction used by `Database.migrate()`.
96
35
  */
97
36
  export type MigrationDirection = 'up' | 'down'
98
37
 
@@ -108,12 +47,17 @@ export type MigrationJournalRow = {
108
47
  }
109
48
 
110
49
  /**
111
- * Effective status for a known migration.
50
+ * Effective migration status.
51
+ *
52
+ * - `applied`: the current migration matches its journal entry.
53
+ * - `pending`: the current migration has not been applied.
54
+ * - `drifted`: the current migration differs from its journal entry.
55
+ * - `missing`: an applied journal entry has no migration in the current set.
112
56
  */
113
- export type MigrationStatus = 'applied' | 'pending' | 'drifted'
57
+ export type MigrationStatus = 'applied' | 'pending' | 'drifted' | 'missing'
114
58
 
115
59
  /**
116
- * Status row returned by `runner.status()` and `runner.up/down(...)`.
60
+ * Status entry returned by database migration operations.
117
61
  */
118
62
  export type MigrationStatusEntry = {
119
63
  id: string
@@ -125,10 +69,13 @@ export type MigrationStatusEntry = {
125
69
  }
126
70
 
127
71
  /**
128
- * Common options for `runner.up(...)` and `runner.down(...)`.
72
+ * Bounds and dry-run options for a migration operation.
129
73
  * `to` and `step` are mutually exclusive.
74
+ *
75
+ * `to` accepts a bare migration id (`20260301113000`) or the full `id_name`
76
+ * directory form (`20260301113000_add_user_status`).
130
77
  */
131
- export type MigrateOptions =
78
+ export type MigrationOperationOptions =
132
79
  | {
133
80
  to: string
134
81
  step?: never
@@ -146,171 +93,52 @@ export type MigrateOptions =
146
93
  }
147
94
 
148
95
  /**
149
- * Result shape returned by migration runner commands.
96
+ * Result returned by `Database.migrate()`.
150
97
  */
151
98
  export type MigrateResult = {
152
99
  applied: MigrationStatusEntry[]
153
100
  reverted: MigrationStatusEntry[]
154
101
  /**
155
- * Compiled SQL statements for operations processed during this run.
156
- * Includes planned SQL when running with `dryRun: true`.
102
+ * SQL scripts that were (or, for `dryRun`, would have been) executed.
157
103
  */
158
- sql: SqlStatement[]
104
+ sql: string[]
159
105
  }
160
106
 
161
107
  /**
162
- * Options for `schema.createTable(...)` migration operations.
163
- */
164
- export type CreateTableOptions = { ifNotExists?: boolean }
165
- /**
166
- * Options for `schema.alterTable(...)` migration operations.
167
- */
168
- export type AlterTableOptions = { ifExists?: boolean }
169
- /**
170
- * Options for `schema.dropTable(...)` migration operations.
171
- */
172
- export type DropTableOptions = { ifExists?: boolean; cascade?: boolean }
173
- /**
174
- * Accepts either one index column or multiple (compound index).
175
- */
176
- export type IndexColumns = string | string[]
177
-
178
- /**
179
- * Accepts either one key column or multiple (compound key).
180
- */
181
- export type KeyColumns = string | string[]
182
-
183
- /**
184
- * Accepts either a SQL table name or a `table(...)` object.
185
- */
186
- export type TableInput = string | AnyTable
187
-
188
- /**
189
- * Optional name override for constraints and indexes.
190
- */
191
- export type NamedConstraintOptions = {
192
- name?: string
193
- }
194
-
195
- /**
196
- * Foreign key options for migration APIs.
108
+ * Runtime-agnostic migration registry abstraction.
197
109
  */
198
- export type ForeignKeyOptions = NamedConstraintOptions & {
199
- onDelete?: ForeignKeyAction
200
- onUpdate?: ForeignKeyAction
110
+ export type MigrationRegistry = {
111
+ register(migration: MigrationDescriptor): void
112
+ list(): MigrationDescriptor[]
201
113
  }
202
114
 
203
115
  /**
204
- * Index options for migration APIs.
116
+ * Migration collection accepted by `db.migrate(...)` and `db.migrationStatus(...)`.
205
117
  */
206
- export type CreateIndexOptions = NamedConstraintOptions &
207
- Omit<IndexDefinition, 'table' | 'name' | 'columns'> & {
208
- ifNotExists?: boolean
209
- }
118
+ export type Migrations = MigrationDescriptor[] | MigrationRegistry
210
119
 
211
120
  /**
212
- * Builder API available inside `schema.alterTable(name, table => ...)`.
121
+ * Function that initializes application data in a database.
213
122
  */
214
- export interface AlterTableBuilder {
215
- /** Adds a column during an `alterTable` migration. */
216
- addColumn(name: string, definition: ColumnDefinition | ColumnBuilder): void
217
- /** Changes an existing column during an `alterTable` migration. */
218
- changeColumn(name: string, definition: ColumnDefinition | ColumnBuilder): void
219
- /** Renames a column during an `alterTable` migration. */
220
- renameColumn(from: string, to: string): void
221
- /** Drops a column during an `alterTable` migration. */
222
- dropColumn(name: string, options?: { ifExists?: boolean }): void
223
- /** Adds a primary key during an `alterTable` migration. */
224
- addPrimaryKey(columns: KeyColumns, options?: NamedConstraintOptions): void
225
- /** Drops a primary key during an `alterTable` migration. */
226
- dropPrimaryKey(name: string): void
227
- /** Adds a unique constraint during an `alterTable` migration. */
228
- addUnique(columns: KeyColumns, options?: NamedConstraintOptions): void
229
- /** Drops a unique constraint during an `alterTable` migration. */
230
- dropUnique(name: string): void
231
- /** Adds a foreign key during an `alterTable` migration. */
232
- addForeignKey(
233
- columns: KeyColumns,
234
- refTable: TableInput,
235
- refColumns?: KeyColumns,
236
- options?: ForeignKeyOptions,
237
- ): void
238
- /** Drops a foreign key during an `alterTable` migration. */
239
- dropForeignKey(name: string): void
240
- /** Adds a check constraint during an `alterTable` migration. */
241
- addCheck(expression: string, options?: NamedConstraintOptions): void
242
- /** Drops a check constraint during an `alterTable` migration. */
243
- dropCheck(name: string): void
244
- /** Adds an index during an `alterTable` migration. */
245
- addIndex(columns: IndexColumns, options?: CreateIndexOptions): void
246
- /** Drops an index during an `alterTable` migration. */
247
- dropIndex(name: string): void
248
- /** Sets the table comment during an `alterTable` migration. */
249
- comment(text: string): void
250
- }
123
+ export type Seed = (db: Database) => void | Promise<void>
251
124
 
252
125
  /**
253
- * DDL-focused operations mixed into the migration `db` object.
126
+ * Options for applying or reverting migrations through `Database.migrate()`.
254
127
  */
255
- export interface MigrationSchema {
256
- /** Creates a table in the migration schema. */
257
- createTable<table extends AnyTable>(table: table, options?: CreateTableOptions): Promise<void>
258
- /** Alters an existing table in the migration schema. */
259
- alterTable(
260
- table: TableInput,
261
- migrate: (table: AlterTableBuilder) => void,
262
- options?: AlterTableOptions,
263
- ): Promise<void>
264
- /** Renames a table in the migration schema. */
265
- renameTable(from: TableInput, to: string): Promise<void>
266
- /** Drops a table from the migration schema. */
267
- dropTable(table: TableInput, options?: DropTableOptions): Promise<void>
268
- /** Creates an index in the migration schema. */
269
- createIndex(table: TableInput, columns: IndexColumns, options?: CreateIndexOptions): Promise<void>
270
- /** Drops an index from the migration schema. */
271
- dropIndex(table: TableInput, name: string, options?: { ifExists?: boolean }): Promise<void>
272
- /** Renames an index in the migration schema. */
273
- renameIndex(table: TableInput, from: string, to: string): Promise<void>
274
- /** Adds a foreign key in the migration schema. */
275
- addForeignKey(
276
- table: TableInput,
277
- columns: KeyColumns,
278
- refTable: TableInput,
279
- refColumns?: KeyColumns,
280
- options?: ForeignKeyOptions,
281
- ): Promise<void>
282
- /** Drops a foreign key in the migration schema. */
283
- dropForeignKey(table: TableInput, name: string): Promise<void>
284
- /** Adds a check constraint in the migration schema. */
285
- addCheck(table: TableInput, expression: string, options?: NamedConstraintOptions): Promise<void>
286
- /** Drops a check constraint in the migration schema. */
287
- dropCheck(table: TableInput, name: string): Promise<void>
288
- /**
289
- * Adds raw SQL to the migration plan as a migration operation.
290
- */
291
- plan(sql: string | SqlStatement): Promise<void>
128
+ export type DatabaseMigrateOptions = MigrationOperationOptions & {
129
+ /** Migration direction. Defaults to `up`. */
130
+ direction?: MigrationDirection
292
131
  /**
293
- * Returns `true` when the table exists in the current database.
294
- */
295
- hasTable(table: TableInput): Promise<boolean>
296
- /**
297
- * Returns `true` when the column exists on the given table.
132
+ * Journal table used to record applied migrations.
133
+ * Defaults to `data_table_migrations`.
298
134
  */
299
- hasColumn(table: TableInput, column: string): Promise<boolean>
300
- }
301
-
302
- /**
303
- * Runtime-agnostic migration registry abstraction.
304
- */
305
- export type MigrationRegistry = {
306
- register(migration: MigrationDescriptor): void
307
- list(): MigrationDescriptor[]
135
+ journalTable?: string
308
136
  }
309
137
 
310
138
  /**
311
- * Options for creating a migration runner.
139
+ * Options for reading migration status through `Database.migrationStatus()`.
312
140
  */
313
- export type MigrationRunnerOptions = {
141
+ export interface DatabaseMigrationStatusOptions {
314
142
  /**
315
143
  * Journal table used to record applied migrations.
316
144
  * Defaults to `data_table_migrations`.
@@ -319,10 +147,16 @@ export type MigrationRunnerOptions = {
319
147
  }
320
148
 
321
149
  /**
322
- * Migration runner API for applying, reverting, and inspecting migration state.
150
+ * Options for rebuilding a database through `Database.reset()`.
323
151
  */
324
- export type MigrationRunner = {
325
- up(options?: MigrateOptions): Promise<MigrateResult>
326
- down(options?: MigrateOptions): Promise<MigrateResult>
327
- status(): Promise<MigrationStatusEntry[]>
152
+ export interface DatabaseResetOptions {
153
+ /** Migrations to apply after wiping the database. */
154
+ migrations: Migrations
155
+ /** Function that initializes application data after migrations finish. */
156
+ seed?: Seed
157
+ /**
158
+ * Journal table used to record applied migrations.
159
+ * Defaults to `data_table_migrations`.
160
+ */
161
+ journalTable?: string
328
162
  }
@@ -22,7 +22,7 @@ type PredicateColumn<input extends string | ColumnReferenceLike> = NormalizeColu
22
22
  string
23
23
 
24
24
  /**
25
- * Normalized predicate representation consumed by adapters.
25
+ * Normalized predicate representation consumed by drivers.
26
26
  */
27
27
  export type Predicate<column extends string = string> =
28
28
  | {
package/src/lib/query.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { JoinClause, JoinType, SelectColumn } from './adapter.ts'
1
+ import type { JoinClause, JoinType, SelectColumn } from './driver.ts'
2
2
  import { DataTableQueryError, DataTableValidationError } from './errors.ts'
3
3
  import type {
4
4
  MergeColumnTypeMaps,
@@ -143,6 +143,11 @@ type QueryResultMap<row extends Record<string, unknown>, loaded extends Record<s
143
143
  upsert: WriteResult | WriteRowResult<row>
144
144
  }
145
145
 
146
+ /**
147
+ * Convenience alias for any {@link Query} regardless of its source, columns,
148
+ * row shape, loaded relations, or execution phase. Use this in helper APIs
149
+ * that accept a query but don't care about the specific generic parameters.
150
+ */
146
151
  export type AnyQuery = Query<any, any, any, any, any>
147
152
 
148
153
  type QuerySource<input extends AnyQuery> =
@@ -212,6 +217,17 @@ export const querySnapshot = Symbol('querySnapshot')
212
217
 
213
218
  declare const queryTypeBrand: unique symbol
214
219
 
220
+ /**
221
+ * Type-safe query builder for `@remix-run/data-table` sources. Construct via
222
+ * {@link query} and chain calls (`select`, `where`, `orderBy`, etc.) to build
223
+ * up a plan; run it by `await`-ing on a runtime-bound query, or by passing an
224
+ * unbound one to a runtime.
225
+ *
226
+ * The five generic parameters track, in order: the source/table, the column
227
+ * type map, the projected row shape, any loaded relations, and the
228
+ * binding/execution-mode phase. Most consumers do not need to spell them out
229
+ * — `query(table)` infers everything.
230
+ */
215
231
  export class Query<
216
232
  source extends AnyQuerySource,
217
233
  columnTypes extends Record<string, unknown> = QuerySourceColumnTypes<source>,
@@ -684,6 +700,23 @@ export class Query<
684
700
  }
685
701
  }
686
702
 
703
+ /**
704
+ * Begin a {@link Query} against a `@remix-run/data-table` source. The returned
705
+ * builder is in `'all'` execution mode and unbound; chain `select`, `where`,
706
+ * `orderBy`, etc. to refine the plan, and `await` it after binding it to a
707
+ * runtime to materialize results.
708
+ *
709
+ * @param table The table or source to query.
710
+ * @returns An unbound {@link Query} builder rooted at `table`.
711
+ *
712
+ * @example
713
+ * ```ts
714
+ * let activeUsers = await query(users)
715
+ * .where({ status: 'active' })
716
+ * .orderBy('createdAt', 'desc')
717
+ * .select(['id', 'email'])
718
+ * ```
719
+ */
687
720
  export function query<
688
721
  tableName extends string,
689
722
  row extends Record<string, unknown>,
@@ -1,31 +1,10 @@
1
- import type { DataManipulationOperation, DataMigrationOperation, TableRef } from './adapter.ts'
1
+ import type { TableRef } from './driver.ts'
2
2
 
3
3
  /**
4
4
  * Function used to quote SQL identifiers for a dialect.
5
5
  */
6
6
  export type QuoteIdentifier = (value: string) => string
7
7
 
8
- /**
9
- * Type guard that narrows an operation to the data-manipulation union.
10
- * @param operation Operation to inspect.
11
- * @returns `true` when the operation is a data-manipulation operation.
12
- */
13
- export function isDataManipulationOperation(
14
- operation: DataManipulationOperation | DataMigrationOperation,
15
- ): operation is DataManipulationOperation {
16
- return (
17
- operation.kind === 'select' ||
18
- operation.kind === 'count' ||
19
- operation.kind === 'exists' ||
20
- operation.kind === 'insert' ||
21
- operation.kind === 'insertMany' ||
22
- operation.kind === 'update' ||
23
- operation.kind === 'delete' ||
24
- operation.kind === 'upsert' ||
25
- operation.kind === 'raw'
26
- )
27
- }
28
-
29
8
  /**
30
9
  * Normalizes an arbitrary join type string into `inner`, `left`, or `right`.
31
10
  * @param type Input join type.
package/src/lib/sql.ts CHANGED
@@ -69,7 +69,7 @@ export function isSqlStatement(value: unknown): value is SqlStatement {
69
69
 
70
70
  /**
71
71
  * Creates a SQL statement from raw text and values.
72
- * @param text SQL text containing placeholders expected by the target adapter.
72
+ * @param text SQL text containing placeholders expected by the target database.
73
73
  * @param values Placeholder values.
74
74
  * @returns A normalized SQL statement.
75
75
  * @example
package/src/lib/table.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ColumnDefinition } from './adapter.ts'
1
+ import type { ColumnDefinition } from './driver.ts'
2
2
  import { ColumnBuilder } from './column.ts'
3
3
  import type { ColumnInput as ColumnBuilderInput, ColumnOutput } from './column.ts'
4
4
  import type { Predicate, WhereInput } from './operators.ts'
@@ -1 +1 @@
1
- export { loadMigrations } from '../lib/migrations-node.ts'
1
+ export { loadMigrations, loadSeed } from '../lib/migrations-node.ts'
package/src/migrations.ts CHANGED
@@ -1,26 +1,15 @@
1
1
  export type {
2
- AlterTableBuilder,
3
- CreateMigrationInput,
4
- Migration,
5
- MigrationContext,
2
+ DatabaseMigrateOptions,
3
+ DatabaseMigrationStatusOptions,
4
+ DatabaseResetOptions,
5
+ MigrateResult,
6
6
  MigrationDescriptor,
7
7
  MigrationDirection,
8
8
  MigrationJournalRow,
9
- MigrationSchema,
10
9
  MigrationRegistry,
11
- MigrationRunner,
12
- MigrationRunnerOptions,
13
10
  MigrationStatus,
14
11
  MigrationStatusEntry,
15
12
  MigrationTransactionMode,
16
- MigrateOptions,
17
- MigrateResult,
18
- KeyColumns,
19
- TableInput,
20
13
  } from './lib/migrations.ts'
21
- export { createMigration } from './lib/migrations.ts'
22
- export type { ColumnNamespace } from './lib/column.ts'
23
- export { ColumnBuilder, column } from './lib/column.ts'
24
14
  export { createMigrationRegistry } from './lib/migrations/registry.ts'
25
- export { createMigrationRunner } from './lib/migrations/runner.ts'
26
- export { parseMigrationFilename } from './lib/migrations/filename.ts'
15
+ export { parseMigrationDirectoryName } from './lib/migrations/directory-name.ts'