soul-cli 0.6.1 → 0.7.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 (64) hide show
  1. package/.all-contributorsrc +73 -0
  2. package/.editorconfig +26 -0
  3. package/.env.sample +9 -0
  4. package/.eslintignore +2 -0
  5. package/.eslintrc.js +24 -0
  6. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  7. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  8. package/.github/workflows/npm-publish.yml +23 -0
  9. package/.husky/pre-commit +5 -0
  10. package/CONTRIBUTING.md +92 -0
  11. package/LICENSE +21 -0
  12. package/README.md +186 -1
  13. package/docs/api/root-examples.md +41 -0
  14. package/docs/api/rows-examples.md +225 -0
  15. package/docs/api/tables-examples.md +217 -0
  16. package/docs/api-examples.md +62 -0
  17. package/docs/extensions-examples.md +167 -0
  18. package/docs/logo.png +0 -0
  19. package/docs/soul-studio.png +0 -0
  20. package/docs/ws/rows-examples.md +122 -0
  21. package/docs/ws-examples.md +42 -0
  22. package/package.json +17 -2
  23. package/src/cli.js +59 -1
  24. package/src/commands.js +22 -0
  25. package/src/config/index.js +48 -3
  26. package/src/constants/api.js +26 -0
  27. package/src/constants/auth.js +5 -0
  28. package/src/constants/index.js +13 -0
  29. package/src/constants/messages.js +41 -0
  30. package/src/constants/roles.js +3 -0
  31. package/src/constants/tables.js +47 -0
  32. package/src/controllers/auth/common.js +16 -0
  33. package/src/controllers/auth/index.js +5 -0
  34. package/src/controllers/auth/tables.js +96 -0
  35. package/src/controllers/auth/token.js +243 -0
  36. package/src/controllers/auth/user.js +394 -0
  37. package/src/controllers/auth.test.js +372 -0
  38. package/src/controllers/rows.js +48 -55
  39. package/src/controllers/rows.test.js +146 -40
  40. package/src/controllers/tables.test.js +58 -28
  41. package/src/db/schema.js +118 -0
  42. package/src/index.js +32 -7
  43. package/src/middlewares/api.js +79 -0
  44. package/src/middlewares/auth.js +85 -0
  45. package/src/middlewares/broadcast.js +1 -1
  46. package/src/middlewares/validation.js +3 -2
  47. package/src/routes/auth.js +29 -0
  48. package/src/routes/rows.js +17 -5
  49. package/src/routes/tables.js +26 -4
  50. package/src/schemas/auth.js +67 -0
  51. package/src/schemas/index.js +6 -1
  52. package/src/schemas/rows.js +20 -0
  53. package/src/schemas/tables.js +18 -2
  54. package/src/server.js +3 -2
  55. package/src/services/authService.js +78 -0
  56. package/src/services/index.js +2 -1
  57. package/src/services/rowService.js +36 -7
  58. package/src/services/tableService.js +127 -1
  59. package/src/swagger/index.js +67 -0
  60. package/src/swagger/swagger.json +292 -60
  61. package/src/tests/index.js +4 -5
  62. package/src/tests/testData.js +23 -10
  63. package/src/utils/index.js +78 -0
  64. /package/{src/services/.gitkeep → docs/extensions/api-examples.js} +0 -0
