@remix-run/data-table 0.2.1 → 0.3.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 (66) hide show
  1. package/README.md +76 -74
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/lib/adapter.d.ts +10 -271
  5. package/dist/lib/adapter.d.ts.map +1 -1
  6. package/dist/lib/column.d.ts.map +1 -1
  7. package/dist/lib/column.js +10 -1
  8. package/dist/lib/migrations/directive.d.ts +12 -0
  9. package/dist/lib/migrations/directive.d.ts.map +1 -0
  10. package/dist/lib/migrations/directive.js +28 -0
  11. package/dist/lib/migrations/directory-name.d.ts +12 -0
  12. package/dist/lib/migrations/directory-name.d.ts.map +1 -0
  13. package/dist/lib/migrations/directory-name.js +18 -0
  14. package/dist/lib/migrations/journal-store.d.ts +1 -1
  15. package/dist/lib/migrations/journal-store.d.ts.map +1 -1
  16. package/dist/lib/migrations/journal-store.js +18 -18
  17. package/dist/lib/migrations/registry.d.ts +1 -1
  18. package/dist/lib/migrations/registry.js +1 -1
  19. package/dist/lib/migrations/runner.d.ts +2 -2
  20. package/dist/lib/migrations/runner.d.ts.map +1 -1
  21. package/dist/lib/migrations/runner.js +44 -71
  22. package/dist/lib/migrations-node.d.ts +7 -4
  23. package/dist/lib/migrations-node.d.ts.map +1 -1
  24. package/dist/lib/migrations-node.js +52 -41
  25. package/dist/lib/migrations.d.ts +19 -200
  26. package/dist/lib/migrations.d.ts.map +1 -1
  27. package/dist/lib/migrations.js +1 -38
  28. package/dist/lib/query.d.ts +33 -0
  29. package/dist/lib/query.d.ts.map +1 -1
  30. package/dist/lib/query.js +28 -0
  31. package/dist/lib/sql-helpers.d.ts +1 -7
  32. package/dist/lib/sql-helpers.d.ts.map +1 -1
  33. package/dist/lib/sql-helpers.js +0 -16
  34. package/dist/migrations.d.ts +2 -5
  35. package/dist/migrations.d.ts.map +1 -1
  36. package/dist/migrations.js +1 -3
  37. package/dist/sql-helpers.d.ts +1 -1
  38. package/dist/sql-helpers.d.ts.map +1 -1
  39. package/dist/sql-helpers.js +1 -1
  40. package/package.json +2 -2
  41. package/src/index.ts +1 -34
  42. package/src/lib/adapter.ts +10 -330
  43. package/src/lib/column.ts +14 -2
  44. package/src/lib/migrations/directive.ts +38 -0
  45. package/src/lib/migrations/directory-name.ts +23 -0
  46. package/src/lib/migrations/journal-store.ts +22 -18
  47. package/src/lib/migrations/registry.ts +1 -1
  48. package/src/lib/migrations/runner.ts +51 -85
  49. package/src/lib/migrations-node.ts +58 -38
  50. package/src/lib/migrations.ts +19 -224
  51. package/src/lib/query.ts +33 -0
  52. package/src/lib/sql-helpers.ts +1 -22
  53. package/src/migrations.ts +3 -13
  54. package/src/sql-helpers.ts +0 -1
  55. package/dist/lib/migrations/filename.d.ts +0 -12
  56. package/dist/lib/migrations/filename.d.ts.map +0 -1
  57. package/dist/lib/migrations/filename.js +0 -20
  58. package/dist/lib/migrations/helpers.d.ts +0 -11
  59. package/dist/lib/migrations/helpers.d.ts.map +0 -1
  60. package/dist/lib/migrations/helpers.js +0 -77
  61. package/dist/lib/migrations/schema-api.d.ts +0 -7
  62. package/dist/lib/migrations/schema-api.d.ts.map +0 -1
  63. package/dist/lib/migrations/schema-api.js +0 -326
  64. package/src/lib/migrations/filename.ts +0 -25
  65. package/src/lib/migrations/helpers.ts +0 -108
  66. package/src/lib/migrations/schema-api.ts +0 -417
