soul-cli 0.9.3 → 0.11.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/.github/pull_request_template.md +8 -0
- package/.github/workflows/ci.yml +2 -0
- package/.github/workflows/pr-title.yml +27 -0
- package/.github/workflows/release.yml +44 -0
- package/.release-please-manifest.json +3 -0
- package/AGENTS.md +31 -0
- package/CHANGELOG.md +47 -0
- package/CLAUDE.md +1 -0
- package/README.md +6 -5
- package/ROADMAP.md +83 -25
- package/SECURITY.md +19 -0
- package/docs/api/rows-examples.md +10 -2
- package/package.json +10 -10
- package/release-please-config.json +10 -0
- package/src/controllers/rows.js +115 -92
- package/src/controllers/rows.query.test.js +369 -0
- package/src/index.js +8 -0
- package/src/schemas/rows.js +4 -1
- package/src/server.js +2 -3
- package/src/services/rowService.js +12 -6
- package/src/services/rowService.test.js +37 -0
- package/src/swagger/swagger.json +20 -2
- package/src/tests/index.js +1 -1
- 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 +3 -2
- package/.github/workflows/npm-publish.yml +0 -36
package/src/server.js
CHANGED
|
@@ -12,9 +12,8 @@ const { authConstants } = require('./constants');
|
|
|
12
12
|
|
|
13
13
|
if (config.startWithStudio) {
|
|
14
14
|
(async () => {
|
|
15
|
-
const { handler: soulStudioHandler } =
|
|
16
|
-
'soul-studio/build/handler.js'
|
|
17
|
-
);
|
|
15
|
+
const { handler: soulStudioHandler } =
|
|
16
|
+
await import('soul-studio/build/handler.js');
|
|
18
17
|
// Deliberately not `app.use('/studio', soulStudioHandler)`: Express
|
|
19
18
|
// strips the mount prefix from `req.url` before invoking a path-mounted
|
|
20
19
|
// middleware, but soul-studio's adapter-node build has `paths.base:
|
|
@@ -4,11 +4,11 @@ const { quoteIdentifier } = require('../utils/sql');
|
|
|
4
4
|
module.exports = (db) => {
|
|
5
5
|
return {
|
|
6
6
|
get(data) {
|
|
7
|
-
const query = `SELECT ${data.
|
|
8
|
-
data.
|
|
9
|
-
} ${data.
|
|
10
|
-
data.
|
|
11
|
-
} LIMIT ? OFFSET ?`;
|
|
7
|
+
const query = `SELECT ${data.distinct ? 'DISTINCT ' : ''}${
|
|
8
|
+
data.schemaString || '*'
|
|
9
|
+
} FROM ${data.tableName} ${data.extendString || ''} ${
|
|
10
|
+
data.whereString || ''
|
|
11
|
+
} ${data.groupString || ''} ${data.orderString || ''} LIMIT ? OFFSET ?`;
|
|
12
12
|
|
|
13
13
|
const statement = db.prepare(query);
|
|
14
14
|
const result = statement.all(
|
|
@@ -30,7 +30,13 @@ module.exports = (db) => {
|
|
|
30
30
|
},
|
|
31
31
|
|
|
32
32
|
getCount(data) {
|
|
33
|
-
const
|
|
33
|
+
const from = `FROM ${data.tableName} ${data.whereString}`;
|
|
34
|
+
let query = `SELECT COUNT(*) as total ${from}`;
|
|
35
|
+
if (data.groupString) {
|
|
36
|
+
query = `SELECT COUNT(*) as total FROM (SELECT 1 ${from} ${data.groupString})`;
|
|
37
|
+
} else if (data.distinctString) {
|
|
38
|
+
query = `SELECT COUNT(*) as total FROM (SELECT DISTINCT ${data.distinctString} ${from})`;
|
|
39
|
+
}
|
|
34
40
|
const statement = db.prepare(query);
|
|
35
41
|
const result = statement.get(...data.whereStringValues).total;
|
|
36
42
|
return result;
|
|
@@ -149,5 +149,42 @@ describe('rowService', () => {
|
|
|
149
149
|
});
|
|
150
150
|
expect(total).toBe(2);
|
|
151
151
|
});
|
|
152
|
+
|
|
153
|
+
it('supports distinct, group by and their totals', () => {
|
|
154
|
+
rowService.save({ tableName: 'authors', fields: { name: 'Ada' } });
|
|
155
|
+
rowService.save({ tableName: 'authors', fields: { name: 'Beta' } });
|
|
156
|
+
const base = {
|
|
157
|
+
tableName: 'authors',
|
|
158
|
+
whereString: '',
|
|
159
|
+
whereStringValues: [],
|
|
160
|
+
schemaString: 'authors.name',
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const distinct = rowService.get({ ...base, distinct: true });
|
|
164
|
+
expect(distinct).toHaveLength(2);
|
|
165
|
+
expect(
|
|
166
|
+
rowService.getCount({
|
|
167
|
+
...base,
|
|
168
|
+
distinctString: 'authors.name',
|
|
169
|
+
}),
|
|
170
|
+
).toBe(2);
|
|
171
|
+
|
|
172
|
+
const grouped = rowService.get({
|
|
173
|
+
...base,
|
|
174
|
+
schemaString: 'authors.name, COUNT(*) AS "count"',
|
|
175
|
+
groupString: 'GROUP BY authors.name',
|
|
176
|
+
orderString: ' ORDER BY authors.name ASC',
|
|
177
|
+
});
|
|
178
|
+
expect(grouped).toEqual([
|
|
179
|
+
{ name: 'Ada', count: 2 },
|
|
180
|
+
{ name: 'Beta', count: 1 },
|
|
181
|
+
]);
|
|
182
|
+
expect(
|
|
183
|
+
rowService.getCount({
|
|
184
|
+
...base,
|
|
185
|
+
groupString: 'GROUP BY authors.name',
|
|
186
|
+
}),
|
|
187
|
+
).toBe(2);
|
|
188
|
+
});
|
|
152
189
|
});
|
|
153
190
|
});
|
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
|
},
|
|
@@ -237,7 +237,25 @@
|
|
|
237
237
|
},
|
|
238
238
|
{
|
|
239
239
|
"name": "_filters",
|
|
240
|
-
"description": "Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20.",
|
|
240
|
+
"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.",
|
|
241
|
+
"in": "query",
|
|
242
|
+
"type": "string"
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
"name": "_distinct",
|
|
246
|
+
"description": "Return unique combinations of these columns. e.g. ?_distinct=category,price",
|
|
247
|
+
"in": "query",
|
|
248
|
+
"type": "string"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"name": "_group_by",
|
|
252
|
+
"description": "Group rows by these columns. e.g. ?_group_by=category",
|
|
253
|
+
"in": "query",
|
|
254
|
+
"type": "string"
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
"name": "_aggregate",
|
|
258
|
+
"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
259
|
"in": "query",
|
|
242
260
|
"type": "string"
|
|
243
261
|
},
|
package/src/tests/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const dropTestTable = (table = 'users') => {
|
|
|
10
10
|
const dropTestDatabase = async (path = 'test.db') => {
|
|
11
11
|
// delete test database file e.g. test.db
|
|
12
12
|
try {
|
|
13
|
-
await unlink(path), console.log(`successfully deleted ${path}`);
|
|
13
|
+
(await unlink(path), console.log(`successfully deleted ${path}`));
|
|
14
14
|
} catch (error) {
|
|
15
15
|
console.error('there was an error:', error);
|
|
16
16
|
}
|
|
@@ -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"');
|
package/src/websocket.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const { WebSocketServer } = require('ws');
|
|
2
|
-
const cookie = require('cookie');
|
|
3
2
|
|
|
4
3
|
const db = require('./db/index');
|
|
5
4
|
const config = require('./config/index');
|
|
@@ -50,7 +49,9 @@ wss.on('connection', async function (ws, request) {
|
|
|
50
49
|
// REST API -- unauthenticated/unauthorized clients must not be able to
|
|
51
50
|
// subscribe to row changes on a table they can't read via the REST API.
|
|
52
51
|
if (config.auth) {
|
|
53
|
-
|
|
52
|
+
// cookie v2 is ESM-only, so it is loaded lazily here.
|
|
53
|
+
const { parseCookie } = await import('cookie');
|
|
54
|
+
const cookies = parseCookie(request.headers.cookie || '');
|
|
54
55
|
const accessToken = cookies.accessToken;
|
|
55
56
|
|
|
56
57
|
if (!accessToken) {
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# Publishes to npm via Trusted Publishing (OIDC) -- no long-lived npm
|
|
2
|
-
# token/secret involved. Requires a matching Trusted Publisher configured
|
|
3
|
-
# on the package's npmjs.com settings: GitHub Actions, owner/repo
|
|
4
|
-
# thevahidal/soul, workflow filename `npm-publish.yml`, action `npm publish`
|
|
5
|
-
# allowed. See https://docs.npmjs.com/trusted-publishers.
|
|
6
|
-
#
|
|
7
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
8
|
-
|
|
9
|
-
name: Node.js Package
|
|
10
|
-
|
|
11
|
-
on:
|
|
12
|
-
release:
|
|
13
|
-
types: [created]
|
|
14
|
-
|
|
15
|
-
permissions:
|
|
16
|
-
contents: read
|
|
17
|
-
id-token: write
|
|
18
|
-
|
|
19
|
-
jobs:
|
|
20
|
-
publish-npm:
|
|
21
|
-
runs-on: ubuntu-latest
|
|
22
|
-
steps:
|
|
23
|
-
- uses: actions/checkout@v4
|
|
24
|
-
- uses: actions/setup-node@v4
|
|
25
|
-
with:
|
|
26
|
-
node-version: 22
|
|
27
|
-
registry-url: https://registry.npmjs.org/
|
|
28
|
-
|
|
29
|
-
# Trusted Publishing needs npm CLI >=11.5.1; the version bundled with
|
|
30
|
-
# setup-node's Node 22 isn't guaranteed to be that recent.
|
|
31
|
-
- name: Check tag matches package.json version
|
|
32
|
-
run: test "v$(node -p 'require("./package.json").version')" = "${{ github.event.release.tag_name }}"
|
|
33
|
-
- run: npm install -g npm@latest
|
|
34
|
-
- run: npm ci
|
|
35
|
-
- run: npm test
|
|
36
|
-
- run: npm publish
|