soul-cli 0.6.0 → 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 +30 -31
  39. package/src/controllers/rows.test.js +209 -18
  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 +72 -27
  63. package/src/utils/index.js +78 -0
  64. /package/{src/services/.gitkeep → docs/extensions/api-examples.js} +0 -0
@@ -1,6 +1,10 @@
1
+ const { not } = require('joi');
1
2
  const supertest = require('supertest');
2
3
 
3
4
  const app = require('../index');
5
+ const config = require('../config');
6
+ const { generateToken } = require('../utils');
7
+
4
8
  const requestWithSupertest = supertest(app);
5
9
 
6
10
  function queryString(params) {
@@ -13,7 +17,15 @@ function queryString(params) {
13
17
 
14
18
  describe('Rows Endpoints', () => {
15
19
  it('GET /tables/:name/rows should return a list of all rows', async () => {
16
- const res = await requestWithSupertest.get('/api/tables/users/rows');
20
+ const accessToken = await generateToken(
21
+ { username: 'John', isSuperuser: true },
22
+ config.tokenSecret,
23
+ '1H',
24
+ );
25
+
26
+ const res = await requestWithSupertest
27
+ .get('/api/tables/users/rows')
28
+ .set('Cookie', [`accessToken=${accessToken}`]);
17
29
 
18
30
  expect(res.status).toEqual(200);
19
31
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -25,6 +37,12 @@ describe('Rows Endpoints', () => {
25
37
  });
26
38
 
27
39
  it('GET /tables/:name/rows?_limit=8&_schema=firstName,lastName&_ordering:-firstName&_page=2: should query the rows by the provided query params', async () => {
40
+ const accessToken = await generateToken(
41
+ { username: 'John', isSuperuser: true },
42
+ config.tokenSecret,
43
+ '1H',
44
+ );
45
+
28
46
  const params = {
29
47
  _search: 'a',
30
48
  _ordering: '-firstName',
@@ -33,9 +51,9 @@ describe('Rows Endpoints', () => {
33
51
  _page: 2,
34
52
  };
35
53
  const query = queryString(params);
36
- const res = await requestWithSupertest.get(
37
- `/api/tables/users/rows?${query}`
38
- );
54
+ const res = await requestWithSupertest
55
+ .get(`/api/tables/users/rows?${query}`)
56
+ .set('Cookie', [`accessToken=${accessToken}`]);
39
57
 
40
58
  expect(res.status).toEqual(200);
41
59
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -48,38 +66,178 @@ describe('Rows Endpoints', () => {
48
66
  `/tables/users/rows?${queryString({
49
67
  ...params,
50
68
  _page: params._page + 1,
51
- }).toString()}`
69
+ }).toString()}`,
52
70
  );
53
71
 
54
72
  expect(res.body.previous).toEqual(
55
73
  `/tables/users/rows?${queryString({
56
74
  ...params,
57
75
  _page: params._page - 1,
58
- }).toString()}`
76
+ }).toString()}`,
59
77
  );
60
78
  });
61
79
 
62
80
  it('GET /tables/:name/rows: should return a null field', async () => {
63
- const res = await requestWithSupertest.get(
64
- '/api/tables/users/rows?_filters=firstName__null,lastName__notnull'
81
+ const accessToken = await generateToken(
82
+ { username: 'John', isSuperuser: true },
83
+ config.tokenSecret,
84
+ '1H',
65
85
  );
66
86
 
87
+ const res = await requestWithSupertest
88
+ .get('/api/tables/users/rows?_filters=firstName__null,lastName__notnull')
89
+ .set('Cookie', [`accessToken=${accessToken}`]);
90
+
67
91
  expect(res.status).toEqual(200);
68
92
  expect(res.body.data[0].firstName).toBeNull();
69
93
  expect(res.body.data[0].lastName).not.toBeNull();
70
94
  });
71
95
 
96
+ it('GET /tables/:name/rows: should successfully retrieve users created after 2010-01-01 00:00:00.', async () => {
97
+ const accessToken = await generateToken(
98
+ { username: 'John', isSuperuser: true },
99
+ config.tokenSecret,
100
+ '1H',
101
+ );
102
+
103
+ const date = '2010-01-01 00:00:00';
104
+ const res = await requestWithSupertest
105
+ .get(`/api/tables/users/rows?_filters=createdAt__gte:${date}`)
106
+ .set('Cookie', [`accessToken=${accessToken}`]);
107
+
108
+ res.body.data.map((user) => {
109
+ const createdAt = new Date(user.createdAt);
110
+ const referenceDate = new Date(date);
111
+ expect(createdAt.getTime()).toBeGreaterThan(referenceDate.getTime());
112
+ });
113
+
114
+ expect(res.status).toEqual(200);
115
+ expect(res.body.data[0]).toHaveProperty('id');
116
+ expect(res.body.data[0]).toHaveProperty('firstName');
117
+ expect(res.body.data[0]).toHaveProperty('lastName');
118
+ expect(res.body.data[0]).toHaveProperty('createdAt');
119
+ });
120
+
121
+ it('GET /tables/:name/rows: should successfully retrieve users created before 2008-01-20 00:00:00.', async () => {
122
+ const accessToken = await generateToken(
123
+ { username: 'John', isSuperuser: true },
124
+ config.tokenSecret,
125
+ '1H',
126
+ );
127
+
128
+ const date = '2008-01-20 00:00:00';
129
+ const res = await requestWithSupertest
130
+ .get(`/api/tables/users/rows?_filters=createdAt__lte:${date}`)
131
+ .set('Cookie', [`accessToken=${accessToken}`]);
132
+
133
+ res.body.data.map((user) => {
134
+ const createdAt = new Date(user.createdAt);
135
+ const referenceDate = new Date(date);
136
+ expect(createdAt.getTime()).toBeLessThan(referenceDate.getTime());
137
+ });
138
+
139
+ expect(res.status).toEqual(200);
140
+ expect(res.body.data[0]).toHaveProperty('id');
141
+ expect(res.body.data[0]).toHaveProperty('firstName');
142
+ expect(res.body.data[0]).toHaveProperty('lastName');
143
+ expect(res.body.data[0]).toHaveProperty('createdAt');
144
+ });
145
+
146
+ it('GET /tables/:name/rows: should successfully retrieve users created at 2013-01-08 00:00:00', async () => {
147
+ const accessToken = await generateToken(
148
+ { username: 'John', isSuperuser: true },
149
+ config.tokenSecret,
150
+ '1H',
151
+ );
152
+
153
+ const date = '2013-01-08 00:00:00';
154
+ const res = await requestWithSupertest
155
+ .get(`/api/tables/users/rows?_filters=createdAt__eq:${date}`)
156
+ .set('Cookie', [`accessToken=${accessToken}`]);
157
+
158
+ res.body.data.map((user) => {
159
+ const createdAt = new Date(user.createdAt);
160
+ const referenceDate = new Date(date);
161
+ expect(createdAt.getTime()).toEqual(referenceDate.getTime());
162
+ });
163
+
164
+ expect(res.status).toEqual(200);
165
+ expect(res.body.data[0]).toHaveProperty('id');
166
+ expect(res.body.data[0]).toHaveProperty('firstName');
167
+ expect(res.body.data[0]).toHaveProperty('lastName');
168
+ expect(res.body.data[0]).toHaveProperty('createdAt');
169
+ });
170
+
171
+ it('GET /tables/:name/rows: should successfully retrieve users created at 2007-01-08 00:00:00', async () => {
172
+ const accessToken = await generateToken(
173
+ { username: 'John', isSuperuser: true },
174
+ config.tokenSecret,
175
+ '1H',
176
+ );
177
+
178
+ const date = '2007-01-08 00:00:00';
179
+ const res = await requestWithSupertest
180
+ .get(`/api/tables/users/rows?_filters=createdAt__eq:${date}`)
181
+ .set('Cookie', [`accessToken=${accessToken}`]);
182
+
183
+ //There are no users that are created at 2007-01-08 00:00:00 so the API should return empty data
184
+ expect(res.body.data).toHaveLength(0);
185
+ expect(res.status).toEqual(200);
186
+ });
187
+
188
+ it('GET /tables/:name/rows: should successfully retrieve users that are not created at 2021-01-08 00:00:00', async () => {
189
+ const accessToken = await generateToken(
190
+ { username: 'John', isSuperuser: true },
191
+ config.tokenSecret,
192
+ '1H',
193
+ );
194
+
195
+ const date = '2021-01-08 00:00:00';
196
+ const res = await requestWithSupertest
197
+ .get(`/api/tables/users/rows?_filters=createdAt__neq:${date}`)
198
+ .set('Cookie', [`accessToken=${accessToken}`]);
199
+
200
+ res.body.data.map((user) => {
201
+ const createdAt = new Date(user.createdAt);
202
+ const referenceDate = new Date(date);
203
+ expect(createdAt.getTime()).not.toEqual(referenceDate.getTime());
204
+ });
205
+
206
+ expect(res.status).toEqual(200);
207
+ expect(res.body.data[0]).toHaveProperty('id');
208
+ expect(res.body.data[0]).toHaveProperty('firstName');
209
+ expect(res.body.data[0]).toHaveProperty('lastName');
210
+ expect(res.body.data[0]).toHaveProperty('createdAt');
211
+ });
212
+
72
213
  it('POST /tables/:name/rows should insert a new row and return the lastInsertRowid', async () => {
214
+ const accessToken = await generateToken(
215
+ { username: 'John', isSuperuser: true },
216
+ config.tokenSecret,
217
+ '1H',
218
+ );
219
+
73
220
  const res = await requestWithSupertest
74
221
  .post('/api/tables/users/rows')
222
+ .set('Cookie', [`accessToken=${accessToken}`])
75
223
  .send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
224
+
76
225
  expect(res.status).toEqual(201);
77
226
  expect(res.type).toEqual(expect.stringContaining('json'));
78
227
  expect(res.body).toHaveProperty('data');
79
228
  });
80
229
 
81
230
  it('GET /tables/:name/rows/:pks should return a row by its primary key', async () => {
82
- const res = await requestWithSupertest.get('/api/tables/users/rows/1');
231
+ const accessToken = await generateToken(
232
+ { username: 'John', isSuperuser: true },
233
+ config.tokenSecret,
234
+ '1H',
235
+ );
236
+
237
+ const res = await requestWithSupertest
238
+ .get('/api/tables/users/rows/1')
239
+ .set('Cookie', [`accessToken=${accessToken}`]);
240
+
83
241
  expect(res.status).toEqual(200);
84
242
  expect(res.type).toEqual(expect.stringContaining('json'));
85
243
  expect(res.body).toHaveProperty('data');
@@ -89,20 +247,39 @@ describe('Rows Endpoints', () => {
89
247
  });
90
248
 
91
249
  it('PUT /tables/:name/rows/:pks should update a row by its primary key and return the number of changes', async () => {
250
+ const accessToken = await generateToken(
251
+ { username: 'John', isSuperuser: true },
252
+ config.tokenSecret,
253
+ '1H',
254
+ );
92
255
  const res = await requestWithSupertest
93
256
  .put('/api/tables/users/rows/1')
257
+ .set('Cookie', [`accessToken=${accessToken}`])
94
258
  .send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
95
259
  expect(res.status).toEqual(200);
96
260
  expect(res.type).toEqual(expect.stringContaining('json'));
97
261
  });
98
262
 
99
263
  it('DELETE /tables/:name/rows/:pks should delete a row by its primary key and return the number of changes', async () => {
100
- const res = await requestWithSupertest.delete('/api/tables/users/rows/1');
264
+ const accessToken = await generateToken(
265
+ { username: 'John', isSuperuser: true },
266
+ config.tokenSecret,
267
+ '1H',
268
+ );
269
+
270
+ const res = await requestWithSupertest
271
+ .delete('/api/tables/users/rows/1')
272
+ .set('Cookie', [`accessToken=${accessToken}`]);
101
273
  expect(res.status).toEqual(200);
102
274
  expect(res.type).toEqual(expect.stringContaining('json'));
103
275
  });
104
276
 
105
277
  it('POST /tables/:name/rows should insert a new row if any of the value of the object being inserted is null', async () => {
278
+ const accessToken = await generateToken(
279
+ { username: 'John', isSuperuser: true },
280
+ config.tokenSecret,
281
+ '1H',
282
+ );
106
283
  const res = await requestWithSupertest
107
284
  .post('/api/tables/users/rows')
108
285
  .send({
@@ -110,18 +287,25 @@ describe('Rows Endpoints', () => {
110
287
  firstName: null,
111
288
  lastName: 'Doe',
112
289
  email: null,
113
- username: 'Jane'
114
- }
115
- });
290
+ username: 'Jane',
291
+ },
292
+ })
293
+ .set('Cookie', [`accessToken=${accessToken}`]);
116
294
  expect(res.status).toEqual(201);
117
295
  expect(res.type).toEqual(expect.stringContaining('json'));
118
296
  expect(res.body).toHaveProperty('data');
119
297
  });
120
298
 
121
299
  it('GET /tables/:name/rows should return values if any of the IDs from the array match the user ID.', async () => {
122
- const res = await requestWithSupertest.get(
123
- '/api/tables/users/rows?_filters=id:[2,3]'
300
+ const accessToken = await generateToken(
301
+ { username: 'John', isSuperuser: true },
302
+ config.tokenSecret,
303
+ '1H',
124
304
  );
305
+
306
+ const res = await requestWithSupertest
307
+ .get('/api/tables/users/rows?_filters=id:[2,3]')
308
+ .set('Cookie', [`accessToken=${accessToken}`]);
125
309
  expect(res.status).toEqual(200);
126
310
  expect(res.body).toHaveProperty('data');
127
311
  expect(res.body.data).toEqual(expect.any(Array));
@@ -129,13 +313,20 @@ describe('Rows Endpoints', () => {
129
313
  });
130
314
 
131
315
  it('GET /tables/:name/rows should return values if the provided ID matches the user ID.', async () => {
132
- const res = await requestWithSupertest.get(
133
- '/api/tables/users/rows?_filters=id:2,firstName:Michael,lastName:Lee'
316
+ const accessToken = await generateToken(
317
+ { username: 'John', isSuperuser: true },
318
+ config.tokenSecret,
319
+ '1H',
134
320
  );
321
+
322
+ const res = await requestWithSupertest
323
+ .get(
324
+ '/api/tables/users/rows?_filters=id:2,firstName:Michael,lastName:Lee',
325
+ )
326
+ .set('Cookie', [`accessToken=${accessToken}`]);
135
327
  expect(res.status).toEqual(200);
136
328
  expect(res.body).toHaveProperty('data');
137
329
  expect(res.body.data).toEqual(expect.any(Array));
138
330
  expect(res.body.data.length).toEqual(1);
139
331
  });
140
-
141
332
  });
@@ -1,11 +1,23 @@
1
1
  const supertest = require('supertest');
2
2
 
3
3
  const app = require('../index');
4
+ const { generateToken } = require('../utils');
5
+ const config = require('../config');
6
+
4
7
  const requestWithSupertest = supertest(app);
5
8
 
6
9
  describe('Tables Endpoints', () => {
7
10
  it('GET /tables should return a list of all tables', async () => {
8
- const res = await requestWithSupertest.get('/api/tables');
11
+ const accessToken = await generateToken(
12
+ { username: 'John', isSuperuser: true },
13
+ config.tokenSecret,
14
+ '1H',
15
+ );
16
+
17
+ const res = await requestWithSupertest
18
+ .get('/api/tables')
19
+ .set('Cookie', [`accessToken=${accessToken}`]);
20
+
9
21
  expect(res.status).toEqual(200);
10
22
  expect(res.type).toEqual(expect.stringContaining('json'));
11
23
  expect(res.body).toHaveProperty('data');
@@ -14,33 +26,42 @@ describe('Tables Endpoints', () => {
14
26
  });
15
27
 
16
28
  it('POST /tables should create a new table and return generated schema', async () => {
17
- const res = await requestWithSupertest.post('/api/tables').send({
18
- name: 'pets',
19
- autoAddCreatedAt: true,
20
- autoAddUpdatedAt: false,
21
- schema: [
22
- {
23
- name: 'owner',
24
- type: 'INTEGER',
25
- foreignKey: {
26
- table: 'users',
27
- column: 'id',
28
- onDelete: 'CASCADE',
29
- onUpdate: 'CASCADE',
29
+ const accessToken = await generateToken(
30
+ { username: 'John', isSuperuser: true },
31
+ config.tokenSecret,
32
+ '1H',
33
+ );
34
+
35
+ const res = await requestWithSupertest
36
+ .post('/api/tables')
37
+ .send({
38
+ name: 'pets',
39
+ autoAddCreatedAt: true,
40
+ autoAddUpdatedAt: false,
41
+ schema: [
42
+ {
43
+ name: 'owner',
44
+ type: 'INTEGER',
45
+ foreignKey: {
46
+ table: 'users',
47
+ column: 'id',
48
+ onDelete: 'CASCADE',
49
+ onUpdate: 'CASCADE',
50
+ },
30
51
  },
31
- },
32
- {
33
- name: 'name',
34
- type: 'TEXT',
35
- notNull: true,
36
- },
37
- {
38
- name: 'petId',
39
- unique: true,
40
- type: 'INTEGER',
41
- },
42
- ],
43
- });
52
+ {
53
+ name: 'name',
54
+ type: 'TEXT',
55
+ notNull: true,
56
+ },
57
+ {
58
+ name: 'petId',
59
+ unique: true,
60
+ type: 'INTEGER',
61
+ },
62
+ ],
63
+ })
64
+ .set('Cookie', [`accessToken=${accessToken}`]);
44
65
 
45
66
  expect(res.status).toEqual(201);
46
67
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -53,7 +74,16 @@ describe('Tables Endpoints', () => {
53
74
  });
54
75
 
55
76
  it('GET /tables/:name should return schema of the table', async () => {
56
- const res = await requestWithSupertest.get('/api/tables/users');
77
+ const accessToken = await generateToken(
78
+ { username: 'John', isSuperuser: true },
79
+ config.tokenSecret,
80
+ '1H',
81
+ );
82
+
83
+ const res = await requestWithSupertest
84
+ .get('/api/tables/users')
85
+ .set('Cookie', [`accessToken=${accessToken}`]);
86
+
57
87
  expect(res.status).toEqual(200);
58
88
  expect(res.type).toEqual(expect.stringContaining('json'));
59
89
  expect(res.body).toHaveProperty('data');
@@ -0,0 +1,118 @@
1
+ const { dbConstants } = require('../constants');
2
+
3
+ const { tableFields, ROLES_TABLE, USERS_TABLE } = dbConstants;
4
+
5
+ module.exports = {
6
+ roleSchema: [
7
+ {
8
+ name: tableFields.ROLE_NAME,
9
+ type: 'TEXT',
10
+ primaryKey: false,
11
+ notNull: true,
12
+ unique: true,
13
+ },
14
+ ],
15
+
16
+ userSchema: [
17
+ {
18
+ name: tableFields.USERNAME,
19
+ type: 'TEXT',
20
+ primaryKey: false,
21
+ notNull: true,
22
+ unique: true,
23
+ },
24
+ {
25
+ name: tableFields.HASHED_PASSWORD,
26
+ type: 'TEXT',
27
+ primaryKey: false,
28
+ notNull: true,
29
+ unique: false,
30
+ },
31
+ {
32
+ name: tableFields.SALT,
33
+ type: 'TEXT',
34
+ primaryKey: false,
35
+ notNull: true,
36
+ unique: false,
37
+ },
38
+
39
+ {
40
+ name: tableFields.IS_SUPERUSER,
41
+ type: 'BOOLEAN',
42
+ primaryKey: false,
43
+ notNull: true,
44
+ unique: false,
45
+ },
46
+ ],
47
+
48
+ rolePermissionSchema: [
49
+ {
50
+ name: tableFields.ROLE_ID,
51
+ type: 'NUMERIC',
52
+ primaryKey: false,
53
+ notNull: true,
54
+ unique: false,
55
+ foreignKey: { table: ROLES_TABLE, column: tableFields.ID },
56
+ },
57
+
58
+ {
59
+ name: tableFields.TABLE_NAME,
60
+ type: 'TEXT',
61
+ primaryKey: false,
62
+ notNull: true,
63
+ unique: false,
64
+ },
65
+
66
+ {
67
+ name: tableFields.CREATE,
68
+ type: 'BOOLEAN',
69
+ primaryKey: false,
70
+ notNull: true,
71
+ unique: false,
72
+ },
73
+
74
+ {
75
+ name: tableFields.READ,
76
+ type: 'BOOLEAN',
77
+ primaryKey: false,
78
+ notNull: true,
79
+ unique: false,
80
+ },
81
+
82
+ {
83
+ name: tableFields.UPDATE,
84
+ type: 'BOOLEAN',
85
+ primaryKey: false,
86
+ notNull: true,
87
+ unique: false,
88
+ },
89
+
90
+ {
91
+ name: tableFields.DELETE,
92
+ type: 'BOOLEAN',
93
+ primaryKey: false,
94
+ notNull: true,
95
+ unique: false,
96
+ },
97
+ ],
98
+
99
+ usersRoleSchema: [
100
+ {
101
+ name: tableFields.USER_ID,
102
+ type: 'NUMERIC',
103
+ primaryKey: false,
104
+ notNull: true,
105
+ unique: false,
106
+ foreignKey: { table: USERS_TABLE, column: tableFields.ID },
107
+ },
108
+
109
+ {
110
+ name: tableFields.ROLE_ID,
111
+ type: 'NUMERIC',
112
+ primaryKey: false,
113
+ notNull: true,
114
+ unique: false,
115
+ foreignKey: { table: ROLES_TABLE, column: tableFields.ID },
116
+ },
117
+ ],
118
+ };
package/src/index.js CHANGED
@@ -7,25 +7,35 @@ const expressWinston = require('express-winston');
7
7
  const cors = require('cors');
8
8
  const rateLimit = require('express-rate-limit');
9
9
  const swaggerUi = require('swagger-ui-express');
10
+ const cookieParser = require('cookie-parser');
10
11
 
11
12
  const config = require('./config/index');
12
13
  const db = require('./db/index');
14
+
13
15
  const rootRoutes = require('./routes/index');
14
16
  const tablesRoutes = require('./routes/tables');
15
17
  const rowsRoutes = require('./routes/rows');
18
+ const authRoutes = require('./routes/auth');
19
+
16
20
  const swaggerFile = require('./swagger/swagger.json');
17
21
  const { setupExtensions } = require('./extensions');
22
+ const {
23
+ createDefaultTables,
24
+ createInitialUser,
25
+ } = require('./controllers/auth');
18
26
 
19
- const app = express();
27
+ const { runCLICommands } = require('./commands');
20
28
 
29
+ const app = express();
21
30
  app.get('/health', (req, res) => {
22
31
  res.send('OK');
23
32
  });
24
33
 
25
34
  app.use(bodyParser.json());
35
+ app.use(cookieParser());
26
36
 
27
37
  // Activate wal mode
28
- db.pragma('journal_mode = WAL');
38
+ db.exec('PRAGMA journal_mode = WAL');
29
39
 
30
40
  // Enable CORS
31
41
  let corsOrigin = config.cors.origin;
@@ -34,9 +44,7 @@ if (corsOrigin.includes('*')) {
34
44
  corsOrigin = '*';
35
45
  }
36
46
 
37
- const corsOptions = {
38
- origin: corsOrigin,
39
- };
47
+ const corsOptions = { origin: corsOrigin };
40
48
 
41
49
  app.use(cors(corsOptions));
42
50
 
@@ -47,13 +55,15 @@ if (config.verbose !== null) {
47
55
  transports: [new winston.transports.Console()],
48
56
  format: winston.format.combine(
49
57
  winston.format.colorize(),
50
- winston.format.json()
58
+
59
+ winston.format.json(),
51
60
  ),
52
61
  meta: false,
53
62
  msg: 'HTTP {{req.method}} {{req.url}}',
54
63
  expressFormat: true,
64
+
55
65
  colorize: false,
56
- })
66
+ }),
57
67
  );
58
68
  }
59
69
 
@@ -69,11 +79,26 @@ if (config.rateLimit.enabled) {
69
79
  app.use(limiter);
70
80
  }
71
81
 
82
+ // If Auth mode is activated then create auth tables in the DB & create a super user if there are no users in the DB
83
+ if (config.auth) {
84
+ createDefaultTables();
85
+ createInitialUser();
86
+ } else {
87
+ console.warn(
88
+ 'Warning: Soul is running in open mode without authentication or authorization for API endpoints. Please be aware that your API endpoints will not be secure.',
89
+ );
90
+ }
91
+
92
+ // If the user has passed custom CLI commands run the command and exit to avoid running the server
93
+ runCLICommands();
94
+
72
95
  app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
73
96
  app.use('/api', rootRoutes);
74
97
  app.use('/api/tables', tablesRoutes);
75
98
  app.use('/api/tables', rowsRoutes);
76
99
 
100
+ app.use('/api/auth', authRoutes);
101
+
77
102
  setupExtensions(app, db);
78
103
 
79
104
  module.exports = app;