soul-cli 0.4.0 → 0.5.2
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/.env.sample +4 -2
- package/package.json +3 -2
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -3
- package/src/controllers/rows.js +76 -12
- package/src/controllers/rows.test.js +53 -0
- package/src/server.js +20 -1
- package/src/swagger/swagger.json +2 -2
- package/src/tests/index.js +7 -3
- package/src/tests/testData.js +30 -0
package/.env.sample
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
CORE_PORT=8000
|
|
2
2
|
|
|
3
3
|
NODE_ENV=development
|
|
4
4
|
|
|
5
5
|
CORS_ORIGIN_WHITELIST=http://localhost:3000,http://127.0.0.1:3000
|
|
6
6
|
|
|
7
|
-
RATE_LIMIT_ENABLED=
|
|
7
|
+
RATE_LIMIT_ENABLED=false
|
|
8
8
|
RATE_LIMIT_WINDOW_MS=1000
|
|
9
9
|
RATE_LIMIT_MAX_REQUESTS=10
|
|
10
10
|
|
|
11
11
|
DB=foobar.db
|
|
12
|
+
|
|
13
|
+
START_WITH_STUDIO=false
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "A SQLite REST and Realtime server",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
@@ -11,7 +11,7 @@
|
|
|
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
|
|
14
|
+
"test": "cross-env NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"express-rate-limit": "^6.7.0",
|
|
33
33
|
"express-winston": "^4.2.0",
|
|
34
34
|
"joi": "^17.8.3",
|
|
35
|
+
"soul-studio": "^0.0.1",
|
|
35
36
|
"swagger-ui-express": "^4.6.1",
|
|
36
37
|
"winston": "^3.8.2",
|
|
37
38
|
"ws": "^8.12.1",
|
package/src/cli.js
CHANGED
package/src/config/index.js
CHANGED
|
@@ -10,7 +10,7 @@ dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
|
10
10
|
|
|
11
11
|
const envVarsSchema = Joi.object()
|
|
12
12
|
.keys({
|
|
13
|
-
|
|
13
|
+
CORE_PORT: Joi.number().positive().default(8000),
|
|
14
14
|
|
|
15
15
|
NODE_ENV: Joi.string()
|
|
16
16
|
.valid('production', 'development', 'test')
|
|
@@ -26,6 +26,8 @@ const envVarsSchema = Joi.object()
|
|
|
26
26
|
RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().default(10),
|
|
27
27
|
|
|
28
28
|
EXTENSIONS: Joi.string().default(null),
|
|
29
|
+
|
|
30
|
+
START_WITH_STUDIO: Joi.boolean().default(false)
|
|
29
31
|
})
|
|
30
32
|
.unknown();
|
|
31
33
|
|
|
@@ -34,7 +36,7 @@ const env = {
|
|
|
34
36
|
};
|
|
35
37
|
|
|
36
38
|
if (argv.port) {
|
|
37
|
-
env.
|
|
39
|
+
env.CORE_PORT = argv.port;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
if (argv.verbose) {
|
|
@@ -68,7 +70,7 @@ module.exports = {
|
|
|
68
70
|
isDevelopment: envVars.NODE_ENV === 'development',
|
|
69
71
|
isTest: envVars.NODE_ENV === 'test',
|
|
70
72
|
|
|
71
|
-
port: argv.port || envVars.
|
|
73
|
+
port: argv.port || envVars.CORE_PORT,
|
|
72
74
|
verbose: argv['verbose'] || envVars.VERBOSE,
|
|
73
75
|
|
|
74
76
|
db: {
|
|
@@ -87,4 +89,6 @@ module.exports = {
|
|
|
87
89
|
extensions: {
|
|
88
90
|
path: argv.extensions || envVars.EXTENSIONS,
|
|
89
91
|
},
|
|
92
|
+
|
|
93
|
+
startWithStudio: argv.studio || envVars.START_WITH_STUDIO
|
|
90
94
|
};
|
package/src/controllers/rows.js
CHANGED
|
@@ -6,6 +6,17 @@ const quotePrimaryKeys = (pks) => {
|
|
|
6
6
|
return quotedPks;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
const operators = {
|
|
10
|
+
eq: '=',
|
|
11
|
+
lt: '<',
|
|
12
|
+
gt: '>',
|
|
13
|
+
lte: '<=',
|
|
14
|
+
gte: '>=',
|
|
15
|
+
neq: '!=',
|
|
16
|
+
null: 'IS NULL',
|
|
17
|
+
notnull: 'IS NOT NULL',
|
|
18
|
+
};
|
|
19
|
+
|
|
9
20
|
// Return paginated rows of a table
|
|
10
21
|
const listTableRows = async (req, res) => {
|
|
11
22
|
/*
|
|
@@ -45,6 +56,7 @@ const listTableRows = async (req, res) => {
|
|
|
45
56
|
in: 'query',
|
|
46
57
|
}
|
|
47
58
|
*/
|
|
59
|
+
let params = '';
|
|
48
60
|
const { name: tableName } = req.params;
|
|
49
61
|
const {
|
|
50
62
|
_page = 1,
|
|
@@ -64,18 +76,47 @@ const listTableRows = async (req, res) => {
|
|
|
64
76
|
// filtering is case insensitive
|
|
65
77
|
// e.g. ?_filters=name:John,age:20
|
|
66
78
|
// will filter by name like '%John%' and age like '%20%'
|
|
79
|
+
let filters = [];
|
|
80
|
+
try {
|
|
81
|
+
filters = _filters.split(',').map((filter) => {
|
|
82
|
+
let [key, value] = filter.split(':');
|
|
83
|
+
|
|
84
|
+
let field = key.split('__')[0];
|
|
85
|
+
let fieldOperator = key.split('__')[1];
|
|
86
|
+
|
|
87
|
+
if (!fieldOperator) {
|
|
88
|
+
fieldOperator = 'eq';
|
|
89
|
+
} else if (!operators[fieldOperator]) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Invalid field operator "${fieldOperator}" for field "${field}". You can only use the following operators after the "${field}" field: __lt, __gt, __lte, __gte, __eq, __neq.`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let operator = operators[fieldOperator];
|
|
96
|
+
if (['null', 'notnull'].includes(fieldOperator)) {
|
|
97
|
+
value = null;
|
|
98
|
+
}
|
|
67
99
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
100
|
+
return { field, operator, value };
|
|
101
|
+
});
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return res.status(400).json({
|
|
104
|
+
message: error.message,
|
|
105
|
+
error: error,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
72
108
|
|
|
73
109
|
let whereString = '';
|
|
74
110
|
if (_filters !== '') {
|
|
75
111
|
whereString += ' WHERE ';
|
|
76
112
|
whereString += filters
|
|
77
|
-
.map((filter) =>
|
|
113
|
+
.map((filter) =>
|
|
114
|
+
filter.value !== null
|
|
115
|
+
? `${tableName}.${filter.field} ${filter.operator} '${filter.value}'`
|
|
116
|
+
: `${tableName}.${filter.field} ${filter.operator}`
|
|
117
|
+
)
|
|
78
118
|
.join(' AND ');
|
|
119
|
+
params = `_filters=${_filters}&`;
|
|
79
120
|
}
|
|
80
121
|
|
|
81
122
|
// if _search is provided, search rows by it
|
|
@@ -95,6 +136,7 @@ const listTableRows = async (req, res) => {
|
|
|
95
136
|
.map((field) => `${tableName}.${field.name} LIKE '%${_search}%'`)
|
|
96
137
|
.join(' OR ');
|
|
97
138
|
whereString += ')';
|
|
139
|
+
params += `_search=${_search}&`;
|
|
98
140
|
} catch (error) {
|
|
99
141
|
return res.status(400).json({
|
|
100
142
|
message: error.message,
|
|
@@ -111,6 +153,7 @@ const listTableRows = async (req, res) => {
|
|
|
111
153
|
orderString += ` ORDER BY ${_ordering.replace('-', '')} ${
|
|
112
154
|
_ordering.startsWith('-') ? 'DESC' : 'ASC'
|
|
113
155
|
}`;
|
|
156
|
+
params += `_ordering=${_ordering}&`;
|
|
114
157
|
}
|
|
115
158
|
|
|
116
159
|
// if _schema is provided, return only those fields
|
|
@@ -123,6 +166,7 @@ const listTableRows = async (req, res) => {
|
|
|
123
166
|
schemaFields.forEach((field) => {
|
|
124
167
|
schemaString += `${tableName}.${field},`;
|
|
125
168
|
});
|
|
169
|
+
params += `_schema=${_schema}&`;
|
|
126
170
|
} else {
|
|
127
171
|
schemaString = `${tableName}.*`;
|
|
128
172
|
}
|
|
@@ -157,7 +201,9 @@ const listTableRows = async (req, res) => {
|
|
|
157
201
|
// }
|
|
158
202
|
// }
|
|
159
203
|
|
|
204
|
+
let foreignKeyError = { error: '', message: '' };
|
|
160
205
|
let extendString = '';
|
|
206
|
+
|
|
161
207
|
if (_extend) {
|
|
162
208
|
const extendFields = _extend.split(',');
|
|
163
209
|
extendFields.forEach((extendedField) => {
|
|
@@ -168,7 +214,9 @@ const listTableRows = async (req, res) => {
|
|
|
168
214
|
.find((fk) => fk.from === extendedField);
|
|
169
215
|
|
|
170
216
|
if (!foreignKey) {
|
|
171
|
-
throw new Error(
|
|
217
|
+
throw new Error(
|
|
218
|
+
`Foreign key not found for extended field '${extendedField}'`
|
|
219
|
+
);
|
|
172
220
|
}
|
|
173
221
|
|
|
174
222
|
const { table: joinedTableName } = foreignKey;
|
|
@@ -198,12 +246,19 @@ const listTableRows = async (req, res) => {
|
|
|
198
246
|
|
|
199
247
|
schemaString += extendFieldsString;
|
|
200
248
|
} catch (error) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
error: error,
|
|
204
|
-
});
|
|
249
|
+
foreignKeyError.error = error;
|
|
250
|
+
foreignKeyError.message = error.message;
|
|
205
251
|
}
|
|
206
252
|
});
|
|
253
|
+
|
|
254
|
+
params += `_extend=${_extend}&`;
|
|
255
|
+
|
|
256
|
+
if (foreignKeyError.error) {
|
|
257
|
+
return res.status(400).json({
|
|
258
|
+
message: foreignKeyError.message,
|
|
259
|
+
error: foreignKeyError.error,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
207
262
|
}
|
|
208
263
|
|
|
209
264
|
// get paginated rows
|
|
@@ -233,8 +288,17 @@ const listTableRows = async (req, res) => {
|
|
|
233
288
|
.get().total;
|
|
234
289
|
|
|
235
290
|
const next =
|
|
236
|
-
data.length === limit
|
|
237
|
-
|
|
291
|
+
data.length === limit
|
|
292
|
+
? `/tables/${tableName}/rows?${params}_limit=${_limit}&_page=${
|
|
293
|
+
page + 1
|
|
294
|
+
}`
|
|
295
|
+
: null;
|
|
296
|
+
const previous =
|
|
297
|
+
page > 1
|
|
298
|
+
? `/tables/${tableName}/rows?${params}_limit=${_limit}&_page=${
|
|
299
|
+
page - 1
|
|
300
|
+
}`
|
|
301
|
+
: null;
|
|
238
302
|
|
|
239
303
|
res.json({
|
|
240
304
|
data,
|
|
@@ -3,6 +3,14 @@ const supertest = require('supertest');
|
|
|
3
3
|
const app = require('../index');
|
|
4
4
|
const requestWithSupertest = supertest(app);
|
|
5
5
|
|
|
6
|
+
function queryString(params) {
|
|
7
|
+
const queryString = Object.keys(params)
|
|
8
|
+
.map((key) => `${key}=${params[key]}`)
|
|
9
|
+
.join('&');
|
|
10
|
+
|
|
11
|
+
return queryString;
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
describe('Rows Endpoints', () => {
|
|
7
15
|
it('GET /tables/:name/rows should return a list of all rows', async () => {
|
|
8
16
|
const res = await requestWithSupertest.get('/api/tables/users/rows');
|
|
@@ -16,6 +24,51 @@ describe('Rows Endpoints', () => {
|
|
|
16
24
|
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
17
25
|
});
|
|
18
26
|
|
|
27
|
+
it('GET /tables/:name/rows?_limit=8&_schema=firstName,lastName&_ordering:-firstName&_page=2: should query the rows by the provided query params', async () => {
|
|
28
|
+
const params = {
|
|
29
|
+
_search: 'a',
|
|
30
|
+
_ordering: '-firstName',
|
|
31
|
+
_schema: 'firstName,lastName',
|
|
32
|
+
_limit: 8,
|
|
33
|
+
_page: 2,
|
|
34
|
+
};
|
|
35
|
+
const query = queryString(params);
|
|
36
|
+
const res = await requestWithSupertest.get(
|
|
37
|
+
`/api/tables/users/rows?${query}`
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(res.status).toEqual(200);
|
|
41
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
42
|
+
expect(res.body).toHaveProperty('data');
|
|
43
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
44
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
45
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
46
|
+
|
|
47
|
+
expect(res.body.next).toEqual(
|
|
48
|
+
`/tables/users/rows?${queryString({
|
|
49
|
+
...params,
|
|
50
|
+
_page: params._page + 1,
|
|
51
|
+
}).toString()}`
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(res.body.previous).toEqual(
|
|
55
|
+
`/tables/users/rows?${queryString({
|
|
56
|
+
...params,
|
|
57
|
+
_page: params._page - 1,
|
|
58
|
+
}).toString()}`
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
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'
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(res.status).toEqual(200);
|
|
68
|
+
expect(res.body.data[0].firstName).toBeNull();
|
|
69
|
+
expect(res.body.data[0].lastName).not.toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
19
72
|
it('POST /tables/:name/rows should insert a new row and return the lastInsertRowid', async () => {
|
|
20
73
|
const res = await requestWithSupertest
|
|
21
74
|
.post('/api/tables/users/rows')
|
package/src/server.js
CHANGED
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const http = require('http');
|
|
4
|
+
const express = require('express');
|
|
4
5
|
|
|
5
6
|
const app = require('./index');
|
|
6
7
|
const { wss } = require('./websocket');
|
|
7
8
|
const config = require('./config/index');
|
|
8
9
|
|
|
10
|
+
if (config.startWithStudio) {
|
|
11
|
+
(async () => {
|
|
12
|
+
const { handler: soulStudioHandler } = await import('soul-studio/build/handler.js');
|
|
13
|
+
app.use('/studio', soulStudioHandler);
|
|
14
|
+
})();
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
const server = http.createServer(app);
|
|
10
18
|
|
|
11
19
|
const port = config.port;
|
|
20
|
+
|
|
21
|
+
const baseURL = `http://localhost:${port}`;
|
|
22
|
+
const coreURL = `${baseURL}/api/`;
|
|
23
|
+
const studioURL = `${baseURL}/studio/`;
|
|
24
|
+
|
|
12
25
|
server.listen(port, () => {
|
|
13
|
-
console.log(`Soul is running
|
|
26
|
+
console.log(`Soul is running...`);
|
|
27
|
+
console.log(` > Core API at ${coreURL}`);
|
|
28
|
+
|
|
29
|
+
if (config.startWithStudio) {
|
|
30
|
+
console.log(` > Studio at ${studioURL}`);
|
|
31
|
+
require('child_process').exec(`open ${studioURL}`);
|
|
32
|
+
}
|
|
14
33
|
});
|
|
15
34
|
|
|
16
35
|
server.on('upgrade', (request, socket, head) => {
|
package/src/swagger/swagger.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.2
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"title": "Soul API",
|
|
6
6
|
"description": "API Documentation for <b>Soul</b>, a SQLite REST and realtime server. "
|
|
7
7
|
},
|
|
@@ -755,4 +755,4 @@
|
|
|
755
755
|
"$ref": "#/definitions/Transaction"
|
|
756
756
|
}
|
|
757
757
|
}
|
|
758
|
-
}
|
|
758
|
+
}
|
package/src/tests/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const fs = require('fs');
|
|
|
2
2
|
const { unlink } = require('fs/promises');
|
|
3
3
|
|
|
4
4
|
const db = require('../db/index');
|
|
5
|
+
const { testNames } = require('./testData');
|
|
5
6
|
|
|
6
7
|
const dropTestTable = (table = 'users') => {
|
|
7
8
|
db.prepare(`DROP TABLE IF EXISTS ${table}`).run();
|
|
@@ -31,10 +32,13 @@ const createTestTable = (table = 'users') => {
|
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
const insertIntoTestTable = (table = 'users') => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
'Doe'
|
|
35
|
+
const statement = db.prepare(
|
|
36
|
+
`INSERT INTO ${table} (firstName, lastName) VALUES (?, ?)`
|
|
37
37
|
);
|
|
38
|
+
|
|
39
|
+
for (const user of testNames) {
|
|
40
|
+
statement.run(user.firstName, user.lastName);
|
|
41
|
+
}
|
|
38
42
|
};
|
|
39
43
|
|
|
40
44
|
module.exports = {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const testNames = [
|
|
2
|
+
{ firstName: 'Emily', lastName: 'William' },
|
|
3
|
+
{ firstName: 'Michael', lastName: 'Lee' },
|
|
4
|
+
{ firstName: 'Sarah', lastName: 'Johnson' },
|
|
5
|
+
{ firstName: 'David', lastName: 'Chen' },
|
|
6
|
+
{ firstName: 'Olivia', lastName: 'William' },
|
|
7
|
+
{ firstName: 'William', lastName: 'Kim' },
|
|
8
|
+
{ firstName: 'Sophia', lastName: 'Singh' },
|
|
9
|
+
{ firstName: 'James', lastName: 'Rodriguez' },
|
|
10
|
+
{ firstName: 'Ava', lastName: 'Patel' },
|
|
11
|
+
{ firstName: 'Benjamin', lastName: 'Garcia' },
|
|
12
|
+
{ firstName: 'Isabella', lastName: 'Nguyen' },
|
|
13
|
+
{ firstName: 'Ethan', lastName: 'Lee' },
|
|
14
|
+
{ firstName: 'Mia', lastName: 'Wilson' },
|
|
15
|
+
{ firstName: 'Alexander', lastName: 'William' },
|
|
16
|
+
{ firstName: 'Charlotte', lastName: 'Hernandez' },
|
|
17
|
+
{ firstName: 'Liam', lastName: 'Gonzalez' },
|
|
18
|
+
{ firstName: 'Emma', lastName: 'Gomez' },
|
|
19
|
+
{ firstName: 'Noah', lastName: 'Perez' },
|
|
20
|
+
{ firstName: 'Avery', lastName: 'Ramirez' },
|
|
21
|
+
{ firstName: 'Jacob', lastName: 'Turner' },
|
|
22
|
+
{ firstName: 'Abigail', lastName: 'Williams' },
|
|
23
|
+
{ firstName: 'Elijah', lastName: 'Hall' },
|
|
24
|
+
{ firstName: 'Mila', lastName: 'Flores' },
|
|
25
|
+
{ firstName: 'Evelyn', lastName: 'Morales' },
|
|
26
|
+
{ firstName: 'Logan', lastName: 'Collins' },
|
|
27
|
+
{ firstName: null, lastName: 'Flores' },
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
module.exports = { testNames };
|