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,19 +1,27 @@
1
+ const { apiConstants } = require('../constants');
2
+
1
3
  module.exports = (db) => {
2
4
  return {
3
5
  get(data) {
4
- const query = `SELECT ${data.schemaString} FROM ${data.tableName} ${data.extendString} ${data.whereString} ${data.orderString} LIMIT ? OFFSET ?`;
6
+ const query = `SELECT ${data.schemaString || '*'} FROM ${
7
+ data.tableName
8
+ } ${data.extendString || ''} ${data.whereString || ''} ${
9
+ data.orderString || ''
10
+ } LIMIT ? OFFSET ?`;
11
+
5
12
  const statement = db.prepare(query);
6
13
  const result = statement.all(
7
14
  ...data.whereStringValues,
8
- data.limit,
9
- data.page
15
+ data.limit || apiConstants.DEFAULT_PAGE_LIMIT,
16
+ data.page || apiConstants.DEFAULT_PAGE_INDEX,
10
17
  );
18
+
11
19
  return result;
12
20
  },
13
21
 
14
22
  getById(data) {
15
23
  const pks = data.pks.split(',');
16
- const placeholders = pks.map((pk) => '?').join(',');
24
+ const placeholders = pks.map(() => '?').join(',');
17
25
  const query = `SELECT ${data.schemaString} FROM ${data.tableName} ${data.extendString} WHERE ${data.tableName}.${data.lookupField} in (${placeholders})`;
18
26
  const statement = db.prepare(query);
19
27
  const result = statement.all(...pks);
@@ -29,7 +37,9 @@ module.exports = (db) => {
29
37
 
30
38
  save(data) {
31
39
  // wrap text values in quotes
32
- const fieldsString = Object.keys(data.fields).join(', ');
40
+ const fieldsString = Object.keys(data.fields)
41
+ .map((field) => `'${field}'`)
42
+ .join(', ');
33
43
 
34
44
  // wrap text values in quotes
35
45
  const valuesString = Object.values(data.fields).map((value) => value);
@@ -48,9 +58,28 @@ module.exports = (db) => {
48
58
  return result;
49
59
  },
50
60
 
61
+ bulkWrite(data) {
62
+ const { tableName, fields } = data;
63
+ const fieldNames = Object.keys(fields[0]);
64
+ const valueSets = fields.map((row) => Object.values(row));
65
+
66
+ const placeholders = fieldNames.map(() => '?');
67
+ const valuesString = valueSets
68
+ .map(() => `(${placeholders.join(',')})`)
69
+ .join(',');
70
+
71
+ const query = `INSERT INTO ${tableName} (${fieldNames
72
+ .map((field) => `'${field}'`)
73
+ .join(', ')}) VALUES ${valuesString}`;
74
+
75
+ const statement = db.prepare(query);
76
+ const result = statement.run(...valueSets.flat());
77
+ return result;
78
+ },
79
+
51
80
  update(data) {
52
81
  const pks = data.pks.split(',');
53
- const placeholders = pks.map((pk) => '?').join(',');
82
+ const placeholders = pks.map(() => '?').join(',');
54
83
  const query = `UPDATE ${data.tableName} SET ${data.fieldsString} WHERE ${data.lookupField} in (${placeholders})`;
55
84
  const statement = db.prepare(query);
56
85
  const result = statement.run(...pks);
@@ -59,7 +88,7 @@ module.exports = (db) => {
59
88
 
60
89
  delete(data) {
61
90
  const pks = data.pks.split(',');
62
- const placeholders = pks.map((pk) => '?').join(',');
91
+ const placeholders = pks.map(() => '?').join(',');
63
92
  const query = `DELETE FROM ${data.tableName} WHERE ${data.lookupField} in (${placeholders})`;
64
93
  const statement = db.prepare(query);
65
94
  const result = statement.run(...pks);
@@ -1,5 +1,131 @@
1
1
  module.exports = (db) => {
2
2
  return {
3
- get() {},
3
+ createTable(tableName, schema, options = {}) {
4
+ const {
5
+ autoAddCreatedAt = true,
6
+ autoAddUpdatedAt = true,
7
+ multipleUniqueConstraints,
8
+ } = options;
9
+
10
+ let indices = [];
11
+
12
+ let schemaString = schema
13
+ .map(({ name, type, notNull, unique, primaryKey, foreignKey }) => {
14
+ let column = `'${name}' '${type}'`;
15
+
16
+ if (notNull) {
17
+ column += ' NOT NULL';
18
+ }
19
+ if (unique) {
20
+ column += ' UNIQUE';
21
+ }
22
+ if (primaryKey) {
23
+ column += ' PRIMARY KEY';
24
+ }
25
+ if (foreignKey) {
26
+ column += ` REFERENCES ${foreignKey.table}(${foreignKey.column})`;
27
+ }
28
+ if (foreignKey && foreignKey.onDelete) {
29
+ column += ` ON DELETE ${foreignKey.onDelete}`;
30
+ }
31
+ if (foreignKey && foreignKey.onUpdate) {
32
+ column += ` ON UPDATE ${foreignKey.onUpdate}`;
33
+ }
34
+
35
+ return column;
36
+ })
37
+ .join(', ');
38
+
39
+ // add id if primary key is not defined
40
+ if (!schema.find((field) => field.primaryKey)) {
41
+ schemaString = `
42
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
43
+ ${schemaString}
44
+ `;
45
+ }
46
+
47
+ // add created at and updated at
48
+ if (autoAddCreatedAt) {
49
+ schemaString = `${schemaString}, createdAt DATETIME DEFAULT CURRENT_TIMESTAMP`;
50
+ }
51
+
52
+ if (autoAddUpdatedAt) {
53
+ schemaString = `${schemaString}, updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP`;
54
+ }
55
+
56
+ if (multipleUniqueConstraints) {
57
+ schemaString = `${schemaString}, CONSTRAINT ${
58
+ multipleUniqueConstraints.name
59
+ } UNIQUE (${multipleUniqueConstraints.fields
60
+ .map((field) => field)
61
+ .join(' ,')})`;
62
+ }
63
+
64
+ let indicesString = indices
65
+ .map((field) => {
66
+ return `
67
+ CREATE INDEX ${tableName}_${field}_index
68
+ ON ${tableName} (${field})
69
+ `;
70
+ })
71
+ .join(';');
72
+
73
+ const query = `CREATE TABLE ${tableName} (${schemaString})`;
74
+
75
+ try {
76
+ db.prepare(query).run();
77
+
78
+ if (indicesString) {
79
+ db.prepare(indicesString).run();
80
+ }
81
+
82
+ db.prepare(`PRAGMA table_info(${tableName})`).all();
83
+ } catch (error) {
84
+ console.log(error);
85
+ }
86
+ },
87
+
88
+ listTables(options = {}) {
89
+ const { search, ordering, exclude } = options;
90
+
91
+ let query = `SELECT name FROM sqlite_master WHERE type IN ('table', 'view')`;
92
+
93
+ // if search is provided, search the tables
94
+ // e.g. search=users
95
+ if (search) {
96
+ query += ` AND name LIKE $searchQuery`;
97
+ }
98
+
99
+ // if exclude is passed don't return the some tables
100
+ // e.g. exclude=['_users', '_roles']
101
+ if (exclude) {
102
+ const excludeTables = exclude.map((field) => `'${field}'`).join(' ,');
103
+ query += `AND name NOT IN (${excludeTables});`;
104
+ }
105
+
106
+ // if ordering is provided, order the tables
107
+ // e.g. ordering=name (ascending) or ?_ordering=-name (descending)
108
+ if (ordering) {
109
+ query += ` ORDER BY $ordering`;
110
+ }
111
+
112
+ try {
113
+ const tables = db.prepare(query).all({
114
+ searchQuery: `%${search}%`,
115
+ ordering: `${ordering?.replace('-', '')} ${
116
+ ordering?.startsWith('-') ? 'DESC' : 'ASC'
117
+ }`,
118
+ });
119
+ return tables;
120
+ } catch (error) {
121
+ console.log(error);
122
+ }
123
+ },
124
+
125
+ checkTableExists(tableName) {
126
+ const query = `SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`;
127
+ const result = db.prepare(query).get();
128
+ return result;
129
+ },
4
130
  };
5
131
  };
@@ -31,6 +31,10 @@ const doc = {
31
31
  name: 'Rows',
32
32
  description: 'Rows endpoints',
33
33
  },
34
+ {
35
+ name: 'Auth',
36
+ description: 'Auth endpoints',
37
+ },
34
38
  ],
35
39
  securityDefinitions: {},
36
40
  definitions: {
@@ -122,6 +126,69 @@ const doc = {
122
126
  TransactionRequestBody: {
123
127
  $ref: '#/definitions/Transaction',
124
128
  },
129
+ ObtainAccessTokenRequestBody: {
130
+ fields: {
131
+ username: '@john',
132
+ password: 'Ak22#cPM33@v*#',
133
+ },
134
+ },
135
+
136
+ ObtainAccessTokenSuccessResponse: {
137
+ message: 'Success',
138
+ data: {
139
+ userId: 1,
140
+ },
141
+ },
142
+
143
+ InvalidCredentialErrorResponse: {
144
+ message: 'Invalid username or password',
145
+ },
146
+
147
+ UserRegisterationRequestBody: {
148
+ fields: {
149
+ username: '@john',
150
+ password: 'Ak22#cPM33@v*#',
151
+ },
152
+ },
153
+
154
+ WeakPasswordErrorResponse: {
155
+ message: 'This password is weak, please use another password',
156
+ },
157
+
158
+ UsernameTakenErrorResponse: {
159
+ message: 'This username is taken',
160
+ },
161
+
162
+ DefaultRoleNotCreatedErrorResponse: {
163
+ message: 'Please restart soul so a default role can be created',
164
+ },
165
+
166
+ UserNotFoundErrorResponse: {
167
+ message: 'User not found',
168
+ },
169
+
170
+ InvalidRefreshTokenErrorResponse: {
171
+ message: 'Invalid refresh token',
172
+ },
173
+
174
+ ChangePasswordRequestBody: {
175
+ fields: {
176
+ currentPassword: 'Ak22#cPM33@v*#',
177
+ newPassword: 'hKB33o@3245CD$',
178
+ },
179
+ },
180
+
181
+ ChangePasswordSuccessResponse: {
182
+ message: 'Password updated successfully',
183
+ data: { id: 1, username: '@john' },
184
+ },
185
+
186
+ RefreshAccessTokenSuccessResponse: {
187
+ message: 'Success',
188
+ data: { userId: 1 },
189
+ },
190
+
191
+ InvalidPasswordErrorResponse: { message: 'Invalid password' },
125
192
  },
126
193
  };
127
194