@@ -147,7 +147,7 @@ export type DataManipulationOperation =
147
147
  | RawOperation
148
148
 
149
149
  /**
150
- * Qualified table reference used in migration operations.
150
+ * Qualified table reference.
151
151
  */
152
152
  export type TableRef = {
153
153
  name: string
@@ -242,313 +242,6 @@ export type ColumnDefinition = {
242
242
  charset?: string
243
243
  }
244
244
 
245
- /**
246
- * Primary key constraint definition.
247
- */
248
- export type PrimaryKeyConstraint = {
249
- columns: string[]
250
- name: string
251
- }
252
-
253
- /**
254
- * Unique constraint definition.
255
- */
256
- export type UniqueConstraint = {
257
- columns: string[]
258
- name: string
259
- }
260
-
261
- /**
262
- * Check constraint definition.
263
- */
264
- export type CheckConstraint = {
265
- expression: string
266
- name: string
267
- }
268
-
269
- /**
270
- * Foreign key constraint definition.
271
- */
272
- export type ForeignKeyConstraint = {
273
- columns: string[]
274
- references: {
275
- table: TableRef
276
- columns: string[]
277
- }
278
- name: string
279
- onDelete?: ForeignKeyAction
280
- onUpdate?: ForeignKeyAction
281
- }
282
-
283
- /**
284
- * Index method used when creating an index.
285
- */
286
- export type IndexMethod = 'btree' | 'hash' | 'gin' | 'gist' | 'fulltext' | (string & {})
287
-
288
- /**
289
- * Index definition used in schema operations.
290
- */
291
- export type IndexDefinition = {
292
- table: TableRef
293
- name: string
294
- columns: string[]
295
- unique?: boolean
296
- where?: string
297
- using?: IndexMethod
298
- }
299
-
300
- /**
301
- * Operation that creates a new table.
302
- */
303
- export type CreateTableOperation = {
304
- kind: 'createTable'
305
- table: TableRef
306
- ifNotExists?: boolean
307
- columns: Record<string, ColumnDefinition>
308
- primaryKey?: PrimaryKeyConstraint
309
- uniques?: UniqueConstraint[]
310
- checks?: CheckConstraint[]
311
- foreignKeys?: ForeignKeyConstraint[]
312
- comment?: string
313
- }
314
-
315
- /**
316
- * Alter-table change that adds a column.
317
- */
318
- export type AddColumnChange = {
319
- kind: 'addColumn'
320
- column: string
321
- definition: ColumnDefinition
322
- }
323
-
324
- /**
325
- * Alter-table change that replaces a column definition.
326
- */
327
- export type ChangeColumnChange = {
328
- kind: 'changeColumn'
329
- column: string
330
- definition: ColumnDefinition
331
- }
332
-
333
- /**
334
- * Alter-table change that renames a column.
335
- */
336
- export type RenameColumnChange = {
337
- kind: 'renameColumn'
338
- from: string
339
- to: string
340
- }
341
-
342
- /**
343
- * Alter-table change that drops a column.
344
- */
345
- export type DropColumnChange = {
346
- kind: 'dropColumn'
347
- column: string
348
- ifExists?: boolean
349
- }
350
-
351
- /**
352
- * Alter-table change that adds a primary key.
353
- */
354
- export type AddPrimaryKeyChange = {
355
- kind: 'addPrimaryKey'
356
- constraint: PrimaryKeyConstraint
357
- }
358
-
359
- /**
360
- * Alter-table change that drops a primary key.
361
- */
362
- export type DropPrimaryKeyChange = {
363
- kind: 'dropPrimaryKey'
364
- name: string
365
- }
366
-
367
- /**
368
- * Alter-table change that adds a unique constraint.
369
- */
370
- export type AddUniqueChange = {
371
- kind: 'addUnique'
372
- constraint: UniqueConstraint
373
- }
374
-
375
- /**
376
- * Alter-table change that drops a unique constraint.
377
- */
378
- export type DropUniqueChange = {
379
- kind: 'dropUnique'
380
- name: string
381
- }
382
-
383
- /**
384
- * Alter-table change that adds a foreign key constraint.
385
- */
386
- export type AddForeignKeyChange = {
387
- kind: 'addForeignKey'
388
- constraint: ForeignKeyConstraint
389
- }
390
-
391
- /**
392
- * Alter-table change that drops a foreign key constraint.
393
- */
394
- export type DropForeignKeyChange = {
395
- kind: 'dropForeignKey'
396
- name: string
397
- }
398
-
399
- /**
400
- * Alter-table change that adds a check constraint.
401
- */
402
- export type AddCheckChange = {
403
- kind: 'addCheck'
404
- constraint: CheckConstraint
405
- }
406
-
407
- /**
408
- * Alter-table change that drops a check constraint.
409
- */
410
- export type DropCheckChange = {
411
- kind: 'dropCheck'
412
- name: string
413
- }
414
-
415
- /**
416
- * Alter-table change that updates a table comment.
417
- */
418
- export type SetTableCommentChange = {
419
- kind: 'setTableComment'
420
- comment: string
421
- }
422
-
423
- /**
424
- * Union of supported `alterTable` changes.
425
- */
426
- export type AlterTableChange =
427
- | AddColumnChange
428
- | ChangeColumnChange
429
- | RenameColumnChange
430
- | DropColumnChange
431
- | AddPrimaryKeyChange
432
- | DropPrimaryKeyChange
433
- | AddUniqueChange
434
- | DropUniqueChange
435
- | AddForeignKeyChange
436
- | DropForeignKeyChange
437
- | AddCheckChange
438
- | DropCheckChange
439
- | SetTableCommentChange
440
-
441
- /**
442
- * Operation that applies one or more table changes.
443
- */
444
- export type AlterTableOperation = {
445
- kind: 'alterTable'
446
- table: TableRef
447
- changes: AlterTableChange[]
448
- ifExists?: boolean
449
- }
450
-
451
- /**
452
- * Operation that renames a table.
453
- */
454
- export type RenameTableOperation = {
455
- kind: 'renameTable'
456
- from: TableRef
457
- to: TableRef
458
- }
459
-
460
- /**
461
- * Operation that drops a table.
462
- */
463
- export type DropTableOperation = {
464
- kind: 'dropTable'
465
- table: TableRef
466
- ifExists?: boolean
467
- cascade?: boolean
468
- }
469
-
470
- /**
471
- * Operation that creates an index.
472
- */
473
- export type CreateIndexOperation = {
474
- kind: 'createIndex'
475
- index: IndexDefinition
476
- ifNotExists?: boolean
477
- }
478
-
479
- /**
480
- * Operation that drops an index.
481
- */
482
- export type DropIndexOperation = {
483
- kind: 'dropIndex'
484
- table: TableRef
485
- name: string
486
- ifExists?: boolean
487
- }
488
-
489
- /**
490
- * Operation that renames an index.
491
- */
492
- export type RenameIndexOperation = {
493
- kind: 'renameIndex'
494
- table: TableRef
495
- from: string
496
- to: string
497
- }
498
-
499
- /**
500
- * Operation that adds a table-level foreign key.
501
- */
502
- export type AddForeignKeyOperation = {
503
- kind: 'addForeignKey'
504
- table: TableRef
505
- constraint: ForeignKeyConstraint
506
- }
507
-
508
- /**
509
- * Operation that drops a table-level foreign key.
510
- */
511
- export type DropForeignKeyOperation = {
512
- kind: 'dropForeignKey'
513
- table: TableRef
514
- name: string
515
- }
516
-
517
- /**
518
- * Operation that adds a table-level check constraint.
519
- */
520
- export type AddCheckOperation = {
521
- kind: 'addCheck'
522
- table: TableRef
523
- constraint: CheckConstraint
524
- }
525
-
526
- /**
527
- * Operation that drops a table-level check constraint.
528
- */
529
- export type DropCheckOperation = {
530
- kind: 'dropCheck'
531
- table: TableRef
532
- name: string
533
- }
534
-
535
- /**
536
- * Union of schema and migration operations understood by adapters.
537
- */
538
- export type DataMigrationOperation =
539
- | CreateTableOperation
540
- | AlterTableOperation
541
- | RenameTableOperation
542
- | DropTableOperation
543
- | CreateIndexOperation
544
- | DropIndexOperation
545
- | RenameIndexOperation
546
- | AddForeignKeyOperation
547
- | DropForeignKeyOperation
548
- | AddCheckOperation
549
- | DropCheckOperation
550
- | RawOperation
551
-
552
245
  /**
553
246
  * Opaque transaction handle supplied by adapters.
554
247
  */
@@ -573,14 +266,6 @@ export type DataManipulationRequest = {
573
266
  transaction?: TransactionToken
574
267
  }
575
268
 
576
- /**
577
- * Adapter migration request payload.
578
- */
579
- export type DataMigrationRequest = {
580
- operation: DataMigrationOperation
581
- transaction?: TransactionToken
582
- }
583
-
584
269
  /**
585
270
  * Adapter data-manipulation result payload.
586
271
  */
@@ -590,16 +275,6 @@ export type DataManipulationResult = {
590
275
  insertId?: unknown
591
276
  }
592
277
 
593
- /**
594
- * Adapter data-migration result payload.
595
- */
596
- export type DataMigrationResult = {
597
- /**
598
- * Number of migration operations processed by the adapter call.
599
- */
600
- affectedOperations?: number
601
- }
602
-
603
278
  /**
604
279
  * Declares adapter feature support.
605
280
  */
@@ -624,12 +299,17 @@ export interface DatabaseAdapter {
624
299
  dialect: string
625
300
  /** Feature flags describing the adapter's supported behaviors. */
626
301
  capabilities: AdapterCapabilities
627
- /** Compiles a data or migration operation into executable SQL statements. */
628
- compileSql(operation: DataManipulationOperation | DataMigrationOperation): SqlStatement[]
302
+ /** Compiles a data-manipulation operation into executable SQL statements. */
303
+ compileSql(operation: DataManipulationOperation): SqlStatement[]
629
304
  /** Executes a data-manipulation request. */
630
305
  execute(request: DataManipulationRequest): Promise<DataManipulationResult>
631
- /** Executes a migration request. */
632
- migrate(request: DataMigrationRequest): Promise<DataMigrationResult>
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
+ */
312
+ executeScript(sql: string, transaction?: TransactionToken): Promise<void>
633
313
  /** Checks whether a table exists. */
634
314
  hasTable(table: TableRef, transaction?: TransactionToken): Promise<boolean>
635
315
  /** Checks whether a column exists on a table. */
package/src/lib/column.ts CHANGED
@@ -1,5 +1,17 @@
1
- import type { ColumnDefinition, ForeignKeyAction, IdentityOptions } from './adapter.ts'
2
- import { toTableRef } from './migrations/helpers.ts'
1
+ import type { ColumnDefinition, ForeignKeyAction, IdentityOptions, TableRef } from './adapter.ts'
2
+
3
+ function toTableRef(name: string): TableRef {
4
+ let segments = name.split('.')
5
+
6
+ if (segments.length === 1) {
7
+ return { name }
8
+ }
9
+
10
+ return {
11
+ schema: segments[0],
12
+ name: segments.slice(1).join('.'),
13
+ }
14
+ }
3
15
 
4
16
  /**
5
17
  * Chainable builder used to describe physical column definitions.
@@ -0,0 +1,38 @@
1
+ import type { MigrationTransactionMode } from '../migrations.ts'
2
+
3
+ const directivePattern = /^\s*--\s*data-table\/transaction\s*:\s*(auto|required|none)\s*$/i
4
+
5
+ /**
6
+ * Parses an optional `-- data-table/transaction: auto|required|none` directive
7
+ * from any single-line SQL comment in the script.
8
+ *
9
+ * Returns `undefined` when no directive is present.
10
+ * @param sql SQL script contents.
11
+ * @returns The declared transaction mode, or `undefined` when not declared.
12
+ * @throws when more than one directive is present or the value is invalid.
13
+ */
14
+ export function parseTransactionDirective(sql: string): MigrationTransactionMode | undefined {
15
+ let match: MigrationTransactionMode | undefined
16
+
17
+ for (let line of sql.split(/\r?\n/)) {
18
+ let trimmed = line.trim()
19
+
20
+ if (!trimmed.startsWith('--')) {
21
+ continue
22
+ }
23
+
24
+ let result = directivePattern.exec(trimmed)
25
+
26
+ if (!result) {
27
+ continue
28
+ }
29
+
30
+ if (match !== undefined) {
31
+ throw new Error('Migration script declares more than one transaction directive')
32
+ }
33
+
34
+ match = result[1].toLowerCase() as MigrationTransactionMode
35
+ }
36
+
37
+ return match
38
+ }
@@ -0,0 +1,23 @@
1
+ const migrationDirectoryPattern = /^(\d{14})_(.+)$/
2
+
3
+ /**
4
+ * Parses a migration directory name into `{ id, name }`.
5
+ *
6
+ * Expected format: `YYYYMMDDHHmmss_name`.
7
+ * @param name Migration directory basename.
8
+ * @returns Parsed migration id and name.
9
+ */
10
+ export function parseMigrationDirectoryName(name: string): { id: string; name: string } {
11
+ let match = name.match(migrationDirectoryPattern)
12
+
13
+ if (!match) {
14
+ throw new Error(
15
+ 'Invalid migration directory name "' + name + '". Expected format YYYYMMDDHHmmss_name',
16
+ )
17
+ }
18
+
19
+ return {
20
+ id: match[1],
21
+ name: match[2],
22
+ }
23
+ }
@@ -2,32 +2,36 @@ import type { DatabaseAdapter, TransactionToken } from '../adapter.ts'
2
2
  import { rawSql } from '../sql.ts'
3
3
  import type { MigrationDescriptor, MigrationJournalRow } from '../migrations.ts'
4
4
 
5
- export function normalizeChecksum(migration: MigrationDescriptor): string {
6
- if (migration.checksum) {
7
- return migration.checksum
5
+ export async function computeChecksum(migration: MigrationDescriptor): Promise<string> {
6
+ let digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(migration.up))
7
+ return bytesToHex(new Uint8Array(digest))
8
+ }
9
+
10
+ function bytesToHex(bytes: Uint8Array): string {
11
+ let hex = ''
12
+
13
+ for (let byte of bytes) {
14
+ hex += byte.toString(16).padStart(2, '0')
8
15
  }
9
16
 
10
- return migration.id + ':' + migration.name
17
+ return hex
11
18
  }
12
19
 
13
20
  export async function ensureMigrationJournal(
14
21
  adapter: DatabaseAdapter,
15
22
  tableName: string,
16
23
  ): Promise<void> {
17
- await adapter.migrate({
18
- operation: {
19
- kind: 'createTable',
20
- table: { name: tableName },
21
- ifNotExists: true,
22
- columns: {
23
- id: { type: 'varchar', length: 64, nullable: false, primaryKey: true },
24
- name: { type: 'varchar', length: 255, nullable: false },
25
- checksum: { type: 'varchar', length: 128, nullable: false },
26
- batch: { type: 'integer', nullable: false },
27
- applied_at: { type: 'timestamp', nullable: false, default: { kind: 'now' } },
28
- },
29
- },
30
- })
24
+ await adapter.executeScript(
25
+ 'create table if not exists ' +
26
+ tableName +
27
+ ' (' +
28
+ 'id varchar(64) not null primary key, ' +
29
+ 'name varchar(255) not null, ' +
30
+ 'checksum varchar(128) not null, ' +
31
+ 'batch integer not null, ' +
32
+ 'applied_at timestamp not null default current_timestamp' +
33
+ ')',
34
+ )
31
35
  }
32
36
 
33
37
  export async function hasMigrationJournal(
@@ -33,7 +33,7 @@ export function resolveMigrations(
33
33
  * import { createMigrationRegistry } from 'remix/data-table/migrations'
34
34
  *
35
35
  * let registry = createMigrationRegistry()
36
- * registry.register({ id, name, migration })
36
+ * registry.register({ id, name, up, down })
37
37
  * ```
38
38
  */
39
39
  export function createMigrationRegistry(initial: MigrationDescriptor[] = []): MigrationRegistry {