soul-cli 0.0.1 → 0.0.3
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 +1 -1
- package/README.md +1 -3
- package/package.json +22 -6
- package/{soul → src}/cli.js +7 -0
- package/{soul → src}/config/index.js +18 -8
- 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 +9 -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/.env.sample
CHANGED
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.3",
|
|
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
|
|
|
@@ -4,21 +4,24 @@ const path = require('path');
|
|
|
4
4
|
|
|
5
5
|
const { yargs, usage, options } = require('../cli');
|
|
6
6
|
|
|
7
|
+
const { argv } = yargs;
|
|
8
|
+
|
|
7
9
|
dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
8
10
|
|
|
9
11
|
const envVarsSchema = Joi.object()
|
|
10
12
|
.keys({
|
|
11
|
-
PORT: Joi.number().positive().
|
|
13
|
+
PORT: Joi.number().positive().default(8000),
|
|
12
14
|
|
|
13
15
|
NODE_ENV: Joi.string()
|
|
14
16
|
.valid('production', 'development', 'test')
|
|
15
17
|
.required(),
|
|
16
18
|
|
|
17
19
|
DB: Joi.string().required(),
|
|
20
|
+
VERBOSE: Joi.string().valid('console', null).default(null),
|
|
18
21
|
|
|
19
22
|
CORS_ORIGIN_WHITELIST: Joi.string().required(),
|
|
20
23
|
|
|
21
|
-
RATE_LIMIT_ENABLED: Joi.boolean().
|
|
24
|
+
RATE_LIMIT_ENABLED: Joi.boolean().default(false),
|
|
22
25
|
RATE_LIMIT_WINDOW_MS: Joi.number().positive().required(),
|
|
23
26
|
RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().required(),
|
|
24
27
|
})
|
|
@@ -26,22 +29,29 @@ const envVarsSchema = Joi.object()
|
|
|
26
29
|
|
|
27
30
|
const { value: envVars, error } = envVarsSchema
|
|
28
31
|
.prefs({ errors: { label: 'key' } })
|
|
29
|
-
.validate(
|
|
32
|
+
.validate({
|
|
33
|
+
...process.env,
|
|
34
|
+
|
|
35
|
+
// Allow overriding of config with CLI args
|
|
36
|
+
PORT: argv.port,
|
|
37
|
+
VERBOSE: argv['verbose'],
|
|
38
|
+
DB: argv.database,
|
|
39
|
+
RATE_LIMIT_ENABLED: argv['rate-limit-enabled'],
|
|
40
|
+
});
|
|
30
41
|
|
|
31
42
|
if (error) {
|
|
32
43
|
throw new Error(`Config validation error: ${error.message}`);
|
|
33
44
|
}
|
|
34
45
|
|
|
35
|
-
const { argv } = yargs;
|
|
36
|
-
|
|
37
46
|
module.exports = {
|
|
38
47
|
env: envVars.NODE_ENV,
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
isProduction: envVars.NODE_ENV === 'production',
|
|
50
|
+
isDevelopment: envVars.NODE_ENV === 'development',
|
|
51
|
+
isTest: envVars.NODE_ENV === 'test',
|
|
43
52
|
|
|
44
53
|
port: argv.port || envVars.PORT,
|
|
54
|
+
verbose: argv['verbose'] || envVars.VERBOSE,
|
|
45
55
|
|
|
46
56
|
db: {
|
|
47
57
|
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
|
+
});
|