soul-cli 0.10.0 → 0.12.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/.env.sample +1 -0
- package/.github/workflows/ci.yml +2 -0
- package/.prettierrc +4 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +25 -0
- package/README.md +3 -0
- package/ROADMAP.md +83 -25
- package/docs/api/batch-examples.md +55 -0
- package/docs/api/rows-examples.md +10 -2
- package/docs/api-examples.md +1 -0
- package/docs/auth.md +10 -0
- package/package.json +1 -1
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -0
- package/src/config/index.test.js +10 -0
- package/src/constants/messages.js +1 -1
- package/src/controllers/batch.js +128 -0
- package/src/controllers/batch.test.js +180 -0
- package/src/controllers/rows.js +223 -213
- package/src/controllers/rows.query.test.js +369 -0
- package/src/index.js +2 -0
- package/src/middlewares/api.js +6 -2
- package/src/middlewares/api.test.js +27 -13
- package/src/middlewares/auth.js +52 -33
- package/src/middlewares/auth.test.js +137 -3
- package/src/middlewares/broadcast.js +7 -11
- package/src/routes/batch.js +12 -0
- package/src/routes/rows.js +1 -0
- package/src/schemas/batch.js +27 -0
- package/src/schemas/rows.js +4 -1
- package/src/services/authService.js +31 -4
- package/src/services/authService.test.js +76 -0
- package/src/services/rowService.js +12 -6
- package/src/services/rowService.test.js +37 -0
- package/src/swagger/index.js +14 -16
- package/src/swagger/swagger.json +68 -59
- package/src/utils/query.js +285 -0
- package/src/utils/query.test.js +147 -0
- package/src/utils/sql.js +24 -0
- package/src/utils/sql.test.js +14 -0
- package/src/websocket.js +10 -8
- package/src/websocket.test.js +52 -0
- package/src/schemas/index.js +0 -30
package/src/swagger/swagger.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.10.0",
|
|
5
5
|
"title": "Soul API",
|
|
6
6
|
"description": "API Documentation for <b>Soul</b>, a SQLite REST and realtime server. "
|
|
7
7
|
},
|
|
@@ -54,6 +54,34 @@
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
|
+
"/api/batch": {
|
|
58
|
+
"post": {
|
|
59
|
+
"tags": ["Rows"],
|
|
60
|
+
"summary": "Batch",
|
|
61
|
+
"description": "Run row inserts, updates and deletes atomically: if one fails, none are applied.",
|
|
62
|
+
"parameters": [
|
|
63
|
+
{
|
|
64
|
+
"name": "body",
|
|
65
|
+
"in": "body",
|
|
66
|
+
"required": true,
|
|
67
|
+
"schema": {
|
|
68
|
+
"$ref": "#/definitions/BatchRequestBody"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
],
|
|
72
|
+
"responses": {
|
|
73
|
+
"200": {
|
|
74
|
+
"description": "OK"
|
|
75
|
+
},
|
|
76
|
+
"400": {
|
|
77
|
+
"description": "Bad Request"
|
|
78
|
+
},
|
|
79
|
+
"401": {
|
|
80
|
+
"description": "Unauthorized"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
57
85
|
"/api/tables/": {
|
|
58
86
|
"get": {
|
|
59
87
|
"tags": ["Tables"],
|
|
@@ -237,7 +265,25 @@
|
|
|
237
265
|
},
|
|
238
266
|
{
|
|
239
267
|
"name": "_filters",
|
|
240
|
-
"description": "Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20.",
|
|
268
|
+
"description": "Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20. Operators: __eq, __neq, __lt, __gt, __lte, __gte, __null, __notnull, __like, __contains, __startswith, __between, __in, __notin. Lists use brackets, e.g. id:[1,2]. Wrap a value in double quote characters to use , : | in it. (a:1|b:2) is an OR group and ! negates a filter or group.",
|
|
269
|
+
"in": "query",
|
|
270
|
+
"type": "string"
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"name": "_distinct",
|
|
274
|
+
"description": "Return unique combinations of these columns. e.g. ?_distinct=category,price",
|
|
275
|
+
"in": "query",
|
|
276
|
+
"type": "string"
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
"name": "_group_by",
|
|
280
|
+
"description": "Group rows by these columns. e.g. ?_group_by=category",
|
|
281
|
+
"in": "query",
|
|
282
|
+
"type": "string"
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
"name": "_aggregate",
|
|
286
|
+
"description": "Aggregates: count, sum:col, avg:col, min:col, max:col. e.g. ?_group_by=category&_aggregate=count,sum:price. Results are named count, sum_price, ...",
|
|
241
287
|
"in": "query",
|
|
242
288
|
"type": "string"
|
|
243
289
|
},
|
|
@@ -415,9 +461,6 @@
|
|
|
415
461
|
}
|
|
416
462
|
],
|
|
417
463
|
"responses": {
|
|
418
|
-
"200": {
|
|
419
|
-
"description": "OK"
|
|
420
|
-
},
|
|
421
464
|
"400": {
|
|
422
465
|
"description": "Bad Request"
|
|
423
466
|
},
|
|
@@ -457,9 +500,6 @@
|
|
|
457
500
|
}
|
|
458
501
|
],
|
|
459
502
|
"responses": {
|
|
460
|
-
"200": {
|
|
461
|
-
"description": "OK"
|
|
462
|
-
},
|
|
463
503
|
"400": {
|
|
464
504
|
"description": "Bad Request"
|
|
465
505
|
},
|
|
@@ -468,9 +508,6 @@
|
|
|
468
508
|
},
|
|
469
509
|
"403": {
|
|
470
510
|
"description": "Forbidden"
|
|
471
|
-
},
|
|
472
|
-
"404": {
|
|
473
|
-
"description": "Not Found"
|
|
474
511
|
}
|
|
475
512
|
}
|
|
476
513
|
}
|
|
@@ -536,52 +573,6 @@
|
|
|
536
573
|
"type": "object",
|
|
537
574
|
"properties": {}
|
|
538
575
|
},
|
|
539
|
-
"Query": {
|
|
540
|
-
"type": "object",
|
|
541
|
-
"properties": {
|
|
542
|
-
"query": {
|
|
543
|
-
"type": "string",
|
|
544
|
-
"example": "SELECT * FROM users"
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
},
|
|
548
|
-
"Statement": {
|
|
549
|
-
"type": "object",
|
|
550
|
-
"properties": {
|
|
551
|
-
"statement": {
|
|
552
|
-
"type": "string",
|
|
553
|
-
"example": "INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)"
|
|
554
|
-
},
|
|
555
|
-
"values": {
|
|
556
|
-
"type": "object",
|
|
557
|
-
"properties": {
|
|
558
|
-
"id": {
|
|
559
|
-
"type": "number",
|
|
560
|
-
"example": 1
|
|
561
|
-
},
|
|
562
|
-
"firstName": {
|
|
563
|
-
"type": "string",
|
|
564
|
-
"example": "John"
|
|
565
|
-
},
|
|
566
|
-
"lastName": {
|
|
567
|
-
"type": "string",
|
|
568
|
-
"example": "Doe"
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
},
|
|
574
|
-
"Transaction": {
|
|
575
|
-
"type": "object",
|
|
576
|
-
"properties": {
|
|
577
|
-
"transaction": {
|
|
578
|
-
"type": "array",
|
|
579
|
-
"items": {
|
|
580
|
-
"$ref": "#/definitions/Statement"
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
},
|
|
585
576
|
"ForeignKey": {
|
|
586
577
|
"type": "object",
|
|
587
578
|
"properties": {
|
|
@@ -793,8 +784,26 @@
|
|
|
793
784
|
}
|
|
794
785
|
}
|
|
795
786
|
},
|
|
796
|
-
"
|
|
797
|
-
"
|
|
787
|
+
"BatchRequestBody": {
|
|
788
|
+
"type": "object",
|
|
789
|
+
"properties": {
|
|
790
|
+
"operations": {
|
|
791
|
+
"type": "array",
|
|
792
|
+
"items": {
|
|
793
|
+
"type": "object",
|
|
794
|
+
"properties": {
|
|
795
|
+
"method": {
|
|
796
|
+
"type": "string",
|
|
797
|
+
"example": "DELETE"
|
|
798
|
+
},
|
|
799
|
+
"path": {
|
|
800
|
+
"type": "string",
|
|
801
|
+
"example": "/api/tables/users/rows/2"
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
}
|
|
798
807
|
},
|
|
799
808
|
"ObtainAccessTokenRequestBody": {
|
|
800
809
|
"type": "object",
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
const {
|
|
2
|
+
operators,
|
|
3
|
+
invalidRequestError,
|
|
4
|
+
quoteIdentifier,
|
|
5
|
+
assertValidColumnName,
|
|
6
|
+
assertValidOperator,
|
|
7
|
+
assertValidAggregate,
|
|
8
|
+
} = require('./sql');
|
|
9
|
+
|
|
10
|
+
const likeOperators = ['like', 'contains', 'startswith'];
|
|
11
|
+
|
|
12
|
+
// Express turns a repeated query param (?a=1&a=2) into an array.
|
|
13
|
+
const assertString = (value, name) => {
|
|
14
|
+
if (typeof value !== 'string') {
|
|
15
|
+
throw invalidRequestError(`${name} must be a single value`);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// A quoted value ("a, b") is only quoted if the closing quote ends the value.
|
|
21
|
+
const readQuoted = (s, i, stops) => {
|
|
22
|
+
let value = '';
|
|
23
|
+
let j = i + 1;
|
|
24
|
+
while (j < s.length) {
|
|
25
|
+
if (s[j] === '"') {
|
|
26
|
+
if (s[j + 1] === '"') {
|
|
27
|
+
value += '"';
|
|
28
|
+
j += 2;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const next = j + 1;
|
|
32
|
+
return next === s.length || stops.includes(s[next])
|
|
33
|
+
? { value, next, quoted: true }
|
|
34
|
+
: null;
|
|
35
|
+
}
|
|
36
|
+
value += s[j++];
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const readRaw = (s, i, stops) => {
|
|
42
|
+
let j = i;
|
|
43
|
+
while (j < s.length && !stops.includes(s[j])) {
|
|
44
|
+
j++;
|
|
45
|
+
}
|
|
46
|
+
return { value: s.slice(i, j), next: j };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const readScalar = (s, i, stops) =>
|
|
50
|
+
(s[i] === '"' && readQuoted(s, i, stops)) || readRaw(s, i, stops);
|
|
51
|
+
|
|
52
|
+
// [a,b,"c,d"]; falls back to a literal value when the bracket is not closed.
|
|
53
|
+
const readList = (s, i, stops) => {
|
|
54
|
+
const values = [];
|
|
55
|
+
let j = i + 1;
|
|
56
|
+
for (;;) {
|
|
57
|
+
const item = readScalar(s, j, [',', ']']);
|
|
58
|
+
values.push(item.value);
|
|
59
|
+
j = item.next;
|
|
60
|
+
if (s[j] === ',') {
|
|
61
|
+
j++;
|
|
62
|
+
} else if (
|
|
63
|
+
s[j] === ']' &&
|
|
64
|
+
(j + 1 === s.length || stops.includes(s[j + 1]))
|
|
65
|
+
) {
|
|
66
|
+
return { value: values, next: j + 1 };
|
|
67
|
+
} else {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const readCondition = (s, i, stops) => {
|
|
74
|
+
let negate = false;
|
|
75
|
+
if (s[i] === '!') {
|
|
76
|
+
negate = true;
|
|
77
|
+
i++;
|
|
78
|
+
}
|
|
79
|
+
const key = readRaw(s, i, [':', ...stops]);
|
|
80
|
+
if (s[key.next] !== ':') {
|
|
81
|
+
return { negate, key: key.value, value: '', next: key.next };
|
|
82
|
+
}
|
|
83
|
+
const start = key.next + 1;
|
|
84
|
+
const value =
|
|
85
|
+
(s[start] === '[' && readList(s, start, stops)) ||
|
|
86
|
+
readScalar(s, start, stops);
|
|
87
|
+
return { negate, key: key.value, ...value };
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// A term is one filter or a (a|b) group; terms are ANDed together.
|
|
91
|
+
const readTerm = (s, i) => {
|
|
92
|
+
let negate = false;
|
|
93
|
+
if (s[i] === '!' && s[i + 1] === '(') {
|
|
94
|
+
negate = true;
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
if (s[i] !== '(') {
|
|
98
|
+
const condition = readCondition(s, i, [',']);
|
|
99
|
+
return { negate: false, alternatives: [condition], next: condition.next };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const alternatives = [];
|
|
103
|
+
let j = i + 1;
|
|
104
|
+
for (;;) {
|
|
105
|
+
const condition = readCondition(s, j, ['|', ')', ',']);
|
|
106
|
+
alternatives.push(condition);
|
|
107
|
+
j = condition.next;
|
|
108
|
+
if (s[j] === '|') {
|
|
109
|
+
j++;
|
|
110
|
+
} else if (s[j] === ')' && (j + 1 === s.length || s[j + 1] === ',')) {
|
|
111
|
+
return { negate, alternatives, next: j + 1 };
|
|
112
|
+
} else {
|
|
113
|
+
throw invalidRequestError(
|
|
114
|
+
`Invalid group in _filters near '${s.slice(i)}'`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const parseFilters = (s) => {
|
|
121
|
+
const terms = [];
|
|
122
|
+
let i = 0;
|
|
123
|
+
for (;;) {
|
|
124
|
+
const term = readTerm(s, i);
|
|
125
|
+
terms.push(term);
|
|
126
|
+
if (term.next >= s.length) {
|
|
127
|
+
return terms;
|
|
128
|
+
}
|
|
129
|
+
i = term.next + 1;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const escapeLike = (value) => value.replace(/[\\%_]/g, '\\$&');
|
|
134
|
+
|
|
135
|
+
const buildCondition = (db, tableName, condition) => {
|
|
136
|
+
const [field, operatorKey] = condition.key.split('__');
|
|
137
|
+
const key = operatorKey || 'eq';
|
|
138
|
+
const { value } = condition;
|
|
139
|
+
|
|
140
|
+
assertValidColumnName(db, tableName, field);
|
|
141
|
+
let operator;
|
|
142
|
+
try {
|
|
143
|
+
operator = assertValidOperator(key);
|
|
144
|
+
} catch {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`Invalid field operator '${key}' for field '${field}'. You can only use the following operators after the '${field}' field: ${Object.keys(
|
|
147
|
+
operators,
|
|
148
|
+
)
|
|
149
|
+
.map((name) => `__${name}`)
|
|
150
|
+
.join(', ')}.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const column = `${tableName}.${field}`;
|
|
155
|
+
const placeholders = (list) => list.map(() => '?').join(', ');
|
|
156
|
+
|
|
157
|
+
if (key === 'null' || key === 'notnull') {
|
|
158
|
+
return { sql: `${column} ${operator}`, values: [] };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (key === 'between') {
|
|
162
|
+
if (!Array.isArray(value) || value.length !== 2) {
|
|
163
|
+
throw invalidRequestError(
|
|
164
|
+
`'${field}__between' needs two values, e.g. ${field}__between:[1,10]`,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
return { sql: `${column} BETWEEN ? AND ?`, values: value };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (key === 'in' || key === 'notin') {
|
|
171
|
+
const list = Array.isArray(value) ? value : [value];
|
|
172
|
+
return {
|
|
173
|
+
sql: `${column} ${operator} (${placeholders(list)})`,
|
|
174
|
+
values: list,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (likeOperators.includes(key)) {
|
|
179
|
+
if (Array.isArray(value)) {
|
|
180
|
+
throw invalidRequestError(`'${field}__${key}' takes a single value`);
|
|
181
|
+
}
|
|
182
|
+
const pattern = {
|
|
183
|
+
like: value,
|
|
184
|
+
contains: `%${escapeLike(value)}%`,
|
|
185
|
+
startswith: `${escapeLike(value)}%`,
|
|
186
|
+
}[key];
|
|
187
|
+
return { sql: `${column} LIKE ? ESCAPE '\\'`, values: [pattern] };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Bracketed values become IN for every comparison operator (existing behavior).
|
|
191
|
+
if (Array.isArray(value)) {
|
|
192
|
+
return { sql: `${column} IN (${placeholders(value)})`, values: value };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (value === '' && !condition.quoted) {
|
|
196
|
+
return { sql: `${column} ${operator}`, values: [] };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return { sql: `${column} ${operator} ?`, values: [value] };
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const buildFilterClause = (db, tableName, filters) => {
|
|
203
|
+
const values = [];
|
|
204
|
+
|
|
205
|
+
const clause = parseFilters(assertString(filters, '_filters'))
|
|
206
|
+
.map((term) => {
|
|
207
|
+
const parts = term.alternatives.map((condition) => {
|
|
208
|
+
const built = buildCondition(db, tableName, condition);
|
|
209
|
+
values.push(...built.values);
|
|
210
|
+
return condition.negate ? `NOT (${built.sql})` : built.sql;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (term.negate) {
|
|
214
|
+
return `NOT (${parts.join(' OR ')})`;
|
|
215
|
+
}
|
|
216
|
+
return parts.length > 1 ? `(${parts.join(' OR ')})` : parts[0];
|
|
217
|
+
})
|
|
218
|
+
.join(' AND ');
|
|
219
|
+
|
|
220
|
+
return { clause, values };
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const buildDistinct = (db, tableName, distinct) => {
|
|
224
|
+
const columns = assertString(distinct, '_distinct')
|
|
225
|
+
.split(',')
|
|
226
|
+
.map((column) => {
|
|
227
|
+
assertValidColumnName(db, tableName, column);
|
|
228
|
+
return `${tableName}.${column}`;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
return columns.join(', ');
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
// _group_by=category&_aggregate=count,sum:price
|
|
235
|
+
const buildGroupedSelect = (db, tableName, groupBy, aggregate) => {
|
|
236
|
+
const groupColumns = groupBy
|
|
237
|
+
? assertString(groupBy, '_group_by').split(',')
|
|
238
|
+
: [];
|
|
239
|
+
groupColumns.forEach((column) =>
|
|
240
|
+
assertValidColumnName(db, tableName, column),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const specs = aggregate
|
|
244
|
+
? assertString(aggregate, '_aggregate').split(',')
|
|
245
|
+
: [];
|
|
246
|
+
const aggregates = specs.map((spec) => {
|
|
247
|
+
const [fn, column, ...extra] = spec.split(':');
|
|
248
|
+
const sqlFunction = assertValidAggregate(fn);
|
|
249
|
+
|
|
250
|
+
if (extra.length > 0) {
|
|
251
|
+
throw invalidRequestError(`Invalid aggregate '${spec}'`);
|
|
252
|
+
}
|
|
253
|
+
if (column === undefined) {
|
|
254
|
+
if (fn !== 'count') {
|
|
255
|
+
throw invalidRequestError(
|
|
256
|
+
`Aggregate '${fn}' needs a column, e.g. ${fn}:price`,
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
return { sql: 'COUNT(*)', alias: 'count' };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
assertValidColumnName(db, tableName, column);
|
|
263
|
+
return {
|
|
264
|
+
sql: `${sqlFunction}(${tableName}.${column})`,
|
|
265
|
+
alias: `${fn}_${column}`,
|
|
266
|
+
};
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
const groupString = groupColumns.map((column) => `${tableName}.${column}`);
|
|
270
|
+
const select = [
|
|
271
|
+
...groupString,
|
|
272
|
+
...aggregates.map(
|
|
273
|
+
({ sql, alias }) => `${sql} AS ${quoteIdentifier(alias)}`,
|
|
274
|
+
),
|
|
275
|
+
];
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
schemaString: select.join(', '),
|
|
279
|
+
groupString: groupString.length ? `GROUP BY ${groupString.join(', ')}` : '',
|
|
280
|
+
groupColumns,
|
|
281
|
+
aliases: aggregates.map(({ alias }) => alias),
|
|
282
|
+
};
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
module.exports = { buildFilterClause, buildDistinct, buildGroupedSelect };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const Database = require('better-sqlite3');
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
buildFilterClause,
|
|
5
|
+
buildDistinct,
|
|
6
|
+
buildGroupedSelect,
|
|
7
|
+
} = require('./query');
|
|
8
|
+
|
|
9
|
+
describe('query utils', () => {
|
|
10
|
+
let db;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
db = new Database(':memory:');
|
|
14
|
+
db.exec(
|
|
15
|
+
'CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, price INTEGER)',
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
db.close();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('buildFilterClause', () => {
|
|
24
|
+
const build = (filters) => buildFilterClause(db, 'items', filters);
|
|
25
|
+
|
|
26
|
+
it('keeps the SQL shape of the existing filters', () => {
|
|
27
|
+
expect(build('id:[1,2],name__neq:a,price__gte:5')).toEqual({
|
|
28
|
+
clause: 'items.id IN (?, ?) AND items.name != ? AND items.price >= ?',
|
|
29
|
+
values: ['1', '2', 'a', '5'],
|
|
30
|
+
});
|
|
31
|
+
expect(build('name__null,price__notnull')).toEqual({
|
|
32
|
+
clause: 'items.name IS NULL AND items.price IS NOT NULL',
|
|
33
|
+
values: [],
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('keeps colons in a value', () => {
|
|
38
|
+
expect(build('name:2010-01-08 00:00:00').values).toEqual([
|
|
39
|
+
'2010-01-08 00:00:00',
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('reads quoted values and doubled quotes', () => {
|
|
44
|
+
expect(build('name:"a, b: c|d"').values).toEqual(['a, b: c|d']);
|
|
45
|
+
expect(build('name:"say ""hi"""').values).toEqual(['say "hi"']);
|
|
46
|
+
expect(build('name:["a,b",c]').values).toEqual(['a,b', 'c']);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('binds an empty quoted string', () => {
|
|
50
|
+
expect(build('name:""')).toEqual({
|
|
51
|
+
clause: 'items.name = ?',
|
|
52
|
+
values: [''],
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('treats an unclosed list or quote as a literal value', () => {
|
|
57
|
+
expect(build('name:[a').values).toEqual(['[a']);
|
|
58
|
+
expect(build('name:"a').values).toEqual(['"a']);
|
|
59
|
+
expect(build('name:"a"b').values).toEqual(['"a"b']);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('builds OR groups and NOT', () => {
|
|
63
|
+
expect(build('(id:1|name:a),!price:5,!(id:2|id:3)')).toEqual({
|
|
64
|
+
clause:
|
|
65
|
+
'(items.id = ? OR items.name = ?) AND NOT (items.price = ?) AND NOT (items.id = ? OR items.id = ?)',
|
|
66
|
+
values: ['1', 'a', '5', '2', '3'],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('builds the new operators', () => {
|
|
71
|
+
expect(build('name__contains:50%_').values).toEqual(['%50\\%\\_%']);
|
|
72
|
+
expect(build('name__startswith:a').values).toEqual(['a%']);
|
|
73
|
+
expect(build('name__like:a_%').values).toEqual(['a_%']);
|
|
74
|
+
expect(build('id__between:[1,5]').clause).toEqual(
|
|
75
|
+
'items.id BETWEEN ? AND ?',
|
|
76
|
+
);
|
|
77
|
+
expect(build('id__notin:[1,2]').clause).toEqual('items.id NOT IN (?, ?)');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('rejects bad input', () => {
|
|
81
|
+
[
|
|
82
|
+
'bogus:1',
|
|
83
|
+
'name__bogus:1',
|
|
84
|
+
'(id:1',
|
|
85
|
+
'(id:1,name:a)',
|
|
86
|
+
'id__between:1',
|
|
87
|
+
'name__like:[a,b]',
|
|
88
|
+
'id:1,',
|
|
89
|
+
].forEach((filters) => {
|
|
90
|
+
expect(() => build(filters)).toThrow();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('sets a 400 status on an unknown column', () => {
|
|
95
|
+
expect(() => build('bogus:1')).toThrow(
|
|
96
|
+
expect.objectContaining({ status: 400 }),
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('repeated query params', () => {
|
|
102
|
+
it('rejects an array instead of a string', () => {
|
|
103
|
+
const repeated = ['id:1', 'id:2'];
|
|
104
|
+
expect(() => buildFilterClause(db, 'items', repeated)).toThrow(
|
|
105
|
+
expect.objectContaining({ status: 400 }),
|
|
106
|
+
);
|
|
107
|
+
expect(() => buildDistinct(db, 'items', repeated)).toThrow(/single/);
|
|
108
|
+
expect(() => buildGroupedSelect(db, 'items', repeated)).toThrow(/single/);
|
|
109
|
+
expect(() =>
|
|
110
|
+
buildGroupedSelect(db, 'items', undefined, repeated),
|
|
111
|
+
).toThrow(/single/);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('buildDistinct', () => {
|
|
116
|
+
it('qualifies validated columns', () => {
|
|
117
|
+
expect(buildDistinct(db, 'items', 'name,price')).toEqual(
|
|
118
|
+
'items.name, items.price',
|
|
119
|
+
);
|
|
120
|
+
expect(() => buildDistinct(db, 'items', 'bogus')).toThrow();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('buildGroupedSelect', () => {
|
|
125
|
+
it('builds the select, group and alias lists', () => {
|
|
126
|
+
expect(
|
|
127
|
+
buildGroupedSelect(db, 'items', 'name', 'count,sum:price'),
|
|
128
|
+
).toEqual({
|
|
129
|
+
schemaString:
|
|
130
|
+
'items.name, COUNT(*) AS "count", SUM(items.price) AS "sum_price"',
|
|
131
|
+
groupString: 'GROUP BY items.name',
|
|
132
|
+
groupColumns: ['name'],
|
|
133
|
+
aliases: ['count', 'sum_price'],
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('allows only an aggregate or only a group', () => {
|
|
138
|
+
expect(buildGroupedSelect(db, 'items', undefined, 'count')).toMatchObject(
|
|
139
|
+
{ groupString: '', groupColumns: [] },
|
|
140
|
+
);
|
|
141
|
+
expect(buildGroupedSelect(db, 'items', 'name', undefined)).toMatchObject({
|
|
142
|
+
schemaString: 'items.name',
|
|
143
|
+
aliases: [],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
package/src/utils/sql.js
CHANGED
|
@@ -14,6 +14,20 @@ const operators = {
|
|
|
14
14
|
neq: '!=',
|
|
15
15
|
null: 'IS NULL',
|
|
16
16
|
notnull: 'IS NOT NULL',
|
|
17
|
+
like: 'LIKE',
|
|
18
|
+
contains: 'LIKE',
|
|
19
|
+
startswith: 'LIKE',
|
|
20
|
+
between: 'BETWEEN',
|
|
21
|
+
in: 'IN',
|
|
22
|
+
notin: 'NOT IN',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const aggregates = {
|
|
26
|
+
count: 'COUNT',
|
|
27
|
+
sum: 'SUM',
|
|
28
|
+
avg: 'AVG',
|
|
29
|
+
min: 'MIN',
|
|
30
|
+
max: 'MAX',
|
|
17
31
|
};
|
|
18
32
|
|
|
19
33
|
const invalidRequestError = (message, status = 400) => {
|
|
@@ -99,8 +113,18 @@ const assertValidOperator = (operator) => {
|
|
|
99
113
|
return operators[operator];
|
|
100
114
|
};
|
|
101
115
|
|
|
116
|
+
const assertValidAggregate = (fn) => {
|
|
117
|
+
if (!Object.prototype.hasOwnProperty.call(aggregates, fn)) {
|
|
118
|
+
throw invalidRequestError(`Invalid aggregate function '${fn}'`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return aggregates[fn];
|
|
122
|
+
};
|
|
123
|
+
|
|
102
124
|
module.exports = {
|
|
103
125
|
operators,
|
|
126
|
+
aggregates,
|
|
127
|
+
assertValidAggregate,
|
|
104
128
|
invalidRequestError,
|
|
105
129
|
quoteIdentifier,
|
|
106
130
|
quoteLiteral,
|
package/src/utils/sql.test.js
CHANGED
|
@@ -4,6 +4,7 @@ const {
|
|
|
4
4
|
assertValidTableName,
|
|
5
5
|
assertValidColumnName,
|
|
6
6
|
assertValidOperator,
|
|
7
|
+
assertValidAggregate,
|
|
7
8
|
quoteIdentifier,
|
|
8
9
|
quoteLiteral,
|
|
9
10
|
operators,
|
|
@@ -81,6 +82,19 @@ describe('sql utils', () => {
|
|
|
81
82
|
});
|
|
82
83
|
});
|
|
83
84
|
|
|
85
|
+
describe('assertValidAggregate', () => {
|
|
86
|
+
it('returns the SQL function for a known aggregate', () => {
|
|
87
|
+
expect(assertValidAggregate('sum')).toBe('SUM');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('throws for an unknown aggregate', () => {
|
|
91
|
+
expect(() => assertValidAggregate('median')).toThrow(
|
|
92
|
+
/Invalid aggregate function/,
|
|
93
|
+
);
|
|
94
|
+
expect(() => assertValidAggregate('toString')).toThrow();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
84
98
|
describe('quoteIdentifier', () => {
|
|
85
99
|
it('wraps an identifier in double quotes', () => {
|
|
86
100
|
expect(quoteIdentifier('name')).toBe('"name"');
|