@remix-run/data-table-mysql 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.
- package/LICENSE +21 -0
- package/README.md +89 -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 +69 -0
- package/dist/lib/adapter.d.ts.map +1 -0
- package/dist/lib/adapter.js +194 -0
- package/dist/lib/sql-compiler.d.ts +8 -0
- package/dist/lib/sql-compiler.d.ts.map +1 -0
- package/dist/lib/sql-compiler.js +339 -0
- package/package.json +48 -7
- package/src/index.ts +9 -0
- package/src/lib/adapter.ts +319 -0
- package/src/lib/sql-compiler.ts +476 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { getTableName, getTablePrimaryKey } from '@remix-run/data-table';
|
|
2
|
+
export function compileMysqlStatement(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, context);
|
|
49
|
+
}
|
|
50
|
+
if (statement.kind === 'insertMany') {
|
|
51
|
+
return compileInsertManyStatement(statement.table, statement.values, 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
|
+
values: context.values,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
if (statement.kind === 'delete') {
|
|
67
|
+
return {
|
|
68
|
+
text: 'delete from ' +
|
|
69
|
+
quotePath(getTableName(statement.table)) +
|
|
70
|
+
compileWhereClause(statement.where, context),
|
|
71
|
+
values: context.values,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (statement.kind === 'upsert') {
|
|
75
|
+
return compileUpsertStatement(statement, context);
|
|
76
|
+
}
|
|
77
|
+
throw new Error('Unsupported statement kind');
|
|
78
|
+
}
|
|
79
|
+
function compileInsertStatement(table, values, context) {
|
|
80
|
+
let columns = Object.keys(values);
|
|
81
|
+
if (columns.length === 0) {
|
|
82
|
+
return {
|
|
83
|
+
text: 'insert into ' + quotePath(getTableName(table)) + ' () values ()',
|
|
84
|
+
values: context.values,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
text: 'insert into ' +
|
|
89
|
+
quotePath(getTableName(table)) +
|
|
90
|
+
' (' +
|
|
91
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
92
|
+
') values (' +
|
|
93
|
+
columns.map((column) => pushValue(context, values[column])).join(', ') +
|
|
94
|
+
')',
|
|
95
|
+
values: context.values,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function compileInsertManyStatement(table, rows, context) {
|
|
99
|
+
if (rows.length === 0) {
|
|
100
|
+
return {
|
|
101
|
+
text: 'select 0 where 1 = 0',
|
|
102
|
+
values: context.values,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
let columns = collectColumns(rows);
|
|
106
|
+
if (columns.length === 0) {
|
|
107
|
+
return {
|
|
108
|
+
text: 'insert into ' + quotePath(getTableName(table)) + ' () values ()',
|
|
109
|
+
values: context.values,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
let values = rows.map((row) => '(' +
|
|
113
|
+
columns
|
|
114
|
+
.map((column) => {
|
|
115
|
+
let value = Object.prototype.hasOwnProperty.call(row, column) ? row[column] : null;
|
|
116
|
+
return pushValue(context, value);
|
|
117
|
+
})
|
|
118
|
+
.join(', ') +
|
|
119
|
+
')');
|
|
120
|
+
return {
|
|
121
|
+
text: 'insert into ' +
|
|
122
|
+
quotePath(getTableName(table)) +
|
|
123
|
+
' (' +
|
|
124
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
125
|
+
') values ' +
|
|
126
|
+
values.join(', '),
|
|
127
|
+
values: context.values,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function compileUpsertStatement(statement, context) {
|
|
131
|
+
let insertColumns = Object.keys(statement.values);
|
|
132
|
+
if (insertColumns.length === 0) {
|
|
133
|
+
throw new Error('upsert requires at least one value');
|
|
134
|
+
}
|
|
135
|
+
let updateValues = statement.update ?? statement.values;
|
|
136
|
+
let updateColumns = Object.keys(updateValues);
|
|
137
|
+
let fallbackNoopColumn = getTablePrimaryKey(statement.table)[0];
|
|
138
|
+
let onDuplicate = updateColumns.length > 0
|
|
139
|
+
? updateColumns
|
|
140
|
+
.map((column) => quotePath(column) + ' = ' + pushValue(context, updateValues[column]))
|
|
141
|
+
.join(', ')
|
|
142
|
+
: quotePath(fallbackNoopColumn) + ' = ' + quotePath(fallbackNoopColumn);
|
|
143
|
+
return {
|
|
144
|
+
text: 'insert into ' +
|
|
145
|
+
quotePath(getTableName(statement.table)) +
|
|
146
|
+
' (' +
|
|
147
|
+
insertColumns.map((column) => quotePath(column)).join(', ') +
|
|
148
|
+
') values (' +
|
|
149
|
+
insertColumns.map((column) => pushValue(context, statement.values[column])).join(', ') +
|
|
150
|
+
') on duplicate key update ' +
|
|
151
|
+
onDuplicate,
|
|
152
|
+
values: context.values,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function compileFromClause(table, joins, context) {
|
|
156
|
+
let output = ' from ' + quotePath(getTableName(table));
|
|
157
|
+
for (let join of joins) {
|
|
158
|
+
output +=
|
|
159
|
+
' ' +
|
|
160
|
+
normalizeJoinType(join.type) +
|
|
161
|
+
' join ' +
|
|
162
|
+
quotePath(getTableName(join.table)) +
|
|
163
|
+
' on ' +
|
|
164
|
+
compilePredicate(join.on, context);
|
|
165
|
+
}
|
|
166
|
+
return output;
|
|
167
|
+
}
|
|
168
|
+
function compileWhereClause(predicates, context) {
|
|
169
|
+
if (predicates.length === 0) {
|
|
170
|
+
return '';
|
|
171
|
+
}
|
|
172
|
+
return (' where ' +
|
|
173
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
174
|
+
}
|
|
175
|
+
function compileGroupByClause(columns) {
|
|
176
|
+
if (columns.length === 0) {
|
|
177
|
+
return '';
|
|
178
|
+
}
|
|
179
|
+
return ' group by ' + columns.map((column) => quotePath(column)).join(', ');
|
|
180
|
+
}
|
|
181
|
+
function compileHavingClause(predicates, context) {
|
|
182
|
+
if (predicates.length === 0) {
|
|
183
|
+
return '';
|
|
184
|
+
}
|
|
185
|
+
return (' having ' +
|
|
186
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
187
|
+
}
|
|
188
|
+
function compileOrderByClause(orderBy) {
|
|
189
|
+
if (orderBy.length === 0) {
|
|
190
|
+
return '';
|
|
191
|
+
}
|
|
192
|
+
return (' order by ' +
|
|
193
|
+
orderBy
|
|
194
|
+
.map((clause) => quotePath(clause.column) + ' ' + clause.direction.toUpperCase())
|
|
195
|
+
.join(', '));
|
|
196
|
+
}
|
|
197
|
+
function compileLimitClause(limit) {
|
|
198
|
+
if (limit === undefined) {
|
|
199
|
+
return '';
|
|
200
|
+
}
|
|
201
|
+
return ' limit ' + String(limit);
|
|
202
|
+
}
|
|
203
|
+
function compileOffsetClause(offset) {
|
|
204
|
+
if (offset === undefined) {
|
|
205
|
+
return '';
|
|
206
|
+
}
|
|
207
|
+
return ' offset ' + String(offset);
|
|
208
|
+
}
|
|
209
|
+
function compilePredicate(predicate, context) {
|
|
210
|
+
if (predicate.type === 'comparison') {
|
|
211
|
+
let column = quotePath(predicate.column);
|
|
212
|
+
if (predicate.operator === 'eq') {
|
|
213
|
+
if (predicate.valueType === 'value' &&
|
|
214
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
215
|
+
return column + ' is null';
|
|
216
|
+
}
|
|
217
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
218
|
+
return column + ' = ' + comparisonValue;
|
|
219
|
+
}
|
|
220
|
+
if (predicate.operator === 'ne') {
|
|
221
|
+
if (predicate.valueType === 'value' &&
|
|
222
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
223
|
+
return column + ' is not null';
|
|
224
|
+
}
|
|
225
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
226
|
+
return column + ' <> ' + comparisonValue;
|
|
227
|
+
}
|
|
228
|
+
if (predicate.operator === 'gt') {
|
|
229
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
230
|
+
return column + ' > ' + comparisonValue;
|
|
231
|
+
}
|
|
232
|
+
if (predicate.operator === 'gte') {
|
|
233
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
234
|
+
return column + ' >= ' + comparisonValue;
|
|
235
|
+
}
|
|
236
|
+
if (predicate.operator === 'lt') {
|
|
237
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
238
|
+
return column + ' < ' + comparisonValue;
|
|
239
|
+
}
|
|
240
|
+
if (predicate.operator === 'lte') {
|
|
241
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
242
|
+
return column + ' <= ' + comparisonValue;
|
|
243
|
+
}
|
|
244
|
+
if (predicate.operator === 'in' || predicate.operator === 'notIn') {
|
|
245
|
+
let values = Array.isArray(predicate.value) ? predicate.value : [];
|
|
246
|
+
if (values.length === 0) {
|
|
247
|
+
return predicate.operator === 'in' ? '1 = 0' : '1 = 1';
|
|
248
|
+
}
|
|
249
|
+
let keyword = predicate.operator === 'in' ? 'in' : 'not in';
|
|
250
|
+
return (column +
|
|
251
|
+
' ' +
|
|
252
|
+
keyword +
|
|
253
|
+
' (' +
|
|
254
|
+
values.map((value) => pushValue(context, value)).join(', ') +
|
|
255
|
+
')');
|
|
256
|
+
}
|
|
257
|
+
if (predicate.operator === 'like') {
|
|
258
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
259
|
+
return column + ' like ' + comparisonValue;
|
|
260
|
+
}
|
|
261
|
+
if (predicate.operator === 'ilike') {
|
|
262
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
263
|
+
return 'lower(' + column + ') like lower(' + comparisonValue + ')';
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (predicate.type === 'between') {
|
|
267
|
+
return (quotePath(predicate.column) +
|
|
268
|
+
' between ' +
|
|
269
|
+
pushValue(context, predicate.lower) +
|
|
270
|
+
' and ' +
|
|
271
|
+
pushValue(context, predicate.upper));
|
|
272
|
+
}
|
|
273
|
+
if (predicate.type === 'null') {
|
|
274
|
+
return (quotePath(predicate.column) + (predicate.operator === 'isNull' ? ' is null' : ' is not null'));
|
|
275
|
+
}
|
|
276
|
+
if (predicate.type === 'logical') {
|
|
277
|
+
if (predicate.predicates.length === 0) {
|
|
278
|
+
return predicate.operator === 'and' ? '1 = 1' : '1 = 0';
|
|
279
|
+
}
|
|
280
|
+
let joiner = predicate.operator === 'and' ? ' and ' : ' or ';
|
|
281
|
+
return predicate.predicates
|
|
282
|
+
.map((child) => '(' + compilePredicate(child, context) + ')')
|
|
283
|
+
.join(joiner);
|
|
284
|
+
}
|
|
285
|
+
throw new Error('Unsupported predicate');
|
|
286
|
+
}
|
|
287
|
+
function compileComparisonValue(predicate, context) {
|
|
288
|
+
if (predicate.valueType === 'column') {
|
|
289
|
+
return quotePath(predicate.value);
|
|
290
|
+
}
|
|
291
|
+
return pushValue(context, predicate.value);
|
|
292
|
+
}
|
|
293
|
+
function normalizeJoinType(type) {
|
|
294
|
+
if (type === 'left') {
|
|
295
|
+
return 'left';
|
|
296
|
+
}
|
|
297
|
+
if (type === 'right') {
|
|
298
|
+
return 'right';
|
|
299
|
+
}
|
|
300
|
+
return 'inner';
|
|
301
|
+
}
|
|
302
|
+
function quoteIdentifier(value) {
|
|
303
|
+
return '`' + value.replace(/`/g, '``') + '`';
|
|
304
|
+
}
|
|
305
|
+
function quotePath(path) {
|
|
306
|
+
if (path === '*') {
|
|
307
|
+
return '*';
|
|
308
|
+
}
|
|
309
|
+
return path
|
|
310
|
+
.split('.')
|
|
311
|
+
.map((segment) => {
|
|
312
|
+
if (segment === '*') {
|
|
313
|
+
return '*';
|
|
314
|
+
}
|
|
315
|
+
return quoteIdentifier(segment);
|
|
316
|
+
})
|
|
317
|
+
.join('.');
|
|
318
|
+
}
|
|
319
|
+
function pushValue(context, value) {
|
|
320
|
+
context.values.push(value);
|
|
321
|
+
return '?';
|
|
322
|
+
}
|
|
323
|
+
function collectColumns(rows) {
|
|
324
|
+
let columns = [];
|
|
325
|
+
let seen = new Set();
|
|
326
|
+
for (let row of rows) {
|
|
327
|
+
for (let key in row) {
|
|
328
|
+
if (!Object.prototype.hasOwnProperty.call(row, key)) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (seen.has(key)) {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
seen.add(key);
|
|
335
|
+
columns.push(key);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return columns;
|
|
339
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/data-table-mysql",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MySQL 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,52 @@
|
|
|
10
10
|
"directory": "packages/data-table-mysql"
|
|
11
11
|
},
|
|
12
12
|
"homepage": "https://github.com/remix-run/remix/tree/main/packages/data-table-mysql#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/node": "^24.6.0",
|
|
31
|
+
"@typescript/native-preview": "7.0.0-dev.20251125.1",
|
|
32
|
+
"mysql2": "^3.15.3",
|
|
33
|
+
"@remix-run/data-table": "0.1.0",
|
|
34
|
+
"@remix-run/data-schema": "0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@remix-run/data-table": "^0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"mysql2": "^3.15.3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"mysql2": {
|
|
44
|
+
"optional": true
|
|
45
|
+
}
|
|
46
|
+
},
|
|
13
47
|
"keywords": [
|
|
14
48
|
"remix",
|
|
15
|
-
"
|
|
49
|
+
"orm",
|
|
50
|
+
"mysql",
|
|
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 --disable-warning=ExperimentalWarning --test",
|
|
58
|
+
"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",
|
|
59
|
+
"typecheck": "tsgo --noEmit"
|
|
19
60
|
}
|
|
20
|
-
}
|
|
61
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
MysqlDatabaseAdapterOptions,
|
|
3
|
+
MysqlDatabaseConnection,
|
|
4
|
+
MysqlDatabasePool,
|
|
5
|
+
MysqlQueryResponse,
|
|
6
|
+
MysqlQueryResultHeader,
|
|
7
|
+
MysqlQueryRows,
|
|
8
|
+
} from './lib/adapter.ts'
|
|
9
|
+
export { createMysqlDatabaseAdapter, MysqlDatabaseAdapter } from './lib/adapter.ts'
|