@remix-run/data-table-sqlite 0.1.0 → 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.
@@ -1,34 +1,34 @@
1
1
  import { getTableName, getTablePrimaryKey } from '@remix-run/data-table'
2
- import type { AdapterStatement, Predicate } from '@remix-run/data-table'
2
+ import type { DataManipulationOperation, Predicate, SqlStatement } from '@remix-run/data-table'
3
+ import {
4
+ collectColumns as collectColumnsHelper,
5
+ normalizeJoinType as normalizeJoinTypeHelper,
6
+ quotePath as quotePathHelper,
7
+ } from '@remix-run/data-table/sql-helpers'
3
8
 
4
- type JoinClause = Extract<AdapterStatement, { kind: 'select' }>['joins'][number]
5
- type UpsertStatement = Extract<AdapterStatement, { kind: 'upsert' }>
6
- type StatementTable = Extract<AdapterStatement, { kind: 'select' }>['table']
7
-
8
- type CompiledSql = {
9
- text: string
10
- values: unknown[]
11
- }
9
+ type JoinClause = Extract<DataManipulationOperation, { kind: 'select' }>['joins'][number]
10
+ type UpsertOperation = Extract<DataManipulationOperation, { kind: 'upsert' }>
11
+ type OperationTable = Extract<DataManipulationOperation, { kind: 'select' }>['table']
12
12
 
13
13
  type CompileContext = {
14
14
  values: unknown[]
15
15
  }
16
16
 
