soul-cli 0.0.7 → 0.0.9
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/package.json +1 -1
- package/src/controllers/rows.js +24 -20
- package/src/controllers/rows.test.js +6 -6
- package/src/controllers/tables.js +2 -1
- package/src/middlewares/validation.js +2 -1
- package/src/routes/rows.js +3 -3
- package/src/schemas/rows.js +3 -3
- package/src/swagger/index.js +9 -0
- package/src/swagger/swagger.json +46 -8
package/package.json
CHANGED
package/src/controllers/rows.js
CHANGED
|
@@ -330,9 +330,9 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
330
330
|
type: 'string'
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
-
#swagger.parameters['
|
|
333
|
+
#swagger.parameters['pks'] = {
|
|
334
334
|
in: 'path',
|
|
335
|
-
description: 'Primary key',
|
|
335
|
+
description: 'Primary key (comma-separated for bulk retrieve)',
|
|
336
336
|
required: true,
|
|
337
337
|
}
|
|
338
338
|
|
|
@@ -358,7 +358,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
358
358
|
}
|
|
359
359
|
|
|
360
360
|
*/
|
|
361
|
-
const { name: tableName,
|
|
361
|
+
const { name: tableName, pks } = req.params;
|
|
362
362
|
const { _lookup_field, _schema, _extend } = req.query;
|
|
363
363
|
|
|
364
364
|
let lookupField = _lookup_field;
|
|
@@ -471,22 +471,25 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
471
471
|
});
|
|
472
472
|
}
|
|
473
473
|
|
|
474
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField}
|
|
474
|
+
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${pks})`;
|
|
475
475
|
|
|
476
476
|
try {
|
|
477
|
-
let data = db.prepare(query).
|
|
477
|
+
let data = db.prepare(query).all();
|
|
478
|
+
|
|
478
479
|
// parse json extended files
|
|
479
480
|
if (_extend) {
|
|
480
481
|
const extendFields = _extend.split(',');
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
482
|
+
data = data.map((row) => {
|
|
483
|
+
Object.keys(row).forEach((key) => {
|
|
484
|
+
if (extendFields.includes(key.replace('_data', ''))) {
|
|
485
|
+
row[key] = JSON.parse(row[key]);
|
|
486
|
+
}
|
|
486
487
|
});
|
|
488
|
+
return row;
|
|
489
|
+
});
|
|
487
490
|
}
|
|
488
491
|
|
|
489
|
-
if (
|
|
492
|
+
if (data.length === 0) {
|
|
490
493
|
return res.status(404).json({
|
|
491
494
|
message: 'Row not found',
|
|
492
495
|
error: 'not_found',
|
|
@@ -516,9 +519,9 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
516
519
|
required: true,
|
|
517
520
|
type: 'string'
|
|
518
521
|
}
|
|
519
|
-
#swagger.parameters['
|
|
522
|
+
#swagger.parameters['pks'] = {
|
|
520
523
|
in: 'path',
|
|
521
|
-
description: 'Primary key',
|
|
524
|
+
description: 'Primary key (comma-separated for bulk update)',
|
|
522
525
|
required: true,
|
|
523
526
|
}
|
|
524
527
|
|
|
@@ -537,7 +540,7 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
537
540
|
}
|
|
538
541
|
*/
|
|
539
542
|
|
|
540
|
-
const { name: tableName,
|
|
543
|
+
const { name: tableName, pks } = req.params;
|
|
541
544
|
const { fields } = req.body;
|
|
542
545
|
const { _lookup_field } = req.query;
|
|
543
546
|
|
|
@@ -576,12 +579,13 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
576
579
|
});
|
|
577
580
|
}
|
|
578
581
|
|
|
579
|
-
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField}
|
|
582
|
+
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${pks})`;
|
|
580
583
|
try {
|
|
581
|
-
db.prepare(query).run();
|
|
584
|
+
const data = db.prepare(query).run();
|
|
582
585
|
|
|
583
586
|
res.json({
|
|
584
587
|
message: 'Row updated',
|
|
588
|
+
data,
|
|
585
589
|
});
|
|
586
590
|
} catch (error) {
|
|
587
591
|
res.status(400).json({
|
|
@@ -603,9 +607,9 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
603
607
|
required: true,
|
|
604
608
|
type: 'string'
|
|
605
609
|
}
|
|
606
|
-
#swagger.parameters['
|
|
610
|
+
#swagger.parameters['pks'] = {
|
|
607
611
|
in: 'path',
|
|
608
|
-
description: 'Primary key',
|
|
612
|
+
description: 'Primary key (comma-separated for bulk delete)',
|
|
609
613
|
required: true,
|
|
610
614
|
}
|
|
611
615
|
#swagger.parameters['_lookup_field'] = {
|
|
@@ -616,7 +620,7 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
616
620
|
}
|
|
617
621
|
|
|
618
622
|
*/
|
|
619
|
-
const { name: tableName,
|
|
623
|
+
const { name: tableName, pks } = req.params;
|
|
620
624
|
const { _lookup_field } = req.query;
|
|
621
625
|
|
|
622
626
|
let lookupField = _lookup_field;
|
|
@@ -636,7 +640,7 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
636
640
|
}
|
|
637
641
|
}
|
|
638
642
|
|
|
639
|
-
const query = `DELETE FROM ${tableName} WHERE ${lookupField}
|
|
643
|
+
const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${pks})`;
|
|
640
644
|
|
|
641
645
|
try {
|
|
642
646
|
const data = db.prepare(query).run();
|
|
@@ -25,17 +25,17 @@ describe('Rows Endpoints', () => {
|
|
|
25
25
|
expect(res.body).toHaveProperty('data');
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
it('GET /tables/:name/rows/:
|
|
28
|
+
it('GET /tables/:name/rows/:pks should return a row by its primary key', async () => {
|
|
29
29
|
const res = await requestWithSupertest.get('/api/tables/users/rows/1');
|
|
30
30
|
expect(res.status).toEqual(200);
|
|
31
31
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
32
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');
|
|
33
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
34
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
35
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
-
it('PUT /tables/:name/rows/:
|
|
38
|
+
it('PUT /tables/:name/rows/:pks should update a row by its primary key and return the number of changes', async () => {
|
|
39
39
|
const res = await requestWithSupertest
|
|
40
40
|
.put('/api/tables/users/rows/1')
|
|
41
41
|
.send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
|
|
@@ -43,7 +43,7 @@ describe('Rows Endpoints', () => {
|
|
|
43
43
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
it('DELETE /tables/:name/rows/:
|
|
46
|
+
it('DELETE /tables/:name/rows/:pks should delete a row by its primary key and return the number of changes', async () => {
|
|
47
47
|
const res = await requestWithSupertest.delete('/api/tables/users/rows/1');
|
|
48
48
|
expect(res.status).toEqual(200);
|
|
49
49
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
@@ -239,10 +239,11 @@ const deleteTable = async (req, res) => {
|
|
|
239
239
|
const { name: tableName } = req.params;
|
|
240
240
|
const query = `DROP TABLE ${tableName}`;
|
|
241
241
|
try {
|
|
242
|
-
db.prepare(query).run();
|
|
242
|
+
const data = db.prepare(query).run();
|
|
243
243
|
|
|
244
244
|
res.json({
|
|
245
245
|
message: 'Table deleted',
|
|
246
|
+
data,
|
|
246
247
|
});
|
|
247
248
|
} catch (error) {
|
|
248
249
|
res.status(400).json({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const validator = (schema) => (req, res, next) => {
|
|
2
2
|
const data = { ...req.body, ...req.params, ...req.query };
|
|
3
|
-
const { error } = schema.validate(data);
|
|
3
|
+
const { value, error } = schema.validate(data);
|
|
4
|
+
|
|
4
5
|
if (error) {
|
|
5
6
|
res.status(400).json({
|
|
6
7
|
message: error.message,
|
package/src/routes/rows.js
CHANGED
|
@@ -17,17 +17,17 @@ router.post(
|
|
|
17
17
|
controllers.insertRowInTable
|
|
18
18
|
);
|
|
19
19
|
router.get(
|
|
20
|
-
'/:name/rows/:
|
|
20
|
+
'/:name/rows/:pks',
|
|
21
21
|
validator(schema.getRowInTableByPK),
|
|
22
22
|
controllers.getRowInTableByPK
|
|
23
23
|
);
|
|
24
24
|
router.put(
|
|
25
|
-
'/:name/rows/:
|
|
25
|
+
'/:name/rows/:pks',
|
|
26
26
|
validator(schema.updateRowInTableByPK),
|
|
27
27
|
controllers.updateRowInTableByPK
|
|
28
28
|
);
|
|
29
29
|
router.delete(
|
|
30
|
-
'/:name/rows/:
|
|
30
|
+
'/:name/rows/:pks',
|
|
31
31
|
validator(schema.deleteRowInTableByPK),
|
|
32
32
|
controllers.deleteRowInTableByPK
|
|
33
33
|
);
|
package/src/schemas/rows.js
CHANGED
|
@@ -18,7 +18,7 @@ const insertRowInTable = Joi.object({
|
|
|
18
18
|
|
|
19
19
|
const getRowInTableByPK = Joi.object({
|
|
20
20
|
name: Joi.string().min(3).max(30).required(),
|
|
21
|
-
|
|
21
|
+
pks: Joi.string().required(),
|
|
22
22
|
_lookup_field: Joi.string().min(3).max(30),
|
|
23
23
|
_schema: Joi.string(),
|
|
24
24
|
_extend: Joi.string(),
|
|
@@ -26,14 +26,14 @@ const getRowInTableByPK = Joi.object({
|
|
|
26
26
|
|
|
27
27
|
const updateRowInTableByPK = Joi.object({
|
|
28
28
|
name: Joi.string().min(3).max(30).required(),
|
|
29
|
-
|
|
29
|
+
pks: Joi.string().required(),
|
|
30
30
|
fields: Joi.object().required(),
|
|
31
31
|
_lookup_field: Joi.string().min(3).max(30),
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
const deleteRowInTableByPK = Joi.object({
|
|
35
35
|
name: Joi.string().min(3).max(30).required(),
|
|
36
|
-
|
|
36
|
+
pks: Joi.string().required(),
|
|
37
37
|
_lookup_field: Joi.string().min(3).max(30),
|
|
38
38
|
});
|
|
39
39
|
|
package/src/swagger/index.js
CHANGED
|
@@ -110,6 +110,15 @@ const doc = {
|
|
|
110
110
|
fields: [{ $ref: '#/definitions/Field' }],
|
|
111
111
|
},
|
|
112
112
|
|
|
113
|
+
BulkUpdateRowsRequestBody: {
|
|
114
|
+
pks: [1, 2, 3],
|
|
115
|
+
fields: [{ $ref: '#/definitions/Field' }],
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
BulkDeleteRowsRequestBody: {
|
|
119
|
+
pks: [1, 2, 3],
|
|
120
|
+
},
|
|
121
|
+
|
|
113
122
|
TransactionRequestBody: {
|
|
114
123
|
$ref: '#/definitions/Transaction',
|
|
115
124
|
},
|
package/src/swagger/swagger.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.9",
|
|
5
5
|
"title": "Soul API",
|
|
6
6
|
"description": "API Documentation for <b>Soul</b>, a simple SQLite RESTful server. "
|
|
7
7
|
},
|
|
@@ -296,7 +296,7 @@
|
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
298
|
},
|
|
299
|
-
"/api/tables/{name}/rows/{
|
|
299
|
+
"/api/tables/{name}/rows/{pks}": {
|
|
300
300
|
"get": {
|
|
301
301
|
"tags": [
|
|
302
302
|
"Rows"
|
|
@@ -312,11 +312,11 @@
|
|
|
312
312
|
"description": "Table name"
|
|
313
313
|
},
|
|
314
314
|
{
|
|
315
|
-
"name": "
|
|
315
|
+
"name": "pks",
|
|
316
316
|
"in": "path",
|
|
317
317
|
"required": true,
|
|
318
318
|
"type": "string",
|
|
319
|
-
"description": "Primary key"
|
|
319
|
+
"description": "Primary key (comma-separated for bulk retrieve)"
|
|
320
320
|
},
|
|
321
321
|
{
|
|
322
322
|
"name": "_schema",
|
|
@@ -367,11 +367,11 @@
|
|
|
367
367
|
"description": "Table name"
|
|
368
368
|
},
|
|
369
369
|
{
|
|
370
|
-
"name": "
|
|
370
|
+
"name": "pks",
|
|
371
371
|
"in": "path",
|
|
372
372
|
"required": true,
|
|
373
373
|
"type": "string",
|
|
374
|
-
"description": "Primary key"
|
|
374
|
+
"description": "Primary key (comma-separated for bulk update)"
|
|
375
375
|
},
|
|
376
376
|
{
|
|
377
377
|
"name": "body",
|
|
@@ -433,11 +433,11 @@
|
|
|
433
433
|
"description": "Table name"
|
|
434
434
|
},
|
|
435
435
|
{
|
|
436
|
-
"name": "
|
|
436
|
+
"name": "pks",
|
|
437
437
|
"in": "path",
|
|
438
438
|
"required": true,
|
|
439
439
|
"type": "string",
|
|
440
|
-
"description": "Primary key"
|
|
440
|
+
"description": "Primary key (comma-separated for bulk delete)"
|
|
441
441
|
},
|
|
442
442
|
{
|
|
443
443
|
"name": "_lookup_field",
|
|
@@ -702,6 +702,44 @@
|
|
|
702
702
|
}
|
|
703
703
|
}
|
|
704
704
|
},
|
|
705
|
+
"BulkUpdateRowsRequestBody": {
|
|
706
|
+
"type": "object",
|
|
707
|
+
"properties": {
|
|
708
|
+
"pks": {
|
|
709
|
+
"type": "array",
|
|
710
|
+
"example": [
|
|
711
|
+
1,
|
|
712
|
+
2,
|
|
713
|
+
3
|
|
714
|
+
],
|
|
715
|
+
"items": {
|
|
716
|
+
"type": "number"
|
|
717
|
+
}
|
|
718
|
+
},
|
|
719
|
+
"fields": {
|
|
720
|
+
"type": "array",
|
|
721
|
+
"items": {
|
|
722
|
+
"$ref": "#/definitions/Field"
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
},
|
|
727
|
+
"BulkDeleteRowsRequestBody": {
|
|
728
|
+
"type": "object",
|
|
729
|
+
"properties": {
|
|
730
|
+
"pks": {
|
|
731
|
+
"type": "array",
|
|
732
|
+
"example": [
|
|
733
|
+
1,
|
|
734
|
+
2,
|
|
735
|
+
3
|
|
736
|
+
],
|
|
737
|
+
"items": {
|
|
738
|
+
"type": "number"
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
},
|
|
705
743
|
"TransactionRequestBody": {
|
|
706
744
|
"$ref": "#/definitions/Transaction"
|
|
707
745
|
}
|