@remix-run/data-table 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +307 -56
- package/dist/index.d.ts +9 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/lib/adapter.d.ts +386 -16
- package/dist/lib/adapter.d.ts.map +1 -1
- package/dist/lib/column.d.ts +193 -0
- package/dist/lib/column.d.ts.map +1 -0
- package/dist/lib/column.js +302 -0
- package/dist/lib/database/execution-context.d.ts +10 -0
- package/dist/lib/database/execution-context.d.ts.map +1 -0
- package/dist/lib/database/execution-context.js +1 -0
- package/dist/lib/database/helpers.d.ts +26 -0
- package/dist/lib/database/helpers.d.ts.map +1 -0
- package/dist/lib/database/helpers.js +116 -0
- package/dist/lib/database/query-execution.d.ts +7 -0
- package/dist/lib/database/query-execution.d.ts.map +1 -0
- package/dist/lib/database/query-execution.js +401 -0
- package/dist/lib/database/relations.d.ts +4 -0
- package/dist/lib/database/relations.d.ts.map +1 -0
- package/dist/lib/database/relations.js +207 -0
- package/dist/lib/database/write-lifecycle.d.ts +13 -0
- package/dist/lib/database/write-lifecycle.d.ts.map +1 -0
- package/dist/lib/database/write-lifecycle.js +279 -0
- package/dist/lib/database.d.ts +141 -238
- package/dist/lib/database.d.ts.map +1 -1
- package/dist/lib/database.js +73 -1122
- package/dist/lib/errors.d.ts +9 -0
- package/dist/lib/errors.d.ts.map +1 -1
- package/dist/lib/errors.js +9 -0
- package/dist/lib/migrations/filename.d.ts +12 -0
- package/dist/lib/migrations/filename.d.ts.map +1 -0
- package/dist/lib/migrations/filename.js +20 -0
- package/dist/lib/migrations/helpers.d.ts +11 -0
- package/dist/lib/migrations/helpers.d.ts.map +1 -0
- package/dist/lib/migrations/helpers.js +77 -0
- package/dist/lib/migrations/journal-store.d.ts +15 -0
- package/dist/lib/migrations/journal-store.d.ts.map +1 -0
- package/dist/lib/migrations/journal-store.js +83 -0
- package/dist/lib/migrations/registry.d.ts +27 -0
- package/dist/lib/migrations/registry.d.ts.map +1 -0
- package/dist/lib/migrations/registry.js +51 -0
- package/dist/lib/migrations/runner.d.ts +20 -0
- package/dist/lib/migrations/runner.d.ts.map +1 -0
- package/dist/lib/migrations/runner.js +273 -0
- package/dist/lib/migrations/schema-api.d.ts +7 -0
- package/dist/lib/migrations/schema-api.d.ts.map +1 -0
- package/dist/lib/migrations/schema-api.js +326 -0
- package/dist/lib/migrations-node.d.ts +17 -0
- package/dist/lib/migrations-node.d.ts.map +1 -0
- package/dist/lib/migrations-node.js +65 -0
- package/dist/lib/migrations.d.ts +292 -0
- package/dist/lib/migrations.d.ts.map +1 -0
- package/dist/lib/migrations.js +38 -0
- package/dist/lib/operators.d.ts +3 -0
- package/dist/lib/operators.d.ts.map +1 -1
- package/dist/lib/query.d.ts +159 -0
- package/dist/lib/query.d.ts.map +1 -0
- package/dist/lib/query.js +401 -0
- package/dist/lib/references.d.ts +0 -1
- package/dist/lib/references.d.ts.map +1 -1
- package/dist/lib/sql-helpers.d.ts +50 -0
- package/dist/lib/sql-helpers.d.ts.map +1 -0
- package/dist/lib/sql-helpers.js +111 -0
- package/dist/lib/sql.d.ts +23 -6
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/sql.js +19 -5
- package/dist/lib/table.d.ts +355 -40
- package/dist/lib/table.d.ts.map +1 -1
- package/dist/lib/table.js +113 -90
- package/dist/migrations/node.d.ts +2 -0
- package/dist/migrations/node.d.ts.map +1 -0
- package/dist/migrations/node.js +1 -0
- package/dist/migrations.d.ts +8 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +5 -0
- package/dist/operators.d.ts +3 -0
- package/dist/operators.d.ts.map +1 -0
- package/dist/operators.js +1 -0
- package/dist/sql-helpers.d.ts +3 -0
- package/dist/sql-helpers.d.ts.map +1 -0
- package/dist/sql-helpers.js +1 -0
- package/package.json +24 -10
- package/src/index.ts +93 -10
- package/src/lib/adapter.ts +469 -25
- package/src/lib/column.ts +384 -0
- package/src/lib/database/execution-context.ts +15 -0
- package/src/lib/database/helpers.ts +216 -0
- package/src/lib/database/query-execution.ts +638 -0
- package/src/lib/database/relations.ts +332 -0
- package/src/lib/database/write-lifecycle.ts +487 -0
- package/src/lib/database.ts +246 -1848
- package/src/lib/errors.ts +10 -0
- package/src/lib/migrations/filename.ts +25 -0
- package/src/lib/migrations/helpers.ts +108 -0
- package/src/lib/migrations/journal-store.ts +122 -0
- package/src/lib/migrations/registry.ts +62 -0
- package/src/lib/migrations/runner.ts +374 -0
- package/src/lib/migrations/schema-api.ts +417 -0
- package/src/lib/migrations-node.ts +71 -0
- package/src/lib/migrations.ts +328 -0
- package/src/lib/operators.ts +3 -0
- package/src/lib/query.ts +958 -0
- package/src/lib/references.ts +0 -1
- package/src/lib/sql-helpers.ts +146 -0
- package/src/lib/sql.ts +23 -6
- package/src/lib/table.ts +484 -156
- package/src/migrations/node.ts +1 -0
- package/src/migrations.ts +26 -0
- package/src/operators.ts +18 -0
- package/src/sql-helpers.ts +9 -0
|
@@ -0,0 +1,328 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* 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
|
+
*
|
|
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
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function createMigration(input: CreateMigrationInput): Migration {
|
|
76
|
+
return {
|
|
77
|
+
up: input.up,
|
|
78
|
+
down: input.down,
|
|
79
|
+
transaction: input.transaction ?? 'auto',
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Migration metadata stored in registries and returned by loaders.
|
|
85
|
+
*/
|
|
86
|
+
export type MigrationDescriptor = {
|
|
87
|
+
id: string
|
|
88
|
+
name: string
|
|
89
|
+
path?: string
|
|
90
|
+
checksum?: string
|
|
91
|
+
migration: Migration
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Direction used by migration runner operations.
|
|
96
|
+
*/
|
|
97
|
+
export type MigrationDirection = 'up' | 'down'
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Row shape persisted in the migration journal table.
|
|
101
|
+
*/
|
|
102
|
+
export type MigrationJournalRow = {
|
|
103
|
+
id: string
|
|
104
|
+
name: string
|
|
105
|
+
checksum: string
|
|
106
|
+
batch: number
|
|
107
|
+
appliedAt: Date
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Effective status for a known migration.
|
|
112
|
+
*/
|
|
113
|
+
export type MigrationStatus = 'applied' | 'pending' | 'drifted'
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Status row returned by `runner.status()` and `runner.up/down(...)`.
|
|
117
|
+
*/
|
|
118
|
+
export type MigrationStatusEntry = {
|
|
119
|
+
id: string
|
|
120
|
+
name: string
|
|
121
|
+
status: MigrationStatus
|
|
122
|
+
appliedAt?: Date
|
|
123
|
+
batch?: number
|
|
124
|
+
checksum?: string
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Common options for `runner.up(...)` and `runner.down(...)`.
|
|
129
|
+
* `to` and `step` are mutually exclusive.
|
|
130
|
+
*/
|
|
131
|
+
export type MigrateOptions =
|
|
132
|
+
| {
|
|
133
|
+
to: string
|
|
134
|
+
step?: never
|
|
135
|
+
dryRun?: boolean
|
|
136
|
+
}
|
|
137
|
+
| {
|
|
138
|
+
to?: never
|
|
139
|
+
step: number
|
|
140
|
+
dryRun?: boolean
|
|
141
|
+
}
|
|
142
|
+
| {
|
|
143
|
+
to?: undefined
|
|
144
|
+
step?: undefined
|
|
145
|
+
dryRun?: boolean
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Result shape returned by migration runner commands.
|
|
150
|
+
*/
|
|
151
|
+
export type MigrateResult = {
|
|
152
|
+
applied: MigrationStatusEntry[]
|
|
153
|
+
reverted: MigrationStatusEntry[]
|
|
154
|
+
/**
|
|
155
|
+
* Compiled SQL statements for operations processed during this run.
|
|
156
|
+
* Includes planned SQL when running with `dryRun: true`.
|
|
157
|
+
*/
|
|
158
|
+
sql: SqlStatement[]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
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.
|
|
197
|
+
*/
|
|
198
|
+
export type ForeignKeyOptions = NamedConstraintOptions & {
|
|
199
|
+
onDelete?: ForeignKeyAction
|
|
200
|
+
onUpdate?: ForeignKeyAction
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Index options for migration APIs.
|
|
205
|
+
*/
|
|
206
|
+
export type CreateIndexOptions = NamedConstraintOptions &
|
|
207
|
+
Omit<IndexDefinition, 'table' | 'name' | 'columns'> & {
|
|
208
|
+
ifNotExists?: boolean
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Builder API available inside `schema.alterTable(name, table => ...)`.
|
|
213
|
+
*/
|
|
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
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* DDL-focused operations mixed into the migration `db` object.
|
|
254
|
+
*/
|
|
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>
|
|
292
|
+
/**
|
|
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.
|
|
298
|
+
*/
|
|
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[]
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Options for creating a migration runner.
|
|
312
|
+
*/
|
|
313
|
+
export type MigrationRunnerOptions = {
|
|
314
|
+
/**
|
|
315
|
+
* Journal table used to record applied migrations.
|
|
316
|
+
* Defaults to `data_table_migrations`.
|
|
317
|
+
*/
|
|
318
|
+
journalTable?: string
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Migration runner API for applying, reverting, and inspecting migration state.
|
|
323
|
+
*/
|
|
324
|
+
export type MigrationRunner = {
|
|
325
|
+
up(options?: MigrateOptions): Promise<MigrateResult>
|
|
326
|
+
down(options?: MigrateOptions): Promise<MigrateResult>
|
|
327
|
+
status(): Promise<MigrationStatusEntry[]>
|
|
328
|
+
}
|
package/src/lib/operators.ts
CHANGED
|
@@ -56,6 +56,9 @@ export type Predicate<column extends string = string> =
|
|
|
56
56
|
predicates: Predicate<column>[]
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Object shorthand accepted in `where` clauses.
|
|
61
|
+
*/
|
|
59
62
|
export type WhereObject<column extends string = string> = Partial<Record<column, unknown>>
|
|
60
63
|
|
|
61
64
|
/**
|