@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.
Files changed (111) hide show
  1. package/README.md +307 -56
  2. package/dist/index.d.ts +9 -5
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +4 -2
  5. package/dist/lib/adapter.d.ts +386 -16
  6. package/dist/lib/adapter.d.ts.map +1 -1
  7. package/dist/lib/column.d.ts +193 -0
  8. package/dist/lib/column.d.ts.map +1 -0
  9. package/dist/lib/column.js +302 -0
  10. package/dist/lib/database/execution-context.d.ts +10 -0
  11. package/dist/lib/database/execution-context.d.ts.map +1 -0
  12. package/dist/lib/database/execution-context.js +1 -0
  13. package/dist/lib/database/helpers.d.ts +26 -0
  14. package/dist/lib/database/helpers.d.ts.map +1 -0
  15. package/dist/lib/database/helpers.js +116 -0
  16. package/dist/lib/database/query-execution.d.ts +7 -0
  17. package/dist/lib/database/query-execution.d.ts.map +1 -0
  18. package/dist/lib/database/query-execution.js +401 -0
  19. package/dist/lib/database/relations.d.ts +4 -0
  20. package/dist/lib/database/relations.d.ts.map +1 -0
  21. package/dist/lib/database/relations.js +207 -0
  22. package/dist/lib/database/write-lifecycle.d.ts +13 -0
  23. package/dist/lib/database/write-lifecycle.d.ts.map +1 -0
  24. package/dist/lib/database/write-lifecycle.js +279 -0
  25. package/dist/lib/database.d.ts +141 -238
  26. package/dist/lib/database.d.ts.map +1 -1
  27. package/dist/lib/database.js +73 -1122
  28. package/dist/lib/errors.d.ts +9 -0
  29. package/dist/lib/errors.d.ts.map +1 -1
  30. package/dist/lib/errors.js +9 -0
  31. package/dist/lib/migrations/filename.d.ts +12 -0
  32. package/dist/lib/migrations/filename.d.ts.map +1 -0
  33. package/dist/lib/migrations/filename.js +20 -0
  34. package/dist/lib/migrations/helpers.d.ts +11 -0
  35. package/dist/lib/migrations/helpers.d.ts.map +1 -0
  36. package/dist/lib/migrations/helpers.js +77 -0
  37. package/dist/lib/migrations/journal-store.d.ts +15 -0
  38. package/dist/lib/migrations/journal-store.d.ts.map +1 -0
  39. package/dist/lib/migrations/journal-store.js +83 -0
  40. package/dist/lib/migrations/registry.d.ts +27 -0
  41. package/dist/lib/migrations/registry.d.ts.map +1 -0
  42. package/dist/lib/migrations/registry.js +51 -0
  43. package/dist/lib/migrations/runner.d.ts +20 -0
  44. package/dist/lib/migrations/runner.d.ts.map +1 -0
  45. package/dist/lib/migrations/runner.js +273 -0
  46. package/dist/lib/migrations/schema-api.d.ts +7 -0
  47. package/dist/lib/migrations/schema-api.d.ts.map +1 -0
  48. package/dist/lib/migrations/schema-api.js +326 -0
  49. package/dist/lib/migrations-node.d.ts +17 -0
  50. package/dist/lib/migrations-node.d.ts.map +1 -0
  51. package/dist/lib/migrations-node.js +65 -0
  52. package/dist/lib/migrations.d.ts +292 -0
  53. package/dist/lib/migrations.d.ts.map +1 -0
  54. package/dist/lib/migrations.js +38 -0
  55. package/dist/lib/operators.d.ts +3 -0
  56. package/dist/lib/operators.d.ts.map +1 -1
  57. package/dist/lib/query.d.ts +159 -0
  58. package/dist/lib/query.d.ts.map +1 -0
  59. package/dist/lib/query.js +401 -0
  60. package/dist/lib/references.d.ts +0 -1
  61. package/dist/lib/references.d.ts.map +1 -1
  62. package/dist/lib/sql-helpers.d.ts +50 -0
  63. package/dist/lib/sql-helpers.d.ts.map +1 -0
  64. package/dist/lib/sql-helpers.js +111 -0
  65. package/dist/lib/sql.d.ts +23 -6
  66. package/dist/lib/sql.d.ts.map +1 -1
  67. package/dist/lib/sql.js +19 -5
  68. package/dist/lib/table.d.ts +355 -40
  69. package/dist/lib/table.d.ts.map +1 -1
  70. package/dist/lib/table.js +113 -90
  71. package/dist/migrations/node.d.ts +2 -0
  72. package/dist/migrations/node.d.ts.map +1 -0
  73. package/dist/migrations/node.js +1 -0
  74. package/dist/migrations.d.ts +8 -0
  75. package/dist/migrations.d.ts.map +1 -0
  76. package/dist/migrations.js +5 -0
  77. package/dist/operators.d.ts +3 -0
  78. package/dist/operators.d.ts.map +1 -0
  79. package/dist/operators.js +1 -0
  80. package/dist/sql-helpers.d.ts +3 -0
  81. package/dist/sql-helpers.d.ts.map +1 -0
  82. package/dist/sql-helpers.js +1 -0
  83. package/package.json +24 -10
  84. package/src/index.ts +93 -10
  85. package/src/lib/adapter.ts +469 -25
  86. package/src/lib/column.ts +384 -0
  87. package/src/lib/database/execution-context.ts +15 -0
  88. package/src/lib/database/helpers.ts +216 -0
  89. package/src/lib/database/query-execution.ts +638 -0
  90. package/src/lib/database/relations.ts +332 -0
  91. package/src/lib/database/write-lifecycle.ts +487 -0
  92. package/src/lib/database.ts +246 -1848
  93. package/src/lib/errors.ts +10 -0
  94. package/src/lib/migrations/filename.ts +25 -0
  95. package/src/lib/migrations/helpers.ts +108 -0
  96. package/src/lib/migrations/journal-store.ts +122 -0
  97. package/src/lib/migrations/registry.ts +62 -0
  98. package/src/lib/migrations/runner.ts +374 -0
  99. package/src/lib/migrations/schema-api.ts +417 -0
  100. package/src/lib/migrations-node.ts +71 -0
  101. package/src/lib/migrations.ts +328 -0
  102. package/src/lib/operators.ts +3 -0
  103. package/src/lib/query.ts +958 -0
  104. package/src/lib/references.ts +0 -1
  105. package/src/lib/sql-helpers.ts +146 -0
  106. package/src/lib/sql.ts +23 -6
  107. package/src/lib/table.ts +484 -156
  108. package/src/migrations/node.ts +1 -0
  109. package/src/migrations.ts +26 -0
  110. package/src/operators.ts +18 -0
  111. package/src/sql-helpers.ts +9 -0