@@ -0,0 +1,372 @@
1
+ const supertest = require('supertest');
2
+
3
+ const app = require('../index');
4
+ const config = require('../config');
5
+ const { generateToken } = require('../utils');
6
+ const { testData } = require('../tests/testData');
7
+
8
+ const requestWithSupertest = supertest(app);
9
+
10
+ describe('Auth Endpoints', () => {
11
+ describe('User Endpoints', () => {
12
+ it('POST /tables/_users/rows should register a user', async () => {
13
+ const accessToken = await generateToken(
14
+ { username: 'John', userId: 1, isSuperuser: true },
15
+ config.tokenSecret,
16
+ '1H',
17
+ );
18
+
19
+ const res = await requestWithSupertest
20
+ .post('/api/tables/_users/rows')
21
+ .set('Cookie', [`accessToken=${accessToken}`])
22
+ .send({
23
+ fields: {
24
+ username: testData.users.user1.username,
25
+ password: testData.strongPassword,
26
+ },
27
+ });
28
+
29
+ expect(res.status).toEqual(201);
30
+ expect(res.type).toEqual(expect.stringContaining('json'));
31
+
32
+ expect(res.body).toHaveProperty('message');
33
+ expect(res.body.message).toBe('Row Inserted');
34
+
35
+ expect(res.body).not.toHaveProperty('password');
36
+ expect(res.body).not.toHaveProperty('hashed_password');
37
+ expect(res.body).not.toHaveProperty('salt');
38
+ });
39
+
40
+ it('POST /tables/_users/rows should throw 400 error if username is not passed', async () => {
41
+ const accessToken = await generateToken(
42
+ { username: 'John', isSuperuser: true },
43
+ config.tokenSecret,
44
+ '1H',
45
+ );
46
+
47
+ const res = await requestWithSupertest
48
+ .post('/api/tables/_users/rows')
49
+ .set('Cookie', [`accessToken=${accessToken}`])
50
+ .send({
51
+ fields: { password: testData.strongPassword },
52
+ });
53
+
54
+ expect(res.status).toEqual(400);
55
+ expect(res.body.message).toBe('username is required');
56
+
57
+ expect(res.body).not.toHaveProperty('password');
58
+ expect(res.body).not.toHaveProperty('hashed_password');
59
+ expect(res.body).not.toHaveProperty('salt');
60
+ });
61
+
62
+ it('POST /tables/_users/rows should throw 400 error if the password is not strong', async () => {
63
+ const accessToken = await generateToken(
64
+ { username: 'John', isSuperuser: true },
65
+ config.tokenSecret,
66
+ '1H',
67
+ );
68
+
69
+ const res = await requestWithSupertest
70
+ .post('/api/tables/_users/rows')
71
+ .set('Cookie', [`accessToken=${accessToken}`])
72
+ .send({
73
+ fields: {
74
+ username: testData.users.user2.username,
75
+ password: testData.weakPassword,
76
+ },
77
+ });
78
+
79
+ expect(res.status).toEqual(400);
80
+ expect(res.body.message).toBe(
81
+ 'This password is weak, please use another password',
82
+ );
83
+
84
+ expect(res.body).not.toHaveProperty('password');
85
+ expect(res.body).not.toHaveProperty('hashed_password');
86
+ expect(res.body).not.toHaveProperty('salt');
87
+ });
88
+
89
+ it('POST /tables/_users/rows should throw 409 error if the username is taken', async () => {
90
+ const accessToken = await generateToken(
91
+ { username: 'John', isSuperuser: true },
92
+ config.tokenSecret,
93
+ '1H',
94
+ );
95
+
96
+ const res = await requestWithSupertest
97
+ .post('/api/tables/_users/rows')
98
+ .set('Cookie', [`accessToken=${accessToken}`])
99
+ .send({
100
+ fields: {
101
+ username: testData.users.user1.username,
102
+ password: testData.strongPassword,
103
+ },
104
+ });
105
+
106
+ expect(res.status).toEqual(409);
107
+ expect(res.body.message).toBe('This username is taken');
108
+
109
+ expect(res.body).not.toHaveProperty('password');
110
+ expect(res.body).not.toHaveProperty('hashed_password');
111
+ expect(res.body).not.toHaveProperty('salt');
112
+ });
113
+
114
+ it('GET /tables/_users/rows should return list of users', async () => {
115
+ const accessToken = await generateToken(
116
+ { username: 'John', isSuperuser: true },
117
+ config.tokenSecret,
118
+ '1H',
119
+ );
120
+
121
+ const res = await requestWithSupertest
122
+ .get('/api/tables/_users/rows')
123
+ .set('Cookie', [`accessToken=${accessToken}`]);
124
+
125
+ expect(res.status).toEqual(200);
126
+ expect(res.body.data[0]).toHaveProperty('id');
127
+ expect(res.body.data[0]).toHaveProperty('username');
128
+ expect(res.body.data[0]).toHaveProperty('is_superuser');
129
+ expect(res.body.data[0]).toHaveProperty('createdAt');
130
+
131
+ expect(res.body.data[0]).not.toHaveProperty('password');
132
+ expect(res.body.data[0]).not.toHaveProperty('hashed_password');
133
+ expect(res.body.data[0]).not.toHaveProperty('salt');
134
+ });
135
+
136
+ it('GET /tables/_users/rows/:id should retrive a single user', async () => {
137
+ const accessToken = await generateToken(
138
+ { username: 'John', isSuperuser: true },
139
+ config.tokenSecret,
140
+ '1H',
141
+ );
142
+
143
+ const res = await requestWithSupertest
144
+ .get('/api/tables/_users/rows/1')
145
+ .set('Cookie', [`accessToken=${accessToken}`]);
146
+
147
+ expect(res.status).toEqual(200);
148
+ expect(res.body.data[0]).toHaveProperty('id');
149
+ expect(res.body.data[0]).toHaveProperty('username');
150
+ expect(res.body.data[0]).toHaveProperty('is_superuser');
151
+ expect(res.body.data[0]).toHaveProperty('createdAt');
152
+
153
+ expect(res.body.data[0]).not.toHaveProperty('password');
154
+ expect(res.body.data[0]).not.toHaveProperty('hashed_password');
155
+ expect(res.body.data[0]).not.toHaveProperty('salt');
156
+ });
157
+
158
+ it('PUT /tables/_users/rows/:id should update a user', async () => {
159
+ const accessToken = await generateToken(
160
+ { username: 'John', isSuperuser: true },
161
+ config.tokenSecret,
162
+ '1H',
163
+ );
164
+
165
+ const res = await requestWithSupertest
166
+ .put('/api/tables/_users/rows/1')
167
+ .set('Cookie', [`accessToken=${accessToken}`])
168
+ .send({
169
+ fields: {
170
+ username: testData.users.user3.username,
171
+ },
172
+ });
173
+
174
+ expect(res.status).toEqual(200);
175
+ expect(res.type).toEqual(expect.stringContaining('json'));
176
+
177
+ expect(res.body).toHaveProperty('message');
178
+ expect(res.body.message).toBe('Row updated');
179
+
180
+ expect(res.body).not.toHaveProperty('password');
181
+ expect(res.body).not.toHaveProperty('hashed_password');
182
+ expect(res.body).not.toHaveProperty('salt');
183
+ });
184
+
185
+ it('DELETE /tables/_users/rows/:id should remove a user', async () => {
186
+ const accessToken = await generateToken(
187
+ { username: 'John', isSuperuser: true },
188
+ config.tokenSecret,
189
+ '1H',
190
+ );
191
+
192
+ const res = await requestWithSupertest
193
+ .delete('/api/tables/_users/rows/2')
194
+ .set('Cookie', [`accessToken=${accessToken}`]);
195
+
196
+ expect(res.status).toEqual(400);
197
+ expect(res.body.message).toBe('FOREIGN KEY constraint failed');
198
+
199
+ expect(res.body).not.toHaveProperty('password');
200
+ expect(res.body).not.toHaveProperty('hashed_password');
201
+ expect(res.body).not.toHaveProperty('salt');
202
+ });
203
+ });
204
+
205
+ describe('Obtain Access Token Endpoint', () => {
206
+ it('POST /auth/token/obtain should return an access token and refresh token values and a success message', async () => {
207
+ const res = await requestWithSupertest
208
+ .post('/api/auth/token/obtain')
209
+ .send({
210
+ fields: {
211
+ username: testData.users.user1.username,
212
+ password: testData.strongPassword,
213
+ },
214
+ });
215
+
216
+ expect(res.status).toEqual(201);
217
+ expect(res.type).toEqual(expect.stringContaining('json'));
218
+ expect(res.body).toHaveProperty('message');
219
+ expect(res.body.message).toBe('Success');
220
+
221
+ expect(res.headers['set-cookie']).toBeDefined();
222
+ expect(res.headers['set-cookie']).toEqual(
223
+ expect.arrayContaining([
224
+ expect.stringContaining('refreshToken='),
225
+ expect.stringContaining('accessToken='),
226
+ ]),
227
+ );
228
+
229
+ expect(res.body).not.toHaveProperty('password');
230
+ expect(res.body).not.toHaveProperty('hashed_password');
231
+ expect(res.body).not.toHaveProperty('salt');
232
+ });
233
+
234
+ it('POST /auth/token/obtain should throw a 401 error if the username does not exist in the DB', async () => {
235
+ const res = await requestWithSupertest
236
+ .post('/api/auth/token/obtain')
237
+ .send({
238
+ fields: {
239
+ username: testData.invalidUsername,
240
+ password: testData.strongPassword,
241
+ },
242
+ });
243
+
244
+ expect(res.status).toEqual(401);
245
+ expect(res.type).toEqual(expect.stringContaining('json'));
246
+ expect(res.body).toHaveProperty('message');
247
+ expect(res.body.message).toBe('Invalid username or password');
248
+
249
+ expect(res.body).not.toHaveProperty('password');
250
+ expect(res.body).not.toHaveProperty('hashed_password');
251
+ expect(res.body).not.toHaveProperty('salt');
252
+ });
253
+
254
+ it('POST /auth/token/obtain should throw a 401 error if the password is invalid', async () => {
255
+ const res = await requestWithSupertest
256
+ .post('/api/auth/token/obtain')
257
+ .send({
258
+ fields: {
259
+ username: testData.users.user1.username,
260
+ password: testData.invalidPassword,
261
+ },
262
+ });
263
+
264
+ expect(res.status).toEqual(401);
265
+ expect(res.type).toEqual(expect.stringContaining('json'));
266
+ expect(res.body).toHaveProperty('message');
267
+ expect(res.body.message).toBe('Invalid username or password');
268
+
269
+ expect(res.body).not.toHaveProperty('password');
270
+ expect(res.body).not.toHaveProperty('hashed_password');
271
+ expect(res.body).not.toHaveProperty('salt');
272
+ });
273
+ });
274
+
275
+ describe('Refresh Access Token Endpoint', () => {
276
+ it('GET /auth/token/refresh should refresh the access and refresh tokens', async () => {
277
+ const refreshToken = await generateToken(
278
+ { username: 'John', userId: 1, isSuperuser: true },
279
+ config.tokenSecret,
280
+ '1H',
281
+ );
282
+
283
+ const res = await requestWithSupertest
284
+ .get('/api/auth/token/refresh')
285
+ .set('Cookie', [`refreshToken=${refreshToken}`]);
286
+
287
+ expect(res.status).toEqual(200);
288
+ expect(res.type).toEqual(expect.stringContaining('json'));
289
+ expect(res.body).toHaveProperty('message');
290
+ expect(res.body.message).toBe('Success');
291
+
292
+ expect(res.headers['set-cookie']).toBeDefined();
293
+ expect(res.headers['set-cookie']).toEqual(
294
+ expect.arrayContaining([
295
+ expect.stringContaining('refreshToken='),
296
+ expect.stringContaining('accessToken='),
297
+ ]),
298
+ );
299
+ });
300
+ });
301
+
302
+ describe('Change Password Endpoint', () => {
303
+ it('PUT /auth/change-password/ should change a password', async () => {
304
+ const accessToken = await generateToken(
305
+ { username: 'John', userId: 2, isSuperuser: true },
306
+ config.tokenSecret,
307
+ '1H',
308
+ );
309
+
310
+ const res = await requestWithSupertest
311
+ .put('/api/auth/change-password')
312
+ .set('Cookie', [`accessToken=${accessToken}`])
313
+ .send({
314
+ fields: {
315
+ currentPassword: testData.strongPassword,
316
+ newPassword: testData.strongPassword2,
317
+ },
318
+ });
319
+
320
+ expect(res.status).toEqual(200);
321
+ expect(res.type).toEqual(expect.stringContaining('json'));
322
+ expect(res.body).toHaveProperty('message');
323
+ expect(res.body.message).toBe('Password updated successfully');
324
+
325
+ expect(res.body).not.toHaveProperty('password');
326
+ expect(res.body).not.toHaveProperty('hashed_password');
327
+ expect(res.body).not.toHaveProperty('salt');
328
+
329
+ // check if the password is really updated
330
+ const res2 = await requestWithSupertest
331
+ .post('/api/auth/token/obtain')
332
+ .send({
333
+ fields: {
334
+ username: testData.users.user1.username,
335
+ password: testData.strongPassword2,
336
+ },
337
+ });
338
+
339
+ expect(res2.status).toEqual(201);
340
+ expect(res2.type).toEqual(expect.stringContaining('json'));
341
+ expect(res2.body).toHaveProperty('message');
342
+ expect(res2.body.message).toBe('Success');
343
+ });
344
+
345
+ it('PUT /auth/change-password/ should throw 401 error if the current password is not valid', async () => {
346
+ const accessToken = await generateToken(
347
+ { username: 'John', userId: 2, isSuperuser: true },
348
+ config.tokenSecret,
349
+ '1H',
350
+ );
351
+
352
+ const res = await requestWithSupertest
353
+ .put('/api/auth/change-password')
354
+ .set('Cookie', [`accessToken=${accessToken}`])
355
+ .send({
356
+ fields: {
357
+ currentPassword: testData.invalidPassword,
358
+ newPassword: testData.strongPassword2,
359
+ },
360
+ });
361
+
362
+ expect(res.status).toEqual(401);
363
+ expect(res.type).toEqual(expect.stringContaining('json'));
364
+ expect(res.body).toHaveProperty('message');
365
+ expect(res.body.message).toBe('Invalid current password');
366
+
367
+ expect(res.body).not.toHaveProperty('password');
368
+ expect(res.body).not.toHaveProperty('hashed_password');
369
+ expect(res.body).not.toHaveProperty('salt');
370
+ });
371
+ });
372
+ });
@@ -1,12 +1,6 @@
1
1
  const db = require('../db/index');