17
- export function compileSqliteStatement(statement: AdapterStatement): CompiledSql {
18
- if (statement.kind === 'raw') {
17
+ export function compileSqliteOperation(operation: DataManipulationOperation): SqlStatement {
18
+ if (operation.kind === 'raw') {
19
19
  return {
20
- text: statement.sql.text,
21
- values: [...statement.sql.values],
20
+ text: operation.sql.text,
21
+ values: [...operation.sql.values],
22
22
  }
23
23
  }
24
24
 
25
25
  let context: CompileContext = { values: [] }
26
26
 
27
- if (statement.kind === 'select') {
27
+ if (operation.kind === 'select') {
28
28
  let selection = '*'
29
29
 
30
- if (statement.select !== '*') {
31
- selection = statement.select
30
+ if (operation.select !== '*') {
31
+ selection = operation.select
32
32
  .map((field) => quotePath(field.column) + ' as ' + quoteIdentifier(field.alias))
33
33
  .join(', ')
34
34
  }
@@ -36,26 +36,26 @@ export function compileSqliteStatement(statement: AdapterStatement): CompiledSql
36
36
  return {
37
37
  text:
38
38
  'select ' +
39
- (statement.distinct ? 'distinct ' : '') +
39
+ (operation.distinct ? 'distinct ' : '') +
40
40
  selection +
41
- compileFromClause(statement.table, statement.joins, context) +
42
- compileWhereClause(statement.where, context) +
43
- compileGroupByClause(statement.groupBy) +
44
- compileHavingClause(statement.having, context) +
45
- compileOrderByClause(statement.orderBy) +
46
- compileLimitClause(statement.limit) +
47
- compileOffsetClause(statement.offset),
41
+ compileFromClause(operation.table, operation.joins, context) +
42
+ compileWhereClause(operation.where, context) +
43
+ compileGroupByClause(operation.groupBy) +
44
+ compileHavingClause(operation.having, context) +
45
+ compileOrderByClause(operation.orderBy) +
46
+ compileLimitClause(operation.limit) +
47
+ compileOffsetClause(operation.offset),
48
48
  values: context.values,
49
49
  }
50
50
  }
51
51
 
52
- if (statement.kind === 'count' || statement.kind === 'exists') {
52
+ if (operation.kind === 'count' || operation.kind === 'exists') {
53
53
  let inner =
54
54
  'select 1' +
55
- compileFromClause(statement.table, statement.joins, context) +
56
- compileWhereClause(statement.where, context) +
57
- compileGroupByClause(statement.groupBy) +
58
- compileHavingClause(statement.having, context)
55
+ compileFromClause(operation.table, operation.joins, context) +
56
+ compileWhereClause(operation.where, context) +
57
+ compileGroupByClause(operation.groupBy) +
58
+ compileHavingClause(operation.having, context)
59
59
 
60
60
  return {
61
61
  text:
@@ -69,62 +69,62 @@ export function compileSqliteStatement(statement: AdapterStatement): CompiledSql
69
69
  }
70
70
  }
71
71
 
72
- if (statement.kind === 'insert') {
73
- return compileInsertStatement(statement.table, statement.values, statement.returning, context)
72
+ if (operation.kind === 'insert') {
73
+ return compileInsertOperation(operation.table, operation.values, operation.returning, context)
74
74
  }
75
75
 
76
- if (statement.kind === 'insertMany') {
77
- return compileInsertManyStatement(
78
- statement.table,
79
- statement.values,
80
- statement.returning,
76
+ if (operation.kind === 'insertMany') {
77
+ return compileInsertManyOperation(
78
+ operation.table,
79
+ operation.values,
80
+ operation.returning,
81
81
  context,
82
82
  )
83
83
  }
84
84
 
85
- if (statement.kind === 'update') {
86
- let columns = Object.keys(statement.changes)
85
+ if (operation.kind === 'update') {
86
+ let columns = Object.keys(operation.changes)
87
87
 
88
88
  return {
89
89
  text:
90
90
  'update ' +
91
- quotePath(getTableName(statement.table)) +
91
+ quotePath(getTableName(operation.table)) +
92
92
  ' set ' +
93
93
  columns
94
94
  .map(
95
- (column) => quotePath(column) + ' = ' + pushValue(context, statement.changes[column]),
95
+ (column) => quotePath(column) + ' = ' + pushValue(context, operation.changes[column]),
96
96
  )
97
97
  .join(', ') +
98
- compileWhereClause(statement.where, context) +
99
- compileReturningClause(statement.returning),
98
+ compileWhereClause(operation.where, context) +
99
+ compileReturningClause(operation.returning),
100
100
  values: context.values,
101
101
  }
102
102
  }
103
103
 
104
- if (statement.kind === 'delete') {
104
+ if (operation.kind === 'delete') {
105
105
  return {
106
106
  text:
107
107
  'delete from ' +
108
- quotePath(getTableName(statement.table)) +
109
- compileWhereClause(statement.where, context) +
110
- compileReturningClause(statement.returning),
108
+ quotePath(getTableName(operation.table)) +
109
+ compileWhereClause(operation.where, context) +
110
+ compileReturningClause(operation.returning),
111
111
  values: context.values,
112
112
  }
113
113
  }
114
114
 
115
- if (statement.kind === 'upsert') {
116
- return compileUpsertStatement(statement, context)
115
+ if (operation.kind === 'upsert') {
116
+ return compileUpsertOperation(operation, context)
117
117
  }
118
118
 
119
- throw new Error('Unsupported statement kind')
119
+ throw new Error('Unsupported operation kind')
120
120
  }
121
121
 
122
- function compileInsertStatement(
123
- table: StatementTable,
122
+ function compileInsertOperation(
123
+ table: OperationTable,
124
124
  values: Record<string, unknown>,
125
125
  returning: '*' | string[] | undefined,
126
126
  context: CompileContext,
127
- ): CompiledSql {
127
+ ): SqlStatement {
128
128
  let columns = Object.keys(values)
129
129
 
130
130
  if (columns.length === 0) {
@@ -152,12 +152,12 @@ function compileInsertStatement(
152
152
  }
153
153
  }
154
154
 
155
- function compileInsertManyStatement(
156
- table: StatementTable,
155
+ function compileInsertManyOperation(
156
+ table: OperationTable,
157
157
  rows: Record<string, unknown>[],
158
158
  returning: '*' | string[] | undefined,
159
159
  context: CompileContext,
160
- ): CompiledSql {
160
+ ): SqlStatement {
161
161
  if (rows.length === 0) {
162
162
  return {
163
163
  text: 'select 0 where 1 = 0',
@@ -203,15 +203,15 @@ function compileInsertManyStatement(
203
203
  }
204
204
  }
205
205
 
206
- function compileUpsertStatement(statement: UpsertStatement, context: CompileContext): CompiledSql {
207
- let insertColumns = Object.keys(statement.values)
208
- let conflictTarget = statement.conflictTarget ?? [...getTablePrimaryKey(statement.table)]
206
+ function compileUpsertOperation(operation: UpsertOperation, context: CompileContext): SqlStatement {
207
+ let insertColumns = Object.keys(operation.values)
208
+ let conflictTarget = operation.conflictTarget ?? [...getTablePrimaryKey(operation.table)]
209
209
 
210
210
  if (insertColumns.length === 0) {
211
211
  throw new Error('upsert requires at least one value')
212
212
  }
213
213
 
214
- let updateValues = statement.update ?? statement.values
214
+ let updateValues = operation.update ?? operation.values
215
215
  let updateColumns = Object.keys(updateValues)
216
216
 
217
217
  let conflictClause = ''
@@ -234,20 +234,20 @@ function compileUpsertStatement(statement: UpsertStatement, context: CompileCont
234
234
  return {
235
235
  text:
236
236
  'insert into ' +
237
- quotePath(getTableName(statement.table)) +
237
+ quotePath(getTableName(operation.table)) +
238
238
  ' (' +
239
239
  insertColumns.map((column) => quotePath(column)).join(', ') +
240
240
  ') values (' +
241
- insertColumns.map((column) => pushValue(context, statement.values[column])).join(', ') +
241
+ insertColumns.map((column) => pushValue(context, operation.values[column])).join(', ') +
242
242
  ')' +
243
243
  conflictClause +
244
- compileReturningClause(statement.returning),
244
+ compileReturningClause(operation.returning),
245
245
  values: context.values,
246
246
  }
247
247
  }
248
248
 
249
249
  function compileFromClause(
250
- table: StatementTable,
250
+ table: OperationTable,
251
251
  joins: JoinClause[],
252
252
  context: CompileContext,
253
253
  ): string {
@@ -458,15 +458,7 @@ function compileComparisonValue(
458
458
  }
459
459
 
460
460
  function normalizeJoinType(type: string): string {
461
- if (type === 'left') {
462
- return 'left'
463
- }
464
-
465
- if (type === 'right') {
466
- return 'right'
467
- }
468
-
469
- return 'inner'
461
+ return normalizeJoinTypeHelper(type)
470
462
  }
471
463
 
472
464
  function quoteIdentifier(value: string): string {
@@ -474,20 +466,7 @@ function quoteIdentifier(value: string): string {
474
466
  }
475
467
 
476
468
  function quotePath(path: string): string {
477
- if (path === '*') {
478
- return '*'
479
- }
480
-
481
- return path
482
- .split('.')
483
- .map((segment) => {
484
- if (segment === '*') {
485
- return '*'
486
- }
487
-
488
- return quoteIdentifier(segment)
489
- })
490
- .join('.')
469
+ return quotePathHelper(path, quoteIdentifier)
491
470
  }
492
471
 
493
472
  function pushValue(context: CompileContext, value: unknown): string {
@@ -504,23 +483,5 @@ function normalizeBoundValue(value: unknown): unknown {
504
483
  }
505
484
 
506
485
  function collectColumns(rows: Record<string, unknown>[]): string[] {
507
- let columns: string[] = []
508
- let seen = new Set<string>()
509
-
510
- for (let row of rows) {
511
- for (let key in row) {
512
- if (!Object.prototype.hasOwnProperty.call(row, key)) {
513
- continue
514
- }
515
-
516
- if (seen.has(key)) {
517
- continue
518
- }
519
-
520
- seen.add(key)
521
- columns.push(key)
522
- }
523
- }
524
-
525
- return columns
486
+ return collectColumnsHelper(rows)
526
487
  }