@remix-run/data-table-mysql 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 +96 -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 +132 -0
- package/dist/lib/adapter.d.ts.map +1 -0
- package/dist/lib/adapter.js +724 -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 +309 -0
- package/package.json +47 -7
- package/src/index.ts +2 -0
- package/src/lib/adapter.ts +931 -0
- package/src/lib/sql-compiler.ts +437 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-compiler.d.ts","sourceRoot":"","sources":["../../src/lib/sql-compiler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAa,YAAY,EAAE,MAAM,uBAAuB,CAAA;AAe/F,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,yBAAyB,GAAG,YAAY,CAgGxF"}
|
|
@@ -0,0 +1,309 @@
|
|
|
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 compileMysqlOperation(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, context);
|
|
50
|
+
}
|
|
51
|
+
if (operation.kind === 'insertMany') {
|
|
52
|
+
return compileInsertManyOperation(operation.table, operation.values, 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
|
+
values: context.values,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (operation.kind === 'delete') {
|
|
68
|
+
return {
|
|
69
|
+
text: 'delete from ' +
|
|
70
|
+
quotePath(getTableName(operation.table)) +
|
|
71
|
+
compileWhereClause(operation.where, context),
|
|
72
|
+
values: context.values,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (operation.kind === 'upsert') {
|
|
76
|
+
return compileUpsertOperation(operation, context);
|
|
77
|
+
}
|
|
78
|
+
throw new Error('Unsupported operation kind');
|
|
79
|
+
}
|
|
80
|
+
function compileInsertOperation(table, values, context) {
|
|
81
|
+
let columns = Object.keys(values);
|
|
82
|
+
if (columns.length === 0) {
|
|
83
|
+
return {
|
|
84
|
+
text: 'insert into ' + quotePath(getTableName(table)) + ' () values ()',
|
|
85
|
+
values: context.values,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
text: 'insert into ' +
|
|
90
|
+
quotePath(getTableName(table)) +
|
|
91
|
+
' (' +
|
|
92
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
93
|
+
') values (' +
|
|
94
|
+
columns.map((column) => pushValue(context, values[column])).join(', ') +
|
|
95
|
+
')',
|
|
96
|
+
values: context.values,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function compileInsertManyOperation(table, rows, context) {
|
|
100
|
+
if (rows.length === 0) {
|
|
101
|
+
return {
|
|
102
|
+
text: 'select 0 where 1 = 0',
|
|
103
|
+
values: context.values,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
let columns = collectColumns(rows);
|
|
107
|
+
if (columns.length === 0) {
|
|
108
|
+
return {
|
|
109
|
+
text: 'insert into ' + quotePath(getTableName(table)) + ' () values ()',
|
|
110
|
+
values: context.values,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
let values = rows.map((row) => '(' +
|
|
114
|
+
columns
|
|
115
|
+
.map((column) => {
|
|
116
|
+
let value = Object.prototype.hasOwnProperty.call(row, column) ? row[column] : null;
|
|
117
|
+
return pushValue(context, value);
|
|
118
|
+
})
|
|
119
|
+
.join(', ') +
|
|
120
|
+
')');
|
|
121
|
+
return {
|
|
122
|
+
text: 'insert into ' +
|
|
123
|
+
quotePath(getTableName(table)) +
|
|
124
|
+
' (' +
|
|
125
|
+
columns.map((column) => quotePath(column)).join(', ') +
|
|
126
|
+
') values ' +
|
|
127
|
+
values.join(', '),
|
|
128
|
+
values: context.values,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function compileUpsertOperation(operation, context) {
|
|
132
|
+
let insertColumns = Object.keys(operation.values);
|
|
133
|
+
if (insertColumns.length === 0) {
|
|
134
|
+
throw new Error('upsert requires at least one value');
|
|
135
|
+
}
|
|
136
|
+
let updateValues = operation.update ?? operation.values;
|
|
137
|
+
let updateColumns = Object.keys(updateValues);
|
|
138
|
+
let fallbackNoopColumn = getTablePrimaryKey(operation.table)[0];
|
|
139
|
+
let onDuplicate = updateColumns.length > 0
|
|
140
|
+
? updateColumns
|
|
141
|
+
.map((column) => quotePath(column) + ' = ' + pushValue(context, updateValues[column]))
|
|
142
|
+
.join(', ')
|
|
143
|
+
: quotePath(fallbackNoopColumn) + ' = ' + quotePath(fallbackNoopColumn);
|
|
144
|
+
return {
|
|
145
|
+
text: 'insert into ' +
|
|
146
|
+
quotePath(getTableName(operation.table)) +
|
|
147
|
+
' (' +
|
|
148
|
+
insertColumns.map((column) => quotePath(column)).join(', ') +
|
|
149
|
+
') values (' +
|
|
150
|
+
insertColumns.map((column) => pushValue(context, operation.values[column])).join(', ') +
|
|
151
|
+
') on duplicate key update ' +
|
|
152
|
+
onDuplicate,
|
|
153
|
+
values: context.values,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function compileFromClause(table, joins, context) {
|
|
157
|
+
let output = ' from ' + quotePath(getTableName(table));
|
|
158
|
+
for (let join of joins) {
|
|
159
|
+
output +=
|
|
160
|
+
' ' +
|
|
161
|
+
normalizeJoinType(join.type) +
|
|
162
|
+
' join ' +
|
|
163
|
+
quotePath(getTableName(join.table)) +
|
|
164
|
+
' on ' +
|
|
165
|
+
compilePredicate(join.on, context);
|
|
166
|
+
}
|
|
167
|
+
return output;
|
|
168
|
+
}
|
|
169
|
+
function compileWhereClause(predicates, context) {
|
|
170
|
+
if (predicates.length === 0) {
|
|
171
|
+
return '';
|
|
172
|
+
}
|
|
173
|
+
return (' where ' +
|
|
174
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
175
|
+
}
|
|
176
|
+
function compileGroupByClause(columns) {
|
|
177
|
+
if (columns.length === 0) {
|
|
178
|
+
return '';
|
|
179
|
+
}
|
|
180
|
+
return ' group by ' + columns.map((column) => quotePath(column)).join(', ');
|
|
181
|
+
}
|
|
182
|
+
function compileHavingClause(predicates, context) {
|
|
183
|
+
if (predicates.length === 0) {
|
|
184
|
+
return '';
|
|
185
|
+
}
|
|
186
|
+
return (' having ' +
|
|
187
|
+
predicates.map((predicate) => '(' + compilePredicate(predicate, context) + ')').join(' and '));
|
|
188
|
+
}
|
|
189
|
+
function compileOrderByClause(orderBy) {
|
|
190
|
+
if (orderBy.length === 0) {
|
|
191
|
+
return '';
|
|
192
|
+
}
|
|
193
|
+
return (' order by ' +
|
|
194
|
+
orderBy
|
|
195
|
+
.map((clause) => quotePath(clause.column) + ' ' + clause.direction.toUpperCase())
|
|
196
|
+
.join(', '));
|
|
197
|
+
}
|
|
198
|
+
function compileLimitClause(limit) {
|
|
199
|
+
if (limit === undefined) {
|
|
200
|
+
return '';
|
|
201
|
+
}
|
|
202
|
+
return ' limit ' + String(limit);
|
|
203
|
+
}
|
|
204
|
+
function compileOffsetClause(offset) {
|
|
205
|
+
if (offset === undefined) {
|
|
206
|
+
return '';
|
|
207
|
+
}
|
|
208
|
+
return ' offset ' + String(offset);
|
|
209
|
+
}
|
|
210
|
+
function compilePredicate(predicate, context) {
|
|
211
|
+
if (predicate.type === 'comparison') {
|
|
212
|
+
let column = quotePath(predicate.column);
|
|
213
|
+
if (predicate.operator === 'eq') {
|
|
214
|
+
if (predicate.valueType === 'value' &&
|
|
215
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
216
|
+
return column + ' is null';
|
|
217
|
+
}
|
|
218
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
219
|
+
return column + ' = ' + comparisonValue;
|
|
220
|
+
}
|
|
221
|
+
if (predicate.operator === 'ne') {
|
|
222
|
+
if (predicate.valueType === 'value' &&
|
|
223
|
+
(predicate.value === null || predicate.value === undefined)) {
|
|
224
|
+
return column + ' is not null';
|
|
225
|
+
}
|
|
226
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
227
|
+
return column + ' <> ' + comparisonValue;
|
|
228
|
+
}
|
|
229
|
+
if (predicate.operator === 'gt') {
|
|
230
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
231
|
+
return column + ' > ' + comparisonValue;
|
|
232
|
+
}
|
|
233
|
+
if (predicate.operator === 'gte') {
|
|
234
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
235
|
+
return column + ' >= ' + comparisonValue;
|
|
236
|
+
}
|
|
237
|
+
if (predicate.operator === 'lt') {
|
|
238
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
239
|
+
return column + ' < ' + comparisonValue;
|
|
240
|
+
}
|
|
241
|
+
if (predicate.operator === 'lte') {
|
|
242
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
243
|
+
return column + ' <= ' + comparisonValue;
|
|
244
|
+
}
|
|
245
|
+
if (predicate.operator === 'in' || predicate.operator === 'notIn') {
|
|
246
|
+
let values = Array.isArray(predicate.value) ? predicate.value : [];
|
|
247
|
+
if (values.length === 0) {
|
|
248
|
+
return predicate.operator === 'in' ? '1 = 0' : '1 = 1';
|
|
249
|
+
}
|
|
250
|
+
let keyword = predicate.operator === 'in' ? 'in' : 'not in';
|
|
251
|
+
return (column +
|
|
252
|
+
' ' +
|
|
253
|
+
keyword +
|
|
254
|
+
' (' +
|
|
255
|
+
values.map((value) => pushValue(context, value)).join(', ') +
|
|
256
|
+
')');
|
|
257
|
+
}
|
|
258
|
+
if (predicate.operator === 'like') {
|
|
259
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
260
|
+
return column + ' like ' + comparisonValue;
|
|
261
|
+
}
|
|
262
|
+
if (predicate.operator === 'ilike') {
|
|
263
|
+
let comparisonValue = compileComparisonValue(predicate, context);
|
|
264
|
+
return 'lower(' + column + ') like lower(' + comparisonValue + ')';
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (predicate.type === 'between') {
|
|
268
|
+
return (quotePath(predicate.column) +
|
|
269
|
+
' between ' +
|
|
270
|
+
pushValue(context, predicate.lower) +
|
|
271
|
+
' and ' +
|
|
272
|
+
pushValue(context, predicate.upper));
|
|
273
|
+
}
|
|
274
|
+
if (predicate.type === 'null') {
|
|
275
|
+
return (quotePath(predicate.column) + (predicate.operator === 'isNull' ? ' is null' : ' is not null'));
|
|
276
|
+
}
|
|
277
|
+
if (predicate.type === 'logical') {
|
|
278
|
+
if (predicate.predicates.length === 0) {
|
|
279
|
+
return predicate.operator === 'and' ? '1 = 1' : '1 = 0';
|
|
280
|
+
}
|
|
281
|
+
let joiner = predicate.operator === 'and' ? ' and ' : ' or ';
|
|
282
|
+
return predicate.predicates
|
|
283
|
+
.map((child) => '(' + compilePredicate(child, context) + ')')
|
|
284
|
+
.join(joiner);
|
|
285
|
+
}
|
|
286
|
+
throw new Error('Unsupported predicate');
|
|
287
|
+
}
|
|
288
|
+
function compileComparisonValue(predicate, context) {
|
|
289
|
+
if (predicate.valueType === 'column') {
|
|
290
|
+
return quotePath(predicate.value);
|
|
291
|
+
}
|
|
292
|
+
return pushValue(context, predicate.value);
|
|
293
|
+
}
|
|
294
|
+
function normalizeJoinType(type) {
|
|
295
|
+
return normalizeJoinTypeHelper(type);
|
|
296
|
+
}
|
|
297
|
+
function quoteIdentifier(value) {
|
|
298
|
+
return '`' + value.replace(/`/g, '``') + '`';
|
|
299
|
+
}
|
|
300
|
+
function quotePath(path) {
|
|
301
|
+
return quotePathHelper(path, quoteIdentifier);
|
|
302
|
+
}
|
|
303
|
+
function pushValue(context, value) {
|
|
304
|
+
context.values.push(value);
|
|
305
|
+
return '?';
|
|
306
|
+
}
|
|
307
|
+
function collectColumns(rows) {
|
|
308
|
+
return collectColumnsHelper(rows);
|
|
309
|
+
}
|
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.2.0",
|
|
4
|
+
"description": "MySQL 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,51 @@
|
|
|
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.2.0"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@remix-run/data-table": "^0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"mysql2": "^3.15.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependenciesMeta": {
|
|
42
|
+
"mysql2": {
|
|
43
|
+
"optional": true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
13
46
|
"keywords": [
|
|
14
47
|
"remix",
|
|
15
|
-
"
|
|
48
|
+
"orm",
|
|
49
|
+
"mysql",
|
|
50
|
+
"database",
|
|
51
|
+
"sql"
|
|
16
52
|
],
|
|
17
|
-
"
|
|
18
|
-
"
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsgo -p tsconfig.build.json",
|
|
55
|
+
"clean": "git clean -fdX",
|
|
56
|
+
"test": "node --test",
|
|
57
|
+
"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",
|
|
58
|
+
"typecheck": "tsgo --noEmit"
|
|
19
59
|
}
|
|
20
|
-
}
|
|
60
|
+
}
|
package/src/index.ts
ADDED