2
2
  const { rowService } = require('../services');
3
3
 
4
- const quotePrimaryKeys = (pks) => {
5
- const primaryKeys = pks.split(',');
6
- const quotedPks = primaryKeys.map((id) => `'${id}'`).join(',');
7
- return quotedPks;
8
- };
9
-
10
4
  const operators = {
11
5
  eq: '=',
12
6
  lt: '<',
@@ -15,11 +9,11 @@ const operators = {
15
9
  gte: '>=',
16
10
  neq: '!=',
17
11
  null: 'IS NULL',
18
- notnull: 'IS NOT NULL'
12
+ notnull: 'IS NOT NULL',
19
13
  };
20
14
 
21
15
  // Return paginated rows of a table
22
- const listTableRows = async (req, res) => {
16
+ const listTableRows = async (req, res, next) => {
23
17
  /*
24
18
  #swagger.tags = ['Rows']
25
19
  #swagger.summary = 'List Rows'
@@ -66,7 +60,7 @@ const listTableRows = async (req, res) => {
66
60
  _ordering,
67
61
  _schema,
68
62
  _extend,
69
- _filters = ''
63
+ _filters = '',
70
64
  } = req.query;
71
65
 
72
66
  const page = parseInt(_page);
@@ -80,7 +74,7 @@ const listTableRows = async (req, res) => {
80
74
  let filters = [];
81
75
 
82
76
  // split the filters by comma(,) except when in an array
83
- const re = /,(?![^\[]*?\])/;
77
+ const re = /,(?![^[]*?\])/;
84
78
  try {
85
79
  filters = _filters.split(re).map((filter) => {
86
80
  //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.
@@ -98,7 +92,7 @@ const listTableRows = async (req, res) => {
98
92
  fieldOperator = 'eq';
99
93
  } else if (!operators[fieldOperator]) {
100
94
  throw new Error(
101
- `Invalid field operator '${fieldOperator}' for field '${field}'. You can only use the following operators after the '${field}' field: __lt, __gt, __lte, __gte, __eq, __neq.`
95
+ `Invalid field operator '${fieldOperator}' for field '${field}'. You can only use the following operators after the '${field}' field: __lt, __gt, __lte, __gte, __eq, __neq.`,
102
96
  );
103
97
  }
104
98
 
@@ -112,7 +106,7 @@ const listTableRows = async (req, res) => {
112
106
  } catch (error) {
113
107
  return res.status(400).json({
114
108
  message: error.message,
115
- error: error
109
+ error: error,
116
110
  });
117
111
  }
118
112
 
@@ -161,7 +155,7 @@ const listTableRows = async (req, res) => {
161
155
  } catch (error) {
162
156
  return res.status(400).json({
163
157
  message: error.message,
164
- error: error
158
+ error: error,
165
159
  });
166
160
  }
167
161
  }
@@ -236,7 +230,7 @@ const listTableRows = async (req, res) => {
236
230
 
237
231
  if (!foreignKey) {
238
232
  throw new Error(
239
- `Foreign key not found for extended field '${extendedField}'`
233
+ `Foreign key not found for extended field '${extendedField}'`,
240
234
  );
241
235
  }
242
236
 
@@ -254,7 +248,7 @@ const listTableRows = async (req, res) => {
254
248
  joinedTableFields
255
249
  .map(
256
250
  (joinedTableField) =>
257
- `'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`
251
+ `'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`,
258
252
  )
259
253
  .join(', ') +
260
254
  ' ) as ' +
@@ -277,7 +271,7 @@ const listTableRows = async (req, res) => {
277
271
  if (foreignKeyError.error) {
278
272
  return res.status(400).json({
279
273
  message: foreignKeyError.message,
280
- error: foreignKeyError.error
274
+ error: foreignKeyError.error,
281
275
  });
282
276
  }
283
277
  }
@@ -291,7 +285,7 @@ const listTableRows = async (req, res) => {
291
285
  orderString,
292
286
  limit,
293
287
  page: limit * (page - 1),
294
- whereStringValues
288
+ whereStringValues,
295
289
  });
296
290
 
297
291
  // parse json extended files
@@ -311,10 +305,10 @@ const listTableRows = async (req, res) => {
311
305
  const total = rowService.getCount({
312
306
  tableName,
313
307
  whereString,
314
- whereStringValues
308
+ whereStringValues,
315
309
  });
316
310
 
317
- const next =
311
+ const nextPage =
318
312
  data.length === limit
319
313
  ? `/tables/${tableName}/rows?${params}_limit=${_limit}&_page=${
320
314
  page + 1
@@ -327,16 +321,15 @@ const listTableRows = async (req, res) => {
327
321
  }`
328
322
  : null;
329
323
 
330
- res.json({
331
- data,
332
- total,
333
- next,
334
- previous
335
- });
324
+ req.response = {
325
+ status: 200,
326
+ payload: { data, total, next: nextPage, previous },
327
+ };
328
+ next();
336
329
  } catch (error) {
337
330
  res.status(400).json({
338
331
  message: error.message,
339
- error: error
332
+ error: error,
340
333
  });
341
334
  }
342
335
  };
@@ -367,7 +360,7 @@ const insertRowInTable = async (req, res, next) => {
367
360
 
368
361
  // Remove null values from fields for accurate query construction.
369
362
  const fields = Object.fromEntries(
370
- Object.entries(queryFields).filter(([_, value]) => value !== null)
363
+ Object.entries(queryFields).filter(([, value]) => value !== null),
371
364
  );
372
365
 
373
366
  try {
@@ -383,14 +376,15 @@ const insertRowInTable = async (req, res, next) => {
383
376
  */
384
377
  res.status(201).json({
385
378
  message: 'Row inserted',
386
- data
379
+ data,
387
380
  });
381
+
388
382
  req.broadcast = {
389
383
  type: 'INSERT',
390
384
  data: {
391
385
  pk: data.lastInsertRowid,
392
- ...fields
393
- }
386
+ ...fields,
387
+ },
394
388
  };
395
389
  next();
396
390
  } catch (error) {
@@ -404,13 +398,13 @@ const insertRowInTable = async (req, res, next) => {
404
398
  */
405
399
  res.status(400).json({
406
400
  message: error.message,
407
- error: error
401
+ error: error,
408
402
  });
409
403
  }
410
404
  };
411
405
 
412
406
  // Get a row by pk
413
- const getRowInTableByPK = async (req, res) => {
407
+ const getRowInTableByPK = async (req, res, next) => {
414
408
  /*
415
409
  #swagger.tags = ['Rows']
416
410
  #swagger.summary = 'Retrieve Row'
@@ -465,7 +459,7 @@ const getRowInTableByPK = async (req, res) => {
465
459
  } catch (error) {
466
460
  return res.status(400).json({
467
461
  message: error.message,
468
- error: error
462
+ error: error,
469
463
  });
470
464
  }
471
465
  }
@@ -542,7 +536,7 @@ const getRowInTableByPK = async (req, res) => {
542
536
  joinedTableFields
543
537
  .map(
544
538
  (joinedTableField) =>
545
- `'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`
539
+ `'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`,
546
540
  )
547
541
  .join(', ') +
548
542
  ' ) as ' +
@@ -557,7 +551,7 @@ const getRowInTableByPK = async (req, res) => {
557
551
  } catch (error) {
558
552
  return res.status(400).json({
559
553
  message: error.message,
560
- error: error
554
+ error: error,
561
555
  });
562
556
  }
563
557
  });
@@ -569,7 +563,7 @@ const getRowInTableByPK = async (req, res) => {
569
563
  tableName,
570
564
  extendString,
571
565
  lookupField,
572
- pks
566
+ pks,
573
567
  });
574
568
 
575
569
  // parse json extended files
@@ -588,17 +582,16 @@ const getRowInTableByPK = async (req, res) => {
588
582
  if (data.length === 0) {
589
583
  return res.status(404).json({
590
584
  message: 'Row not found',
591
- error: 'not_found'
585
+ error: 'not_found',
592
586
  });
593
587
  } else {
594
- res.json({
595
- data
596
- });
588
+ req.response = { status: 200, payload: { data } };
589
+ next();
597
590
  }
598
591
  } catch (error) {
599
592
  return res.status(400).json({
600
593
  message: error.message,
601
- error: error
594
+ error: error,
602
595
  });
603
596
  }
604
597
  };
@@ -652,7 +645,7 @@ const updateRowInTableByPK = async (req, res, next) => {
652
645
  } catch (error) {
653
646
  return res.status(400).json({
654
647
  message: error.message,
655
- error: error
648
+ error: error,
656
649
  });
657
650
  }
658
651
  }
@@ -671,7 +664,7 @@ const updateRowInTableByPK = async (req, res, next) => {
671
664
  if (fieldsString === '') {
672
665
  return res.status(400).json({
673
666
  message: 'No fields provided',
674
- error: 'no_fields_provided'
667
+ error: 'no_fields_provided',
675
668
  });
676
669
  }
677
670
 
@@ -680,26 +673,26 @@ const updateRowInTableByPK = async (req, res, next) => {
680
673
  tableName,
681
674
  fieldsString,
682
675
  lookupField,
683
- pks
676
+ pks,
684
677
  });
685
678
 
686
679
  res.json({
687
680
  message: 'Row updated',
688
- data
681
+ data,
689
682
  });
690
683
  req.broadcast = {
691
684
  type: 'UPDATE',
692
685
  _lookup_field: lookupField,
693
686
  data: {
694
687
  pks: pks.split(','),
695
- ...fields
696
- }
688
+ ...fields,
689
+ },
697
690
  };
698
691
  next();
699
692
  } catch (error) {
700
693
  res.status(400).json({
701
694
  message: error.message,
702
- error: error
695
+ error: error,
703
696
  });
704
697
  }
705
698
  };
@@ -744,7 +737,7 @@ const deleteRowInTableByPK = async (req, res, next) => {
744
737
  } catch (error) {
745
738
  return res.status(400).json({
746
739
  message: error.message,
747
- error: error
740
+ error: error,
748
741
  });
749
742
  }
750
743
  }
@@ -754,26 +747,26 @@ const deleteRowInTableByPK = async (req, res, next) => {
754
747
 
755
748
  if (data.changes === 0) {
756
749
  res.status(404).json({
757
- error: 'not_found'
750
+ error: 'not_found',
758
751
  });
759
752
  } else {
760
753
  res.json({
761
754
  message: 'Row deleted',
762
- data
755
+ data,
763
756
  });
764
757
  req.broadcast = {
765
758
  type: 'DELETE',
766
759
  _lookup_field: lookupField,
767
760
  data: {
768
- pks: pks.split(',')
769
- }
761
+ pks: pks.split(','),
762
+ },
770
763
  };
771
764
  next();
772
765
  }
773
766
  } catch (error) {
774
767
  res.status(400).json({
775
768
  message: error.message,
776
- error: error
769
+ error: error,
777
770
  });
778
771
  }
779
772
  };
@@ -783,5 +776,5 @@ module.exports = {
783
776
  insertRowInTable,
784
777
  getRowInTableByPK,
785
778
  updateRowInTableByPK,
786
- deleteRowInTableByPK
779
+ deleteRowInTableByPK,
787
780
  };