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