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
@@ -2,6 +2,9 @@ const { not } = require('joi');
2
2
  const supertest = require('supertest');
3
3
 
4
4
  const app = require('../index');
5
+ const config = require('../config');
6
+ const { generateToken } = require('../utils');
7
+
5
8
  const requestWithSupertest = supertest(app);
6
9
 
7
10
  function queryString(params) {
@@ -14,7 +17,15 @@ function queryString(params) {
14
17
 
15
18
  describe('Rows Endpoints', () => {
16
19
  it('GET /tables/:name/rows should return a list of all rows', async () => {
17
- 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}`]);
18
29
 
19
30
  expect(res.status).toEqual(200);
20
31
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -26,17 +37,23 @@ describe('Rows Endpoints', () => {
26
37
  });
27
38
 
28
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
+
29
46
  const params = {
30
47
  _search: 'a',
31
48
  _ordering: '-firstName',
32
49
  _schema: 'firstName,lastName',
33
50
  _limit: 8,
34
- _page: 2
51
+ _page: 2,
35
52
  };
36
53
  const query = queryString(params);
37
- const res = await requestWithSupertest.get(
38
- `/api/tables/users/rows?${query}`
39
- );
54
+ const res = await requestWithSupertest
55
+ .get(`/api/tables/users/rows?${query}`)
56
+ .set('Cookie', [`accessToken=${accessToken}`]);
40
57
 
41
58
  expect(res.status).toEqual(200);
42
59
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -48,34 +65,46 @@ describe('Rows Endpoints', () => {
48
65
  expect(res.body.next).toEqual(
49
66
  `/tables/users/rows?${queryString({
50
67
  ...params,
51
- _page: params._page + 1
52
- }).toString()}`
68
+ _page: params._page + 1,
69
+ }).toString()}`,
53
70
  );
54
71
 
55
72
  expect(res.body.previous).toEqual(
56
73
  `/tables/users/rows?${queryString({
57
74
  ...params,
58
- _page: params._page - 1
59
- }).toString()}`
75
+ _page: params._page - 1,
76
+ }).toString()}`,
60
77
  );
61
78
  });
62
79
 
63
80
  it('GET /tables/:name/rows: should return a null field', async () => {
64
- const res = await requestWithSupertest.get(
65
- '/api/tables/users/rows?_filters=firstName__null,lastName__notnull'
81
+ const accessToken = await generateToken(
82
+ { username: 'John', isSuperuser: true },
83
+ config.tokenSecret,
84
+ '1H',
66
85
  );
67
86
 
87
+ const res = await requestWithSupertest
88
+ .get('/api/tables/users/rows?_filters=firstName__null,lastName__notnull')
89
+ .set('Cookie', [`accessToken=${accessToken}`]);
90
+
68
91
  expect(res.status).toEqual(200);
69
92
  expect(res.body.data[0].firstName).toBeNull();
70
93
  expect(res.body.data[0].lastName).not.toBeNull();
71
94
  });
72
95
 
73
96
  it('GET /tables/:name/rows: should successfully retrieve users created after 2010-01-01 00:00:00.', async () => {
74
- const date = '2010-01-01 00:00:00';
75
- const res = await requestWithSupertest.get(
76
- `/api/tables/users/rows?_filters=createdAt__gte:${date}`
97
+ const accessToken = await generateToken(
98
+ { username: 'John', isSuperuser: true },
99
+ config.tokenSecret,
100
+ '1H',
77
101
  );
78
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
+
79
108
  res.body.data.map((user) => {
80
109
  const createdAt = new Date(user.createdAt);
81
110
  const referenceDate = new Date(date);
@@ -90,11 +119,17 @@ describe('Rows Endpoints', () => {
90
119
  });
91
120
 
92
121
  it('GET /tables/:name/rows: should successfully retrieve users created before 2008-01-20 00:00:00.', async () => {
93
- const date = '2008-01-20 00:00:00';
94
- const res = await requestWithSupertest.get(
95
- `/api/tables/users/rows?_filters=createdAt__lte:${date}`
122
+ const accessToken = await generateToken(
123
+ { username: 'John', isSuperuser: true },
124
+ config.tokenSecret,
125
+ '1H',
96
126
  );
97
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
+
98
133
  res.body.data.map((user) => {
99
134
  const createdAt = new Date(user.createdAt);
100
135
  const referenceDate = new Date(date);
@@ -109,11 +144,17 @@ describe('Rows Endpoints', () => {
109
144
  });
110
145
 
111
146
  it('GET /tables/:name/rows: should successfully retrieve users created at 2013-01-08 00:00:00', async () => {
112
- const date = '2013-01-08 00:00:00';
113
- const res = await requestWithSupertest.get(
114
- `/api/tables/users/rows?_filters=createdAt__eq:${date}`
147
+ const accessToken = await generateToken(
148
+ { username: 'John', isSuperuser: true },
149
+ config.tokenSecret,
150
+ '1H',
115
151
  );
116
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
+
117
158
  res.body.data.map((user) => {
118
159
  const createdAt = new Date(user.createdAt);
119
160
  const referenceDate = new Date(date);
@@ -128,22 +169,34 @@ describe('Rows Endpoints', () => {
128
169
  });
129
170
 
130
171
  it('GET /tables/:name/rows: should successfully retrieve users created at 2007-01-08 00:00:00', async () => {
131
- const date = '2007-01-08 00:00:00';
132
- const res = await requestWithSupertest.get(
133
- `/api/tables/users/rows?_filters=createdAt__eq:${date}`
172
+ const accessToken = await generateToken(
173
+ { username: 'John', isSuperuser: true },
174
+ config.tokenSecret,
175
+ '1H',
134
176
  );
135
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
+
136
183
  //There are no users that are created at 2007-01-08 00:00:00 so the API should return empty data
137
184
  expect(res.body.data).toHaveLength(0);
138
185
  expect(res.status).toEqual(200);
139
186
  });
140
187
 
141
188
  it('GET /tables/:name/rows: should successfully retrieve users that are not created at 2021-01-08 00:00:00', async () => {
142
- const date = '2021-01-08 00:00:00';
143
- const res = await requestWithSupertest.get(
144
- `/api/tables/users/rows?_filters=createdAt__neq:${date}`
189
+ const accessToken = await generateToken(
190
+ { username: 'John', isSuperuser: true },
191
+ config.tokenSecret,
192
+ '1H',
145
193
  );
146
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
+
147
200
  res.body.data.map((user) => {
148
201
  const createdAt = new Date(user.createdAt);
149
202
  const referenceDate = new Date(date);
@@ -158,16 +211,33 @@ describe('Rows Endpoints', () => {
158
211
  });
159
212
 
160
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
+
161
220
  const res = await requestWithSupertest
162
221
  .post('/api/tables/users/rows')
222
+ .set('Cookie', [`accessToken=${accessToken}`])
163
223
  .send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
224
+
164
225
  expect(res.status).toEqual(201);
165
226
  expect(res.type).toEqual(expect.stringContaining('json'));
166
227
  expect(res.body).toHaveProperty('data');
167
228
  });
168
229
 
169
230
  it('GET /tables/:name/rows/:pks should return a row by its primary key', async () => {
170
- 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
+
171
241
  expect(res.status).toEqual(200);
172
242
  expect(res.type).toEqual(expect.stringContaining('json'));
173
243
  expect(res.body).toHaveProperty('data');
@@ -177,37 +247,65 @@ describe('Rows Endpoints', () => {
177
247
  });
178
248
 
179
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
+ );
180
255
  const res = await requestWithSupertest
181
256
  .put('/api/tables/users/rows/1')
257
+ .set('Cookie', [`accessToken=${accessToken}`])
182
258
  .send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
183
259
  expect(res.status).toEqual(200);
184
260
  expect(res.type).toEqual(expect.stringContaining('json'));
185
261
  });
186
262
 
187
263
  it('DELETE /tables/:name/rows/:pks should delete a row by its primary key and return the number of changes', async () => {
188
- 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}`]);
189
273
  expect(res.status).toEqual(200);