@@ -35,7 +35,6 @@ export type ColumnReferenceLike<qualifiedName extends string = string> = {
35
35
  tableName: string
36
36
  columnName: string
37
37
  qualifiedName: qualifiedName
38
- schema: unknown
39
38
  }
40
39
  }
41
40
 
@@ -0,0 +1,146 @@
1
+ import type { DataManipulationOperation, DataMigrationOperation, TableRef } from './adapter.ts'
2
+
3
+ /**
4
+ * Function used to quote SQL identifiers for a dialect.
5
+ */
6
+ export type QuoteIdentifier = (value: string) => string
7
+
8
+ /**
9
+ * Type guard that narrows an operation to the data-manipulation union.
10
+ * @param operation Operation to inspect.
11
+ * @returns `true` when the operation is a data-manipulation operation.
12
+ */
13
+ export function isDataManipulationOperation(
14
+ operation: DataManipulationOperation | DataMigrationOperation,
15
+ ): operation is DataManipulationOperation {
16
+ return (
17
+ operation.kind === 'select' ||
18
+ operation.kind === 'count' ||
19
+ operation.kind === 'exists' ||
20
+ operation.kind === 'insert' ||
21
+ operation.kind === 'insertMany' ||
22
+ operation.kind === 'update' ||
23
+ operation.kind === 'delete' ||
24
+ operation.kind === 'upsert' ||
25
+ operation.kind === 'raw'
26
+ )
27
+ }
28
+
29
+ /**
30
+ * Normalizes an arbitrary join type string into `inner`, `left`, or `right`.
31
+ * @param type Input join type.
32
+ * @returns Normalized join type.
33
+ */
34
+ export function normalizeJoinType(type: string): string {
35
+ if (type === 'left') {
36
+ return 'left'
37
+ }
38
+
39
+ if (type === 'right') {
40
+ return 'right'
41
+ }
42
+
43
+ return 'inner'
44
+ }
45
+
46
+ /**
47
+ * Returns stable column order from the union of keys in the provided rows.
48
+ * @param rows Row objects to scan for keys.
49
+ * @returns Deduplicated key list in encounter order.
50
+ */
51
+ export function collectColumns(rows: Record<string, unknown>[]): string[] {
52
+ let columns: string[] = []
53
+ let seen = new Set<string>()
54
+
55
+ for (let row of rows) {
56
+ for (let key in row) {
57
+ if (!Object.prototype.hasOwnProperty.call(row, key)) {
58
+ continue
59
+ }
60
+
61
+ if (seen.has(key)) {
62
+ continue
63
+ }
64
+
65
+ seen.add(key)
66
+ columns.push(key)
67
+ }
68
+ }
69
+
70
+ return columns
71
+ }
72
+
73
+ /**
74
+ * Quotes each segment of a dotted identifier path.
75
+ *
76
+ * Wildcard segments (`*`) are preserved.
77
+ * @param path Dotted path to quote.
78
+ * @param quoteIdentifier Dialect-specific identifier quote function.
79
+ * @returns Quoted path string.
80
+ */
81
+ export function quotePath(path: string, quoteIdentifier: QuoteIdentifier): string {
82
+ if (path === '*') {
83
+ return '*'
84
+ }
85
+
86
+ return path
87
+ .split('.')
88
+ .map((segment) => {
89
+ if (segment === '*') {
90
+ return '*'
91
+ }
92
+
93
+ return quoteIdentifier(segment)
94
+ })
95
+ .join('.')
96
+ }
97
+
98
+ /**
99
+ * Quotes a `{ schema?, name }` table reference using a dialect quote function.
100
+ * @param table Table reference to quote.
101
+ * @param quoteIdentifier Dialect-specific identifier quote function.
102
+ * @returns Quoted table reference.
103
+ */
104
+ export function quoteTableRef(table: TableRef, quoteIdentifier: QuoteIdentifier): string {
105
+ if (table.schema) {
106
+ return quoteIdentifier(table.schema) + '.' + quoteIdentifier(table.name)
107
+ }
108
+
109
+ return quoteIdentifier(table.name)
110
+ }
111
+
112
+ /**
113
+ * Converts a JavaScript value into a SQL literal string.
114
+ * @param value Value to serialize.
115
+ * @param options Serialization options.
116
+ * @param options.booleansAsIntegers When `true`, booleans render as `1`/`0`.
117
+ * @returns SQL literal text.
118
+ */
119
+ export function quoteLiteral(
120
+ value: unknown,
121
+ options?: {
122
+ booleansAsIntegers?: boolean
123
+ },
124
+ ): string {
125
+ if (value === null) {
126
+ return 'null'
127
+ }
128
+
129
+ if (typeof value === 'number' || typeof value === 'bigint') {
130
+ return String(value)
131
+ }
132
+
133
+ if (typeof value === 'boolean') {
134
+ if (options?.booleansAsIntegers) {
135
+ return value ? '1' : '0'
136
+ }
137
+
138
+ return value ? 'true' : 'false'
139
+ }
140
+
141
+ if (value instanceof Date) {
142
+ return quoteLiteral(value.toISOString(), options)
143
+ }
144
+
145
+ return "'" + String(value).replace(/'/g, "''") + "'"
146
+ }
package/src/lib/sql.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  /**
2
- * Parameterized SQL payload with positional `?` placeholders.
2
+ * Parameterized SQL payload.
3
+ *
4
+ * The `text` may contain positional placeholders (`?`) or dialect-native
5
+ * placeholders (for example `$1`, `$2`) depending on compiler stage.
3
6
  */
4
7
  export type SqlStatement = {
5
8
  text: string
@@ -9,8 +12,16 @@ export type SqlStatement = {
9
12
  /**
10
13
  * Tagged-template helper for building parameterized SQL statements.
11
14
  * @param strings Template string parts.
12
- * @param values Interpolated values or nested `SqlStatement` values.
13
- * @returns A normalized SQL statement.
15
+ * @param values Interpolated values or nested {@link SqlStatement} values.
16
+ * @returns A normalized {@link SqlStatement}.
17
+ * @example
18
+ * ```ts
19
+ * import { sql } from 'remix/data-table'
20
+ *
21
+ * let email = 'user@example.com'
22
+ * let statement = sql`select * from users where email = ${email}`
23
+ * // => { text: 'select * from users where email = ?', values: ['user@example.com'] }
24
+ * ```
14
25
  */
15
26
  export function sql(strings: TemplateStringsArray, ...values: unknown[]): SqlStatement {
16
27
  let text = ''
@@ -42,9 +53,9 @@ export function sql(strings: TemplateStringsArray, ...values: unknown[]): SqlSta
42
53
  }
43
54
 
44
55
  /**
45
- * Returns `true` when a value matches the `SqlStatement` shape.
56
+ * Returns `true` when a value matches the {@link SqlStatement} shape.
46
57
  * @param value Value to inspect.
47
- * @returns Whether the value is a SQL statement object.
58
+ * @returns Whether the value is a {@link SqlStatement} object.
48
59
  */
49
60
  export function isSqlStatement(value: unknown): value is SqlStatement {
50
61
  if (typeof value !== 'object' || value === null) {
@@ -58,9 +69,15 @@ export function isSqlStatement(value: unknown): value is SqlStatement {
58
69
 
59
70
  /**
60
71
  * Creates a SQL statement from raw text and values.
61
- * @param text SQL text containing `?` placeholders.
72
+ * @param text SQL text containing placeholders expected by the target adapter.
62
73
  * @param values Placeholder values.
63
74
  * @returns A normalized SQL statement.
75
+ * @example
76
+ * ```ts
77
+ * import { rawSql } from 'remix/data-table'
78
+ *
79
+ * let statement = rawSql('select * from users where id = ?', [1])
80
+ * ```
64
81
  */
65
82
  export function rawSql(text: string, values: unknown[] = []): SqlStatement {
66
83
  return { text, values }