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/controllers/rows.js
CHANGED
|
@@ -3,8 +3,14 @@ const { rowService } = require('../services');
|
|
|
3
3
|
const {
|
|
4
4
|
assertValidTableName,
|
|
5
5
|
assertValidColumnName,
|
|
6
|
-
|
|
6
|
+
invalidRequestError,
|
|
7
|
+
quoteIdentifier,
|
|
7
8
|
} = require('../utils/sql');
|
|
9
|
+
const {
|
|
10
|
+
buildFilterClause,
|
|
11
|
+
buildDistinct,
|
|
12
|
+
buildGroupedSelect,
|
|
13
|
+
} = require('../utils/query');
|
|
8
14
|
|
|
9
15
|
// Return paginated rows of a table
|
|
10
16
|
const listTableRows = async (req, res, next) => {
|
|
@@ -41,7 +47,19 @@ const listTableRows = async (req, res, next) => {
|
|
|
41
47
|
in: 'query',
|
|
42
48
|
}
|
|
43
49
|
#swagger.parameters['_filters'] = {
|
|
44
|
-
description: 'Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20.',
|
|
50
|
+
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.',
|
|
51
|
+
in: 'query',
|
|
52
|
+
}
|
|
53
|
+
#swagger.parameters['_distinct'] = {
|
|
54
|
+
description: 'Return unique combinations of these columns. e.g. ?_distinct=category,price',
|
|
55
|
+
in: 'query',
|
|
56
|
+
}
|
|
57
|
+
#swagger.parameters['_group_by'] = {
|
|
58
|
+
description: 'Group rows by these columns. e.g. ?_group_by=category',
|
|
59
|
+
in: 'query',
|
|
60
|
+
}
|
|
61
|
+
#swagger.parameters['_aggregate'] = {
|
|
62
|
+
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, ...',
|
|
45
63
|
in: 'query',
|
|
46
64
|
}
|
|
47
65
|
*/
|
|
@@ -55,6 +73,9 @@ const listTableRows = async (req, res, next) => {
|
|
|
55
73
|
_schema,
|
|
56
74
|
_extend,
|
|
57
75
|
_filters = '',
|
|
76
|
+
_distinct,
|
|
77
|
+
_group_by,
|
|
78
|
+
_aggregate,
|
|
58
79
|
} = req.query;
|
|
59
80
|
|
|
60
81
|
const page = parseInt(_page);
|
|
@@ -70,76 +91,22 @@ const listTableRows = async (req, res, next) => {
|
|
|
70
91
|
}
|
|
71
92
|
|
|
72
93
|
// if _filters are provided, filter rows by them
|
|
73
|
-
// _filters
|
|
74
|
-
//
|
|
75
|
-
// e.g. ?_filters=name:John,age:20
|
|
76
|
-
// will filter by name like '%John%' and age like '%20%'
|
|
77
|
-
let filters;
|
|
78
|
-
|
|
79
|
-
// split the filters by comma(,) except when in an array
|
|
80
|
-
const re = /,(?![^[]*?\])/;
|
|
81
|
-
try {
|
|
82
|
-
filters =
|
|
83
|
-
_filters === ''
|
|
84
|
-
? []
|
|
85
|
-
: _filters.split(re).map((filter) => {
|
|
86
|
-
//NOTE: When using the _filter parameter, the values are split using the ":" sign, like this (_filters=Total__eq:1). However, if the user sends a date value, such as (_filters=InvoiceDate__eq:2010-01-08 00:00:00), there will be additional colon (":") signs present.
|
|
87
|
-
let [key, ...value] = filter.split(':');
|
|
88
|
-
if (value.length === 1) {
|
|
89
|
-
value = value[0];
|
|
90
|
-
} else {
|
|
91
|
-
value = value.map((element) => element).join(':');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
let field = key.split('__')[0];
|
|
95
|
-
let fieldOperator = key.split('__')[1] || 'eq';
|
|
96
|
-
|
|
97
|
-
assertValidColumnName(db, tableName, field);
|
|
98
|
-
let operator;
|
|
99
|
-
try {
|
|
100
|
-
operator = assertValidOperator(fieldOperator);
|
|
101
|
-
} catch {
|
|
102
|
-
throw new Error(
|
|
103
|
-
`Invalid field operator '${fieldOperator}' for field '${field}'. You can only use the following operators after the '${field}' field: __lt, __gt, __lte, __gte, __eq, __neq.`,
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (['null', 'notnull'].includes(fieldOperator)) {
|
|
108
|
-
value = null;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return { field, operator, value };
|
|
112
|
-
});
|
|
113
|
-
} catch (error) {
|
|
114
|
-
return res.status(error.status || 400).json({
|
|
115
|
-
message: error.message,
|
|
116
|
-
error: error,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
94
|
+
// e.g. ?_filters=name:John,age__gte:20 (filters are ANDed)
|
|
95
|
+
// e.g. ?_filters=id:[1,2],(name:John|age__null),!city:Paris,note:"a, b"
|
|
120
96
|
let whereString = '';
|
|
121
97
|
const whereStringValues = [];
|
|
122
98
|
|
|
123
99
|
if (_filters !== '') {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
} else {
|
|
135
|
-
whereStringValues.push(filter.value);
|
|
136
|
-
return `${tableName}.${filter.field} ${filter.operator} ?`;
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
return `${tableName}.${filter.field} ${filter.operator}`;
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
.join(' AND ');
|
|
100
|
+
try {
|
|
101
|
+
const { clause, values } = buildFilterClause(db, tableName, _filters);
|
|
102
|
+
whereString = ` WHERE ${clause}`;
|
|
103
|
+
whereStringValues.push(...values);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return res.status(error.status || 400).json({
|
|
106
|
+
message: error.message,
|
|
107
|
+
error: error,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
143
110
|
params = `_filters=${_filters}&`;
|
|
144
111
|
}
|
|
145
112
|
|
|
@@ -174,38 +141,88 @@ const listTableRows = async (req, res, next) => {
|
|
|
174
141
|
}
|
|
175
142
|
}
|
|
176
143
|
|
|
144
|
+
// _distinct=col1,col2 returns unique combinations of those columns
|
|
145
|
+
// _group_by=col&_aggregate=count,sum:price groups rows and aggregates them
|
|
146
|
+
const isGrouped = Boolean(_group_by || _aggregate);
|
|
147
|
+
let schemaString = '';
|
|
148
|
+
let groupString = '';
|
|
149
|
+
let distinctString = '';
|
|
150
|
+
let grouped = { groupColumns: [], aliases: [] };
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
if (
|
|
154
|
+
(_distinct || isGrouped) &&
|
|
155
|
+
(_schema || _extend || (_distinct && isGrouped))
|
|
156
|
+
) {
|
|
157
|
+
throw invalidRequestError(
|
|
158
|
+
"_distinct and _group_by/_aggregate can't be combined with each other or with _schema/_extend",
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (_distinct) {
|
|
163
|
+
distinctString = buildDistinct(db, tableName, _distinct);
|
|
164
|
+
schemaString = distinctString;
|
|
165
|
+
params += `_distinct=${_distinct}&`;
|
|
166
|
+
} else if (isGrouped) {
|
|
167
|
+
grouped = buildGroupedSelect(db, tableName, _group_by, _aggregate);
|
|
168
|
+
({ schemaString, groupString } = grouped);
|
|
169
|
+
params += _group_by ? `_group_by=${_group_by}&` : '';
|
|
170
|
+
params += _aggregate ? `_aggregate=${_aggregate}&` : '';
|
|
171
|
+
}
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return res.status(error.status || 400).json({
|
|
174
|
+
message: error.message,
|
|
175
|
+
error: error,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
177
179
|
// if _ordering is provided, order rows by it
|
|
178
180
|
// e.g. ?_ordering=name will order by name
|
|
179
181
|
// e.g. ?_ordering=-name will order by name descending
|
|
182
|
+
// e.g. ?_ordering=category,-price orders by several columns
|
|
180
183
|
// e.g. ?_ordering=publisher.name will order by publisher name, considering that publisher is a foreign key to publishers table
|
|
181
184
|
let orderString = '';
|
|
182
185
|
let extendString = '';
|
|
183
186
|
let joins = {};
|
|
184
187
|
|
|
185
188
|
if (_ordering) {
|
|
186
|
-
const isDesc = _ordering.startsWith('-');
|
|
187
|
-
const cleanOrder = _ordering.replace('-', '');
|
|
188
|
-
|
|
189
189
|
try {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
190
|
+
const orderTerms = _ordering.split(',').map((term) => {
|
|
191
|
+
const isDesc = term.startsWith('-');
|
|
192
|
+
const cleanOrder = isDesc ? term.slice(1) : term;
|
|
193
|
+
const direction = isDesc ? 'DESC' : 'ASC';
|
|
194
|
+
|
|
195
|
+
if (isGrouped) {
|
|
196
|
+
if (grouped.aliases.includes(cleanOrder)) {
|
|
197
|
+
return `${quoteIdentifier(cleanOrder)} ${direction}`;
|
|
198
|
+
}
|
|
199
|
+
if (!grouped.groupColumns.includes(cleanOrder)) {
|
|
200
|
+
throw invalidRequestError(
|
|
201
|
+
`Can only order by a _group_by column or an aggregate: '${cleanOrder}'`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
return `${tableName}.${cleanOrder} ${direction}`;
|
|
205
|
+
}
|
|
193
206
|
|
|
194
|
-
|
|
195
|
-
|
|
207
|
+
if (cleanOrder.includes('.')) {
|
|
208
|
+
// Handle foreign key sort, e.g. publisher.name
|
|
209
|
+
const [relation, field] = cleanOrder.split('.');
|
|
196
210
|
|
|
197
|
-
|
|
211
|
+
if (!joins[relation]) {
|
|
212
|
+
joins[relation] = rowService.getForeignKeyInfo(tableName, relation);
|
|
213
|
+
extendString += ` LEFT JOIN ${joins[relation].joinedTableName} ON ${joins[relation].joinedTableName}.${joins[relation].foreignKey.to} = ${tableName}.${relation}`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
assertValidColumnName(db, joins[relation].joinedTableName, field);
|
|
217
|
+
return `${joins[relation].joinedTableName}.${field} ${direction}`;
|
|
218
|
+
}
|
|
198
219
|
|
|
199
|
-
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${relation}`;
|
|
200
|
-
joins[relation] = { foreignKey, joinedTableName, joinedTableFields };
|
|
201
|
-
orderString += ` ORDER BY ${joinedTableName}.${field} ${
|
|
202
|
-
isDesc ? 'DESC' : 'ASC'
|
|
203
|
-
}`;
|
|
204
|
-
} else {
|
|
205
220
|
// regular field sort
|
|
206
221
|
assertValidColumnName(db, tableName, cleanOrder);
|
|
207
|
-
|
|
208
|
-
}
|
|
222
|
+
return `${cleanOrder} ${direction}`;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
orderString = ` ORDER BY ${orderTerms.join(', ')}`;
|
|
209
226
|
} catch (error) {
|
|
210
227
|
return res.status(error.status || 400).json({
|
|
211
228
|
message: error.message,
|
|
@@ -219,7 +236,6 @@ const listTableRows = async (req, res, next) => {
|
|
|
219
236
|
// if _schema is provided, return only those fields
|
|
220
237
|
// e.g. ?_schema=name,age will return only name and age fields
|
|
221
238
|
// if _schema is not provided, return all fields
|
|
222
|
-
let schemaString = '';
|
|
223
239
|
if (_schema) {
|
|
224
240
|
const schemaFields = _schema.split(',');
|
|
225
241
|
try {
|
|
@@ -234,7 +250,7 @@ const listTableRows = async (req, res, next) => {
|
|
|
234
250
|
});
|
|
235
251
|
}
|
|
236
252
|
params += `_schema=${_schema}&`;
|
|
237
|
-
} else {
|
|
253
|
+
} else if (!_distinct && !isGrouped) {
|
|
238
254
|
schemaString = `${tableName}.*`;
|
|
239
255
|
}
|
|
240
256
|
|
|
@@ -330,7 +346,9 @@ const listTableRows = async (req, res, next) => {
|
|
|
330
346
|
tableName,
|
|
331
347
|
extendString,
|
|
332
348
|
whereString,
|
|
349
|
+
groupString,
|
|
333
350
|
orderString,
|
|
351
|
+
distinct: Boolean(_distinct),
|
|
334
352
|
limit,
|
|
335
353
|
page: limit * (page - 1),
|
|
336
354
|
whereStringValues,
|
|
@@ -350,11 +368,16 @@ const listTableRows = async (req, res, next) => {
|
|
|
350
368
|
}
|
|
351
369
|
|
|
352
370
|
// get total number of rows
|
|
353
|
-
const total =
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
371
|
+
const total =
|
|
372
|
+
isGrouped && !groupString
|
|
373
|
+
? 1
|
|
374
|
+
: rowService.getCount({
|
|
375
|
+
tableName,
|
|
376
|
+
whereString,
|
|
377
|
+
whereStringValues,
|
|
378
|
+
groupString,
|
|
379
|
+
distinctString,
|
|
380
|
+
});
|
|
358
381
|
|
|
359
382
|
const nextPage =
|
|
360
383
|
data.length === limit
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
const supertest = require('supertest');
|
|
2
|
+
|
|
3
|
+
const app = require('../index');
|
|
4
|
+
const db = require('../db/index');
|
|
5
|
+
const config = require('../config');
|
|
6
|
+
const { generateToken } = require('../utils');
|
|
7
|
+
|
|
8
|
+
const requestWithSupertest = supertest(app);
|
|
9
|
+
|
|
10
|
+
describe('Rows query params', () => {
|
|
11
|
+
let accessToken;
|
|
12
|
+
|
|
13
|
+
const get = (query) =>
|
|
14
|
+
requestWithSupertest
|
|
15
|
+
.get(`/api/tables/qtest/rows?${query}`)
|
|
16
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
17
|
+
|
|
18
|
+
const filter = (filters, extra = '') =>
|
|
19
|
+
get(`_filters=${encodeURIComponent(filters)}&_ordering=id${extra}`);
|
|
20
|
+
|
|
21
|
+
const names = (res) => res.body.data.map((row) => row.name);
|
|
22
|
+
|
|
23
|
+
beforeAll(async () => {
|
|
24
|
+
accessToken = await generateToken(
|
|
25
|
+
{ username: 'John', isSuperuser: true },
|
|
26
|
+
config.tokenSecret,
|
|
27
|
+
'1H',
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
db.prepare(
|
|
31
|
+
'CREATE TABLE qtest (id INTEGER PRIMARY KEY, name TEXT, category TEXT, price INTEGER, note TEXT)',
|
|
32
|
+
).run();
|
|
33
|
+
const insert = db.prepare(
|
|
34
|
+
'INSERT INTO qtest (name, category, price, note) VALUES (?, ?, ?, ?)',
|
|
35
|
+
);
|
|
36
|
+
[
|
|
37
|
+
['Apple', 'fruit', 3, null],
|
|
38
|
+
['Banana', 'fruit', 5, 'a,b'],
|
|
39
|
+
['Carrot', 'veg', 2, 'x:y'],
|
|
40
|
+
['Date', 'fruit', 5, 'p|q'],
|
|
41
|
+
['Eggplant', 'veg', 7, '(raw)'],
|
|
42
|
+
['Fig', 'fruit', 9, ''],
|
|
43
|
+
['Grape', 'fruit', 4, '100%'],
|
|
44
|
+
].forEach((row) => insert.run(...row));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
db.prepare('DROP TABLE IF EXISTS qtest').run();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('existing _filters behavior', () => {
|
|
52
|
+
it('ANDs comma-separated filters', async () => {
|
|
53
|
+
const res = await filter('category:fruit,price__gte:5');
|
|
54
|
+
expect(names(res)).toEqual(['Banana', 'Date', 'Fig']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('turns a bracketed value into IN for any operator', async () => {
|
|
58
|
+
const res = await filter('id__neq:[1,2]');
|
|
59
|
+
expect(res.body.data.map((row) => row.id)).toEqual([1, 2]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('keeps colons inside a value', async () => {
|
|
63
|
+
const res = await filter('note:x:y');
|
|
64
|
+
expect(names(res)).toEqual(['Carrot']);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns 400 for an empty value', async () => {
|
|
68
|
+
const res = await filter('note:');
|
|
69
|
+
expect(res.status).toEqual(400);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('returns 400 for an unknown operator and lists the valid ones', async () => {
|
|
73
|
+
const res = await filter('price__bogus:1');
|
|
74
|
+
expect(res.status).toEqual(400);
|
|
75
|
+
expect(res.body.message).toContain('__between');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('filters null values', async () => {
|
|
79
|
+
const res = await filter('note__null');
|
|
80
|
+
expect(names(res)).toEqual(['Apple']);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('_ordering', () => {
|
|
85
|
+
it('orders by several columns', async () => {
|
|
86
|
+
const res = await get('_ordering=category,-price,name');
|
|
87
|
+
expect(names(res)).toEqual([
|
|
88
|
+
'Fig',
|
|
89
|
+
'Banana',
|
|
90
|
+
'Date',
|
|
91
|
+
'Grape',
|
|
92
|
+
'Apple',
|
|
93
|
+
'Eggplant',
|
|
94
|
+
'Carrot',
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('returns 400 when one of the columns does not exist', async () => {
|
|
99
|
+
const res = await get('_ordering=category,bogus');
|
|
100
|
+
expect(res.status).toEqual(400);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('joins a related table once when ordering by it twice', async () => {
|
|
104
|
+
const res = await requestWithSupertest
|
|
105
|
+
.get(
|
|
106
|
+
'/api/tables/posts/rows?_ordering=userId.firstName,-userId.lastName&_extend=userId',
|
|
107
|
+
)
|
|
108
|
+
.set('Cookie', [`accessToken=${accessToken}`]);
|
|
109
|
+
expect(res.status).toEqual(200);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('_distinct', () => {
|
|
114
|
+
it('returns unique values and a matching total', async () => {
|
|
115
|
+
const res = await get('_distinct=category&_ordering=category&_limit=1');
|
|
116
|
+
expect(res.status).toEqual(200);
|
|
117
|
+
expect(res.body.data).toEqual([{ category: 'fruit' }]);
|
|
118
|
+
expect(res.body.total).toEqual(2);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('returns unique combinations of several columns', async () => {
|
|
122
|
+
const res = await get(
|
|
123
|
+
'_distinct=category,price&_ordering=category,price',
|
|
124
|
+
);
|
|
125
|
+
expect(res.body.data).toHaveLength(6);
|
|
126
|
+
expect(res.body.total).toEqual(6);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('returns 400 for an unknown column', async () => {
|
|
130
|
+
const res = await get('_distinct=bogus');
|
|
131
|
+
expect(res.status).toEqual(400);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('returns 400 when combined with _schema', async () => {
|
|
135
|
+
const res = await get('_distinct=category&_schema=name');
|
|
136
|
+
expect(res.status).toEqual(400);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('returns 400 when combined with _group_by', async () => {
|
|
140
|
+
const res = await get('_distinct=category&_group_by=category');
|
|
141
|
+
expect(res.status).toEqual(400);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('operators', () => {
|
|
146
|
+
it('like uses the given pattern', async () => {
|
|
147
|
+
const res = await filter('name__like:A%');
|
|
148
|
+
expect(names(res)).toEqual(['Apple']);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('contains matches anywhere', async () => {
|
|
152
|
+
const res = await filter('name__contains:an');
|
|
153
|
+
expect(names(res)).toEqual(['Banana', 'Eggplant']);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('startswith matches the beginning', async () => {
|
|
157
|
+
const res = await filter('name__startswith:E');
|
|
158
|
+
expect(names(res)).toEqual(['Eggplant']);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('contains treats % and _ literally', async () => {
|
|
162
|
+
expect(names(await filter('note__contains:%'))).toEqual(['Grape']);
|
|
163
|
+
expect(names(await filter('note__contains:_'))).toEqual([]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('between is inclusive', async () => {
|
|
167
|
+
const res = await filter('price__between:[3,5]');
|
|
168
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Grape']);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('between needs exactly two values', async () => {
|
|
172
|
+
expect((await filter('price__between:5')).status).toEqual(400);
|
|
173
|
+
expect((await filter('price__between:[1,2,3]')).status).toEqual(400);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('in and notin take a list or a single value', async () => {
|
|
177
|
+
expect(names(await filter('id__in:[1,2]'))).toEqual(['Apple', 'Banana']);
|
|
178
|
+
expect(names(await filter('id__in:3'))).toEqual(['Carrot']);
|
|
179
|
+
expect(names(await filter('id__notin:[1,2,3,4,5,6]'))).toEqual(['Grape']);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('like operators reject a list', async () => {
|
|
183
|
+
const res = await filter('name__like:[a,b]');
|
|
184
|
+
expect(res.status).toEqual(400);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe('quoted values', () => {
|
|
189
|
+
it('allows commas, colons and pipes inside quotes', async () => {
|
|
190
|
+
expect(names(await filter('note:"a,b"'))).toEqual(['Banana']);
|
|
191
|
+
expect(names(await filter('note:"x:y"'))).toEqual(['Carrot']);
|
|
192
|
+
expect(names(await filter('note:"p|q"'))).toEqual(['Date']);
|
|
193
|
+
expect(names(await filter('note:"(raw)"'))).toEqual(['Eggplant']);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('allows quoted values inside a list and next to other filters', async () => {
|
|
197
|
+
expect(names(await filter('note:["a,b","x:y"]'))).toEqual([
|
|
198
|
+
'Banana',
|
|
199
|
+
'Carrot',
|
|
200
|
+
]);
|
|
201
|
+
expect(names(await filter('note:"a,b",category:fruit'))).toEqual([
|
|
202
|
+
'Banana',
|
|
203
|
+
]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('matches an empty string with ""', async () => {
|
|
207
|
+
expect(names(await filter('note:""'))).toEqual(['Fig']);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('treats an unclosed or trailing-text quote literally', async () => {
|
|
211
|
+
const unclosed = await filter('name:"Apple');
|
|
212
|
+
expect(unclosed.status).toEqual(200);
|
|
213
|
+
expect(unclosed.body.data).toEqual([]);
|
|
214
|
+
expect((await filter('name:"Apple"x')).body.data).toEqual([]);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('OR groups and NOT', () => {
|
|
219
|
+
it('ORs the filters inside a group', async () => {
|
|
220
|
+
const res = await filter('(category:veg|price__gte:9)');
|
|
221
|
+
expect(names(res)).toEqual(['Carrot', 'Eggplant', 'Fig']);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('ANDs a group with other filters', async () => {
|
|
225
|
+
const res = await filter('(category:veg|price__gte:9),price__lt:8');
|
|
226
|
+
expect(names(res)).toEqual(['Carrot', 'Eggplant']);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('protects | inside quoted values', async () => {
|
|
230
|
+
const res = await filter('(note:"p|q"|note:"a,b")');
|
|
231
|
+
expect(names(res)).toEqual(['Banana', 'Date']);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('negates a filter with !', async () => {
|
|
235
|
+
const res = await filter('!category:veg');
|
|
236
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Fig', 'Grape']);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('negates a group with !', async () => {
|
|
240
|
+
const res = await filter('!(category:veg|price__gte:9)');
|
|
241
|
+
expect(names(res)).toEqual(['Apple', 'Banana', 'Date', 'Grape']);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('negates inside a group', async () => {
|
|
245
|
+
const res = await filter('(!category:fruit|id:1)');
|
|
246
|
+
expect(names(res)).toEqual(['Apple', 'Carrot', 'Eggplant']);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('excludes NULL rows when negating, like __neq', async () => {
|
|
250
|
+
const res = await filter('!note:x:y');
|
|
251
|
+
expect(names(res)).toEqual([
|
|
252
|
+
'Banana',
|
|
253
|
+
'Date',
|
|
254
|
+
'Eggplant',
|
|
255
|
+
'Fig',
|
|
256
|
+
'Grape',
|
|
257
|
+
]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('returns 400 for an unclosed group', async () => {
|
|
261
|
+
expect((await filter('(category:veg')).status).toEqual(400);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('returns 400 for a comma inside a group', async () => {
|
|
265
|
+
expect((await filter('(category:veg,price:2)')).status).toEqual(400);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('returns 400 for an unknown column inside a group', async () => {
|
|
269
|
+
expect((await filter('(bogus:1|id:2)')).status).toEqual(400);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('_group_by and _aggregate', () => {
|
|
274
|
+
it('groups rows and aggregates them', async () => {
|
|
275
|
+
const res = await get(
|
|
276
|
+
'_group_by=category&_aggregate=count,sum:price,avg:price,min:price,max:price&_ordering=category',
|
|
277
|
+
);
|
|
278
|
+
expect(res.status).toEqual(200);
|
|
279
|
+
expect(res.body.data).toEqual([
|
|
280
|
+
{
|
|
281
|
+
category: 'fruit',
|
|
282
|
+
count: 5,
|
|
283
|
+
sum_price: 26,
|
|
284
|
+
avg_price: 5.2,
|
|
285
|
+
min_price: 3,
|
|
286
|
+
max_price: 9,
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
category: 'veg',
|
|
290
|
+
count: 2,
|
|
291
|
+
sum_price: 9,
|
|
292
|
+
avg_price: 4.5,
|
|
293
|
+
min_price: 2,
|
|
294
|
+
max_price: 7,
|
|
295
|
+
},
|
|
296
|
+
]);
|
|
297
|
+
expect(res.body.total).toEqual(2);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('orders by an aggregate alias', async () => {
|
|
301
|
+
const res = await get(
|
|
302
|
+
'_group_by=category&_aggregate=sum:price&_ordering=-sum_price',
|
|
303
|
+
);
|
|
304
|
+
expect(res.body.data.map((row) => row.category)).toEqual([
|
|
305
|
+
'fruit',
|
|
306
|
+
'veg',
|
|
307
|
+
]);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('returns a single row when only _aggregate is given', async () => {
|
|
311
|
+
const res = await get('_aggregate=count');
|
|
312
|
+
expect(res.body.data).toEqual([{ count: 7 }]);
|
|
313
|
+
expect(res.body.total).toEqual(1);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('supports count:column', async () => {
|
|
317
|
+
const res = await get('_aggregate=count:note');
|
|
318
|
+
expect(res.body.data).toEqual([{ count_note: 6 }]);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('applies _filters before grouping', async () => {
|
|
322
|
+
const res = await get(
|
|
323
|
+
'_filters=category:fruit&_group_by=category&_aggregate=count',
|
|
324
|
+
);
|
|
325
|
+
expect(res.body.data).toEqual([{ category: 'fruit', count: 5 }]);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('groups without an aggregate', async () => {
|
|
329
|
+
const res = await get('_group_by=category&_ordering=category');
|
|
330
|
+
expect(res.body.data).toEqual([
|
|
331
|
+
{ category: 'fruit' },
|
|
332
|
+
{ category: 'veg' },
|
|
333
|
+
]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('keeps total and pagination links correct', async () => {
|
|
337
|
+
const res = await get('_group_by=price&_ordering=price&_limit=2');
|
|
338
|
+
expect(res.body.data).toHaveLength(2);
|
|
339
|
+
expect(res.body.total).toEqual(6);
|
|
340
|
+
expect(res.body.next).toContain('_group_by=price');
|
|
341
|
+
expect(res.body.previous).toBeNull();
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('returns 400 for bad input', async () => {
|
|
345
|
+
const bad = [
|
|
346
|
+
'_group_by=bogus',
|
|
347
|
+
'_group_by=category&_ordering=name',
|
|
348
|
+
'_group_by=category&_schema=name',
|
|
349
|
+
'_aggregate=median:price',
|
|
350
|
+
'_aggregate=sum',
|
|
351
|
+
'_aggregate=count:bogus',
|
|
352
|
+
'_aggregate=sum:price:x',
|
|
353
|
+
`_aggregate=${encodeURIComponent('count:price) FROM qtest --')}`,
|
|
354
|
+
];
|
|
355
|
+
for (const query of bad) {
|
|
356
|
+
const res = await get(query);
|
|
357
|
+
expect([query, res.status]).toEqual([query, 400]);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('does not run injected SQL in _group_by', async () => {
|
|
362
|
+
const res = await get(
|
|
363
|
+
`_group_by=${encodeURIComponent('category;DROP TABLE qtest')}`,
|
|
364
|
+
);
|
|
365
|
+
expect(res.status).toEqual(400);
|
|
366
|
+
expect((await get('_limit=1')).status).toEqual(200);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
package/src/index.js
CHANGED
|
@@ -31,6 +31,14 @@ const { runCLICommands } = require('./commands');
|
|
|
31
31
|
const app = express();
|
|
32
32
|
|
|
33
33
|
app.use(bodyParser.json());
|
|
34
|
+
// body-parser v2 leaves req.body undefined when there is no body (v1 set {}).
|
|
35
|
+
// The request validators expect `body` to be present, so keep the v1 behaviour.
|
|
36
|
+
app.use((req, res, next) => {
|
|
37
|
+
if (req.body === undefined) {
|
|
38
|
+
req.body = {};
|
|
39
|
+
}
|
|
40
|
+
next();
|
|
41
|
+
});
|
|
34
42
|
app.use(cookieParser());
|
|
35
43
|
app.use(helmet());
|
|
36
44
|
|
package/src/schemas/rows.js
CHANGED
|
@@ -5,10 +5,13 @@ const listTableRows = Joi.object({
|
|
|
5
5
|
_page: Joi.number().integer().min(1).default(1),
|
|
6
6
|
_limit: Joi.number().integer().min(1).default(10),
|
|
7
7
|
_search: Joi.string(),
|
|
8
|
-
_ordering: Joi.string().regex(/^[\w
|
|
8
|
+
_ordering: Joi.string().regex(/^[\w.,-]+$/),
|
|
9
9
|
_schema: Joi.string(),
|
|
10
10
|
_extend: Joi.string(),
|
|
11
11
|
_filters: Joi.string(),
|
|
12
|
+
_distinct: Joi.string(),
|
|
13
|
+
_group_by: Joi.string(),
|
|
14
|
+
_aggregate: Joi.string(),
|
|
12
15
|
}).required(),
|
|
13
16
|
params: Joi.object({
|
|
14
17
|
name: Joi.string(),
|