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.
Files changed (43) hide show
  1. package/.env.sample +1 -0
  2. package/.github/workflows/ci.yml +2 -0
  3. package/.prettierrc +4 -0
  4. package/.release-please-manifest.json +1 -1
  5. package/CHANGELOG.md +25 -0
  6. package/README.md +3 -0
  7. package/ROADMAP.md +83 -25
  8. package/docs/api/batch-examples.md +55 -0
  9. package/docs/api/rows-examples.md +10 -2
  10. package/docs/api-examples.md +1 -0
  11. package/docs/auth.md +10 -0
  12. package/package.json +1 -1
  13. package/src/cli.js +6 -0
  14. package/src/config/index.js +7 -0
  15. package/src/config/index.test.js +10 -0
  16. package/src/constants/messages.js +1 -1
  17. package/src/controllers/batch.js +128 -0
  18. package/src/controllers/batch.test.js +180 -0
  19. package/src/controllers/rows.js +223 -213
  20. package/src/controllers/rows.query.test.js +369 -0
  21. package/src/index.js +2 -0
  22. package/src/middlewares/api.js +6 -2
  23. package/src/middlewares/api.test.js +27 -13
  24. package/src/middlewares/auth.js +52 -33
  25. package/src/middlewares/auth.test.js +137 -3
  26. package/src/middlewares/broadcast.js +7 -11
  27. package/src/routes/batch.js +12 -0
  28. package/src/routes/rows.js +1 -0
  29. package/src/schemas/batch.js +27 -0
  30. package/src/schemas/rows.js +4 -1
  31. package/src/services/authService.js +31 -4
  32. package/src/services/authService.test.js +76 -0
  33. package/src/services/rowService.js +12 -6
  34. package/src/services/rowService.test.js +37 -0
  35. package/src/swagger/index.js +14 -16
  36. package/src/swagger/swagger.json +68 -59
  37. package/src/utils/query.js +285 -0
  38. package/src/utils/query.test.js +147 -0
  39. package/src/utils/sql.js +24 -0
  40. package/src/utils/sql.test.js +14 -0
  41. package/src/websocket.js +10 -8
  42. package/src/websocket.test.js +52 -0
  43. package/src/schemas/index.js +0 -30
package/.env.sample CHANGED
@@ -27,5 +27,6 @@ REFRESH_TOKEN_EXPIRATION_TIME=2D
27
27
  INITIAL_USER_USERNAME
28
28
  INITIAL_USER_PASSWORD
29
29
  TOKEN_SECRET
30
+ ANON_ROLE
30
31
 
31
32
  START_WITH_STUDIO=false
@@ -3,7 +3,9 @@ name: CI
3
3
  on:
4
4
  push:
5
5
  branches: [main]
6
+ paths-ignore: ['**.md', 'docs/**', 'assets/**', '.bruno/**']
6
7
  pull_request:
8
+ paths-ignore: ['**.md', 'docs/**', 'assets/**', '.bruno/**']
7
9
 
8
10
  permissions:
9
11
  contents: read
