soul-cli 0.0.1 → 0.0.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/README.md +1 -3
- package/package.json +22 -6
- package/{soul → src}/cli.js +7 -0
- package/{soul → src}/config/index.js +6 -4
- package/{soul → src}/controllers/index.js +22 -18
- package/src/controllers/index.test.js +43 -0
- package/{soul → src}/controllers/rows.js +163 -3
- package/src/controllers/rows.test.js +51 -0
- package/{soul → src}/controllers/tables.js +98 -24
- package/src/controllers/tables.test.js +62 -0
- package/src/db/index.js +26 -0
- package/{soul → src}/index.js +23 -21
- package/{soul → src}/middlewares/logger.js +0 -0
- package/{soul → src}/middlewares/validation.js +0 -0
- package/{soul → src}/routes/index.js +0 -1
- package/{soul → src}/routes/rows.js +0 -0
- package/{soul → src}/routes/tables.js +1 -1
- package/{soul → src}/schemas/index.js +0 -0
- package/{soul → src}/schemas/rows.js +0 -0
- package/{soul → src}/schemas/tables.js +8 -0
- package/src/server.js +7 -0
- package/{soul → src}/services/.gitkeep +0 -0
- package/src/swagger/index.js +119 -0
- package/src/swagger/swagger.json +705 -0
- package/src/tests/globalSetup.js +11 -0
- package/src/tests/globalTeardown.js +9 -0
- package/src/tests/index.js +45 -0
- package/LICENSE +0 -21
- package/soul/db/index.js +0 -17
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "A SQLite RESTful server",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"soul": "./
|
|
7
|
+
"soul": "./src/server.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"start": "node
|
|
11
|
-
"dev": "NO_CLI=true nodemon
|
|
10
|
+
"start": "node src/server.js",
|
|
11
|
+
"dev": "npm run swagger-autogen && cross-env NO_CLI=true nodemon src/server.js",
|
|
12
|
+
"cli": "nodemon src/server.js --database foobar.db",
|
|
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 PORT=8001 jest --testTimeout=10000"
|
|
12
15
|
},
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
@@ -29,10 +32,23 @@
|
|
|
29
32
|
"express-rate-limit": "^6.6.0",
|
|
30
33
|
"express-winston": "^4.2.0",
|
|
31
34
|
"joi": "^17.6.2",
|
|
35
|
+
"swagger-ui-express": "^4.5.0",
|
|
32
36
|
"winston": "^3.8.2",
|
|
33
37
|
"yargs": "^17.6.0"
|
|
34
38
|
},
|
|
35
39
|
"devDependencies": {
|
|
36
|
-
"
|
|
40
|
+
"cross-env": "^7.0.3",
|
|
41
|
+
"jest": "^29.2.0",
|
|
42
|
+
"nodemon": "^2.0.20",
|
|
43
|
+
"supertest": "^6.3.0",
|
|
44
|
+
"swagger-autogen": "^2.22.0"
|
|
45
|
+
},
|
|
46
|
+
"jest": {
|
|
47
|
+
"testEnvironment": "node",
|
|
48
|
+
"coveragePathIgnorePatterns": [
|
|
49
|
+
"/node_modules/"
|
|
50
|
+
],
|
|
51
|
+
"globalSetup": "./src/tests/globalSetup.js",
|
|
52
|
+
"globalTeardown": "./src/tests/globalTeardown.js"
|
|
37
53
|
}
|
|
38
54
|
}
|
package/{soul → src}/cli.js
RENAMED
|
@@ -27,6 +27,13 @@ if (process.env.NO_CLI !== 'true') {
|
|
|
27
27
|
type: 'boolean',
|
|
28
28
|
demandOption: false,
|
|
29
29
|
})
|
|
30
|
+
.option('V', {
|
|
31
|
+
alias: 'verbose',
|
|
32
|
+
describe: 'Enable verbose logging',
|
|
33
|
+
type: 'string',
|
|
34
|
+
demandOption: false,
|
|
35
|
+
choices: ['console', null],
|
|
36
|
+
})
|
|
30
37
|
.help(true).argv;
|
|
31
38
|
}
|
|
32
39
|
|
|
@@ -8,13 +8,14 @@ dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
|
8
8
|
|
|
9
9
|
const envVarsSchema = Joi.object()
|
|
10
10
|
.keys({
|
|
11
|
-
PORT: Joi.number().positive().
|
|
11
|
+
PORT: Joi.number().positive().default(8000),
|
|
12
12
|
|
|
13
13
|
NODE_ENV: Joi.string()
|
|
14
14
|
.valid('production', 'development', 'test')
|
|
15
15
|
.required(),
|
|
16
16
|
|
|
17
17
|
DB: Joi.string().required(),
|
|
18
|
+
VERBOSE: Joi.string().valid('console', null).default(null),
|
|
18
19
|
|
|
19
20
|
CORS_ORIGIN_WHITELIST: Joi.string().required(),
|
|
20
21
|
|
|
@@ -37,11 +38,12 @@ const { argv } = yargs;
|
|
|
37
38
|
module.exports = {
|
|
38
39
|
env: envVars.NODE_ENV,
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
isProduction: envVars.NODE_ENV === 'production',
|
|
42
|
+
isDevelopment: envVars.NODE_ENV === 'development',
|
|
43
|
+
isTest: envVars.NODE_ENV === 'test',
|
|
43
44
|
|
|
44
45
|
port: argv.port || envVars.PORT,
|
|
46
|
+
verbose: argv['verbose'] || envVars.VERBOSE,
|
|
45
47
|
|
|
46
48
|
db: {
|
|
47
49
|
filename: argv.database || envVars.DB || ':memory:',
|
|
@@ -1,29 +1,23 @@
|
|
|
1
|
-
const e = require('express');
|
|
2
1
|
const db = require('../db/index');
|
|
2
|
+
const version = require('../../package.json').version;
|
|
3
3
|
|
|
4
4
|
// Root endpoint
|
|
5
5
|
const root = async (req, res) => {
|
|
6
|
+
/*
|
|
7
|
+
#swagger.tags = ['Root']
|
|
8
|
+
#swagger.summary = 'Timestamp'
|
|
9
|
+
#swagger.description = 'Endpoint to return server timestamp'
|
|
10
|
+
*/
|
|
11
|
+
|
|
6
12
|
res.json({
|
|
7
13
|
message: 'Soul is running...',
|
|
14
|
+
data: {
|
|
15
|
+
version,
|
|
16
|
+
timestamp: new Date().toISOString(),
|
|
17
|
+
},
|
|
8
18
|
});
|
|
9
19
|
};
|
|
10
20
|
|
|
11
|
-
// Run any query
|
|
12
|
-
const query = async (req, res) => {
|
|
13
|
-
const { query } = req.body;
|
|
14
|
-
try {
|
|
15
|
-
const data = db.prepare(query).all();
|
|
16
|
-
res.json({
|
|
17
|
-
data,
|
|
18
|
-
});
|
|
19
|
-
} catch (error) {
|
|
20
|
-
res.status(400).json({
|
|
21
|
-
message: error.message,
|
|
22
|
-
error: error,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
21
|
// Run any query transactions
|
|
28
22
|
// inspired by https://github.com/proofrock/ws4sqlite
|
|
29
23
|
// e.g. body:
|
|
@@ -56,6 +50,16 @@ const query = async (req, res) => {
|
|
|
56
50
|
//
|
|
57
51
|
|
|
58
52
|
const transaction = async (req, res) => {
|
|
53
|
+
/*
|
|
54
|
+
#swagger.tags = ['Root']
|
|
55
|
+
#swagger.summary = 'Transaction'
|
|
56
|
+
#swagger.description = 'Endpoint to run any transaction, e.g. [{ "query": "" }, { "statement": "", "values": {} }, { "query": "" }]',
|
|
57
|
+
#swagger.parameters['body'] = {
|
|
58
|
+
in: 'body',
|
|
59
|
+
required: true,
|
|
60
|
+
schema: { $ref: "#/definitions/TransactionRequestBody" }
|
|
61
|
+
}
|
|
62
|
+
*/
|
|
59
63
|
const { transaction } = req.body;
|
|
60
64
|
const results = [];
|
|
61
65
|
try {
|
|
@@ -72,6 +76,7 @@ const transaction = async (req, res) => {
|
|
|
72
76
|
}
|
|
73
77
|
});
|
|
74
78
|
})();
|
|
79
|
+
|
|
75
80
|
res.json({
|
|
76
81
|
data: results,
|
|
77
82
|
});
|
|
@@ -85,6 +90,5 @@ const transaction = async (req, res) => {
|
|
|
85
90
|
|
|
86
91
|
module.exports = {
|
|
87
92
|
root,
|
|
88
|
-
query,
|
|
89
93
|
transaction,
|
|
90
94
|
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const supertest = require('supertest');
|
|
2
|
+
|
|
3
|
+
const app = require('../index');
|
|
4
|
+
const requestWithSupertest = supertest(app);
|
|
5
|
+
|
|
6
|
+
describe('Root Endpoints', () => {
|
|
7
|
+
it('GET / should return server version and timestamp', async () => {
|
|
8
|
+
const res = await requestWithSupertest.get('/api');
|
|
9
|
+
expect(res.status).toEqual(200);
|
|
10
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
11
|
+
expect(res.body).toHaveProperty('message');
|
|
12
|
+
expect(res.body).toHaveProperty('data');
|
|
13
|
+
expect(res.body.data).toHaveProperty('version');
|
|
14
|
+
expect(res.body.data).toHaveProperty('timestamp');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('Transaction Endpoint', () => {
|
|
19
|
+
it('POST /transaction should commit transaction and return an array of changes and lastInsertRowid', async () => {
|
|
20
|
+
const res = await requestWithSupertest.post('/api/transaction').send({
|
|
21
|
+
transaction: [
|
|
22
|
+
{
|
|
23
|
+
statement: `CREATE TABLE students (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT)`,
|
|
24
|
+
values: {},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
statement: `INSERT INTO students (id, firstName, lastName) VALUES (:id, :firstName, :lastName)`,
|
|
28
|
+
values: { id: 1, firstName: 'John', lastName: 'Doe' },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
query: `SELECT * FROM students`,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(res.status).toEqual(200);
|
|
37
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
38
|
+
expect(res.body).toHaveProperty('data');
|
|
39
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
40
|
+
expect(res.body.data[0]).toHaveProperty('changes');
|
|
41
|
+
expect(res.body.data[0]).toHaveProperty('lastInsertRowid');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -2,6 +2,39 @@ const db = require('../db/index');
|
|
|
2
2
|
|
|
3
3
|
// Return paginated rows of a table
|
|
4
4
|
const listTableRows = async (req, res) => {
|
|
5
|
+
/*
|
|
6
|
+
#swagger.tags = ['Rows']
|
|
7
|
+
#swagger.summary = 'List Rows'
|
|
8
|
+
#swagger.description = 'Endpoint to list rows of a table.'
|
|
9
|
+
#swagger.parameters['name'] = {
|
|
10
|
+
description: 'Table name.',
|
|
11
|
+
in: 'path',
|
|
12
|
+
}
|
|
13
|
+
#swagger.parameters['_page'] = {
|
|
14
|
+
description: 'Page number.' ,
|
|
15
|
+
in: 'query',
|
|
16
|
+
type: 'number',
|
|
17
|
+
default: 1
|
|
18
|
+
}
|
|
19
|
+
#swagger.parameters['_limit'] = {
|
|
20
|
+
description: 'Number of rows per page.',
|
|
21
|
+
in: 'query',
|
|
22
|
+
type: 'number',
|
|
23
|
+
default: 10
|
|
24
|
+
}
|
|
25
|
+
#swagger.parameters['_ordering'] = {
|
|
26
|
+
description: 'Ordering of rows. e.g. ?_ordering=-age will order rows by age descending.',
|
|
27
|
+
in: 'query',
|
|
28
|
+
}
|
|
29
|
+
#swagger.parameters['_schema'] = {
|
|
30
|
+
description: 'Schema of rows. e.g. ?_schema=name,age will return only name and age fields.',
|
|
31
|
+
in: 'query',
|
|
32
|
+
}
|
|
33
|
+
#swagger.parameters['_extend'] = {
|
|
34
|
+
description: 'Extend rows. e.g. ?_extend=user_id will return user data for each row.',
|
|
35
|
+
in: 'query',
|
|
36
|
+
}
|
|
37
|
+
*/
|
|
5
38
|
const { name } = req.params;
|
|
6
39
|
const {
|
|
7
40
|
_page = 1,
|
|
@@ -156,8 +189,6 @@ const listTableRows = async (req, res) => {
|
|
|
156
189
|
limit * (page - 1)
|
|
157
190
|
}`;
|
|
158
191
|
|
|
159
|
-
console.log({ query });
|
|
160
|
-
|
|
161
192
|
try {
|
|
162
193
|
const data = db.prepare(query).all();
|
|
163
194
|
|
|
@@ -186,6 +217,25 @@ const listTableRows = async (req, res) => {
|
|
|
186
217
|
|
|
187
218
|
// Insert a new row in a table
|
|
188
219
|
const insertRowInTable = async (req, res) => {
|
|
220
|
+
/*
|
|
221
|
+
#swagger.tags = ['Rows']
|
|
222
|
+
#swagger.summary = 'Insert Row'
|
|
223
|
+
#swagger.description = 'Insert a new row in a table'
|
|
224
|
+
#swagger.parameters['name'] = {
|
|
225
|
+
in: 'path',
|
|
226
|
+
description: 'Table name',
|
|
227
|
+
required: true,
|
|
228
|
+
type: 'string'
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#swagger.parameters['body'] = {
|
|
232
|
+
in: 'body',
|
|
233
|
+
required: true,
|
|
234
|
+
type: 'object',
|
|
235
|
+
schema: { $ref: "#/definitions/InsertRowRequestBody" }
|
|
236
|
+
}
|
|
237
|
+
*/
|
|
238
|
+
|
|
189
239
|
const { name } = req.params;
|
|
190
240
|
const { fields } = req.body;
|
|
191
241
|
const fieldsString = Object.keys(fields).join(', ');
|
|
@@ -204,11 +254,27 @@ const insertRowInTable = async (req, res) => {
|
|
|
204
254
|
try {
|
|
205
255
|
const data = db.prepare(query).run();
|
|
206
256
|
|
|
207
|
-
|
|
257
|
+
/*
|
|
258
|
+
#swagger.responses[201] = {
|
|
259
|
+
description: 'Row inserted successfully',
|
|
260
|
+
schema: {
|
|
261
|
+
$ref: "#/definitions/InsertRowSuccessResponse"
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
*/
|
|
265
|
+
res.status(201).json({
|
|
208
266
|
message: 'Row inserted',
|
|
209
267
|
data,
|
|
210
268
|
});
|
|
211
269
|
} catch (error) {
|
|
270
|
+
/*
|
|
271
|
+
#swagger.responses[400] = {
|
|
272
|
+
description: 'Bad request',
|
|
273
|
+
schema: {
|
|
274
|
+
$ref: "#/definitions/InsertRowErrorResponse"
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
*/
|
|
212
278
|
res.status(400).json({
|
|
213
279
|
message: error.message,
|
|
214
280
|
error: error,
|
|
@@ -218,6 +284,45 @@ const insertRowInTable = async (req, res) => {
|
|
|
218
284
|
|
|
219
285
|
// Get a row by pk
|
|
220
286
|
const getRowInTableByPK = async (req, res) => {
|
|
287
|
+
/*
|
|
288
|
+
#swagger.tags = ['Rows']
|
|
289
|
+
#swagger.summary = 'Retrieve Row'
|
|
290
|
+
#swagger.description = 'Retrieve a row by primary key'
|
|
291
|
+
#swagger.parameters['name'] = {
|
|
292
|
+
in: 'path',
|
|
293
|
+
description: 'Table name',
|
|
294
|
+
required: true,
|
|
295
|
+
type: 'string'
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
#swagger.parameters['pk'] = {
|
|
299
|
+
in: 'path',
|
|
300
|
+
description: 'Primary key',
|
|
301
|
+
required: true,
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
#swagger.parameters['_schema'] = {
|
|
305
|
+
in: 'query',
|
|
306
|
+
description: 'Fields to return',
|
|
307
|
+
required: false,
|
|
308
|
+
type: 'string'
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
#swagger.parameters['_extend'] = {
|
|
312
|
+
in: 'query',
|
|
313
|
+
description: 'Foreign keys to extend',
|
|
314
|
+
required: false,
|
|
315
|
+
type: 'string'
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
#swagger.parameters['_field'] = {
|
|
319
|
+
in: 'query',
|
|
320
|
+
description: 'If you want to get field by any other field than primary key, use this parameter',
|
|
321
|
+
required: false,
|
|
322
|
+
type: 'string'
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
*/
|
|
221
326
|
const { name, pk } = req.params;
|
|
222
327
|
const { _field, _schema, _extend } = req.query;
|
|
223
328
|
|
|
@@ -326,6 +431,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
326
431
|
|
|
327
432
|
if (!data) {
|
|
328
433
|
res.status(404).json({
|
|
434
|
+
message: 'Row not found',
|
|
329
435
|
error: 'not_found',
|
|
330
436
|
});
|
|
331
437
|
} else {
|
|
@@ -343,6 +449,37 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
343
449
|
|
|
344
450
|
// Update a row by pk
|
|
345
451
|
const updateRowInTableByPK = async (req, res) => {
|
|
452
|
+
/*
|
|
453
|
+
#swagger.tags = ['Rows']
|
|
454
|
+
#swagger.summary = 'Update Row'
|
|
455
|
+
#swagger.description = 'Update a row by primary key'
|
|
456
|
+
#swagger.parameters['name'] = {
|
|
457
|
+
in: 'path',
|
|
458
|
+
description: 'Table name',
|
|
459
|
+
required: true,
|
|
460
|
+
type: 'string'
|
|
461
|
+
}
|
|
462
|
+
#swagger.parameters['pk'] = {
|
|
463
|
+
in: 'path',
|
|
464
|
+
description: 'Primary key',
|
|
465
|
+
required: true,
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
#swagger.parameters['body'] = {
|
|
469
|
+
in: 'body',
|
|
470
|
+
required: true,
|
|
471
|
+
type: 'object',
|
|
472
|
+
schema: { $ref: "#/definitions/UpdateRowRequestBody" }
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#swagger.parameters['_field'] = {
|
|
476
|
+
in: 'query',
|
|
477
|
+
description: 'If you want to update row by any other field than primary key, use this parameter',
|
|
478
|
+
required: false,
|
|
479
|
+
type: 'string'
|
|
480
|
+
}
|
|
481
|
+
*/
|
|
482
|
+
|
|
346
483
|
const { name, pk } = req.params;
|
|
347
484
|
const { fields } = req.body;
|
|
348
485
|
const { _field } = req.query;
|
|
@@ -385,6 +522,29 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
385
522
|
|
|
386
523
|
// Delete a row by id
|
|
387
524
|
const deleteRowInTableByPK = async (req, res) => {
|
|
525
|
+
/*
|
|
526
|
+
#swagger.tags = ['Rows']
|
|
527
|
+
#swagger.summary = 'Delete Row'
|
|
528
|
+
#swagger.description = 'Delete a row by primary key'
|
|
529
|
+
#swagger.parameters['name'] = {
|
|
530
|
+
in: 'path',
|
|
531
|
+
description: 'Table name',
|
|
532
|
+
required: true,
|
|
533
|
+
type: 'string'
|
|
534
|
+
}
|
|
535
|
+
#swagger.parameters['pk'] = {
|
|
536
|
+
in: 'path',
|
|
537
|
+
description: 'Primary key',
|
|
538
|
+
required: true,
|
|
539
|
+
}
|
|
540
|
+
#swagger.parameters['_field'] = {
|
|
541
|
+
in: 'query',
|
|
542
|
+
description: 'If you want to delete row by any other field than primary key, use this parameter',
|
|
543
|
+
required: false,
|
|
544
|
+
type: 'string'
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
*/
|
|
388
548
|
const { name, pk } = req.params;
|
|
389
549
|
const { _field } = req.query;
|
|
390
550
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const supertest = require('supertest');
|
|
2
|
+
|
|
3
|
+
const app = require('../index');
|
|
4
|
+
const requestWithSupertest = supertest(app);
|
|
5
|
+
|
|
6
|
+
describe('Rows Endpoints', () => {
|
|
7
|
+
it('GET /tables/:name/rows should return a list of all rows', async () => {
|
|
8
|
+
const res = await requestWithSupertest.get('/api/tables/users/rows');
|
|
9
|
+
|
|
10
|
+
expect(res.status).toEqual(200);
|
|
11
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
12
|
+
expect(res.body).toHaveProperty('data');
|
|
13
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
14
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
15
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
16
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('POST /tables/:name/rows should insert a new row and return the lastInsertRowid', async () => {
|
|
20
|
+
const res = await requestWithSupertest
|
|
21
|
+
.post('/api/tables/users/rows')
|
|
22
|
+
.send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
|
|
23
|
+
expect(res.status).toEqual(201);
|
|
24
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
25
|
+
expect(res.body).toHaveProperty('data');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('GET /tables/:name/rows/:pk should return a row by its primary key', async () => {
|
|
29
|
+
const res = await requestWithSupertest.get('/api/tables/users/rows/1');
|
|
30
|
+
expect(res.status).toEqual(200);
|
|
31
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
32
|
+
expect(res.body).toHaveProperty('data');
|
|
33
|
+
expect(res.body.data).toHaveProperty('id');
|
|
34
|
+
expect(res.body.data).toHaveProperty('firstName');
|
|
35
|
+
expect(res.body.data).toHaveProperty('lastName');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('PUT /tables/:name/rows/:pk should update a row by its primary key and return the number of changes', async () => {
|
|
39
|
+
const res = await requestWithSupertest
|
|
40
|
+
.put('/api/tables/users/rows/1')
|
|
41
|
+
.send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
|
|
42
|
+
expect(res.status).toEqual(200);
|
|
43
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('DELETE /tables/:name/rows/:pk should delete a row by its primary key and return the number of changes', async () => {
|
|
47
|
+
const res = await requestWithSupertest.delete('/api/tables/users/rows/1');
|
|
48
|
+
expect(res.status).toEqual(200);
|
|
49
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
const db = require('../db/index');
|
|
2
2
|
|
|
3
3
|
const createTable = async (req, res) => {
|
|
4
|
-
|
|
4
|
+
/*
|
|
5
|
+
#swagger.tags = ['Tables']
|
|
6
|
+
#swagger.summary = 'Create Table'
|
|
7
|
+
#swagger.description = 'Endpoint to create a table'
|
|
8
|
+
#swagger.parameters['body'] = {
|
|
9
|
+
in: 'body',
|
|
10
|
+
required: true,
|
|
11
|
+
type: 'object',
|
|
12
|
+
schema: { $ref: "#/definitions/CreateTableRequestBody" }
|
|
13
|
+
}
|
|
14
|
+
*/
|
|
15
|
+
const {
|
|
16
|
+
name,
|
|
17
|
+
schema,
|
|
18
|
+
autoAddCreatedAt = true,
|
|
19
|
+
autoAddUpdatedAt = true,
|
|
20
|
+
} = req.body;
|
|
5
21
|
|
|
6
22
|
let schemaString = schema
|
|
7
23
|
// support name, type, default, not null, unique, primary key, foreign key, index
|
|
@@ -57,21 +73,51 @@ const createTable = async (req, res) => {
|
|
|
57
73
|
`;
|
|
58
74
|
}
|
|
59
75
|
|
|
60
|
-
// add
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
// add created at and updated at
|
|
77
|
+
if (autoAddCreatedAt) {
|
|
78
|
+
schemaString = `
|
|
79
|
+
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
80
|
+
${schemaString}
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (autoAddUpdatedAt) {
|
|
85
|
+
schemaString = `
|
|
86
|
+
updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
87
|
+
${schemaString}
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
66
90
|
|
|
67
91
|
const query = `CREATE TABLE ${name} (${schemaString})`;
|
|
68
92
|
try {
|
|
69
93
|
db.prepare(query).run();
|
|
70
94
|
|
|
71
|
-
|
|
95
|
+
const generatedSchema = db.prepare(`PRAGMA table_info(${name})`).all();
|
|
96
|
+
|
|
97
|
+
/*
|
|
98
|
+
#swagger.responses[201] = {
|
|
99
|
+
description: 'Table created',
|
|
100
|
+
schema: {
|
|
101
|
+
$ref: "#/definitions/CreateTableSuccessResponse"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
*/
|
|
105
|
+
res.status(201).json({
|
|
72
106
|
message: 'Table created',
|
|
107
|
+
data: {
|
|
108
|
+
name,
|
|
109
|
+
schema: generatedSchema,
|
|
110
|
+
},
|
|
73
111
|
});
|
|
74
112
|
} catch (error) {
|
|
113
|
+
/*
|
|
114
|
+
#swagger.responses[400] = {
|
|
115
|
+
description: 'Bad request',
|
|
116
|
+
schema: {
|
|
117
|
+
$ref: "#/definitions/CreateTableErrorResponse"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
*/
|
|
75
121
|
res.status(400).json({
|
|
76
122
|
message: error.message,
|
|
77
123
|
error: error,
|
|
@@ -81,24 +127,28 @@ const createTable = async (req, res) => {
|
|
|
81
127
|
|
|
82
128
|
// Return all tables
|
|
83
129
|
const listTables = async (req, res) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
130
|
+
/*
|
|
131
|
+
#swagger.tags = ['Tables']
|
|
132
|
+
#swagger.summary = 'List Tables'
|
|
133
|
+
#swagger.description = 'Endpoint to list all tables'
|
|
134
|
+
|
|
135
|
+
#swagger.parameters['_search'] = {
|
|
136
|
+
in: 'query',
|
|
137
|
+
required: false,
|
|
138
|
+
type: 'string',
|
|
139
|
+
description: 'Search term'
|
|
140
|
+
}
|
|
141
|
+
#swagger.parameters['_ordering'] = {
|
|
142
|
+
in: 'query',
|
|
143
|
+
required: false,
|
|
144
|
+
type: 'string',
|
|
145
|
+
description: 'Ordering term'
|
|
146
|
+
}
|
|
147
|
+
*/
|
|
148
|
+
const { _search, _ordering } = req.query;
|
|
92
149
|
|
|
93
150
|
let query = `SELECT name FROM sqlite_master WHERE type = 'table'`;
|
|
94
151
|
|
|
95
|
-
if (Object.keys(filters).length) {
|
|
96
|
-
query += ' AND ';
|
|
97
|
-
query += Object.keys(filters)
|
|
98
|
-
.map((key) => `${key} LIKE '%${filters[key]}%'`)
|
|
99
|
-
.join(' AND ');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
152
|
// if search is provided, search the tables
|
|
103
153
|
// e.g. ?_search=users
|
|
104
154
|
if (_search) {
|
|
@@ -117,7 +167,7 @@ const listTables = async (req, res) => {
|
|
|
117
167
|
const tables = db.prepare(query).all();
|
|
118
168
|
|
|
119
169
|
res.json({
|
|
120
|
-
tables,
|
|
170
|
+
data: tables,
|
|
121
171
|
});
|
|
122
172
|
} catch (error) {
|
|
123
173
|
res.status(400).json({
|
|
@@ -129,6 +179,18 @@ const listTables = async (req, res) => {
|
|
|
129
179
|
|
|
130
180
|
// TODO: Return the schema of a table by name
|
|
131
181
|
const getTableSchema = async (req, res) => {
|
|
182
|
+
/*
|
|
183
|
+
#swagger.tags = ['Tables']
|
|
184
|
+
#swagger.summary = 'Get Table Schema'
|
|
185
|
+
#swagger.description = 'Endpoint to get the schema of a table'
|
|
186
|
+
#swagger.parameters['name'] = {
|
|
187
|
+
in: 'path',
|
|
188
|
+
required: true,
|
|
189
|
+
type: 'string',
|
|
190
|
+
description: 'Name of the table'
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
*/
|
|
132
194
|
const { name } = req.params;
|
|
133
195
|
const query = `PRAGMA table_info(${name})`;
|
|
134
196
|
try {
|
|
@@ -147,6 +209,18 @@ const getTableSchema = async (req, res) => {
|
|
|
147
209
|
|
|
148
210
|
// Delete a table by name
|
|
149
211
|
const deleteTable = async (req, res) => {
|
|
212
|
+
/*
|
|
213
|
+
#swagger.tags = ['Tables']
|
|
214
|
+
#swagger.summary = 'Delete Table'
|
|
215
|
+
#swagger.description = 'Endpoint to delete a table'
|
|
216
|
+
#swagger.parameters['name'] = {
|
|
217
|
+
in: 'path',
|
|
218
|
+
required: true,
|
|
219
|
+
type: 'string',
|
|
220
|
+
description: 'Name of the table'
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
*/
|
|
150
224
|
const { name } = req.params;
|
|
151
225
|
const query = `DROP TABLE ${name}`;
|
|
152
226
|
try {
|