soul-cli 0.6.1 → 0.7.1
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/.all-contributorsrc +73 -0
- package/.editorconfig +26 -0
- package/.env.sample +9 -0
- package/.eslintignore +2 -0
- package/.eslintrc.js +24 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/workflows/npm-publish.yml +23 -0
- package/.husky/pre-commit +5 -0
- package/CONTRIBUTING.md +92 -0
- package/LICENSE +21 -0
- package/README.md +188 -1
- package/docs/api/root-examples.md +41 -0
- package/docs/api/rows-examples.md +225 -0
- package/docs/api/tables-examples.md +217 -0
- package/docs/api-examples.md +62 -0
- package/docs/auth-db-diagram.png +0 -0
- package/docs/auth.md +135 -0
- package/docs/extensions-examples.md +167 -0
- package/docs/logo.png +0 -0
- package/docs/soul-studio.png +0 -0
- package/docs/ws/rows-examples.md +122 -0
- package/docs/ws-examples.md +42 -0
- package/package.json +17 -2
- package/src/cli.js +59 -1
- package/src/commands.js +22 -0
- package/src/config/index.js +48 -3
- package/src/constants/api.js +26 -0
- package/src/constants/auth.js +5 -0
- package/src/constants/index.js +13 -0
- package/src/constants/messages.js +41 -0
- package/src/constants/roles.js +3 -0
- package/src/constants/tables.js +47 -0
- package/src/controllers/auth/common.js +16 -0
- package/src/controllers/auth/index.js +5 -0
- package/src/controllers/auth/tables.js +96 -0
- package/src/controllers/auth/token.js +243 -0
- package/src/controllers/auth/user.js +394 -0
- package/src/controllers/auth.test.js +372 -0
- package/src/controllers/rows.js +48 -55
- package/src/controllers/rows.test.js +146 -40
- package/src/controllers/tables.test.js +58 -28
- package/src/db/schema.js +118 -0
- package/src/index.js +33 -4
- package/src/middlewares/api.js +79 -0
- package/src/middlewares/auth.js +85 -0
- package/src/middlewares/broadcast.js +1 -1
- package/src/middlewares/validation.js +3 -2
- package/src/routes/auth.js +29 -0
- package/src/routes/rows.js +17 -5
- package/src/routes/tables.js +26 -4
- package/src/schemas/auth.js +67 -0
- package/src/schemas/index.js +6 -1
- package/src/schemas/rows.js +20 -0
- package/src/schemas/tables.js +18 -2
- package/src/server.js +3 -2
- package/src/services/authService.js +78 -0
- package/src/services/index.js +2 -1
- package/src/services/rowService.js +36 -7
- package/src/services/tableService.js +127 -1
- package/src/swagger/index.js +67 -0
- package/src/swagger/swagger.json +292 -60
- package/src/tests/index.js +4 -5
- package/src/tests/testData.js +23 -10
- package/src/utils/index.js +78 -0
- /package/{src/services/.gitkeep → docs/extensions/api-examples.js} +0 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# Extensions Examples
|
|
2
|
+
|
|
3
|
+
Soul extensions are a way to extend the functionality of Soul. Extensions are written in JavaScript and can be used to add new endpoints, modify existing endpoints, or add new functionality to Soul.
|
|
4
|
+
|
|
5
|
+
## Types of Extensions
|
|
6
|
+
|
|
7
|
+
- API Extensions: Add new endpoints to Soul
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Setup Environment
|
|
11
|
+
|
|
12
|
+
To follow the below examples we need to download a sample database and also install Soul CLI.
|
|
13
|
+
|
|
14
|
+
### Download Sample Database
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
wget https://raw.githubusercontent.com/lerocha/chinook-database/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite # Download sample sqlite database
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Using Soul CLI
|
|
21
|
+
```bash
|
|
22
|
+
npm install -g soul-cli
|
|
23
|
+
soul -d ./Chinook_Sqlite.sqlite -p 8000 -e "/absolute/path/to/_extensions/"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
<details>
|
|
27
|
+
<summary>Or Using Local Development</summary>
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
git clone https://github.com/thevahidal/soul # Clone project
|
|
31
|
+
npm install # Install dependencies
|
|
32
|
+
npm link # might need `sudo`
|
|
33
|
+
soul -d ./Chinook_Sqlite.sqlite -p 8000 -e "/absolute/path/to/_extensions/"
|
|
34
|
+
```
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## Creating an API extension
|
|
39
|
+
|
|
40
|
+
To create an extension, create a new folder named `_extensions`. Then create a file named `api.js` inside it. This file will contain the extension code.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
const hello = {
|
|
44
|
+
method: 'GET',
|
|
45
|
+
path: '/api/hello-soul',
|
|
46
|
+
handler: (req, res, db) => {
|
|
47
|
+
res.status(200).json({
|
|
48
|
+
message: 'Hello Soul!'
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const timestamp = {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
path: '/api/timestamp',
|
|
56
|
+
handler: (req, res, db) => {
|
|
57
|
+
res.status(200).json({
|
|
58
|
+
timestamp: Date.now(),
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const greetings = {
|
|
64
|
+
method: 'POST',
|
|
65
|
+
path: '/api/greetings/:name',
|
|
66
|
+
handler: (req, res, db) => {
|
|
67
|
+
const { name } = req.params;
|
|
68
|
+
const { greeting } = req.body;
|
|
69
|
+
res.status(200).json({
|
|
70
|
+
message: `${greeting} ${name}!`,
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const searchTables = {
|
|
76
|
+
method: 'GET',
|
|
77
|
+
path: '/api/search-tables',
|
|
78
|
+
handler: (req, res, db) => {
|
|
79
|
+
const { q } = req.query;
|
|
80
|
+
const sql = `
|
|
81
|
+
SELECT name FROM sqlite_master
|
|
82
|
+
WHERE type='table'
|
|
83
|
+
AND name LIKE $searchQuery
|
|
84
|
+
`;
|
|
85
|
+
try {
|
|
86
|
+
const tables = db.prepare(sql).all({
|
|
87
|
+
searchQuery: `%${q}%`,
|
|
88
|
+
});
|
|
89
|
+
res.status(200).json({
|
|
90
|
+
tables,
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
res.status(500).json({
|
|
94
|
+
error: error.message,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = {
|
|
101
|
+
hello,
|
|
102
|
+
timestamp,
|
|
103
|
+
greetings,
|
|
104
|
+
searchTables,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Alright, now we can test if the extension is working:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
curl http://localhost:8000/api/hello-soul
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
It should return:
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"message": "Hello Soul!"
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
And the same for the `timestamp` endpoint:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
curl http://localhost:8000/api/timestamp
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
It should return:
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"timestamp": 1620000000000
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
And `greetings` endpoint:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
curl -X POST -H "Content-Type: application/json" -d '{"greeting": "Hello"}' http://localhost:8000/api/greetings/John
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
It should return:
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{
|
|
147
|
+
"message": "Hello John!"
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
And `list-tables` endpoint:
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
curl http://localhost:8000/api/search-tables?q=al
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
It should return:
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"tables": [
|
|
162
|
+
{
|
|
163
|
+
"name": "Album"
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
```
|
package/docs/logo.png
ADDED
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
## Rows
|
|
2
|
+
|
|
3
|
+
### 1. Rows realtime changes
|
|
4
|
+
|
|
5
|
+
To subscribe to realtime changes (Insert, Update and Delete) in rows of a certain table,
|
|
6
|
+
use `tables/<table-name>` endpoint.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
wscat -c ws://localhost:8000/ws/tables/Employee
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
It should respond with the following message:
|
|
13
|
+
|
|
14
|
+
```json
|
|
15
|
+
{
|
|
16
|
+
"message": "Subscribed to table \"Employee\""
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Note that it's ready to receive messages.
|
|
21
|
+
|
|
22
|
+
Then to test it, in a new terminal, insert a new row in the table `Employee`:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
curl --request POST \
|
|
26
|
+
--url http://localhost:8000/api/tables/Employee/rows \
|
|
27
|
+
--header 'Content-Type: application/json' \
|
|
28
|
+
--data '{
|
|
29
|
+
"fields": {
|
|
30
|
+
"FirstName": "Damien",
|
|
31
|
+
"LastName": "Rice"
|
|
32
|
+
}
|
|
33
|
+
}'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Checkout `wscat` terminal, it should respond with the following message:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"type": "INSERT",
|
|
41
|
+
"data": {
|
|
42
|
+
"pk": 10,
|
|
43
|
+
"FirstName": "Damien",
|
|
44
|
+
"LastName": "Rice"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Then to test update changes, let's change the `FirstName` of the row with `pk` 10:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
curl --request PUT \
|
|
53
|
+
--url http://localhost:8000/api/tables/Employee/rows/10 \
|
|
54
|
+
--header 'Content-Type: application/json' \
|
|
55
|
+
--data '{
|
|
56
|
+
"fields": {
|
|
57
|
+
"FirstName": "Glen"
|
|
58
|
+
}
|
|
59
|
+
}'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Checkout `wscat` terminal, it should respond with the following message:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"type": "UPDATE",
|
|
67
|
+
"_lookup_field": "EmployeeId",
|
|
68
|
+
"data": {
|
|
69
|
+
"pks": ["10"],
|
|
70
|
+
"FirstName": "Glen"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
While we're testing update changes, let's see how it works in bulk update mode:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
curl --request PUT \
|
|
79
|
+
--url http://localhost:8000/api/tables/Employee/rows/8,9,10 \
|
|
80
|
+
--header 'Content-Type: application/json' \
|
|
81
|
+
--data '{
|
|
82
|
+
"fields": {
|
|
83
|
+
"FirstName": "David"
|
|
84
|
+
}
|
|
85
|
+
}'
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Note comma-separated `lookup-values` in the URL. (8,9,10)
|
|
89
|
+
|
|
90
|
+
Checkout `wscat` terminal, it should respond with the following message:
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"type": "UPDATE",
|
|
95
|
+
"_lookup_field": "EmployeeId",
|
|
96
|
+
"data": {
|
|
97
|
+
"pks": ["8", "9", "10"],
|
|
98
|
+
"FirstName": "David"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Then to test delete changes, let's delete the row with `pk` 10:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
curl --request DELETE \
|
|
107
|
+
--url http://localhost:8000/api/tables/Employee/rows/10
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Checkout `wscat` terminal, it should respond with the following message:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"type": "DELETE",
|
|
115
|
+
"_lookup_field": "EmployeeId",
|
|
116
|
+
"data": {
|
|
117
|
+
"pks": ["10"]
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
That's it for realtime changes.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Websocket Examples
|
|
2
|
+
|
|
3
|
+
Soul realtime right now only support `rows` changes.
|
|
4
|
+
|
|
5
|
+
## Setup Environment
|
|
6
|
+
|
|
7
|
+
To follow the below examples we need to download a sample database, install Soul CLI and also a websocket client to test.
|
|
8
|
+
(Please note that you can use any websocket client you want, e.g. Postman, Insomnia, etc.)
|
|
9
|
+
|
|
10
|
+
### Download Sample Database
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
wget https://raw.githubusercontent.com/lerocha/chinook-database/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite # Download sample sqlite database
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Install Websocket client
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm i -g wscat # Install websocket client
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Using Soul CLI
|
|
23
|
+
```bash
|
|
24
|
+
npm install -g soul-cli
|
|
25
|
+
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details>
|
|
29
|
+
<summary>Or Using Local Development</summary>
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
git clone https://github.com/thevahidal/soul # Clone project
|
|
33
|
+
npm install # Install dependencies
|
|
34
|
+
npm link # might need `sudo`
|
|
35
|
+
soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
36
|
+
```
|
|
37
|
+
</details>
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Namespaces
|
|
41
|
+
|
|
42
|
+
1. [/ws/tables](/docs/ws/rows-examples.md) Examples for Rows websockets
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "A SQLite REST and Realtime server",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
"dev": "npm run swagger-autogen && cross-env NO_CLI=true nodemon src/server.js",
|
|
12
12
|
"cli": "nodemon src/server.js --database foobar.db",
|
|
13
13
|
"swagger-autogen": "cross-env NO_CLI=true node src/swagger/index.js",
|
|
14
|
-
"test": "cross-env NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000"
|
|
14
|
+
"test": "cross-env CI=true NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000",
|
|
15
|
+
"prepare": "husky install",
|
|
16
|
+
"lint": "eslint . --fix --max-warnings=0",
|
|
17
|
+
"format": "prettier . --write"
|
|
15
18
|
},
|
|
16
19
|
"repository": {
|
|
17
20
|
"type": "git",
|
|
@@ -24,14 +27,18 @@
|
|
|
24
27
|
},
|
|
25
28
|
"homepage": "https://github.com/thevahidal/soul#readme",
|
|
26
29
|
"dependencies": {
|
|
30
|
+
"bcrypt": "^5.1.1",
|
|
27
31
|
"better-sqlite3": "^8.1.0",
|
|
28
32
|
"body-parser": "^1.20.2",
|
|
33
|
+
"check-password-strength": "^2.0.7",
|
|
34
|
+
"cookie-parser": "^1.4.6",
|
|
29
35
|
"cors": "^2.8.5",
|
|
30
36
|
"dotenv": "^16.0.3",
|
|
31
37
|
"express": "^4.18.2",
|
|
32
38
|
"express-rate-limit": "^6.7.0",
|
|
33
39
|
"express-winston": "^4.2.0",
|
|
34
40
|
"joi": "^17.8.3",
|
|
41
|
+
"jsonwebtoken": "^9.0.2",
|
|
35
42
|
"soul-studio": "^0.0.1",
|
|
36
43
|
"swagger-ui-express": "^4.6.1",
|
|
37
44
|
"winston": "^3.8.2",
|
|
@@ -40,8 +47,12 @@
|
|
|
40
47
|
},
|
|
41
48
|
"devDependencies": {
|
|
42
49
|
"cross-env": "^7.0.3",
|
|
50
|
+
"eslint": "^8.54.0",
|
|
51
|
+
"eslint-config-prettier": "^9.0.0",
|
|
52
|
+
"husky": "^8.0.3",
|
|
43
53
|
"jest": "^29.4.3",
|
|
44
54
|
"nodemon": "^2.0.20",
|
|
55
|
+
"prettier": "3.1.0",
|
|
45
56
|
"supertest": "^6.3.3",
|
|
46
57
|
"swagger-autogen": "^2.23.1"
|
|
47
58
|
},
|
|
@@ -52,5 +63,9 @@
|
|
|
52
63
|
],
|
|
53
64
|
"globalSetup": "./src/tests/globalSetup.js",
|
|
54
65
|
"globalTeardown": "./src/tests/globalTeardown.js"
|
|
66
|
+
},
|
|
67
|
+
"lint-staged": {
|
|
68
|
+
"*.js": "eslint --fix",
|
|
69
|
+
"*.{js,css,md,html,json}": "prettier --write"
|
|
55
70
|
}
|
|
56
71
|
}
|
package/src/cli.js
CHANGED
|
@@ -46,11 +46,69 @@ if (process.env.NO_CLI !== 'true') {
|
|
|
46
46
|
type: 'string',
|
|
47
47
|
demandOption: false,
|
|
48
48
|
})
|
|
49
|
+
.options('a', {
|
|
50
|
+
alias: 'auth',
|
|
51
|
+
describe: 'Enable authentication and authorization',
|
|
52
|
+
type: 'boolean',
|
|
53
|
+
default: false,
|
|
54
|
+
demandOption: false,
|
|
55
|
+
})
|
|
56
|
+
.options('ts', {
|
|
57
|
+
alias: 'tokensecret',
|
|
58
|
+
describe: 'JWT secret for the access and refresh tokens',
|
|
59
|
+
type: 'string',
|
|
60
|
+
default: null,
|
|
61
|
+
demandOption: false,
|
|
62
|
+
})
|
|
63
|
+
.options('atet', {
|
|
64
|
+
alias: 'accesstokenexpirationtime',
|
|
65
|
+
describe: 'JWT expiration time for access token',
|
|
66
|
+
type: 'string',
|
|
67
|
+
default: '5H',
|
|
68
|
+
demandOption: false,
|
|
69
|
+
})
|
|
70
|
+
.options('rtet', {
|
|
71
|
+
alias: 'refreshtokenexpirationtime',
|
|
72
|
+
describe: 'JWT expiration time for refresh token',
|
|
73
|
+
type: 'string',
|
|
74
|
+
default: '3D',
|
|
75
|
+
demandOption: false,
|
|
76
|
+
})
|
|
77
|
+
.options('iuu', {
|
|
78
|
+
alias: 'initialuserusername',
|
|
79
|
+
describe: 'Initial superuser username',
|
|
80
|
+
type: 'string',
|
|
81
|
+
demandOption: false,
|
|
82
|
+
})
|
|
83
|
+
.options('iup', {
|
|
84
|
+
alias: 'initialuserpassword',
|
|
85
|
+
describe: 'Initial superuser password',
|
|
86
|
+
type: 'string',
|
|
87
|
+
demandOption: false,
|
|
88
|
+
})
|
|
49
89
|
.options('S', {
|
|
50
90
|
alias: 'studio',
|
|
51
91
|
describe: 'Start Soul Studio in parallel',
|
|
52
92
|
type: 'boolean',
|
|
53
|
-
demandOption: false
|
|
93
|
+
demandOption: false,
|
|
94
|
+
})
|
|
95
|
+
.command('updatesuperuser', 'Update a superuser', (yargs) => {
|
|
96
|
+
return yargs
|
|
97
|
+
.option('id', {
|
|
98
|
+
describe: 'The ID of the superuser you want to update',
|
|
99
|
+
type: 'number',
|
|
100
|
+
demandOption: true,
|
|
101
|
+
})
|
|
102
|
+
.option('password', {
|
|
103
|
+
describe: 'The new password for the superuser you want to update',
|
|
104
|
+
type: 'string',
|
|
105
|
+
demandOption: false,
|
|
106
|
+
})
|
|
107
|
+
.option('is_superuser', {
|
|
108
|
+
describe: 'The role of the superuser you want to update',
|
|
109
|
+
type: 'boolean',
|
|
110
|
+
demandOption: false,
|
|
111
|
+
});
|
|
54
112
|
})
|
|
55
113
|
.help(true).argv;
|
|
56
114
|
}
|
package/src/commands.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const { yargs } = require('./cli');
|
|
2
|
+
const { updateSuperuser } = require('./controllers/auth');
|
|
3
|
+
|
|
4
|
+
const { argv } = yargs;
|
|
5
|
+
|
|
6
|
+
const runCLICommands = () => {
|
|
7
|
+
// if the updatesuperuser command is passed from the CLI execute the updatesuperuser function
|
|
8
|
+
if (argv._.includes('updatesuperuser')) {
|
|
9
|
+
const { id, password, is_superuser } = argv;
|
|
10
|
+
|
|
11
|
+
if (!password && !is_superuser) {
|
|
12
|
+
console.log(
|
|
13
|
+
'Please provide either the --password or --is_superuser flag when using the updateuser command.',
|
|
14
|
+
);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
} else {
|
|
17
|
+
updateSuperuser({ id, password, is_superuser });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports = { runCLICommands };
|
package/src/config/index.js
CHANGED
|
@@ -2,7 +2,7 @@ const dotenv = require('dotenv');
|
|
|
2
2
|
const Joi = require('joi');
|
|
3
3
|
const path = require('path');
|
|
4
4
|
|
|
5
|
-
const { yargs
|
|
5
|
+
const { yargs } = require('../cli');
|
|
6
6
|
|
|
7
7
|
const { argv } = yargs;
|
|
8
8
|
|
|
@@ -20,6 +20,7 @@ const envVarsSchema = Joi.object()
|
|
|
20
20
|
VERBOSE: Joi.string().valid('console', null).default(null),
|
|
21
21
|
|
|
22
22
|
CORS_ORIGIN_WHITELIST: Joi.string().default('*'),
|
|
23
|
+
AUTH: Joi.boolean().default(false),
|
|
23
24
|
|
|
24
25
|
RATE_LIMIT_ENABLED: Joi.boolean().default(false),
|
|
25
26
|
RATE_LIMIT_WINDOW_MS: Joi.number().positive().default(1000),
|
|
@@ -27,7 +28,14 @@ const envVarsSchema = Joi.object()
|
|
|
27
28
|
|
|
28
29
|
EXTENSIONS: Joi.string().default(null),
|
|
29
30
|
|
|
30
|
-
START_WITH_STUDIO: Joi.boolean().default(false)
|
|
31
|
+
START_WITH_STUDIO: Joi.boolean().default(false),
|
|
32
|
+
|
|
33
|
+
INITIAL_USER_USERNAME: Joi.string(),
|
|
34
|
+
INITIAL_USER_PASSWORD: Joi.string(),
|
|
35
|
+
|
|
36
|
+
TOKEN_SECRET: Joi.string().default(null),
|
|
37
|
+
ACCESS_TOKEN_EXPIRATION_TIME: Joi.string().default('5H'),
|
|
38
|
+
REFRESH_TOKEN_EXPIRATION_TIME: Joi.string().default('3D'),
|
|
31
39
|
})
|
|
32
40
|
.unknown();
|
|
33
41
|
|
|
@@ -51,10 +59,34 @@ if (argv.cors) {
|
|
|
51
59
|
env.CORS_ORIGIN_WHITELIST = argv.cors;
|
|
52
60
|
}
|
|
53
61
|
|
|
62
|
+
if (argv.auth) {
|
|
63
|
+
env.AUTH = argv.auth;
|
|
64
|
+
}
|
|
65
|
+
|
|
54
66
|
if (argv['rate-limit-enabled']) {
|
|
55
67
|
env.RATE_LIMIT_ENABLED = argv['rate-limit-enabled'];
|
|
56
68
|
}
|
|
57
69
|
|
|
70
|
+
if (argv.tokensecret) {
|
|
71
|
+
env.TOKEN_SECRET = argv.tokensecret;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (argv.accesstokenexpirationtime) {
|
|
75
|
+
env.ACCESS_TOKEN_EXPIRATION_TIME = argv.accesstokenexpirationtime;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (argv.refreshtokenexpirationtime) {
|
|
79
|
+
env.REFRESH_TOKEN_EXPIRATION_TIME = argv.refreshtokenexpirationtime;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (argv.initialuserusername) {
|
|
83
|
+
env.INITIAL_USER_USERNAME = argv.initialuserusername;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (argv.initialuserpassword) {
|
|
87
|
+
env.INITIAL_USER_PASSWORD = argv.initialuserpassword;
|
|
88
|
+
}
|
|
89
|
+
|
|
58
90
|
const { value: envVars, error } = envVarsSchema
|
|
59
91
|
.prefs({ errors: { label: 'key' } })
|
|
60
92
|
.validate(env);
|
|
@@ -80,6 +112,19 @@ module.exports = {
|
|
|
80
112
|
origin: argv.cors?.split(',') ||
|
|
81
113
|
envVars.CORS_ORIGIN_WHITELIST?.split(',') || ['*'],
|
|
82
114
|
},
|
|
115
|
+
|
|
116
|
+
auth: argv.auth || envVars.AUTH,
|
|
117
|
+
tokenSecret: argv.tokensecret || envVars.TOKEN_SECRET,
|
|
118
|
+
accessTokenExpirationTime:
|
|
119
|
+
argv.accesstokenexpirationtime || envVars.ACCESS_TOKEN_EXPIRATION_TIME,
|
|
120
|
+
refreshTokenExpirationTime:
|
|
121
|
+
argv.refreshtokenexpirationtime || envVars.REFRESH_TOKEN_EXPIRATION_TIME,
|
|
122
|
+
|
|
123
|
+
initialUserUsername:
|
|
124
|
+
argv.initialuserusername || envVars.INITIAL_USER_USERNAME,
|
|
125
|
+
initialUserPassword:
|
|
126
|
+
argv.initialuserpassword || envVars.INITIAL_USER_PASSWORD,
|
|
127
|
+
|
|
83
128
|
rateLimit: {
|
|
84
129
|
enabled: argv['rate-limit-enabled'] || envVars.RATE_LIMIT_ENABLED,
|
|
85
130
|
windowMs: envVars.RATE_LIMIT_WINDOW,
|
|
@@ -90,5 +135,5 @@ module.exports = {
|
|
|
90
135
|
path: argv.extensions || envVars.EXTENSIONS,
|
|
91
136
|
},
|
|
92
137
|
|
|
93
|
-
startWithStudio: argv.studio || envVars.START_WITH_STUDIO
|
|
138
|
+
startWithStudio: argv.studio || envVars.START_WITH_STUDIO,
|
|
94
139
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
authEndpoints: ['_users', '_roles', '_roles_permissions', '_users_roles'],
|
|
3
|
+
baseTableUrl: '/api/tables',
|
|
4
|
+
universalAccessEndpoints: ['/api/auth/change-password'],
|
|
5
|
+
|
|
6
|
+
DEFAULT_PAGE_LIMIT: 10,
|
|
7
|
+
DEFAULT_PAGE_INDEX: 0,
|
|
8
|
+
PASSWORD: {
|
|
9
|
+
TOO_WEAK: 'Too weak',
|
|
10
|
+
WEAK: 'Weak',
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
httpVerbs: {
|
|
14
|
+
POST: 'POST',
|
|
15
|
+
GET: 'GET',
|
|
16
|
+
PUT: 'PUT',
|
|
17
|
+
DELETE: 'DELETE',
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
httpMethodDefinitions: {
|
|
21
|
+
POST: 'CREATE',
|
|
22
|
+
GET: 'READ',
|
|
23
|
+
PUT: 'UPDATE',
|
|
24
|
+
DELETE: 'DELETE',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const dbConstants = require('./tables');
|
|
2
|
+
const apiConstants = require('./api');
|
|
3
|
+
const constantRoles = require('./roles');
|
|
4
|
+
const responseMessages = require('./messages');
|
|
5
|
+
const authConstants = require('./auth');
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
dbConstants,
|
|
9
|
+
apiConstants,
|
|
10
|
+
constantRoles,
|
|
11
|
+
responseMessages,
|
|
12
|
+
authConstants,
|
|
13
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
successMessage: {
|
|
3
|
+
SUCCESS: 'Success',
|
|
4
|
+
ROW_INSERTED: 'Row Inserted',
|
|
5
|
+
PASSWORD_UPDATE_SUCCESS: 'Password updated successfully',
|
|
6
|
+
USER_UPDATE_SUCCESS: 'User updated successfully',
|
|
7
|
+
INITIAL_USER_CREATED_SUCCESS: 'Initial user created successfully',
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
errorMessage: {
|
|
11
|
+
USERNAME_TAKEN_ERROR: 'This username is taken',
|
|
12
|
+
WEAK_PASSWORD_ERROR: 'This password is weak, please use another password',
|
|
13
|
+
DEFAULT_ROLE_NOT_CREATED_ERROR:
|
|
14
|
+
'Please restart soul so a default role can be created',
|
|
15
|
+
INVALID_USERNAME_PASSWORD_ERROR: 'Invalid username or password',
|
|
16
|
+
INVALID_REFRESH_TOKEN_ERROR: 'Invalid refresh token',
|
|
17
|
+
INVALID_ACCESS_TOKEN_ERROR: 'Invalid access token',
|
|
18
|
+
USER_NOT_FOUND_ERROR: 'User not found',
|
|
19
|
+
INVALID_CURRENT_PASSWORD_ERROR: 'Invalid current password',
|
|
20
|
+
NOT_AUTHORIZED_ERROR: 'Not authorized',
|
|
21
|
+
PERMISSION_NOT_DEFINED_ERROR: 'Permission not defined for this role',
|
|
22
|
+
ROLE_NOT_FOUND_ERROR: 'Role not found for this user',
|
|
23
|
+
AUTH_SET_TO_FALSE_ERROR:
|
|
24
|
+
'You can not access this endpoint while AUTH is set to false',
|
|
25
|
+
RESERVED_TABLE_NAME_ERROR:
|
|
26
|
+
'The table name is reserved. Please choose a different name for the table.',
|
|
27
|
+
SERVER_ERROR: 'Server error',
|
|
28
|
+
|
|
29
|
+
INITIAL_USER_USERNAME_NOT_PASSED_ERROR:
|
|
30
|
+
'Error: You should pass the initial users username either from the CLI with the --iuu or from the environment variable using the INITIAL_USER_USERNAME flag',
|
|
31
|
+
INITIAL_USER_PASSWORD_NOT_PASSED_ERROR:
|
|
32
|
+
'Error: You should pass the initial users password either from the CLI with the --iup or from the environment variable using the INITIAL_USER_PASSWORD flag',
|
|
33
|
+
|
|
34
|
+
USERNAME_REQUIRED_ERROR: 'username is required',
|
|
35
|
+
PASSWORD_REQUIRED_ERROR: 'password is required',
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
infoMessage: {
|
|
39
|
+
INITIAL_USER_ALREADY_CREATED: 'Initial user is already created',
|
|
40
|
+
},
|
|
41
|
+
};
|