@remix-run/data-table-sqlite 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.
@@ -0,0 +1,831 @@
1
+ import type {
2
+ AdapterCapabilityOverrides,
3
+ DataManipulationRequest,
4
+ DataMigrationRequest,
5
+ DataMigrationResult,
6
+ DataMigrationOperation,
7
+ DataManipulationResult,
8
+ DataManipulationOperation,
9
+ DatabaseAdapter,
10
+ ColumnDefinition,
11
+ SqlStatement,
12
+ TableRef,
13
+ TransactionOptions,
14
+ TransactionToken,
15
+ } from '@remix-run/data-table'
16
+ import { getTablePrimaryKey } from '@remix-run/data-table'
17
+ import {
18
+ isDataManipulationOperation as isDataManipulationOperationHelper,
19
+ quoteLiteral as quoteLiteralHelper,
20
+ quoteTableRef as quoteTableRefHelper,
21
+ } from '@remix-run/data-table/sql-helpers'
22
+ import type { Database as BetterSqliteDatabase, RunResult } from 'better-sqlite3'
23
+
24
+ import { compileSqliteOperation } from './sql-compiler.ts'
25
+
26
+ /**
27
+ * Sqlite adapter configuration.
28
+ */
29
+ export type SqliteDatabaseAdapterOptions = {
30
+ capabilities?: AdapterCapabilityOverrides
31
+ }
32
+
33
+ /**
34
+ * `DatabaseAdapter` implementation for Better SQLite3.
35
+ */
36
+ export class SqliteDatabaseAdapter implements DatabaseAdapter {
37
+ /**
38
+ * The SQL dialect identifier reported by this adapter.
39
+ */
40
+ dialect = 'sqlite'
41
+
42
+ /**
43
+ * Feature flags describing the sqlite behaviors supported by this adapter.
44
+ */
45
+ capabilities
46
+
47
+ #database: BetterSqliteDatabase
48
+ #transactions = new Set<string>()
49
+ #transactionCounter = 0
50
+
51
+ constructor(database: BetterSqliteDatabase, options?: SqliteDatabaseAdapterOptions) {
52
+ this.#database = database
53
+ this.capabilities = {
54
+ returning: options?.capabilities?.returning ?? true,
55
+ savepoints: options?.capabilities?.savepoints ?? true,
56
+ upsert: options?.capabilities?.upsert ?? true,
57
+ transactionalDdl: options?.capabilities?.transactionalDdl ?? true,
58
+ migrationLock: options?.capabilities?.migrationLock ?? false,
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Compiles a data or migration operation to sqlite SQL statements.
64
+ * @param operation Operation to compile.
65
+ * @returns Compiled SQL statements.
66
+ */
67
+ compileSql(operation: DataManipulationOperation | DataMigrationOperation): SqlStatement[] {
68
+ if (isDataManipulationOperation(operation)) {
69
+ let compiled = compileSqliteOperation(operation)
70
+ return [{ text: compiled.text, values: compiled.values }]
71
+ }
72
+
73
+ return compileSqliteMigrationOperations(operation)
74
+ }
75
+
76
+ /**
77
+ * Executes a sqlite data-manipulation request.
78
+ * @param request Request to execute.
79
+ * @returns Execution result.
80
+ */
81
+ async execute(request: DataManipulationRequest): Promise<DataManipulationResult> {
82
+ if (request.operation.kind === 'insertMany' && request.operation.values.length === 0) {
83
+ return {
84
+ affectedRows: 0,
85
+ insertId: undefined,
86
+ rows: request.operation.returning ? [] : undefined,
87
+ }
88
+ }
89
+
90
+ let statement = this.compileSql(request.operation)[0]
91
+ let prepared = this.#database.prepare(statement.text)
92
+
93
+ if (prepared.reader) {
94
+ let rows = normalizeRows(prepared.all(...statement.values))
95
+
96
+ if (request.operation.kind === 'count' || request.operation.kind === 'exists') {
97
+ rows = normalizeCountRows(rows)
98
+ }
99
+
100
+ return {
101
+ rows,
102
+ affectedRows: normalizeAffectedRowsForReader(request.operation.kind, rows),
103
+ insertId: normalizeInsertIdForReader(request.operation.kind, request.operation, rows),
104
+ }
105
+ }
106
+
107
+ let result = prepared.run(...statement.values)
108
+
109
+ return {
110
+ affectedRows: normalizeAffectedRowsForRun(request.operation.kind, result),
111
+ insertId: normalizeInsertIdForRun(request.operation.kind, request.operation, result),
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Executes sqlite migration operations.
117
+ * @param request Migration request to execute.
118
+ * @returns Migration result.
119
+ */
120
+ async migrate(request: DataMigrationRequest): Promise<DataMigrationResult> {
121
+ let statements = this.compileSql(request.operation)
122
+
123
+ for (let statement of statements) {
124
+ let prepared = this.#database.prepare(statement.text)
125
+ prepared.run(...statement.values)
126
+ }
127
+
128
+ return {
129
+ affectedOperations: statements.length,
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Checks whether a table exists in sqlite.
135
+ * @param table Table reference to inspect.
136
+ * @param transaction Optional transaction token.
137
+ * @returns `true` when the table exists.
138
+ */
139
+ async hasTable(table: TableRef, transaction?: TransactionToken): Promise<boolean> {
140
+ if (transaction) {
141
+ this.#assertTransaction(transaction)
142
+ }
143
+
144
+ let masterTable = table.schema
145
+ ? quoteIdentifier(table.schema) + '.sqlite_master'
146
+ : 'sqlite_master'
147
+ let statement = this.#database.prepare(
148
+ 'select 1 from ' + masterTable + ' where type = ? and name = ? limit 1',
149
+ )
150
+ let row = statement.get('table', table.name)
151
+ return row !== undefined
152
+ }
153
+
154
+ /**
155
+ * Checks whether a column exists in sqlite.
156
+ * @param table Table reference to inspect.
157
+ * @param column Column name to look up.
158
+ * @param transaction Optional transaction token.
159
+ * @returns `true` when the column exists.
160
+ */
161
+ async hasColumn(
162
+ table: TableRef,
163
+ column: string,
164
+ transaction?: TransactionToken,
165
+ ): Promise<boolean> {
166
+ if (transaction) {
167
+ this.#assertTransaction(transaction)
168
+ }
169
+
170
+ let schemaPrefix = table.schema ? quoteIdentifier(table.schema) + '.' : ''
171
+ let statement = this.#database.prepare(
172
+ 'pragma ' + schemaPrefix + 'table_info(' + quoteIdentifier(table.name) + ')',
173
+ )
174
+ let rows = statement.all() as Array<Record<string, unknown>>
175
+
176
+ return rows.some((row) => row.name === column)
177
+ }
178
+
179
+ /**
180
+ * Starts a sqlite transaction.
181
+ * @param options Transaction options.
182
+ * @returns Transaction token.
183
+ */
184
+ async beginTransaction(options?: TransactionOptions): Promise<TransactionToken> {
185
+ if (options?.isolationLevel === 'read uncommitted') {
186
+ this.#database.pragma('read_uncommitted = true')
187
+ }
188
+
189
+ this.#database.exec('begin')
190
+
191
+ this.#transactionCounter += 1
192
+ let token = { id: 'tx_' + String(this.#transactionCounter) }
193
+ this.#transactions.add(token.id)
194
+
195
+ return token
196
+ }
197
+
198
+ /**
199
+ * Commits an open sqlite transaction.
200
+ * @param token Transaction token to commit.
201
+ * @returns A promise that resolves when the transaction is committed.
202
+ */
203
+ async commitTransaction(token: TransactionToken): Promise<void> {
204
+ this.#assertTransaction(token)
205
+ this.#database.exec('commit')
206
+ this.#transactions.delete(token.id)
207
+ }
208
+
209
+ /**
210
+ * Rolls back an open sqlite transaction.
211
+ * @param token Transaction token to roll back.
212
+ * @returns A promise that resolves when the transaction is rolled back.
213
+ */
214
+ async rollbackTransaction(token: TransactionToken): Promise<void> {
215
+ this.#assertTransaction(token)
216
+ this.#database.exec('rollback')
217
+ this.#transactions.delete(token.id)
218
+ }
219
+
220
+ /**
221
+ * Creates a savepoint in an open sqlite transaction.
222
+ * @param token Transaction token to use.
223
+ * @param name Savepoint name.
224
+ * @returns A promise that resolves when the savepoint is created.
225
+ */
226
+ async createSavepoint(token: TransactionToken, name: string): Promise<void> {
227
+ this.#assertTransaction(token)
228
+ this.#database.exec('savepoint ' + quoteIdentifier(name))
229
+ }
230
+
231
+ /**
232
+ * Rolls back to a savepoint in an open sqlite transaction.
233
+ * @param token Transaction token to use.
234
+ * @param name Savepoint name.
235
+ * @returns A promise that resolves when the rollback completes.
236
+ */
237
+ async rollbackToSavepoint(token: TransactionToken, name: string): Promise<void> {
238
+ this.#assertTransaction(token)
239
+ this.#database.exec('rollback to savepoint ' + quoteIdentifier(name))
240
+ }
241
+
242
+ /**
243
+ * Releases a savepoint in an open sqlite transaction.
244
+ * @param token Transaction token to use.
245
+ * @param name Savepoint name.
246
+ * @returns A promise that resolves when the savepoint is released.
247
+ */
248
+ async releaseSavepoint(token: TransactionToken, name: string): Promise<void> {
249
+ this.#assertTransaction(token)
250
+ this.#database.exec('release savepoint ' + quoteIdentifier(name))
251
+ }
252
+
253
+ #assertTransaction(token: TransactionToken): void {
254
+ if (!this.#transactions.has(token.id)) {
255
+ throw new Error('Unknown transaction token: ' + token.id)
256
+ }
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Creates a sqlite `DatabaseAdapter`.
262
+ * @param database Better SQLite3 database instance.
263
+ * @param options Optional adapter capability overrides.
264
+ * @returns A configured sqlite adapter.
265
+ * @example
266
+ * ```ts
267
+ * import BetterSqlite3 from 'better-sqlite3'
268
+ * import { createDatabase } from 'remix/data-table'
269
+ * import { createSqliteDatabaseAdapter } from 'remix/data-table-sqlite'
270
+ *
271
+ * let sqlite = new BetterSqlite3('./data/app.db')
272
+ * let adapter = createSqliteDatabaseAdapter(sqlite)
273
+ * let db = createDatabase(adapter)
274
+ * ```
275
+ */
276
+ export function createSqliteDatabaseAdapter(
277
+ database: BetterSqliteDatabase,
278
+ options?: SqliteDatabaseAdapterOptions,
279
+ ): SqliteDatabaseAdapter {
280
+ return new SqliteDatabaseAdapter(database, options)
281
+ }
282
+
283
+ function normalizeRows(rows: unknown[]): Record<string, unknown>[] {
284
+ return rows.map((row) => {
285
+ if (typeof row !== 'object' || row === null) {
286
+ return {}
287
+ }
288
+
289
+ return { ...(row as Record<string, unknown>) }
290
+ })
291
+ }
292
+
293
+ function normalizeCountRows(rows: Record<string, unknown>[]): Record<string, unknown>[] {
294
+ return rows.map((row) => {
295
+ let count = row.count
296
+
297
+ if (typeof count === 'string') {
298
+ let numeric = Number(count)
299
+
300
+ if (!Number.isNaN(numeric)) {
301
+ return {
302
+ ...row,
303
+ count: numeric,
304
+ }
305
+ }
306
+ }
307
+
308
+ if (typeof count === 'bigint') {
309
+ return {
310
+ ...row,
311
+ count: Number(count),
312
+ }
313
+ }
314
+
315
+ return row
316
+ })
317
+ }
318
+
319
+ function normalizeAffectedRowsForReader(
320
+ kind: DataManipulationRequest['operation']['kind'],
321
+ rows: Record<string, unknown>[],
322
+ ): number | undefined {
323
+ if (isWriteOperationKind(kind)) {
324
+ return rows.length
325
+ }
326
+
327
+ return undefined
328
+ }
329
+
330
+ function normalizeInsertIdForReader(
331
+ kind: DataManipulationRequest['operation']['kind'],
332
+ operation: DataManipulationRequest['operation'],
333
+ rows: Record<string, unknown>[],
334
+ ): unknown {
335
+ if (!isInsertOperationKind(kind) || !isInsertOperation(operation)) {
336
+ return undefined
337
+ }
338
+
339
+ let primaryKey = getTablePrimaryKey(operation.table)
340
+
341
+ if (primaryKey.length !== 1) {
342
+ return undefined
343
+ }
344
+
345
+ let key = primaryKey[0]
346
+ let row = rows[rows.length - 1]
347
+
348
+ return row ? row[key] : undefined
349
+ }
350
+
351
+ function normalizeAffectedRowsForRun(
352
+ kind: DataManipulationRequest['operation']['kind'],
353
+ result: RunResult,
354
+ ): number | undefined {
355
+ if (kind === 'select' || kind === 'count' || kind === 'exists') {
356
+ return undefined
357
+ }
358
+
359
+ return result.changes
360
+ }
361
+
362
+ function normalizeInsertIdForRun(
363
+ kind: DataManipulationRequest['operation']['kind'],
364
+ operation: DataManipulationRequest['operation'],
365
+ result: RunResult,
366
+ ): unknown {
367
+ if (!isInsertOperationKind(kind) || !isInsertOperation(operation)) {
368
+ return undefined
369
+ }
370
+
371
+ if (getTablePrimaryKey(operation.table).length !== 1) {
372
+ return undefined
373
+ }
374
+
375
+ return result.lastInsertRowid
376
+ }
377
+
378
+ function quoteIdentifier(value: string): string {
379
+ return '"' + value.replace(/"/g, '""') + '"'
380
+ }
381
+
382
+ function quoteTableRef(table: TableRef): string {
383
+ return quoteTableRefHelper(table, quoteIdentifier)
384
+ }
385
+
386
+ function quoteLiteral(value: unknown): string {
387
+ return quoteLiteralHelper(value, { booleansAsIntegers: true })
388
+ }
389
+
390
+ function isWriteOperationKind(kind: DataManipulationRequest['operation']['kind']): boolean {
391
+ return (
392
+ kind === 'insert' ||
393
+ kind === 'insertMany' ||
394
+ kind === 'update' ||
395
+ kind === 'delete' ||
396
+ kind === 'upsert'
397
+ )
398
+ }
399
+
400
+ function isInsertOperationKind(kind: DataManipulationRequest['operation']['kind']): boolean {
401
+ return kind === 'insert' || kind === 'insertMany' || kind === 'upsert'
402
+ }
403
+
404
+ function isInsertOperation(
405
+ operation: DataManipulationRequest['operation'],
406
+ ): operation is Extract<
407
+ DataManipulationRequest['operation'],
408
+ { kind: 'insert' | 'insertMany' | 'upsert' }
409
+ > {
410
+ return (
411
+ operation.kind === 'insert' || operation.kind === 'insertMany' || operation.kind === 'upsert'
412
+ )
413
+ }
414
+
415
+ function isDataManipulationOperation(
416
+ operation: DataManipulationOperation | DataMigrationOperation,
417
+ ): operation is DataManipulationOperation {
418
+ return isDataManipulationOperationHelper(operation)
419
+ }
420
+
421
+ function compileSqliteMigrationOperations(operation: DataMigrationOperation): SqlStatement[] {
422
+ if (operation.kind === 'raw') {
423
+ return [{ text: operation.sql.text, values: [...operation.sql.values] }]
424
+ }
425
+
426
+ if (operation.kind === 'createTable') {
427
+ let columns = Object.keys(operation.columns).map(
428
+ (columnName) =>
429
+ quoteIdentifier(columnName) + ' ' + compileSqliteColumn(operation.columns[columnName]),
430
+ )
431
+ let constraints: string[] = []
432
+
433
+ if (operation.primaryKey) {
434
+ constraints.push(
435
+ 'constraint ' +
436
+ quoteIdentifier(operation.primaryKey.name) +
437
+ ' primary key (' +
438
+ operation.primaryKey.columns.map((column) => quoteIdentifier(column)).join(', ') +
439
+ ')',
440
+ )
441
+ }
442
+
443
+ for (let unique of operation.uniques ?? []) {
444
+ constraints.push(
445
+ 'constraint ' +
446
+ quoteIdentifier(unique.name) +
447
+ ' ' +
448
+ 'unique (' +
449
+ unique.columns.map((column) => quoteIdentifier(column)).join(', ') +
450
+ ')',
451
+ )
452
+ }
453
+
454
+ for (let check of operation.checks ?? []) {
455
+ constraints.push(
456
+ 'constraint ' + quoteIdentifier(check.name) + ' ' + 'check (' + check.expression + ')',
457
+ )
458
+ }
459
+
460
+ for (let foreignKey of operation.foreignKeys ?? []) {
461
+ let clause =
462
+ 'constraint ' +
463
+ quoteIdentifier(foreignKey.name) +
464
+ ' ' +
465
+ 'foreign key (' +
466
+ foreignKey.columns.map((column) => quoteIdentifier(column)).join(', ') +
467
+ ') references ' +
468
+ quoteTableRef(foreignKey.references.table) +
469
+ ' (' +
470
+ foreignKey.references.columns.map((column) => quoteIdentifier(column)).join(', ') +
471
+ ')'
472
+
473
+ if (foreignKey.onDelete) {
474
+ clause += ' on delete ' + foreignKey.onDelete
475
+ }
476
+
477
+ if (foreignKey.onUpdate) {
478
+ clause += ' on update ' + foreignKey.onUpdate
479
+ }
480
+
481
+ constraints.push(clause)
482
+ }
483
+
484
+ return [
485
+ {
486
+ text:
487
+ 'create table ' +
488
+ (operation.ifNotExists ? 'if not exists ' : '') +
489
+ quoteTableRef(operation.table) +
490
+ ' (' +
491
+ [...columns, ...constraints].join(', ') +
492
+ ')',
493
+ values: [],
494
+ },
495
+ ]
496
+ }
497
+
498
+ if (operation.kind === 'alterTable') {
499
+ let statements: SqlStatement[] = []
500
+
501
+ for (let change of operation.changes) {
502
+ let sql = 'alter table ' + quoteTableRef(operation.table) + ' '
503
+
504
+ if (change.kind === 'addColumn') {
505
+ sql +=
506
+ 'add column ' +
507
+ quoteIdentifier(change.column) +
508
+ ' ' +
509
+ compileSqliteColumn(change.definition)
510
+ } else if (change.kind === 'changeColumn') {
511
+ sql +=
512
+ 'alter column ' +
513
+ quoteIdentifier(change.column) +
514
+ ' type ' +
515
+ compileSqliteColumnType(change.definition)
516
+ } else if (change.kind === 'renameColumn') {
517
+ sql += 'rename column ' + quoteIdentifier(change.from) + ' to ' + quoteIdentifier(change.to)
518
+ } else if (change.kind === 'dropColumn') {
519
+ sql += 'drop column ' + quoteIdentifier(change.column)
520
+ } else if (change.kind === 'addPrimaryKey') {
521
+ sql +=
522
+ 'add primary key (' +
523
+ change.constraint.columns.map((column) => quoteIdentifier(column)).join(', ') +
524
+ ')'
525
+ } else if (change.kind === 'dropPrimaryKey') {
526
+ sql += 'drop primary key'
527
+ } else if (change.kind === 'addUnique') {
528
+ sql +=
529
+ 'add ' +
530
+ 'constraint ' +
531
+ quoteIdentifier(change.constraint.name) +
532
+ ' ' +
533
+ 'unique (' +
534
+ change.constraint.columns.map((column) => quoteIdentifier(column)).join(', ') +
535
+ ')'
536
+ } else if (change.kind === 'dropUnique') {
537
+ sql += 'drop constraint ' + quoteIdentifier(change.name)
538
+ } else if (change.kind === 'addForeignKey') {
539
+ sql +=
540
+ 'add ' +
541
+ 'constraint ' +
542
+ quoteIdentifier(change.constraint.name) +
543
+ ' ' +
544
+ 'foreign key (' +
545
+ change.constraint.columns.map((column) => quoteIdentifier(column)).join(', ') +
546
+ ') references ' +
547
+ quoteTableRef(change.constraint.references.table) +
548
+ ' (' +
549
+ change.constraint.references.columns.map((column) => quoteIdentifier(column)).join(', ') +
550
+ ')'
551
+ } else if (change.kind === 'dropForeignKey') {
552
+ sql += 'drop constraint ' + quoteIdentifier(change.name)
553
+ } else if (change.kind === 'addCheck') {
554
+ sql +=
555
+ 'add ' +
556
+ 'constraint ' +
557
+ quoteIdentifier(change.constraint.name) +
558
+ ' ' +
559
+ 'check (' +
560
+ change.constraint.expression +
561
+ ')'
562
+ } else if (change.kind === 'dropCheck') {
563
+ sql += 'drop constraint ' + quoteIdentifier(change.name)
564
+ } else if (change.kind === 'setTableComment') {
565
+ continue
566
+ } else {
567
+ continue
568
+ }
569
+
570
+ statements.push({ text: sql, values: [] })
571
+ }
572
+
573
+ return statements
574
+ }
575
+
576
+ if (operation.kind === 'renameTable') {
577
+ return [
578
+ {
579
+ text:
580
+ 'alter table ' +
581
+ quoteTableRef(operation.from) +
582
+ ' rename to ' +
583
+ quoteIdentifier(operation.to.name),
584
+ values: [],
585
+ },
586
+ ]
587
+ }
588
+
589
+ if (operation.kind === 'dropTable') {
590
+ return [
591
+ {
592
+ text:
593
+ 'drop table ' + (operation.ifExists ? 'if exists ' : '') + quoteTableRef(operation.table),
594
+ values: [],
595
+ },
596
+ ]
597
+ }
598
+
599
+ if (operation.kind === 'createIndex') {
600
+ return [
601
+ {
602
+ text:
603
+ 'create ' +
604
+ (operation.index.unique ? 'unique ' : '') +
605
+ 'index ' +
606
+ (operation.ifNotExists ? 'if not exists ' : '') +
607
+ quoteIdentifier(operation.index.name) +
608
+ ' on ' +
609
+ quoteTableRef(operation.index.table) +
610
+ ' (' +
611
+ operation.index.columns.map((column) => quoteIdentifier(column)).join(', ') +
612
+ ')' +
613
+ (operation.index.where ? ' where ' + operation.index.where : ''),
614
+ values: [],
615
+ },
616
+ ]
617
+ }
618
+
619
+ if (operation.kind === 'dropIndex') {
620
+ return [
621
+ {
622
+ text:
623
+ 'drop index ' +
624
+ (operation.ifExists ? 'if exists ' : '') +
625
+ quoteIdentifier(operation.name),
626
+ values: [],
627
+ },
628
+ ]
629
+ }
630
+
631
+ if (operation.kind === 'renameIndex') {
632
+ return [
633
+ {
634
+ text:
635
+ 'alter table ' +
636
+ quoteTableRef(operation.table) +
637
+ ' rename index ' +
638
+ quoteIdentifier(operation.from) +
639
+ ' to ' +
640
+ quoteIdentifier(operation.to),
641
+ values: [],
642
+ },
643
+ ]
644
+ }
645
+
646
+ if (operation.kind === 'addForeignKey') {
647
+ return [
648
+ {
649
+ text:
650
+ 'alter table ' +
651
+ quoteTableRef(operation.table) +
652
+ ' add ' +
653
+ 'constraint ' +
654
+ quoteIdentifier(operation.constraint.name) +
655
+ ' ' +
656
+ 'foreign key (' +
657
+ operation.constraint.columns.map((column) => quoteIdentifier(column)).join(', ') +
658
+ ') references ' +
659
+ quoteTableRef(operation.constraint.references.table) +
660
+ ' (' +
661
+ operation.constraint.references.columns
662
+ .map((column) => quoteIdentifier(column))
663
+ .join(', ') +
664
+ ')' +
665
+ (operation.constraint.onDelete ? ' on delete ' + operation.constraint.onDelete : '') +
666
+ (operation.constraint.onUpdate ? ' on update ' + operation.constraint.onUpdate : ''),
667
+ values: [],
668
+ },
669
+ ]
670
+ }
671
+
672
+ if (operation.kind === 'dropForeignKey') {
673
+ return [
674
+ {
675
+ text:
676
+ 'alter table ' +
677
+ quoteTableRef(operation.table) +
678
+ ' drop constraint ' +
679
+ quoteIdentifier(operation.name),
680
+ values: [],
681
+ },
682
+ ]
683
+ }
684
+
685
+ if (operation.kind === 'addCheck') {
686
+ return [
687
+ {
688
+ text:
689
+ 'alter table ' +
690
+ quoteTableRef(operation.table) +
691
+ ' add ' +
692
+ 'constraint ' +
693
+ quoteIdentifier(operation.constraint.name) +
694
+ ' ' +
695
+ 'check (' +
696
+ operation.constraint.expression +
697
+ ')',
698
+ values: [],
699
+ },
700
+ ]
701
+ }
702
+
703
+ if (operation.kind === 'dropCheck') {
704
+ return [
705
+ {
706
+ text:
707
+ 'alter table ' +
708
+ quoteTableRef(operation.table) +
709
+ ' drop constraint ' +
710
+ quoteIdentifier(operation.name),
711
+ values: [],
712
+ },
713
+ ]
714
+ }
715
+
716
+ throw new Error('Unsupported data migration operation kind')
717
+ }
718
+
719
+ function compileSqliteColumn(definition: ColumnDefinition): string {
720
+ let parts = [compileSqliteColumnType(definition)]
721
+
722
+ if (definition.nullable === false) {
723
+ parts.push('not null')
724
+ }
725
+
726
+ if (definition.default) {
727
+ if (definition.default.kind === 'now') {
728
+ parts.push('default current_timestamp')
729
+ } else if (definition.default.kind === 'sql') {
730
+ parts.push('default ' + definition.default.expression)
731
+ } else {
732
+ parts.push('default ' + quoteLiteral(definition.default.value))
733
+ }
734
+ }
735
+
736
+ if (definition.primaryKey) {
737
+ parts.push('primary key')
738
+ }
739
+
740
+ if (definition.unique) {
741
+ parts.push('unique')
742
+ }
743
+
744
+ if (definition.computed) {
745
+ parts.push('generated always as (' + definition.computed.expression + ')')
746
+ parts.push(definition.computed.stored ? 'stored' : 'virtual')
747
+ }
748
+
749
+ if (definition.references) {
750
+ let clause =
751
+ 'references ' +
752
+ quoteTableRef(definition.references.table) +
753
+ ' (' +
754
+ definition.references.columns.map((column) => quoteIdentifier(column)).join(', ') +
755
+ ')'
756
+
757
+ if (definition.references.onDelete) {
758
+ clause += ' on delete ' + definition.references.onDelete
759
+ }
760
+
761
+ if (definition.references.onUpdate) {
762
+ clause += ' on update ' + definition.references.onUpdate
763
+ }
764
+
765
+ parts.push(clause)
766
+ }
767
+
768
+ if (definition.checks && definition.checks.length > 0) {
769
+ for (let check of definition.checks) {
770
+ parts.push('check (' + check.expression + ')')
771
+ }
772
+ }
773
+
774
+ return parts.join(' ')
775
+ }
776
+
777
+ function compileSqliteColumnType(definition: ColumnDefinition): string {
778
+ if (definition.type === 'varchar') {
779
+ return 'text'
780
+ }
781
+
782
+ if (definition.type === 'text') {
783
+ return 'text'
784
+ }
785
+
786
+ if (definition.type === 'integer') {
787
+ return 'integer'
788
+ }
789
+
790
+ if (definition.type === 'bigint') {
791
+ return 'integer'
792
+ }
793
+
794
+ if (definition.type === 'decimal') {
795
+ return 'numeric'
796
+ }
797
+
798
+ if (definition.type === 'boolean') {
799
+ return 'integer'
800
+ }
801
+
802
+ if (definition.type === 'uuid') {
803
+ return 'text'
804
+ }
805
+
806
+ if (definition.type === 'date') {
807
+ return 'text'
808
+ }
809
+
810
+ if (definition.type === 'time') {
811
+ return 'text'
812
+ }
813
+
814
+ if (definition.type === 'timestamp') {
815
+ return 'text'
816
+ }
817
+
818
+ if (definition.type === 'json') {
819
+ return 'text'
820
+ }
821
+
822
+ if (definition.type === 'binary') {
823
+ return 'blob'
824
+ }
825
+
826
+ if (definition.type === 'enum') {
827
+ return 'text'
828
+ }
829
+
830
+ return 'text'
831
+ }