@remix-run/data-table 0.3.0 → 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 (82) hide show
  1. package/README.md +205 -79
  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/database/execution-context.d.ts +5 -5
  11. package/dist/lib/database/execution-context.d.ts.map +1 -1
  12. package/dist/lib/database/execution-context.js +1 -0
  13. package/dist/lib/database/helpers.js +6 -6
  14. package/dist/lib/database/query-execution.d.ts.map +1 -1
  15. package/dist/lib/database/query-execution.js +17 -17
  16. package/dist/lib/database/relations.js +5 -5
  17. package/dist/lib/database/write-lifecycle.d.ts +2 -2
  18. package/dist/lib/database/write-lifecycle.d.ts.map +1 -1
  19. package/dist/lib/database/write-lifecycle.js +5 -5
  20. package/dist/lib/database.d.ts +74 -59
  21. package/dist/lib/database.d.ts.map +1 -1
  22. package/dist/lib/database.js +176 -83
  23. package/dist/lib/{adapter.d.ts → driver.d.ts} +46 -48
  24. package/dist/lib/driver.d.ts.map +1 -0
  25. package/dist/lib/errors.d.ts +2 -2
  26. package/dist/lib/errors.d.ts.map +1 -1
  27. package/dist/lib/errors.js +4 -4
  28. package/dist/lib/migrations/journal-store.d.ts +6 -6
  29. package/dist/lib/migrations/journal-store.d.ts.map +1 -1
  30. package/dist/lib/migrations/journal-store.js +20 -11
  31. package/dist/lib/migrations/runner.d.ts +12 -19
  32. package/dist/lib/migrations/runner.d.ts.map +1 -1
  33. package/dist/lib/migrations/runner.js +151 -129
  34. package/dist/lib/migrations-node.d.ts +19 -1
  35. package/dist/lib/migrations-node.d.ts.map +1 -1
  36. package/dist/lib/migrations-node.js +22 -1
  37. package/dist/lib/migrations.d.ts +55 -20
  38. package/dist/lib/migrations.d.ts.map +1 -1
  39. package/dist/lib/operators.d.ts +1 -1
  40. package/dist/lib/operators.js +1 -1
  41. package/dist/lib/query.d.ts +1 -1
  42. package/dist/lib/query.d.ts.map +1 -1
  43. package/dist/lib/query.js +4 -4
  44. package/dist/lib/sql-helpers.d.ts +1 -1
  45. package/dist/lib/sql-helpers.d.ts.map +1 -1
  46. package/dist/lib/sql.d.ts +1 -1
  47. package/dist/lib/sql.js +1 -1
  48. package/dist/lib/table.d.ts +1 -1
  49. package/dist/lib/table.d.ts.map +1 -1
  50. package/dist/lib/table.js +5 -5
  51. package/dist/migrations/node.d.ts +1 -1
  52. package/dist/migrations/node.d.ts.map +1 -1
  53. package/dist/migrations/node.js +1 -1
  54. package/dist/migrations.d.ts +1 -2
  55. package/dist/migrations.d.ts.map +1 -1
  56. package/dist/migrations.js +2 -3
  57. package/dist/operators.js +1 -1
  58. package/dist/sql-helpers.js +1 -1
  59. package/package.json +13 -9
  60. package/src/cli.ts +120 -0
  61. package/src/index.ts +19 -20
  62. package/src/lib/column.ts +1 -1
  63. package/src/lib/database/execution-context.ts +10 -6
  64. package/src/lib/database/helpers.ts +1 -1
  65. package/src/lib/database/query-execution.ts +15 -11
  66. package/src/lib/database/write-lifecycle.ts +4 -4
  67. package/src/lib/database.ts +223 -96
  68. package/src/lib/{adapter.ts → driver.ts} +48 -48
  69. package/src/lib/errors.ts +4 -4
  70. package/src/lib/migrations/journal-store.ts +21 -11
  71. package/src/lib/migrations/runner.ts +200 -147
  72. package/src/lib/migrations-node.ts +23 -1
  73. package/src/lib/migrations.ts +58 -19
  74. package/src/lib/operators.ts +1 -1
  75. package/src/lib/query.ts +1 -1
  76. package/src/lib/sql-helpers.ts +1 -1
  77. package/src/lib/sql.ts +1 -1
  78. package/src/lib/table.ts +1 -1
  79. package/src/migrations/node.ts +1 -1
  80. package/src/migrations.ts +3 -4
  81. package/dist/lib/adapter.d.ts.map +0 -1
  82. /package/dist/lib/{adapter.js → driver.js} +0 -0
@@ -1,10 +1,9 @@
1
1
  import type { AnyTable, OrderByClause } from './table.ts'
2
2
  import type { Predicate } from './operators.ts'
3
3
  import type { SqlStatement } from './sql.ts'
