@remix-run/data-table 0.1.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.
- package/README.md +306 -55
- 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 +23 -8
- 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
package/src/lib/references.ts
CHANGED
|
@@ -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
|
|
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
|
|
13
|
-
* @returns A normalized
|
|
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
|
|
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
|
|
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
|
|
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 }
|