190
274
  expect(res.type).toEqual(expect.stringContaining('json'));
191
275
  });
192
276
 
193
277
  it('POST /tables/:name/rows should insert a new row if any of the value of the object being inserted is null', async () => {
194
- const res = await requestWithSupertest.post('/api/tables/users/rows').send({
195
- fields: {
196
- firstName: null,
197
- lastName: 'Doe',
198
- email: null,
199
- username: 'Jane'
200
- }
201
- });
278
+ const accessToken = await generateToken(
279
+ { username: 'John', isSuperuser: true },
280
+ config.tokenSecret,
281
+ '1H',
282
+ );
283
+ const res = await requestWithSupertest
284
+ .post('/api/tables/users/rows')
285
+ .send({
286
+ fields: {
287
+ firstName: null,
288
+ lastName: 'Doe',
289
+ email: null,
290
+ username: 'Jane',
291
+ },
292
+ })
293
+ .set('Cookie', [`accessToken=${accessToken}`]);
202
294
  expect(res.status).toEqual(201);
203
295
  expect(res.type).toEqual(expect.stringContaining('json'));
204
296
  expect(res.body).toHaveProperty('data');
205
297
  });
206
298
 
207
299
  it('GET /tables/:name/rows should return values if any of the IDs from the array match the user ID.', async () => {
208
- const res = await requestWithSupertest.get(
209
- '/api/tables/users/rows?_filters=id:[2,3]'
300
+ const accessToken = await generateToken(
301
+ { username: 'John', isSuperuser: true },
302
+ config.tokenSecret,
303
+ '1H',
210
304
  );
305
+
306
+ const res = await requestWithSupertest
307
+ .get('/api/tables/users/rows?_filters=id:[2,3]')
308
+ .set('Cookie', [`accessToken=${accessToken}`]);
211
309
  expect(res.status).toEqual(200);
212
310
  expect(res.body).toHaveProperty('data');
213
311
  expect(res.body.data).toEqual(expect.any(Array));
@@ -215,9 +313,17 @@ describe('Rows Endpoints', () => {
215
313
  });
216
314
 
217
315
  it('GET /tables/:name/rows should return values if the provided ID matches the user ID.', async () => {
218
- const res = await requestWithSupertest.get(
219
- '/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',
220
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}`]);
221
327
  expect(res.status).toEqual(200);
222
328
  expect(res.body).toHaveProperty('data');
223
329
  expect(res.body.data).toEqual(expect.any(Array));
@@ -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;