soul-cli 0.10.0 → 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/workflows/ci.yml +2 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +12 -0
- package/ROADMAP.md +83 -25
- package/docs/api/rows-examples.md +10 -2
- package/package.json +1 -1
- package/src/controllers/rows.js +115 -92
- package/src/controllers/rows.query.test.js +369 -0
- package/src/schemas/rows.js +4 -1
- package/src/services/rowService.js +12 -6
- package/src/services/rowService.test.js +37 -0
- package/src/swagger/swagger.json +20 -2
- 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/.github/workflows/ci.yml
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.11.0](https://github.com/thevahidal/soul/compare/soul-cli-v0.10.0...soul-cli-v0.11.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add distinct, group/aggregate, new filter operators, OR groups and NOT to rows query ([95b70e4](https://github.com/thevahidal/soul/commit/95b70e4984fca5377b71b41e6736afb13a65083f))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* reject repeated query params in row query helpers ([41f1cce](https://github.com/thevahidal/soul/commit/41f1cce2dc6d1a4467018612e31b285fe540c924))
|
|
14
|
+
|
|
3
15
|
## [0.10.0](https://github.com/thevahidal/soul/compare/soul-cli-v0.9.4...soul-cli-v0.10.0) (2026-11-16)
|
|
4
16
|
|
|
5
17
|
|
package/ROADMAP.md
CHANGED
|
@@ -63,22 +63,41 @@ realtime API removes that entire failure surface.
|
|
|
63
63
|
|
|
64
64
|
## Roadmap
|
|
65
65
|
|
|
66
|
-
Ordered by
|
|
66
|
+
Ordered by importance / leverage. Each item should be scoped and planned
|
|
67
67
|
properly (via `/plan` or similar) when picked up — this is direction, not
|
|
68
68
|
a detailed spec.
|
|
69
69
|
|
|
70
|
-
### 1.
|
|
70
|
+
### 1. Query capabilities
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
Small, user-requested, and everything downstream (MCP `query_rows`, Studio)
|
|
73
|
+
inherits it, so do it first. Every identifier stays allowlist-validated via
|
|
74
|
+
`src/utils/sql.js`; values stay `?`-bound.
|
|
75
|
+
|
|
76
|
+
- `_distinct=col1,col2` → `SELECT DISTINCT`.
|
|
77
|
+
- `_group_by=col` + `_aggregate=count,sum:price,avg:price,min:x,max:x`
|
|
78
|
+
(aggregate function is an allowlist, like `operators`). Optional
|
|
79
|
+
`having`-style filters on aggregates later. Keep `getCount` correct for
|
|
80
|
+
pagination totals.
|
|
81
|
+
- More filter operators: `like`/`contains`/`startswith`, `between`,
|
|
82
|
+
`not in`, and OR groups (today all filters are ANDed).
|
|
83
|
+
- Multi-column `_ordering`.
|
|
84
|
+
- Make `_filters` parsing robust to values containing `,` and `:`.
|
|
85
|
+
|
|
86
|
+
Workaround until then: SQLite views are already queryable through the rows
|
|
87
|
+
API (`assertValidTableName` accepts views), so a view with `GROUP BY` works.
|
|
88
|
+
|
|
89
|
+
### 2. MCP server
|
|
90
|
+
|
|
91
|
+
Highest signal for where Soul's actual users increasingly are. Expose the
|
|
92
|
+
already-hardened service layer as MCP tools so agents (Claude Code, Cursor,
|
|
93
|
+
etc.) can attach to a Soul-backed DB directly instead of hand-rolling HTTP
|
|
94
|
+
calls.
|
|
76
95
|
|
|
77
96
|
Tools to expose (mirroring existing REST semantics):
|
|
78
97
|
|
|
79
98
|
- `list_tables`, `get_schema`
|
|
80
99
|
- `query_rows` (reuse the existing `_filters`/`_search`/`_ordering`/
|
|
81
|
-
`_schema`/`_extend` semantics from `rows.js
|
|
100
|
+
`_schema`/`_extend` semantics from `rows.js`, plus item 1)
|
|
82
101
|
- `insert_row`, `update_row`, `delete_row`
|
|
83
102
|
- `create_table` (JSON schema in, same shape as the existing REST
|
|
84
103
|
`POST /tables`)
|
|
@@ -89,18 +108,24 @@ logic needed, just a transport layer. Open question to resolve when
|
|
|
89
108
|
starting: ship as a separate package, or a mode on the existing CLI
|
|
90
109
|
(e.g. `soul --mcp`)?
|
|
91
110
|
|
|
92
|
-
###
|
|
111
|
+
### 3. Row-level permissions
|
|
93
112
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
113
|
+
Permissions today are per table and per verb only. Most real apps need
|
|
114
|
+
"users only see/edit their own rows" (e.g. a `user_id = :current_user`
|
|
115
|
+
rule per table/verb). Biggest gap between "works" and "production-ready"
|
|
116
|
+
for multi-user apps, and the natural foundation for item 4.
|
|
98
117
|
|
|
99
|
-
|
|
100
|
-
filter/ordering/table-name payloads) as both a visible regression guard
|
|
101
|
-
and something to point to.
|
|
118
|
+
### 4. Scoped agent credentials
|
|
102
119
|
|
|
103
|
-
|
|
120
|
+
Package the existing per-table CRUD permission/role system (see
|
|
121
|
+
`authService.hasTablePermission`) as a named feature: give an agent a key
|
|
122
|
+
that can only read/write specific tables. Likely needs a lighter API-key
|
|
123
|
+
auth mode alongside the current username/password + JWT flow, scoped at
|
|
124
|
+
key-creation time to specific tables/verbs (and rows, once item 3 lands).
|
|
125
|
+
Addresses a real, currently annoying problem in agentic products
|
|
126
|
+
(over-broad tool access).
|
|
127
|
+
|
|
128
|
+
### 5. Schema evolution (LLM-native schema workflows)
|
|
104
129
|
|
|
105
130
|
Soul's `createTable` API already takes a JSON schema description rather
|
|
106
131
|
than raw DDL — lean into that explicitly ("describe your schema, don't
|
|
@@ -109,7 +134,38 @@ endpoint) so an agent can iterate on a schema across a conversation
|
|
|
109
134
|
instead of only create-once. Consider migration history/versioning if
|
|
110
135
|
this sees real use.
|
|
111
136
|
|
|
112
|
-
###
|
|
137
|
+
### 6. Write-path hardening
|
|
138
|
+
|
|
139
|
+
- `bulkWrite` is a single `INSERT ... VALUES`; large batches will hit
|
|
140
|
+
SQLite's bound-variable limit. Chunk inside a transaction.
|
|
141
|
+
- Bulk update/delete by filter.
|
|
142
|
+
- Upsert (`ON CONFLICT`).
|
|
143
|
+
- Multi-operation transactions.
|
|
144
|
+
|
|
145
|
+
### 7. Production operations
|
|
146
|
+
|
|
147
|
+
- Health/readiness endpoint and basic metrics.
|
|
148
|
+
- Backup story documented in `docs/self-hosting.md` (e.g. Litestream).
|
|
149
|
+
- Structured logging and deployment guidance (Docker, reverse proxy).
|
|
150
|
+
|
|
151
|
+
### 8. Auth completeness
|
|
152
|
+
|
|
153
|
+
Audit `docs/auth.md` against what real apps need and fill gaps: password
|
|
154
|
+
reset, email verification, API keys (overlaps item 4), optional OAuth.
|
|
155
|
+
Verify what exists before scoping.
|
|
156
|
+
|
|
157
|
+
### 9. Make the safety properties part of the pitch
|
|
158
|
+
|
|
159
|
+
Write up the specific bug classes found and fixed in the hardening pass
|
|
160
|
+
(injection via unvalidated identifiers, the WS auth gap, the rate-limit
|
|
161
|
+
bug) as the actual argument for "safe to let an agent drive this." The
|
|
162
|
+
regression tests added during that work are the receipts.
|
|
163
|
+
|
|
164
|
+
Consider: an ongoing adversarial/fuzz test suite (random or LLM-generated
|
|
165
|
+
filter/ordering/table-name payloads) as both a visible regression guard
|
|
166
|
+
and something to point to. Extend it to the new query params from item 1.
|
|
167
|
+
|
|
168
|
+
### 10. Soul Studio v1 — scoped, not full-featured
|
|
113
169
|
|
|
114
170
|
Target workflow: agent writes to the DB via Soul, human opens Studio to
|
|
115
171
|
inspect and correct it. Not a general-purpose admin framework.
|
|
@@ -118,16 +174,18 @@ Needs: table/row browser, inline edit, respecting the same permission
|
|
|
118
174
|
model as the API. Explicitly **not** in scope for v1: dashboards, complex
|
|
119
175
|
relation UIs, anything Retool-shaped — get the core loop solid first.
|
|
120
176
|
|
|
121
|
-
###
|
|
177
|
+
### 11. Search and pagination
|
|
122
178
|
|
|
123
|
-
|
|
124
|
-
`
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
179
|
+
- Full-text search via FTS5 instead of `LIKE` across every column in
|
|
180
|
+
`_search`.
|
|
181
|
+
- Cursor (keyset) pagination; offset pagination gets slow on deep pages.
|
|
182
|
+
|
|
183
|
+
### 12. File storage
|
|
184
|
+
|
|
185
|
+
Attach files to rows (local disk first, S3-compatible later). Not needed
|
|
186
|
+
for the agent-backend niche, so lowest priority.
|
|
129
187
|
|
|
130
|
-
###
|
|
188
|
+
### Cheap wins (do anytime, alongside the above)
|
|
131
189
|
|
|
132
190
|
- Ship an `llms.txt`.
|
|
133
191
|
- Explicitly point agents at the already-generated OpenAPI/Swagger spec
|
|
@@ -32,11 +32,19 @@ Response
|
|
|
32
32
|
- `_page` e.g. `?_page=2`, to get the second page of results.
|
|
33
33
|
- `_limit` e.g. `?_limit=20`, to get 20 results per page.
|
|
34
34
|
- `_search` e.g. `?_search=rock`, to search across all fields.
|
|
35
|
-
- `_ordering` e.g. `?_ordering=-Title`, to order rows by title descending, or without `-` to sort ascending, e.g. `?_ordering=Title
|
|
35
|
+
- `_ordering` e.g. `?_ordering=-Title`, to order rows by title descending, or without `-` to sort ascending, e.g. `?_ordering=Title`. Use commas to order by several columns, e.g. `?_ordering=ArtistId,-Title`.
|
|
36
36
|
- `_schema` e.g. `?_schema=Title,ArtistId`, to get only the Title and ArtistId columns.
|
|
37
37
|
- `_extend` e.g. `?_extend=ArtistId`, to get the Artist object related to the Album.
|
|
38
|
+
- `_distinct` e.g. `?_distinct=ArtistId`, to get the unique values of a column (or unique combinations, e.g. `?_distinct=ArtistId,Title`). Can't be combined with `_schema`, `_extend`, `_group_by` or `_aggregate`.
|
|
39
|
+
- `_group_by` and `_aggregate` e.g. `?_group_by=ArtistId&_aggregate=count,max:Milliseconds`, to group rows and aggregate them. Aggregates are `count`, `count:col`, `sum:col`, `avg:col`, `min:col`, `max:col` and are returned as `count`, `max_Milliseconds`, ... `_aggregate` alone returns a single row. `_filters` apply before grouping, `_ordering` can use a grouped column or an aggregate name (e.g. `-count`), and `total` is the number of groups.
|
|
38
40
|
- `_filters` e.g. `?_filters=ArtistId:[1,2,3],Title:Rock`, to get only the rows where the ArtistId can be 1,2 or 3 and the Title is Rock.
|
|
39
|
-
NOTE: If you want to use comparison operators in the filter, you can use these operators after the field: `__eq, __neq, __lt, __gt, __lte, __gte, __null, __notnull` . For example, you can use `/invoices/rows?_filters=InvoiceId__neq:1,Total__gte:5,BillingPostalCode__notnull`
|
|
41
|
+
NOTE: If you want to use comparison operators in the filter, you can use these operators after the field: `__eq, __neq, __lt, __gt, __lte, __gte, __null, __notnull, __like, __contains, __startswith, __between, __in, __notin` . For example, you can use `/invoices/rows?_filters=InvoiceId__neq:1,Total__gte:5,BillingPostalCode__notnull`
|
|
42
|
+
|
|
43
|
+
- `__like:A%` uses SQL `LIKE` wildcards; `__contains:rock` and `__startswith:Rock` match literally.
|
|
44
|
+
- `__between:[1,10]` is inclusive. `__in:[1,2]` and `__notin:[1,2]` take a list.
|
|
45
|
+
- Wrap a value in double quotes to use `,` `:` `|` `(` `)` in it, e.g. `Title:"Rock, Paper"`; write `""` for a literal quote. Quotes only count when the closing quote ends the value.
|
|
46
|
+
- An OR group is written in parentheses: `(Total__gt:5|BillingPostalCode__null),Country:USA`. Groups and other filters are ANDed.
|
|
47
|
+
- `!` negates a filter or a group: `!Country:USA`, `!(Total__gt:5|Total__null)`. Like `__neq`, a negated filter skips rows where the column is NULL.
|
|
40
48
|
|
|
41
49
|
Example with query params
|
|
42
50
|
|
package/package.json
CHANGED
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
|