@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.
- package/LICENSE +21 -0
- package/README.md +82 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/adapter.d.ts +119 -0
- package/dist/lib/adapter.d.ts.map +1 -0
- package/dist/lib/adapter.js +652 -0
- package/dist/lib/sql-compiler.d.ts +3 -0
- package/dist/lib/sql-compiler.d.ts.map +1 -0
- package/dist/lib/sql-compiler.js +347 -0
- package/package.json +48 -7
- package/src/index.ts +2 -0
- package/src/lib/adapter.ts +831 -0
- package/src/lib/sql-compiler.ts +487 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { getTableName, getTablePrimaryKey } from '@remix-run/data-table';
|
|
2
|
+
import { collectColumns as collectColumnsHelper, normalizeJoinType as normalizeJoinTypeHelper, quotePath as quotePathHelper, } from '@remix-run/data-table/sql-helpers';
|
|
3
|
+
export function compileSqliteOperation(operation) {
|
|
4
|
+
if (operation.kind === 'raw') {
|
|
5
|
+
return {
|
|
6
|
+
text: operation.sql.text,
|
|
7
|
+
values: [...operation.sql.values],
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
let context = { values: [] };
|
|
11
|
+
if (operation.kind === 'select') {
|
|
12
|
+
let selection = '*';
|
|
13
|
+
if (operation.select !== '*') {
|
|
14
|
+
selection = operation.select
|
|
15
|
+
.map((field) => quotePath(field.column) + ' as ' + quoteIdentifier(field.alias))
|
|
16
|
+
.join(', ');
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
text: 'select ' +
|
|
20
|
+
(operation.distinct ? 'distinct ' : '') +
|
|
21
|
+
selection +
|
|
22
|
+
compileFromClause(operation.table, operation.joins, context) +
|
|
23
|
+
compileWhereClause(operation.where, context) +
|
|
24
|
+
compileGroupByClause(operation.groupBy) +
|
|
25
|
+
compileHavingClause(operation.having, context) +
|
|
26
|
+
compileOrderByClause(operation.orderBy) +
|
|
27
|
+
compileLimitClause(operation.limit) +
|
|
28
|
+
compileOffsetClause(operation.offset),
|
|
29
|
+
values: context.values,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (operation.kind === 'count' || operation.kind === 'exists') {
|
|
33
|
+
let inner = 'select 1' +
|
|
34
|
+
compileFromClause(operation.table, operation.joins, context) +
|
|
35
|
+
compileWhereClause(operation.where, context) +
|
|
36
|
+
compileGroupByClause(operation.groupBy) +
|
|
37
|
+
compileHavingClause(operation.having, context);
|
|
38
|
+
return {
|
|
39
|
+
text: 'select count(*) as ' +
|
|
40
|
+
quoteIdentifier('count') +
|
|
41
|
+
' from (' +
|
|
42
|
+
inner +
|
|
43
|
+
') as ' +
|
|
44
|
+
quoteIdentifier('__dt_count'),
|
|
45
|
+
values: context.values,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (operation.kind === 'insert') {
|
|
49
|
+
return compileInsertOperation(operation.table, operation.values, operation.returning, context);
|
|
50
|
+
}
|
|
51
|
+
if (operation.kind === 'insertMany') {
|
|
52
|
+
return compileInsertManyOperation(operation.table, operation.values, operation.returning, context);
|
|
53
|
+
}
|
|
54
|
+
if (operation.kind === 'update') {
|
|
55
|
+
let columns = Object.keys(operation.changes);
|
|
56
|
+
return {
|
|
57
|
+
text: 'update ' +
|
|
58
|
+
quotePath(getTableName(operation.table)) +
|
|
59
|
+
' set ' +
|
|
60
|
+
columns
|
|
61
|
+
.map((column) => quotePath(column) + ' = ' + pushValue(context, operation.changes[column]))
|
|
62
|
+
.join(', ') +
|
|
63
|
+
compileWhereClause(operation.where, context) +
|
|
64
|
+
compileReturningClause(operation.returning),
|
|
65
|
+
values: context.values,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (operation.kind === 'delete') {
|
|
69
|
+
return {
|
|
70
|
+
text: 'delete from ' +
|
|
71
|
+
quotePath(getTableName(operation.table)) +
|
|
72
|
+
compileWhereClause(operation.where, context) +
|
|
73
|
+
compileReturningClause(operation.returning),
|
|
74
|
+
values: context.values,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (operation.kind === 'upsert') {
|
|
78
|
+
return compileUpsertOperation(operation, context);
|
|
79
|
+
}
|
|
80
|
+
throw new Error('Unsupported operation kind');
|
|
81
|
+
}
|
|
82
|
+
function compileInsertOperation(table, values, returning, context) {
|
|
83
|
+
let columns = Object.keys(values);
|
|
84
|
+
if (columns.length === 0) {
|
|
85
|
+
return {
|
|
86
|
+
text: 'insert into ' +
|
|
87
|
+
quotePath(getTableName(table)) +
|
|
88
|
+
' default values' +
|
|
89
|
+
compileReturningClause(returning),
|
|
90
|
+
values: context.values,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
text: 'insert into ' +
|
|
95
|
+
quotePath(getTableName(table)) +
|
|
96
|
+
' (' +
|
|
97
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
98
|
+
') values (' +
|
|
99
|
+
columns.map((column) => pushValue(context, values[column])).join(', ') +
|
|
100
|
+
')' +
|
|
101
|
+
compileReturningClause(returning),
|
|
102
|
+
values: context.values,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function compileInsertManyOperation(table, rows, returning, context) {
|
|
106
|
+
if (rows.length === 0) {
|
|
107
|
+
return {
|
|
108
|
+
text: 'select 0 where 1 = 0',
|
|
109
|
+
values: context.values,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
let columns = collectColumns(rows);
|
|
113
|
+
if (columns.length === 0) {
|
|
114
|
+
return {
|
|
115
|
+
text: 'insert into ' +
|
|
116
|
+
quotePath(getTableName(table)) +
|
|
117
|
+
' default values' +
|
|
118
|
+
compileReturningClause(returning),
|
|
119
|
+
values: context.values,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
text: 'insert into ' +
|
|
124
|
+
quotePath(getTableName(table)) +
|
|
125
|
+
' (' +
|
|
126
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
127
|
+
') values ' +
|
|
128
|
+
rows
|
|
129
|
+
.map((row) => '(' +
|
|
130
|
+
columns
|
|
131
|
+
.map((column) => {
|
|
132
|
+
let value = Object.prototype.hasOwnProperty.call(row, column) ? row[column] : null;
|
|
133
|
+
return pushValue(context, value);
|
|
134
|
+
})
|
|
135
|
+
.join(', ') +
|
|
136
|
+
')')
|
|
137
|
+
.join(', ') +
|
|
138
|
+
compileReturningClause(returning),
|
|
139
|
+
values: context.values,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function compileUpsertOperation(operation, context) {
|
|
143
|
+
let insertColumns = Object.keys(operation.values);
|
|
144
|
+
let conflictTarget = operation.conflictTarget ?? [...getTablePrimaryKey(operation.table)];
|
|
145
|
+
if (insertColumns.length === 0) {
|
|
146
|
+
throw new Error('upsert requires at least one value');
|
|
147
|
+
}
|
|
148
|
+
let updateValues = operation.update ?? operation.values;
|
|
149
|
+
let updateColumns = Object.keys(updateValues);
|
|
150
|
+
let conflictClause = '';
|
|
151
|
+
if (updateColumns.length === 0) {
|
|
152
|
+
conflictClause =
|
|
153
|
+
' on conflict (' +
|
|
154
|
+
conflictTarget.map((column) => quotePath(column)).join(', ') +
|
|
155
|
+
') do nothing';
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
conflictClause =
|
|
159
|
+
' on conflict (' +
|
|
160
|
+
conflictTarget.map((column) => quotePath(column)).join(', ') +
|
|
161
|
+
') do update set ' +
|
|
162
|
+
updateColumns
|
|
163
|
+
.map((column) => quotePath(column) + ' = ' + pushValue(context, updateValues[column]))
|
|
164
|
+
.join(', ');
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
text: 'insert into ' +
|
|
168
|
+
quotePath(getTableName(operation.table)) +
|
|
169
|
+
' (' +
|
|
170
|
+
insertColumns.map((column) => quotePath(column)).join(', ') +
|
|
171
|
+
') values (' +
|
|
172
|
+
insertColumns.map((column) => pushValue(context, operation.values[column])).join(', ') +
|
|
173
|
+
')' +
|
|
174
|
+
conflictClause +
|
|
175
|
+
compileReturningClause(operation.returning),
|
|
176
|
+
values: context.values,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function compileFromClause(table, joins, context) {
|
|
180
|
+
let output = ' from ' + quotePath(getTableName(table));
|
|
181
|
+
for (let join of joins) {
|
|
182
|
+
output +=
|
|
183
|
+
' ' +
|
|
184
|
+
normalizeJoinType(join.type) +
|
|
185
|
+
' join ' +
|
|
186
|
+
quotePath(getTableName(join.table)) +
|
|
187
|
+
' on ' +
|
|
188
|
+
compilePredicate(join.on, context);
|
|
189
|
+
}
|
|
190
|
+
return output;
|
|
191
|
+
}
|
|
192
|
+
function compileWhereClause(predicates, context) {
|
|
193
|
+
if (predicates.length === 0) {
|
|
194
|
+
return '';
|
|
195
|
+
}
|
|
196
|
+
return (' where ' +
|
|
197
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
198
|
+
}
|
|
199
|
+
function compileGroupByClause(columns) {
|
|
200
|
+
if (columns.length === 0) {
|
|
201
|
+
return '';
|
|
202
|
+
}
|
|
203
|
+
return ' group by ' + columns.map((column) => quotePath(column)).join(', ');
|
|
204
|
+
}
|
|
205
|
+
function compileHavingClause(predicates, context) {
|
|
206
|
+
if (predicates.length === 0) {
|
|
207
|
+
return '';
|
|
208
|
+
}
|
|
209
|
+
return (' having ' +
|
|
210
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
211
|
+
}
|
|
212
|
+
function compileOrderByClause(orderBy) {
|
|
213
|
+
if (orderBy.length === 0) {
|
|
214
|
+
return '';
|
|
215
|
+
}
|
|
216
|
+
return (' order by ' +
|
|
217
|
+
orderBy
|
|
218
|
+
.map((clause) => quotePath(clause.column) + ' ' + clause.direction.toUpperCase())
|
|
219
|
+
.join(', '));
|
|
220
|
+
}
|
|
221
|
+
function compileLimitClause(limit) {
|
|
222
|
+
if (limit === undefined) {
|
|
223
|
+
return '';
|
|
224
|
+
}
|
|
225
|
+
return ' limit ' + String(limit);
|
|
226
|
+
}
|
|
227
|
+
function compileOffsetClause(offset) {
|
|
228
|
+
if (offset === undefined) {
|
|
229
|
+
return '';
|
|
230
|
+
}
|
|
231
|
+
return ' offset ' + String(offset);
|
|
232
|
+
}
|
|
233
|
+
function compileReturningClause(returning) {
|
|
234
|
+
if (!returning) {
|
|
235
|
+
return '';
|
|
236
|
+
}
|
|
237
|
+
if (returning === '*') {
|
|
238
|
+
return ' returning *';
|
|
239
|
+
}
|
|
240
|
+
return ' returning ' + returning.map((column) => quotePath(column)).join(', ');
|
|
241
|
+
}
|
|
242
|
+
function compilePredicate(predicate, context) {
|
|
243
|
+
if (predicate.type === 'comparison') {
|
|
244
|
+
let column = quotePath(predicate.column);
|
|
245
|
+
if (predicate.operator === 'eq') {
|
|
246
|
+
if (predicate.valueType === 'value' &&
|
|
247
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
248
|
+
return column + ' is null';
|
|
249
|
+
}
|
|
250
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
251
|
+
return column + ' = ' + comparisonValue;
|
|
252
|
+
}
|
|
253
|
+
if (predicate.operator === 'ne') {
|
|
254
|
+
if (predicate.valueType === 'value' &&
|
|
255
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
256
|
+
return column + ' is not null';
|
|
257
|
+
}
|
|
258
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
259
|
+
return column + ' <> ' + comparisonValue;
|
|
260
|
+
}
|
|
261
|
+
if (predicate.operator === 'gt') {
|
|
262
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
263
|
+
return column + ' > ' + comparisonValue;
|
|
264
|
+
}
|
|
265
|
+
if (predicate.operator === 'gte') {
|
|
266
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
267
|
+
return column + ' >= ' + comparisonValue;
|
|
268
|
+
}
|
|
269
|
+
if (predicate.operator === 'lt') {
|
|
270
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
271
|
+
return column + ' < ' + comparisonValue;
|
|
272
|
+
}
|
|
273
|
+
if (predicate.operator === 'lte') {
|
|
274
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
275
|
+
return column + ' <= ' + comparisonValue;
|
|
276
|
+
}
|
|
277
|
+
if (predicate.operator === 'in' || predicate.operator === 'notIn') {
|
|
278
|
+
let values = Array.isArray(predicate.value) ? predicate.value : [];
|
|
279
|
+
if (values.length === 0) {
|
|
280
|
+
return predicate.operator === 'in' ? '1 = 0' : '1 = 1';
|
|
281
|
+
}
|
|
282
|
+
let keyword = predicate.operator === 'in' ? 'in' : 'not in';
|
|
283
|
+
return (column +
|
|
284
|
+
' ' +
|
|
285
|
+
keyword +
|
|
286
|
+
' (' +
|
|
287
|
+
values.map((value) => pushValue(context, value)).join(', ') +
|
|
288
|
+
')');
|
|
289
|
+
}
|
|
290
|
+
if (predicate.operator === 'like') {
|
|
291
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
292
|
+
return column + ' like ' + comparisonValue;
|
|
293
|
+
}
|
|
294
|
+
if (predicate.operator === 'ilike') {
|
|
295
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
296
|
+
return 'lower(' + column + ') like lower(' + comparisonValue + ')';
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (predicate.type === 'between') {
|
|
300
|
+
return (quotePath(predicate.column) +
|
|
301
|
+
' between ' +
|
|
302
|
+
pushValue(context, predicate.lower) +
|
|
303
|
+
' and ' +
|
|
304
|
+
pushValue(context, predicate.upper));
|
|
305
|
+
}
|
|
306
|
+
if (predicate.type === 'null') {
|
|
307
|
+
return (quotePath(predicate.column) + (predicate.operator === 'isNull' ? ' is null' : ' is not null'));
|
|
308
|
+
}
|
|
309
|
+
if (predicate.type === 'logical') {
|
|
310
|
+
if (predicate.predicates.length === 0) {
|
|
311
|
+
return predicate.operator === 'and' ? '1 = 1' : '1 = 0';
|
|
312
|
+
}
|
|
313
|
+
let joiner = predicate.operator === 'and' ? ' and ' : ' or ';
|
|
314
|
+
return predicate.predicates
|
|
315
|
+
.map((child) => '(' + compilePredicate(child, context) + ')')
|
|
316
|
+
.join(joiner);
|
|
317
|
+
}
|
|
318
|
+
throw new Error('Unsupported predicate');
|
|
319
|
+
}
|
|
320
|
+
function compileComparisonValue(predicate, context) {
|
|
321
|
+
if (predicate.valueType === 'column') {
|
|
322
|
+
return quotePath(predicate.value);
|
|
323
|
+
}
|
|
324
|
+
return pushValue(context, predicate.value);
|
|
325
|
+
}
|
|
326
|
+
function normalizeJoinType(type) {
|
|
327
|
+
return normalizeJoinTypeHelper(type);
|
|
328
|
+
}
|
|
329
|
+
function quoteIdentifier(value) {
|
|
330
|
+
return '"' + value.replace(/"/g, '""') + '"';
|
|
331
|
+
}
|
|
332
|
+
function quotePath(path) {
|
|
333
|
+
return quotePathHelper(path, quoteIdentifier);
|
|
334
|
+
}
|
|
335
|
+
function pushValue(context, value) {
|
|
336
|
+
context.values.push(normalizeBoundValue(value));
|
|
337
|
+
return '?';
|
|
338
|
+
}
|
|
339
|
+
function normalizeBoundValue(value) {
|
|
340
|
+
if (typeof value === 'boolean') {
|
|
341
|
+
return value ? 1 : 0;
|
|
342
|
+
}
|
|
343
|
+
return value;
|
|
344
|
+
}
|
|
345
|
+
function collectColumns(rows) {
|
|
346
|
+
return collectColumnsHelper(rows);
|
|
347
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/data-table-sqlite",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "SQLite adapter for remix/data-table",
|
|
5
|
+
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
@@ -10,11 +10,52 @@
|
|
|
10
10
|
"directory": "packages/data-table-sqlite"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://github.com/remix-run/remix/tree/main/packages/data-table-sqlite#readme",
|
|
13
|
+
"files": [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"README.md",
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"!src/**/*.test.ts"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
31
|
+
"@types/node": "^24.6.0",
|
|
32
|
+
"@typescript/native-preview": "7.0.0-dev.20251125.1",
|
|
33
|
+
"better-sqlite3": "^12.4.1",
|
|
34
|
+
"@remix-run/data-table": "0.2.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@remix-run/data-table": "^0.2.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"better-sqlite3": "^12.4.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"better-sqlite3": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
13
47
|
"keywords": [
|
|
14
48
|
"remix",
|
|
15
|
-
"
|
|
49
|
+
"orm",
|
|
50
|
+
"sqlite",
|
|
51
|
+
"database",
|
|
52
|
+
"sql"
|
|
16
53
|
],
|
|
17
|
-
"
|
|
18
|
-
"
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsgo -p tsconfig.build.json",
|
|
56
|
+
"clean": "git clean -fdX",
|
|
57
|
+
"test": "node --test",
|
|
58
|
+
"test:coverage": "node --experimental-test-coverage --test-coverage-include='src/**/*.ts' --test-coverage-lines=90 --test-coverage-branches=90 --test-coverage-functions=90 --test ./src/lib/adapter.test.ts ./src/lib/sql-compiler.test.ts",
|
|
59
|
+
"typecheck": "tsgo --noEmit"
|
|
19
60
|
}
|
|
20
|
-
}
|
|
61
|
+
}
|
package/src/index.ts
ADDED