package/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "singleQuote": true,
3
+ "trailingComma": "all"
4
+ }
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.10.0"
2
+ ".": "0.12.0"
3
3
  }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.12.0](https://github.com/thevahidal/soul/compare/soul-cli-v0.11.0...soul-cli-v0.12.0) (2026-11-16)
4
+
5
+
6
+ ### Features
7
+
8
+ * add anonymous role for requests without a token ([2bb7f2e](https://github.com/thevahidal/soul/commit/2bb7f2edfc72363ad83dc6677319efc14c066d8c))
9
+ * add atomic batch endpoint for row writes ([#275](https://github.com/thevahidal/soul/issues/275)) ([760bd39](https://github.com/thevahidal/soul/commit/760bd397b98837dee72264223e8ad1d9f6493b9e))
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * allow read-only access to auth tables when auth is off ([5428103](https://github.com/thevahidal/soul/commit/54281030d0f11a3df7a0cb14c397dc7d2e24c9c3))
15
+
16
+ ## [0.11.0](https://github.com/thevahidal/soul/compare/soul-cli-v0.10.0...soul-cli-v0.11.0) (2026-11-16)
17
+
18
+
19
+ ### Features
20
+
21
+ * add distinct, group/aggregate, new filter operators, OR groups and NOT to rows query ([95b70e4](https://github.com/thevahidal/soul/commit/95b70e4984fca5377b71b41e6736afb13a65083f))
22
+
23
+
24
+ ### Bug Fixes
25
+
26
+ * reject repeated query params in row query helpers ([41f1cce](https://github.com/thevahidal/soul/commit/41f1cce2dc6d1a4467018612e31b285fe540c924))
27
+
3
28
  ## [0.10.0](https://github.com/thevahidal/soul/compare/soul-cli-v0.9.4...soul-cli-v0.10.0) (2026-11-16)
4
29
 
5
30
 
package/README.md CHANGED
@@ -73,6 +73,7 @@ Options:
73
73
  --iup, --initialuserpassword Initial user password [string]
74
74
 
75
75
  --ts, --tokensecret Token Secret [string]
76
+ --ar, --anonrole Role name for requests without a token [string]
76
77
  --atet, --accesstokenexpirationtime Access Token Expiration Time (Default: 5H) [string]
77
78
  --rtet, --refreshtokenexpirationtime Refresh Token Expiration Time (Default: 1D) [string]
78
79
  -S, --studio Start Soul Studio in parallel
@@ -102,6 +103,8 @@ Run the Soul command with the necessary parameters:
102
103
 
103
104
  ```
104
105
 
106
+ > Note: Without `-a`, the auth tables (`_users`, `_roles`, `_roles_permissions`, `_users_roles`) are read-only.
107
+
105
108
  > Note: When configuring your JWT Secret, it is recommended to use a long string value for enhanced security. It is advisable to use a secret that is at least 10 characters in length.
106
109
 
107
110
  In this example:
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 priority / leverage. Each item should be scoped and planned
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. MCP server (start here)
70
+ ### 1. Query capabilities
71
71
 
72
- Smallest scope, highest signal for where Soul's actual users increasingly
73
- are. Expose the already-hardened service layer as MCP tools so agents
74
- (Claude Code, Cursor, etc.) can attach to a Soul-backed DB directly instead
75
- of hand-rolling HTTP calls.
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
- ### 2. Make the safety properties part of the pitch
111
+ ### 3. Row-level permissions
93
112
 
94
- Write up the specific bug classes found and fixed in the hardening pass
95
- (injection via unvalidated identifiers, the WS auth gap, the rate-limit
96
- bug) as the actual argument for "safe to let an agent drive this." The
97
- regression tests added during that work are the receipts.
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
- Consider: an ongoing adversarial/fuzz test suite (random or LLM-generated
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
- ### 3. LLM-native schema workflows
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
- ### 4. Soul Studio v1 — scoped, not full-featured
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
- ### 5. Scoped agent credentials
177
+ ### 11. Search and pagination
122
178
 
123
- Package the existing per-table CRUD permission/role system (see
124
- `authService.hasTablePermission`) as a named feature: give an agent a key
125
- that can only read/write specific tables. Likely needs a lighter API-key
126
- auth mode alongside the current username/password + JWT flow, scoped at
127
- key-creation time to specific tables/verbs. Addresses a real, currently
128
- annoying problem in agentic products (over-broad tool access).
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
- ### 6. Cheap wins
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
@@ -0,0 +1,55 @@
1
+ ## Batch
2
+
3
+ `POST /api/batch` runs several row writes (insert, update, delete) in one transaction. If any operation fails, none are applied.
4
+
5
+ Each operation is the REST call you would make on its own: a `method`, a `path` and an optional `body`. Supported paths are `/api/tables/<table-name>/rows` (`POST`) and `/api/tables/<table-name>/rows/<pks>` (`PUT`, `DELETE`, with `?_lookup_field=` supported).
6
+
7
+ ```bash
8
+ curl --request POST \
9
+ --url http://localhost:8000/api/batch \
10
+ --header 'Content-Type: application/json' \
11
+ --data '{
12
+ "operations": [
13
+ {
14
+ "method": "POST",
15
+ "path": "/api/tables/Album/rows",
16
+ "body": { "fields": { "Title": "New Album", "ArtistId": 1 } }
17
+ },
18
+ {
19
+ "method": "PUT",
20
+ "path": "/api/tables/Artist/rows/1",
21
+ "body": { "fields": { "Name": "Renamed" } }
22
+ },
23
+ { "method": "DELETE", "path": "/api/tables/Album/rows/2" }
24
+ ]
25
+ }'
26
+ ```
27
+
28
+ Response: one result per operation, in order.
29
+
30
+ ```json
31
+ {
32
+ "results": [
33
+ { "status": 201, "body": { "message": "Row inserted", "data": {} } },
34
+ { "status": 200, "body": { "message": "Row updated", "data": {} } },
35
+ { "status": 200, "body": { "message": "Row deleted", "data": {} } }
36
+ ]
37
+ }
38
+ ```
39
+
40
+ If an operation fails, the whole batch is rolled back and the response has the status of the failure plus the `index` of the failing operation:
41
+
42
+ ```json
43
+ {
44
+ "message": "table Album has no column named Nope",
45
+ "index": 0
46
+ }
47
+ ```
48
+
49
+ ### Limits
50
+
51
+ - Between 1 and 100 operations per request.
52
+ - Only row writes. `GET` and other paths are rejected.
53
+ - Tables `_users`, `_roles`, `_users_roles` and `_roles_permissions` are not allowed.
54
+ - In auth mode, every operation needs the same table permission as the equivalent single request. If one is denied, the response is `403` and nothing is written.
55
+ - WebSocket events are sent only after the batch commits.
@@ -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
 
@@ -38,6 +38,7 @@ soul -d ./Chinook_Sqlite.sqlite -p 8000
38
38
  3. [/api/auth/](api/auth-examples.md) Examples for Authentication / Authorization endpoints
39
39
  1. [/api/{table-name}/rows/](api/rows-auth-examples.md) Examples for Rows endpoints in Auth mode
40
40
  4. [/api/](api/root-examples.md) Examples for Root endpoints
41
+ 5. [/api/batch](api/batch-examples.md) Examples for atomic batches of row writes
41
42
 
42
43
  ## Handling Errors
43
44
 
package/docs/auth.md CHANGED
@@ -101,6 +101,16 @@ Soul uses the `default` role to assign permissions to new users.
101
101
 
102
102
  The same happens when Soul boots up, but for all existing tables, making sure that all tables have the `default` role assigned to them.
103
103
 
104
+ ###### Anonymous Role
105
+
106
+ Set `ANON_ROLE` (or `--anonrole`) to the name of a role to let requests without an access token through, limited to that role's permissions. For example, to make the `posts` table publicly readable:
107
+
108
+ 1. Create a role named `public` in `_roles`.
109
+ 2. Add a `_roles_permissions` row for it with `table_name` `posts` and `read` `true`.
110
+ 3. Start Soul with `ANON_ROLE=public`.
111
+
112
+ The anon role also applies to logged-in users, on top of their own roles. It never applies to system tables (names starting with `_`), and an invalid or expired token is still rejected with a 401.
113
+
104
114
  ##### Users Roles
105
115
 
106
116
  To assign roles to users we have a join table called `_users_roles` with the following attributes:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soul-cli",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "A SQLite REST and Realtime server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -64,6 +64,12 @@ if (process.env.NO_CLI !== 'true') {
64
64
  type: 'string',
65
65
  demandOption: false,
66
66
  })
67
+ .options('ar', {
68
+ alias: 'anonrole',
69
+ describe: 'Role name applied to requests without an access token',
70
+ type: 'string',
71
+ demandOption: false,
72
+ })
67
73
  .options('atet', {
68
74
  alias: 'accesstokenexpirationtime',
69
75
  describe: 'JWT expiration time for access token',
@@ -37,6 +37,7 @@ const envVarsSchema = Joi.object()
37
37
  INITIAL_USER_PASSWORD: Joi.string(),
38
38
 
39
39
  TOKEN_SECRET: Joi.string(),
40
+ ANON_ROLE: Joi.string(),
40
41
  ACCESS_TOKEN_EXPIRATION_TIME: Joi.string(),
41
42
  REFRESH_TOKEN_EXPIRATION_TIME: Joi.string(),
42
43
  })
@@ -82,6 +83,10 @@ if (argv.refreshtokenexpirationtime) {
82
83
  env.REFRESH_TOKEN_EXPIRATION_TIME = argv.refreshtokenexpirationtime;
83
84
  }
84
85
 
86
+ if (argv.anonrole) {
87
+ env.ANON_ROLE = argv.anonrole;
88
+ }
89
+
85
90
  if (argv.initialuserusername) {
86
91
  env.INITIAL_USER_USERNAME = argv.initialuserusername;
87
92
  }
@@ -127,6 +132,8 @@ module.exports = {
127
132
  envVars.REFRESH_TOKEN_EXPIRATION_TIME ||
128
133
  '3D',
129
134
 
135
+ anonRole: argv.anonrole || envVars.ANON_ROLE || null,
136
+
130
137
  initialUserUsername:
131
138
  argv.initialuserusername || envVars.INITIAL_USER_USERNAME,
132
139
  initialUserPassword:
@@ -30,6 +30,16 @@ describe('Config', () => {
30
30
  expect(config.rateLimit.max).toBe(10);
31
31
  });
32
32
 
33
+ it('reads the anonymous role from ANON_ROLE and defaults to null', () => {
34
+ process.env.ANON_ROLE = 'public';
35
+ jest.resetModules();
36
+ expect(require('./index').anonRole).toBe('public');
37
+
38
+ delete process.env.ANON_ROLE;
39
+ jest.resetModules();
40
+ expect(require('./index').anonRole).toBeNull();
41
+ });
42
+
33
43
  it('reads cookie SameSite/Secure from COOKIE_SAMESITE and COOKIE_SECURE', () => {
34
44
  process.env.COOKIE_SAMESITE = 'none';
35
45
  process.env.COOKIE_SECURE = 'true';
@@ -23,7 +23,7 @@ module.exports = {
23
23
  PERMISSION_NOT_DEFINED_ERROR: 'Permission not defined for this role',
24
24
  ROLE_NOT_FOUND_ERROR: 'Role not found for this user',
25
25
  AUTH_SET_TO_FALSE_ERROR:
26
- 'You can not access this endpoint while AUTH is set to false',
26
+ 'Auth tables are read-only while AUTH is set to false',
27
27
  RESERVED_TABLE_NAME_ERROR:
28
28
  'The table name is reserved. Please choose a different name for the table.',
29
29
  SERVER_ERROR: 'Server error',
@@ -0,0 +1,128 @@
1
+ const config = require('../config');
2
+ const db = require('../db/index');
3
+ const { authService } = require('../services');
4
+ const { dbConstants, responseMessages } = require('../constants');
5
+ const { invalidRequestError } = require('../utils/sql');
6
+ const { broadcastToTable } = require('../middlewares/broadcast');
7
+ const rowSchemas = require('../schemas/rows');
8
+ const { insertRow, updateRows, deleteRows } = require('./rows');
9
+
10
+ const ROW_PATH = /^\/api\/tables\/([^/]+)\/rows(?:\/([^/]+))?$/;
11
+
12
+ const routes = {
13
+ POST: {
14
+ schema: rowSchemas.insertRowInTable,
15
+ run: ({ params, body }) =>
16
+ insertRow({ tableName: params.name, fields: body.fields }),
17
+ },
18
+ PUT: {
19
+ schema: rowSchemas.updateRowInTableByPK,
20
+ run: ({ params, query, body }) =>
21
+ updateRows({
22
+ tableName: params.name,
23
+ pks: params.pks,
24
+ fields: body.fields,
25
+ lookupField: query._lookup_field,
26
+ }),
27
+ },
28
+ DELETE: {
29
+ schema: rowSchemas.deleteRowInTableByPK,
30
+ run: ({ params, query }) =>
31
+ deleteRows({
32
+ tableName: params.name,
33
+ pks: params.pks,
34
+ lookupField: query._lookup_field,
35
+ }),
36
+ },
37
+ };
38
+
39
+ // Validate and authorize one operation; returns what is needed to run it.
40
+ const prepareOperation = ({ method, path, body = {} }, user) => {
41
+ const url = new URL(path, 'http://soul');
42
+ const match = ROW_PATH.exec(url.pathname);
43
+
44
+ if (!match) {
45
+ throw invalidRequestError(`Unsupported path '${path}'`);
46
+ }
47
+
48
+ const [, name, pks] = match;
49
+ const { schema, run } = routes[method];
50
+ const { value, error } = schema.validate({
51
+ params: pks ? { name, pks } : { name },
52
+ query: Object.fromEntries(url.searchParams),
53
+ body,
54
+ cookies: {},
55
+ });
56
+
57
+ if (error) {
58
+ throw invalidRequestError(error.message);
59
+ }
60
+
61
+ if (dbConstants.reservedTableNames.includes(name)) {
62
+ throw invalidRequestError(`Table '${name}' is not allowed in a batch`);
63
+ }
64
+
65
+ if (
66
+ config.auth &&
67
+ !authService.hasTablePermission({
68
+ payload: user || null,
69
+ tableName: name,
70
+ verb: method,
71
+ })
72
+ ) {
73
+ throw invalidRequestError(
74
+ responseMessages.errorMessage.NOT_AUTHORIZED_ERROR,
75
+ 403,
76
+ );
77
+ }
78
+
79
+ return { tableName: name, run: () => run(value) };
80
+ };
81
+
82
+ const withIndex = (index, fn) => {
83
+ try {
84
+ return fn();
85
+ } catch (error) {
86
+ error.index = index;
87
+ throw error;
88
+ }
89
+ };
90
+
91
+ const batch = async (req, res) => {
92
+ /*
93
+ #swagger.tags = ['Rows']
94
+ #swagger.summary = 'Batch'
95
+ #swagger.description = 'Run row inserts, updates and deletes atomically: if one fails, none are applied.'
96
+ #swagger.parameters['body'] = {
97
+ in: 'body',
98
+ required: true,
99
+ type: 'object',
100
+ schema: { $ref: '#/definitions/BatchRequestBody' }
101
+ }
102
+ */
103
+ try {
104
+ const operations = req.body.operations.map((operation, index) =>
105
+ withIndex(index, () => prepareOperation(operation, req.user)),
106
+ );
107
+
108
+ const results = db.transaction(() =>
109
+ operations.map((operation, index) => withIndex(index, operation.run)),
110
+ )();
111
+
112
+ res.json({
113
+ results: results.map(({ status, body }) => ({ status, body })),
114
+ });
115
+
116
+ results.forEach(({ broadcast }, index) =>
117
+ broadcastToTable(operations[index].tableName, broadcast),
118
+ );
119
+ } catch (error) {
120
+ res.status(error.status || 400).json({
121
+ message: error.message,
122
+ index: error.index,
123
+ error: error.body?.error,
124
+ });
125
+ }
126
+ };
127
+
128
+ module.exports = { batch };