4
- import type { Pretty } from './types.ts'
5
4
 
6
5
  /**
7
- * Supported SQL join kinds.
6
+ * SQL join kinds supported by database drivers.
8
7
  */
9
8
  export type JoinType = 'inner' | 'left' | 'right'
10
9
 
@@ -31,7 +30,7 @@ export type SelectColumn = {
31
30
  export type ReturningSelection = '*' | string[]
32
31
 
33
32
  /**
34
- * Canonical select statement shape consumed by adapters.
33
+ * Canonical select statement shape consumed by drivers.
35
34
  */
36
35
  export type SelectOperation<table extends AnyTable = AnyTable> = {
37
36
  kind: 'select'
@@ -48,7 +47,7 @@ export type SelectOperation<table extends AnyTable = AnyTable> = {
48
47
  }
49
48
 
50
49
  /**
51
- * Canonical count statement shape consumed by adapters.
50
+ * Canonical count statement shape consumed by drivers.
52
51
  */
53
52
  export type CountOperation<table extends AnyTable = AnyTable> = {
54
53
  kind: 'count'
@@ -60,7 +59,7 @@ export type CountOperation<table extends AnyTable = AnyTable> = {
60
59
  }
61
60
 
62
61
  /**
63
- * Canonical exists statement shape consumed by adapters.
62
+ * Canonical exists statement shape consumed by drivers.
64
63
  */
65
64
  export type ExistsOperation<table extends AnyTable = AnyTable> = {
66
65
  kind: 'exists'
@@ -72,7 +71,7 @@ export type ExistsOperation<table extends AnyTable = AnyTable> = {
72
71
  }
73
72
 
74
73
  /**
75
- * Canonical insert statement shape consumed by adapters.
74
+ * Canonical insert statement shape consumed by drivers.
76
75
  */
77
76
  export type InsertOperation<table extends AnyTable = AnyTable> = {
78
77
  kind: 'insert'
@@ -82,7 +81,7 @@ export type InsertOperation<table extends AnyTable = AnyTable> = {
82
81
  }
83
82
 
84
83
  /**
85
- * Canonical bulk-insert statement shape consumed by adapters.
84
+ * Canonical bulk-insert statement shape consumed by drivers.
86
85
  */
87
86
  export type InsertManyOperation<table extends AnyTable = AnyTable> = {
88
87
  kind: 'insertMany'
@@ -92,7 +91,7 @@ export type InsertManyOperation<table extends AnyTable = AnyTable> = {
92
91
  }
93
92
 
94
93
  /**
95
- * Canonical update statement shape consumed by adapters.
94
+ * Canonical update statement shape consumed by drivers.
96
95
  */
97
96
  export type UpdateOperation<table extends AnyTable = AnyTable> = {
98
97
  kind: 'update'
@@ -103,7 +102,7 @@ export type UpdateOperation<table extends AnyTable = AnyTable> = {
103
102
  }
104
103
 
105
104
  /**
106
- * Canonical delete statement shape consumed by adapters.
105
+ * Canonical delete statement shape consumed by drivers.
107
106
  */
108
107
  export type DeleteOperation<table extends AnyTable = AnyTable> = {
109
108
  kind: 'delete'
@@ -113,7 +112,7 @@ export type DeleteOperation<table extends AnyTable = AnyTable> = {
113
112
  }
114
113
 
115
114
  /**
116
- * Canonical upsert statement shape consumed by adapters.
115
+ * Canonical upsert statement shape consumed by drivers.
117
116
  */
118
117
  export type UpsertOperation<table extends AnyTable = AnyTable> = {
119
118
  kind: 'upsert'
@@ -243,7 +242,7 @@ export type ColumnDefinition = {
243
242
  }
244
243
 
245
244
  /**
246
- * Opaque transaction handle supplied by adapters.
245
+ * Opaque transaction handle supplied by database drivers.
247
246
  */
248
247
  export type TransactionToken = {
249
248
  id: string
@@ -251,7 +250,7 @@ export type TransactionToken = {
251
250
  }
252
251
 
253
252
  /**
254
- * Transaction hints that adapters may apply when supported by the dialect.
253
+ * Transaction hints that database drivers may apply when supported by the dialect.
255
254
  */
256
255
  export type TransactionOptions = {
257
256
  isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable'
@@ -259,7 +258,7 @@ export type TransactionOptions = {
259
258
  }
260
259
 
261
260
  /**
262
- * Adapter execution request payload.
261
+ * Database driver execution request payload.
263
262
  */
264
263
  export type DataManipulationRequest = {
265
264
  operation: DataManipulationOperation
@@ -267,7 +266,7 @@ export type DataManipulationRequest = {
267
266
  }
268
267
 
269
268
  /**
270
- * Adapter data-manipulation result payload.
269
+ * Database data-manipulation result payload.
271
270
  */
272
271
  export type DataManipulationResult = {
273
272
  rows?: Record<string, unknown>[]
@@ -276,58 +275,59 @@ export type DataManipulationResult = {
276
275
  }
277
276
 
278
277
  /**
279
- * Declares adapter feature support.
278
+ * Declares database feature support.
280
279
  */
281
- export type AdapterCapabilities = {
282
- returning: boolean
283
- savepoints: boolean
284
- upsert: boolean
285
- transactionalDdl: boolean
286
- migrationLock: boolean
280
+ export type DatabaseCapabilities = {
281
+ readonly returning: boolean
282
+ readonly savepoints: boolean
283
+ readonly upsert: boolean
284
+ readonly transactionalDdl: boolean
285
+ readonly migrationLock: boolean
287
286
  }
288
287
 
289
288
  /**
290
- * Partial capabilities used to override adapter defaults.
289
+ * Low-level contract that connects a `Database` to a database engine.
291
290
  */
292
- export type AdapterCapabilityOverrides = Pretty<Partial<AdapterCapabilities>>
293
-
294
- /**
295
- * Runtime contract implemented by concrete database adapters.
296
- */
297
- export interface DatabaseAdapter {
298
- /** Database dialect name exposed by the adapter. */
299
- dialect: string
300
- /** Feature flags describing the adapter's supported behaviors. */
301
- capabilities: AdapterCapabilities
302
- /** Compiles a data-manipulation operation into executable SQL statements. */
303
- compileSql(operation: DataManipulationOperation): SqlStatement[]
291
+ export interface DatabaseDriver<dialect extends string = string> {
292
+ /** Stable identifier for the SQL dialect. */
293
+ readonly dialect: dialect
294
+ /** Immutable feature flags used by shared query and migration behavior. */
295
+ readonly capabilities: DatabaseCapabilities
304
296
  /** Executes a data-manipulation request. */
305
297
  execute(request: DataManipulationRequest): Promise<DataManipulationResult>
306
- /**
307
- * Executes a raw SQL script that may contain multiple statements.
308
- *
309
- * Drivers must be configured to accept multi-statement scripts where required
310
- * (for example, mysql2 needs `multipleStatements: true`).
311
- */
298
+ /** Executes a raw SQL script that may contain multiple statements. */
312
299
  executeScript(sql: string, transaction?: TransactionToken): Promise<void>
313
- /** Checks whether a table exists. */
314
- hasTable(table: TableRef, transaction?: TransactionToken): Promise<boolean>
315
- /** Checks whether a column exists on a table. */
316
- hasColumn(table: TableRef, column: string, transaction?: TransactionToken): Promise<boolean>
317
300
  /** Starts a new database transaction. */
318
301
  beginTransaction(options?: TransactionOptions): Promise<TransactionToken>
319
302
  /** Commits an open transaction. */
320
303
  commitTransaction(token: TransactionToken): Promise<void>
321
304
  /** Rolls back an open transaction. */
322
305
  rollbackTransaction(token: TransactionToken): Promise<void>
306
+ /** Checks whether a table exists. */
307
+ hasTable(table: TableRef, transaction?: TransactionToken): Promise<boolean>
308
+ /** Checks whether a column exists on a table. */
309
+ hasColumn(table: TableRef, column: string, transaction?: TransactionToken): Promise<boolean>
323
310
  /** Creates a savepoint inside an open transaction. */
324
311
  createSavepoint(token: TransactionToken, name: string): Promise<void>
325
312
  /** Rolls back to a previously created savepoint. */
326
313
  rollbackToSavepoint(token: TransactionToken, name: string): Promise<void>
327
314
  /** Releases a previously created savepoint. */
328
315
  releaseSavepoint(token: TransactionToken, name: string): Promise<void>
329
- /** Acquires the adapter's migration lock when supported. */
330
- acquireMigrationLock?(): Promise<void>
331
- /** Releases the adapter's migration lock when supported. */
332
- releaseMigrationLock?(): Promise<void>
316
+ /** Destructively recreates the configured database. */
317
+ wipe(): Promise<void>
318
+ /** Releases connection handles owned by the driver. Must be safe to call repeatedly. */
319
+ close(): void | Promise<void>
320
+ /**
321
+ * Runs migration work while holding a driver-specific lock.
322
+ *
323
+ * The callback receives a driver bound to the connection that owns the lock. Drivers must
324
+ * release the lock when the callback rejects as well as when it resolves.
325
+ * @param name Logical migration lock name.
326
+ * @param run Migration work to run with the connection-bound driver.
327
+ * @returns The callback result.
328
+ */
329
+ withMigrationLock?<result>(
330
+ name: string,
331
+ run: (driver: DatabaseDriver<dialect>) => Promise<result>,
332
+ ): Promise<result>
333
333
  }
package/src/lib/errors.ts CHANGED
@@ -77,9 +77,9 @@ export class DataTableQueryError extends DataTableError {
77
77
  }
78
78
 
79
79
  /**
80
- * Thrown when adapter execution fails.
80
+ * Thrown when database execution fails.
81
81
  */
82
- export class DataTableAdapterError extends DataTableError {
82
+ export class DataTableDatabaseError extends DataTableError {
83
83
  constructor(
84
84
  message: string,
85
85
  options?: {
@@ -88,12 +88,12 @@ export class DataTableAdapterError extends DataTableError {
88
88
  },
89
89
  ) {
90
90
  super(message, {
91
- code: 'DATA_TABLE_ADAPTER_ERROR',
91
+ code: 'DATA_TABLE_DATABASE_ERROR',
92
92
  cause: options?.cause,
93
93
  metadata: options?.metadata,
94
94
  })
95
95
 
96
- this.name = 'DataTableAdapterError'
96
+ this.name = 'DataTableDatabaseError'
97
97
  }
98
98
  }
99
99
 
@@ -1,4 +1,4 @@
1
- import type { DatabaseAdapter, TransactionToken } from '../adapter.ts'
1
+ import type { DatabaseDriver, TransactionToken } from '../driver.ts'
2
2
  import { rawSql } from '../sql.ts'
3
3
  import type { MigrationDescriptor, MigrationJournalRow } from '../migrations.ts'
4
4
 
@@ -18,10 +18,10 @@ function bytesToHex(bytes: Uint8Array): string {
18
18
  }
19
19
 
20
20
  export async function ensureMigrationJournal(
21
- adapter: DatabaseAdapter,
21
+ driver: DatabaseDriver,
22
22
  tableName: string,
23
23
  ): Promise<void> {
24
- await adapter.executeScript(
24
+ await driver.executeScript(
25
25
  'create table if not exists ' +
26
26
  tableName +
27
27
  ' (' +
@@ -35,11 +35,11 @@ export async function ensureMigrationJournal(
35
35
  }
36
36
 
37
37
  export async function hasMigrationJournal(
38
- adapter: DatabaseAdapter,
38
+ driver: DatabaseDriver,
39
39
  tableName: string,
40
40
  ): Promise<boolean> {
41
41
  try {
42
- await adapter.execute({
42
+ await driver.execute({
43
43
  operation: {
44
44
  kind: 'raw',
45
45
  sql: rawSql('select 1 from ' + tableName + ' limit 1'),
@@ -48,15 +48,25 @@ export async function hasMigrationJournal(
48
48
 
49
49
  return true
50
50
  } catch {
51
+ // The probe fails both when the journal table is missing and when the
52
+ // database itself is unreachable. Only the former means "no journal yet";
53
+ // rethrow connection/auth failures via a table-independent query.
54
+ await driver.execute({
55
+ operation: {
56
+ kind: 'raw',
57
+ sql: rawSql('select 1'),
58
+ },
59
+ })
60
+
51
61
  return false
52
62
  }
53
63
  }
54
64
 
55
65
  export async function loadJournalRows(
56
- adapter: DatabaseAdapter,
66
+ driver: DatabaseDriver,
57
67
  tableName: string,
58
68
  ): Promise<MigrationJournalRow[]> {
59
- let result = await adapter.execute({
69
+ let result = await driver.execute({
60
70
  operation: {
61
71
  kind: 'raw',
62
72
  sql: rawSql(
@@ -77,7 +87,7 @@ export async function loadJournalRows(
77
87
  }
78
88
 
79
89
  export async function insertJournalRow(
80
- adapter: DatabaseAdapter,
90
+ driver: DatabaseDriver,
81
91
  tableName: string,
82
92
  row: {
83
93
  id: string
@@ -87,7 +97,7 @@ export async function insertJournalRow(
87
97
  },
88
98
  transaction?: TransactionToken,
89
99
  ): Promise<void> {
90
- await adapter.execute({
100
+ await driver.execute({
91
101
  operation: {
92
102
  kind: 'raw',
93
103
  sql: rawSql('insert into ' + tableName + ' (id, name, checksum, batch) values (?, ?, ?, ?)', [
@@ -102,12 +112,12 @@ export async function insertJournalRow(
102
112
  }
103
113
 
104
114
  export async function deleteJournalRow(
105
- adapter: DatabaseAdapter,
115
+ driver: DatabaseDriver,
106
116
  tableName: string,
107
117
  id: string,
108
118
  transaction?: TransactionToken,
109
119
  ): Promise<void> {
110
- await adapter.execute({
120
+ await driver.execute({
111
121
  operation: {
112
122
  kind: 'raw',
113
123
  sql: rawSql('delete from ' + tableName + ' where id = ?', [id]),