soul-cli 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.sample +1 -0
- package/.github/workflows/ci.yml +2 -0
- package/.prettierrc +4 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +25 -0
- package/README.md +3 -0
- package/ROADMAP.md +83 -25
- package/docs/api/batch-examples.md +55 -0
- package/docs/api/rows-examples.md +10 -2
- package/docs/api-examples.md +1 -0
- package/docs/auth.md +10 -0
- package/package.json +1 -1
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -0
- package/src/config/index.test.js +10 -0
- package/src/constants/messages.js +1 -1
- package/src/controllers/batch.js +128 -0
- package/src/controllers/batch.test.js +180 -0
- package/src/controllers/rows.js +223 -213
- package/src/controllers/rows.query.test.js +369 -0
- package/src/index.js +2 -0
- package/src/middlewares/api.js +6 -2
- package/src/middlewares/api.test.js +27 -13
- package/src/middlewares/auth.js +52 -33
- package/src/middlewares/auth.test.js +137 -3
- package/src/middlewares/broadcast.js +7 -11
- package/src/routes/batch.js +12 -0
- package/src/routes/rows.js +1 -0
- package/src/schemas/batch.js +27 -0
- package/src/schemas/rows.js +4 -1
- package/src/services/authService.js +31 -4
- package/src/services/authService.test.js +76 -0
- package/src/services/rowService.js +12 -6
- package/src/services/rowService.test.js +37 -0
- package/src/swagger/index.js +14 -16
- package/src/swagger/swagger.json +68 -59
- package/src/utils/query.js +285 -0
- package/src/utils/query.test.js +147 -0
- package/src/utils/sql.js +24 -0
- package/src/utils/sql.test.js +14 -0
- package/src/websocket.js +10 -8
- package/src/websocket.test.js +52 -0
- package/src/schemas/index.js +0 -30
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
jest.mock('../config', () => ({
|
|
1
|
+
jest.mock('../config', () => ({
|
|
2
|
+
auth: false,
|
|
3
|
+
tokenSecret: 'test-secret',
|
|
4
|
+
anonRole: null,
|
|
5
|
+
}));
|
|
2
6
|
jest.mock('../utils/index', () => ({ decodeToken: jest.fn() }));
|
|
3
7
|
jest.mock('../utils/logger', () => ({ error: jest.fn() }));
|
|
4
8
|
jest.mock('../services', () => ({
|
|
@@ -8,7 +12,7 @@ jest.mock('../services', () => ({
|
|
|
8
12
|
const config = require('../config');
|
|
9
13
|
const { decodeToken } = require('../utils/index');
|
|
10
14
|
const { authService } = require('../services');
|
|
11
|
-
const { hasAccess } = require('./auth');
|
|
15
|
+
const { authenticate, hasAccess } = require('./auth');
|
|
12
16
|
|
|
13
17
|
describe('hasAccess', () => {
|
|
14
18
|
let req, res, next;
|
|
@@ -16,11 +20,12 @@ describe('hasAccess', () => {
|
|
|
16
20
|
beforeEach(() => {
|
|
17
21
|
jest.clearAllMocks();
|
|
18
22
|
config.auth = false;
|
|
23
|
+
config.anonRole = null;
|
|
19
24
|
req = {
|
|
20
25
|
params: {},
|
|
21
26
|
method: 'GET',
|
|
22
27
|
originalUrl: '/api/tables/items/rows',
|
|
23
|
-
cookies: {},
|
|
28
|
+
cookies: { accessToken: 'token' },
|
|
24
29
|
};
|
|
25
30
|
res = { status: jest.fn().mockReturnThis(), send: jest.fn() };
|
|
26
31
|
next = jest.fn();
|
|
@@ -90,6 +95,72 @@ describe('hasAccess', () => {
|
|
|
90
95
|
expect(next).not.toHaveBeenCalled();
|
|
91
96
|
});
|
|
92
97
|
|
|
98
|
+
it('returns 401 when there is no token and no anon role is configured', async () => {
|
|
99
|
+
config.auth = true;
|
|
100
|
+
req.cookies = {};
|
|
101
|
+
|
|
102
|
+
await hasAccess(req, res, next);
|
|
103
|
+
|
|
104
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
105
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
106
|
+
expect(next).not.toHaveBeenCalled();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('checks permissions with a null payload when there is no token but an anon role is configured', async () => {
|
|
110
|
+
config.auth = true;
|
|
111
|
+
req.cookies = {};
|
|
112
|
+
config.anonRole = 'public';
|
|
113
|
+
authService.hasTablePermission.mockReturnValue(true);
|
|
114
|
+
req.params.name = 'items';
|
|
115
|
+
|
|
116
|
+
await hasAccess(req, res, next);
|
|
117
|
+
|
|
118
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
119
|
+
expect(authService.hasTablePermission).toHaveBeenCalledWith({
|
|
120
|
+
payload: null,
|
|
121
|
+
tableName: 'items',
|
|
122
|
+
verb: 'GET',
|
|
123
|
+
});
|
|
124
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('returns 403 when the anon role does not allow the request', async () => {
|
|
128
|
+
config.auth = true;
|
|
129
|
+
req.cookies = {};
|
|
130
|
+
config.anonRole = 'public';
|
|
131
|
+
authService.hasTablePermission.mockReturnValue(false);
|
|
132
|
+
req.params.name = 'items';
|
|
133
|
+
|
|
134
|
+
await hasAccess(req, res, next);
|
|
135
|
+
|
|
136
|
+
expect(res.status).toHaveBeenCalledWith(403);
|
|
137
|
+
expect(next).not.toHaveBeenCalled();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('does not let anonymous requests use universal endpoints', async () => {
|
|
141
|
+
config.auth = true;
|
|
142
|
+
req.cookies = {};
|
|
143
|
+
config.anonRole = 'public';
|
|
144
|
+
authService.hasTablePermission.mockReturnValue(false);
|
|
145
|
+
req.originalUrl = '/api/auth/change-password';
|
|
146
|
+
|
|
147
|
+
await hasAccess(req, res, next);
|
|
148
|
+
|
|
149
|
+
expect(res.status).toHaveBeenCalledWith(403);
|
|
150
|
+
expect(next).not.toHaveBeenCalled();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('still returns 401 for an invalid token when an anon role is configured', async () => {
|
|
154
|
+
config.auth = true;
|
|
155
|
+
config.anonRole = 'public';
|
|
156
|
+
decodeToken.mockRejectedValue(new Error('invalid token'));
|
|
157
|
+
|
|
158
|
+
await hasAccess(req, res, next);
|
|
159
|
+
|
|
160
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
161
|
+
expect(authService.hasTablePermission).not.toHaveBeenCalled();
|
|
162
|
+
});
|
|
163
|
+
|
|
93
164
|
it('returns 401 and logs when an unexpected error is thrown', async () => {
|
|
94
165
|
config.auth = true;
|
|
95
166
|
decodeToken.mockResolvedValue({ userId: 1, isSuperuser: true });
|
|
@@ -103,3 +174,66 @@ describe('hasAccess', () => {
|
|
|
103
174
|
expect(res.send).toHaveBeenCalledWith({ message: 'boom' });
|
|
104
175
|
});
|
|
105
176
|
});
|
|
177
|
+
|
|
178
|
+
describe('authenticate', () => {
|
|
179
|
+
let req, res, next;
|
|
180
|
+
|
|
181
|
+
beforeEach(() => {
|
|
182
|
+
jest.clearAllMocks();
|
|
183
|
+
req = { cookies: { accessToken: 'token' } };
|
|
184
|
+
res = { status: jest.fn().mockReturnThis(), send: jest.fn() };
|
|
185
|
+
next = jest.fn();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('skips authentication when auth is disabled', async () => {
|
|
189
|
+
config.auth = false;
|
|
190
|
+
|
|
191
|
+
await authenticate(req, res, next);
|
|
192
|
+
|
|
193
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
194
|
+
expect(decodeToken).not.toHaveBeenCalled();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('sets req.user from a valid token', async () => {
|
|
198
|
+
config.auth = true;
|
|
199
|
+
decodeToken.mockResolvedValue({ username: 'john' });
|
|
200
|
+
|
|
201
|
+
await authenticate(req, res, next);
|
|
202
|
+
|
|
203
|
+
expect(req.user).toEqual({ username: 'john' });
|
|
204
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('returns 401 without a token when no anon role is configured', async () => {
|
|
208
|
+
config.auth = true;
|
|
209
|
+
config.anonRole = undefined;
|
|
210
|
+
req.cookies = {};
|
|
211
|
+
|
|
212
|
+
await authenticate(req, res, next);
|
|
213
|
+
|
|
214
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
215
|
+
expect(next).not.toHaveBeenCalled();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('lets a request without a token through when an anon role is configured', async () => {
|
|
219
|
+
config.auth = true;
|
|
220
|
+
config.anonRole = 'anon';
|
|
221
|
+
req.cookies = {};
|
|
222
|
+
|
|
223
|
+
await authenticate(req, res, next);
|
|
224
|
+
|
|
225
|
+
expect(next).toHaveBeenCalledTimes(1);
|
|
226
|
+
expect(req.user).toBeUndefined();
|
|
227
|
+
config.anonRole = undefined;
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('returns 401 for an invalid token', async () => {
|
|
231
|
+
config.auth = true;
|
|
232
|
+
decodeToken.mockRejectedValue(new Error('bad'));
|
|
233
|
+
|
|
234
|
+
await authenticate(req, res, next);
|
|
235
|
+
|
|
236
|
+
expect(res.status).toHaveBeenCalledWith(401);
|
|
237
|
+
expect(next).not.toHaveBeenCalled();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
const { websocketSubscribers } = require('../websocket');
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const subscribers = websocketSubscribers?.get(tableName);
|
|
8
|
-
|
|
9
|
-
if (subscribers) {
|
|
10
|
-
subscribers.forEach(({ ws }) => {
|
|
11
|
-
ws.send(JSON.stringify(data));
|
|
12
|
-
});
|
|
13
|
-
}
|
|
3
|
+
const broadcastToTable = (tableName, data) => {
|
|
4
|
+
websocketSubscribers?.get(tableName)?.forEach(({ ws }) => {
|
|
5
|
+
ws.send(JSON.stringify(data));
|
|
6
|
+
});
|
|
14
7
|
};
|
|
15
8
|
|
|
9
|
+
const broadcast = (req) => broadcastToTable(req.params.name, req.broadcast);
|
|
10
|
+
|
|
16
11
|
module.exports = {
|
|
17
12
|
broadcast,
|
|
13
|
+
broadcastToTable,
|
|
18
14
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/batch');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const { authenticate } = require('../middlewares/auth');
|
|
6
|
+
const schema = require('../schemas/batch');
|
|
7
|
+
|
|
8
|
+
const router = express.Router();
|
|
9
|
+
|
|
10
|
+
router.post('/batch', authenticate, validator(schema.batch), controllers.batch);
|
|
11
|
+
|
|
12
|
+
module.exports = router;
|
package/src/routes/rows.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
const MAX_OPERATIONS = 100;
|
|
4
|
+
|
|
5
|
+
const batch = Joi.object({
|
|
6
|
+
query: Joi.object().required(),
|
|
7
|
+
params: Joi.object().required(),
|
|
8
|
+
body: Joi.object({
|
|
9
|
+
operations: Joi.array()
|
|
10
|
+
.items(
|
|
11
|
+
Joi.object({
|
|
12
|
+
method: Joi.string().valid('POST', 'PUT', 'DELETE').required(),
|
|
13
|
+
path: Joi.string().required(),
|
|
14
|
+
body: Joi.object(),
|
|
15
|
+
}),
|
|
16
|
+
)
|
|
17
|
+
.min(1)
|
|
18
|
+
.max(MAX_OPERATIONS)
|
|
19
|
+
.required(),
|
|
20
|
+
}).required(),
|
|
21
|
+
cookies: Joi.object({
|
|
22
|
+
refreshToken: Joi.string().optional(),
|
|
23
|
+
accessToken: Joi.string().optional(),
|
|
24
|
+
}).unknown(true),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
module.exports = { batch };
|
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(),
|
|
@@ -2,6 +2,7 @@ const buildRowService = require('./rowService');
|
|
|
2
2
|
|
|
3
3
|
const { constantRoles, dbConstants, apiConstants } = require('../constants');
|
|
4
4
|
const { toBoolean } = require('../utils');
|
|
5
|
+
const config = require('../config');
|
|
5
6
|
|
|
6
7
|
const {
|
|
7
8
|
USERS_TABLE,
|
|
@@ -109,10 +110,26 @@ module.exports = (db) => {
|
|
|
109
110
|
return result;
|
|
110
111
|
},
|
|
111
112
|
|
|
113
|
+
getAnonRoleId() {
|
|
114
|
+
if (!config.anonRole) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const [role] = rowService.get({
|
|
119
|
+
tableName: ROLES_TABLE,
|
|
120
|
+
whereString: `WHERE ${tableFields.ROLE_NAME}=?`,
|
|
121
|
+
whereStringValues: [config.anonRole],
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return role ? role.id : null;
|
|
125
|
+
},
|
|
126
|
+
|
|
112
127
|
// Shared by the REST `hasAccess` middleware and the WebSocket connection
|
|
113
128
|
// handler so the two auth surfaces can't drift apart.
|
|
129
|
+
// A null payload is an anonymous request. The anon role also applies to
|
|
130
|
+
// authenticated users, but never to system (`_`-prefixed) tables.
|
|
114
131
|
hasTablePermission({ payload, tableName, verb }) {
|
|
115
|
-
if (toBoolean(payload.isSuperuser)) {
|
|
132
|
+
if (payload && toBoolean(payload.isSuperuser)) {
|
|
116
133
|
return true;
|
|
117
134
|
}
|
|
118
135
|
|
|
@@ -120,9 +137,19 @@ module.exports = (db) => {
|
|
|
120
137
|
return false;
|
|
121
138
|
}
|
|
122
139
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
140
|
+
const roleIds = [...(payload?.roleIds || [])];
|
|
141
|
+
if (!tableName.startsWith('_')) {
|
|
142
|
+
const anonRoleId = this.getAnonRoleId();
|
|
143
|
+
if (anonRoleId !== null) {
|
|
144
|
+
roleIds.push(anonRoleId);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (roleIds.length === 0) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const rolePermissions = this.getPermissionByRoleIds({ roleIds });
|
|
126
153
|
const resourcePermission = rolePermissions.filter(
|
|
127
154
|
(row) => row.table_name === tableName,
|
|
128
155
|
);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const Database = require('better-sqlite3');
|
|
2
2
|
|
|
3
3
|
const buildAuthService = require('./authService');
|
|
4
|
+
const config = require('../config');
|
|
4
5
|
|
|
5
6
|
// hasTablePermission is exercised in the rest of the suite only via
|
|
6
7
|
// superuser tokens (which short-circuit at the very first check) or via
|
|
@@ -113,3 +114,78 @@ describe('authService.hasTablePermission', () => {
|
|
|
113
114
|
).toBe(true);
|
|
114
115
|
});
|
|
115
116
|
});
|
|
117
|
+
|
|
118
|
+
describe('authService anonymous role', () => {
|
|
119
|
+
let db;
|
|
120
|
+
let authService;
|
|
121
|
+
|
|
122
|
+
beforeEach(() => {
|
|
123
|
+
db = new Database(':memory:');
|
|
124
|
+
db.exec(`
|
|
125
|
+
CREATE TABLE _roles (id INTEGER PRIMARY KEY, name TEXT);
|
|
126
|
+
CREATE TABLE _roles_permissions (
|
|
127
|
+
id INTEGER PRIMARY KEY,
|
|
128
|
+
role_id INTEGER,
|
|
129
|
+
table_name TEXT,
|
|
130
|
+
"create" BOOLEAN DEFAULT 0,
|
|
131
|
+
read BOOLEAN DEFAULT 0,
|
|
132
|
+
"update" BOOLEAN DEFAULT 0,
|
|
133
|
+
"delete" BOOLEAN DEFAULT 0
|
|
134
|
+
);
|
|
135
|
+
INSERT INTO _roles (id, name) VALUES (1, 'public'), (2, 'editor');
|
|
136
|
+
INSERT INTO _roles_permissions (role_id, table_name, read) VALUES (1, 'items', 1);
|
|
137
|
+
INSERT INTO _roles_permissions (role_id, table_name, read) VALUES (1, '_users', 1);
|
|
138
|
+
INSERT INTO _roles_permissions (role_id, table_name, "update") VALUES (2, 'items', 1);
|
|
139
|
+
`);
|
|
140
|
+
authService = buildAuthService(db);
|
|
141
|
+
config.anonRole = 'public';
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
afterEach(() => {
|
|
145
|
+
config.anonRole = null;
|
|
146
|
+
db.close();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const check = (payload, tableName, verb) =>
|
|
150
|
+
authService.hasTablePermission({ payload, tableName, verb });
|
|
151
|
+
|
|
152
|
+
it('getAnonRoleId returns the id of the configured role', () => {
|
|
153
|
+
expect(authService.getAnonRoleId()).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('getAnonRoleId returns null when unset or when the role does not exist', () => {
|
|
157
|
+
config.anonRole = null;
|
|
158
|
+
expect(authService.getAnonRoleId()).toBeNull();
|
|
159
|
+
|
|
160
|
+
config.anonRole = 'missing';
|
|
161
|
+
expect(authService.getAnonRoleId()).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('allows an anonymous request only for what the anon role grants', () => {
|
|
165
|
+
expect(check(null, 'items', 'GET')).toBe(true);
|
|
166
|
+
expect(check(null, 'items', 'POST')).toBe(false);
|
|
167
|
+
expect(check(null, 'other', 'GET')).toBe(false);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('denies an anonymous request when no anon role is configured', () => {
|
|
171
|
+
config.anonRole = null;
|
|
172
|
+
expect(check(null, 'items', 'GET')).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('denies an anonymous request when the configured role does not exist', () => {
|
|
176
|
+
config.anonRole = 'missing';
|
|
177
|
+
expect(check(null, 'items', 'GET')).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('never applies the anon role to system tables', () => {
|
|
181
|
+
expect(check(null, '_users', 'GET')).toBe(false);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('lets an authenticated user fall back to the anon role permissions', () => {
|
|
185
|
+
const payload = { isSuperuser: false, roleIds: [2] };
|
|
186
|
+
|
|
187
|
+
expect(check(payload, 'items', 'PUT')).toBe(true);
|
|
188
|
+
expect(check(payload, 'items', 'GET')).toBe(true);
|
|
189
|
+
expect(check(payload, 'items', 'DELETE')).toBe(false);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
@@ -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/index.js
CHANGED
|
@@ -42,20 +42,6 @@ const doc = {
|
|
|
42
42
|
name: 'users',
|
|
43
43
|
},
|
|
44
44
|
Row: {},
|
|
45
|
-
Query: {
|
|
46
|
-
query: 'SELECT * FROM users',
|
|
47
|
-
},
|
|
48
|
-
Statement: {
|
|
49
|
-
statement:
|
|
50
|
-
'INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)',
|
|
51
|
-
values: { id: 1, firstName: 'John', lastName: 'Doe' },
|
|
52
|
-
},
|
|
53
|
-
Transaction: {
|
|
54
|
-
transaction: [
|
|
55
|
-
{ $ref: '#/definitions/Query' },
|
|
56
|
-
{ $ref: '#/definitions/Statement' },
|
|
57
|
-
],
|
|
58
|
-
},
|
|
59
45
|
ForeignKey: {
|
|
60
46
|
table: 'users',
|
|
61
47
|
column: 'id',
|
|
@@ -123,8 +109,20 @@ const doc = {
|
|
|
123
109
|
pks: [1, 2, 3],
|
|
124
110
|
},
|
|
125
111
|
|
|
126
|
-
|
|
127
|
-
|
|
112
|
+
BatchRequestBody: {
|
|
113
|
+
operations: [
|
|
114
|
+
{
|
|
115
|
+
method: 'POST',
|
|
116
|
+
path: '/api/tables/users/rows',
|
|
117
|
+
body: { fields: { firstName: 'John', lastName: 'Doe' } },
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
method: 'PUT',
|
|
121
|
+
path: '/api/tables/users/rows/1',
|
|
122
|
+
body: { fields: { lastName: 'Smith' } },
|
|
123
|
+
},
|
|
124
|
+
{ method: 'DELETE', path: '/api/tables/users/rows/2' },
|
|
125
|
+
],
|
|
128
126
|
},
|
|
129
127
|
ObtainAccessTokenRequestBody: {
|
|
130
128
|
fields: {
|