soul-cli 0.0.8 → 0.1.0
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/Chinook_Sqlite.sqlite +0 -0
- package/Chinook_Sqlite.sqlite-shm +0 -0
- package/Chinook_Sqlite.sqlite-wal +0 -0
- package/package.json +3 -2
- package/src/cli.js +1 -1
- package/src/controllers/rows.js +50 -177
- package/src/controllers/rows.test.js +6 -6
- package/src/controllers/tables.js +2 -1
- package/src/middlewares/broadcast.js +18 -0
- package/src/routes/rows.js +10 -16
- package/src/schemas/rows.js +3 -18
- package/src/server.js +14 -1
- package/src/swagger/index.js +1 -1
- package/src/swagger/swagger.json +9 -132
- package/src/websocket.js +70 -0
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "A SQLite
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A SQLite REST and Realtime server",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"soul": "./src/server.js"
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"joi": "^17.6.2",
|
|
35
35
|
"swagger-ui-express": "^4.5.0",
|
|
36
36
|
"winston": "^3.8.2",
|
|
37
|
+
"ws": "^8.10.0",
|
|
37
38
|
"yargs": "^17.6.0"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
package/src/cli.js
CHANGED
package/src/controllers/rows.js
CHANGED
|
@@ -245,7 +245,7 @@ const listTableRows = async (req, res) => {
|
|
|
245
245
|
};
|
|
246
246
|
|
|
247
247
|
// Insert a new row in a table
|
|
248
|
-
const insertRowInTable = async (req, res) => {
|
|
248
|
+
const insertRowInTable = async (req, res, next) => {
|
|
249
249
|
/*
|
|
250
250
|
#swagger.tags = ['Rows']
|
|
251
251
|
#swagger.summary = 'Insert Row'
|
|
@@ -301,6 +301,14 @@ const insertRowInTable = async (req, res) => {
|
|
|
301
301
|
message: 'Row inserted',
|
|
302
302
|
data,
|
|
303
303
|
});
|
|
304
|
+
req.broadcast = {
|
|
305
|
+
type: 'INSERT',
|
|
306
|
+
data: {
|
|
307
|
+
pk: data.lastInsertRowid,
|
|
308
|
+
...fields,
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
next();
|
|
304
312
|
} catch (error) {
|
|
305
313
|
/*
|
|
306
314
|
#swagger.responses[400] = {
|
|
@@ -330,9 +338,9 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
330
338
|
type: 'string'
|
|
331
339
|
}
|
|
332
340
|
|
|
333
|
-
#swagger.parameters['
|
|
341
|
+
#swagger.parameters['pks'] = {
|
|
334
342
|
in: 'path',
|
|
335
|
-
description: 'Primary key',
|
|
343
|
+
description: 'Primary key (comma-separated for bulk retrieve)',
|
|
336
344
|
required: true,
|
|
337
345
|
}
|
|
338
346
|
|
|
@@ -358,7 +366,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
358
366
|
}
|
|
359
367
|
|
|
360
368
|
*/
|
|
361
|
-
const { name: tableName,
|
|
369
|
+
const { name: tableName, pks } = req.params;
|
|
362
370
|
const { _lookup_field, _schema, _extend } = req.query;
|
|
363
371
|
|
|
364
372
|
let lookupField = _lookup_field;
|
|
@@ -471,22 +479,25 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
471
479
|
});
|
|
472
480
|
}
|
|
473
481
|
|
|
474
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField}
|
|
482
|
+
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${pks})`;
|
|
475
483
|
|
|
476
484
|
try {
|
|
477
|
-
let data = db.prepare(query).
|
|
485
|
+
let data = db.prepare(query).all();
|
|
486
|
+
|
|
478
487
|
// parse json extended files
|
|
479
488
|
if (_extend) {
|
|
480
489
|
const extendFields = _extend.split(',');
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
490
|
+
data = data.map((row) => {
|
|
491
|
+
Object.keys(row).forEach((key) => {
|
|
492
|
+
if (extendFields.includes(key.replace('_data', ''))) {
|
|
493
|
+
row[key] = JSON.parse(row[key]);
|
|
494
|
+
}
|
|
486
495
|
});
|
|
496
|
+
return row;
|
|
497
|
+
});
|
|
487
498
|
}
|
|
488
499
|
|
|
489
|
-
if (
|
|
500
|
+
if (data.length === 0) {
|
|
490
501
|
return res.status(404).json({
|
|
491
502
|
message: 'Row not found',
|
|
492
503
|
error: 'not_found',
|
|
@@ -505,7 +516,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
505
516
|
};
|
|
506
517
|
|
|
507
518
|
// Update a row by pk
|
|
508
|
-
const updateRowInTableByPK = async (req, res) => {
|
|
519
|
+
const updateRowInTableByPK = async (req, res, next) => {
|
|
509
520
|
/*
|
|
510
521
|
#swagger.tags = ['Rows']
|
|
511
522
|
#swagger.summary = 'Update Row'
|
|
@@ -516,9 +527,9 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
516
527
|
required: true,
|
|
517
528
|
type: 'string'
|
|
518
529
|
}
|
|
519
|
-
#swagger.parameters['
|
|
530
|
+
#swagger.parameters['pks'] = {
|
|
520
531
|
in: 'path',
|
|
521
|
-
description: 'Primary key',
|
|
532
|
+
description: 'Primary key (comma-separated for bulk update)',
|
|
522
533
|
required: true,
|
|
523
534
|
}
|
|
524
535
|
|
|
@@ -537,7 +548,7 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
537
548
|
}
|
|
538
549
|
*/
|
|
539
550
|
|
|
540
|
-
const { name: tableName,
|
|
551
|
+
const { name: tableName, pks } = req.params;
|
|
541
552
|
const { fields } = req.body;
|
|
542
553
|
const { _lookup_field } = req.query;
|
|
543
554
|
|
|
@@ -576,97 +587,23 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
576
587
|
});
|
|
577
588
|
}
|
|
578
589
|
|
|
579
|
-
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} = '${pk}'`;
|
|
580
|
-
try {
|
|
581
|
-
const data = db.prepare(query).run();
|
|
582
|
-
|
|
583
|
-
res.json({
|
|
584
|
-
message: 'Row updated',
|
|
585
|
-
data,
|
|
586
|
-
});
|
|
587
|
-
} catch (error) {
|
|
588
|
-
res.status(400).json({
|
|
589
|
-
message: error.message,
|
|
590
|
-
error: error,
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
|
|
595
|
-
// Update a row by pk
|
|
596
|
-
const bulkUpdateRowsInTableByPK = async (req, res) => {
|
|
597
|
-
/*
|
|
598
|
-
#swagger.tags = ['Rows']
|
|
599
|
-
#swagger.summary = 'Bulk Update Rows'
|
|
600
|
-
#swagger.description = 'Bulk Update rows by primary key'
|
|
601
|
-
#swagger.parameters['name'] = {
|
|
602
|
-
in: 'path',
|
|
603
|
-
description: 'Table name',
|
|
604
|
-
required: true,
|
|
605
|
-
type: 'string'
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
#swagger.parameters['body'] = {
|
|
609
|
-
in: 'body',
|
|
610
|
-
required: true,
|
|
611
|
-
type: 'object',
|
|
612
|
-
schema: { $ref: "#/definitions/BulkUpdateRowsRequestBody" }
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
#swagger.parameters['_lookup_field'] = {
|
|
616
|
-
in: 'query',
|
|
617
|
-
description: 'If you want to update row by any other field than primary key, use this parameter',
|
|
618
|
-
required: false,
|
|
619
|
-
type: 'string'
|
|
620
|
-
}
|
|
621
|
-
*/
|
|
622
|
-
|
|
623
|
-
const { name: tableName } = req.params;
|
|
624
|
-
const { pks, fields } = req.body;
|
|
625
|
-
const { _lookup_field } = req.query;
|
|
626
|
-
|
|
627
|
-
let lookupField = _lookup_field;
|
|
628
|
-
|
|
629
|
-
if (!_lookup_field) {
|
|
630
|
-
// find the primary key of the table
|
|
631
|
-
try {
|
|
632
|
-
lookupField = db
|
|
633
|
-
.prepare(`PRAGMA table_info(${tableName})`)
|
|
634
|
-
.all()
|
|
635
|
-
.find((field) => field.pk === 1).name;
|
|
636
|
-
} catch (error) {
|
|
637
|
-
return res.status(400).json({
|
|
638
|
-
message: error.message,
|
|
639
|
-
error: error,
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
// wrap text values in quotes
|
|
645
|
-
const fieldsString = Object.keys(fields)
|
|
646
|
-
.map((key) => {
|
|
647
|
-
let value = fields[key];
|
|
648
|
-
if (typeof value === 'string') {
|
|
649
|
-
value = `'${value}'`;
|
|
650
|
-
}
|
|
651
|
-
return `${key} = ${value}`;
|
|
652
|
-
})
|
|
653
|
-
.join(', ');
|
|
654
|
-
|
|
655
|
-
if (fieldsString === '') {
|
|
656
|
-
return res.status(400).json({
|
|
657
|
-
message: 'No fields provided',
|
|
658
|
-
error: 'no_fields_provided',
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
|
|
662
590
|
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${pks})`;
|
|
663
591
|
try {
|
|
664
592
|
const data = db.prepare(query).run();
|
|
665
593
|
|
|
666
594
|
res.json({
|
|
667
|
-
message: '
|
|
595
|
+
message: 'Row updated',
|
|
668
596
|
data,
|
|
669
597
|
});
|
|
598
|
+
req.broadcast = {
|
|
599
|
+
type: 'UPDATE',
|
|
600
|
+
_lookup_field: lookupField,
|
|
601
|
+
data: {
|
|
602
|
+
pks: pks.split(','),
|
|
603
|
+
...fields,
|
|
604
|
+
},
|
|
605
|
+
};
|
|
606
|
+
next();
|
|
670
607
|
} catch (error) {
|
|
671
608
|
res.status(400).json({
|
|
672
609
|
message: error.message,
|
|
@@ -676,7 +613,7 @@ const bulkUpdateRowsInTableByPK = async (req, res) => {
|
|
|
676
613
|
};
|
|
677
614
|
|
|
678
615
|
// Delete a row by id
|
|
679
|
-
const deleteRowInTableByPK = async (req, res) => {
|
|
616
|
+
const deleteRowInTableByPK = async (req, res, next) => {
|
|
680
617
|
/*
|
|
681
618
|
#swagger.tags = ['Rows']
|
|
682
619
|
#swagger.summary = 'Delete Row'
|
|
@@ -687,73 +624,10 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
687
624
|
required: true,
|
|
688
625
|
type: 'string'
|
|
689
626
|
}
|
|
690
|
-
#swagger.parameters['
|
|
691
|
-
in: 'path',
|
|
692
|
-
description: 'Primary key',
|
|
693
|
-
required: true,
|
|
694
|
-
}
|
|
695
|
-
#swagger.parameters['_lookup_field'] = {
|
|
696
|
-
in: 'query',
|
|
697
|
-
description: 'If you want to delete row by any other field than primary key, use this parameter',
|
|
698
|
-
required: false,
|
|
699
|
-
type: 'string'
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
*/
|
|
703
|
-
const { name: tableName, pk } = req.params;
|
|
704
|
-
const { _lookup_field } = req.query;
|
|
705
|
-
|
|
706
|
-
let lookupField = _lookup_field;
|
|
707
|
-
|
|
708
|
-
if (!_lookup_field) {
|
|
709
|
-
// find the primary key of the table
|
|
710
|
-
try {
|
|
711
|
-
lookupField = db
|
|
712
|
-
.prepare(`PRAGMA table_info(${tableName})`)
|
|
713
|
-
.all()
|
|
714
|
-
.find((field) => field.pk === 1).name;
|
|
715
|
-
} catch (error) {
|
|
716
|
-
return res.status(400).json({
|
|
717
|
-
message: error.message,
|
|
718
|
-
error: error,
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
const query = `DELETE FROM ${tableName} WHERE ${lookupField} = '${pk}'`;
|
|
724
|
-
|
|
725
|
-
try {
|
|
726
|
-
const data = db.prepare(query).run();
|
|
727
|
-
|
|
728
|
-
if (data.changes === 0) {
|
|
729
|
-
res.status(404).json({
|
|
730
|
-
error: 'not_found',
|
|
731
|
-
});
|
|
732
|
-
} else {
|
|
733
|
-
res.json({
|
|
734
|
-
message: 'Row deleted',
|
|
735
|
-
data,
|
|
736
|
-
});
|
|
737
|
-
}
|
|
738
|
-
} catch (error) {
|
|
739
|
-
res.status(400).json({
|
|
740
|
-
message: error.message,
|
|
741
|
-
error: error,
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
};
|
|
745
|
-
|
|
746
|
-
// Bulk Delete rows by id
|
|
747
|
-
const bulkDeleteRowsInTableByPK = async (req, res) => {
|
|
748
|
-
/*
|
|
749
|
-
#swagger.tags = ['Rows']
|
|
750
|
-
#swagger.summary = 'Bulk Delete Rows'
|
|
751
|
-
#swagger.description = 'Bulks Delete rows by primary key'
|
|
752
|
-
#swagger.parameters['name'] = {
|
|
627
|
+
#swagger.parameters['pks'] = {
|
|
753
628
|
in: 'path',
|
|
754
|
-
description: '
|
|
629
|
+
description: 'Primary key (comma-separated for bulk delete)',
|
|
755
630
|
required: true,
|
|
756
|
-
type: 'string'
|
|
757
631
|
}
|
|
758
632
|
#swagger.parameters['_lookup_field'] = {
|
|
759
633
|
in: 'query',
|
|
@@ -762,16 +636,9 @@ const bulkDeleteRowsInTableByPK = async (req, res) => {
|
|
|
762
636
|
type: 'string'
|
|
763
637
|
}
|
|
764
638
|
|
|
765
|
-
#swagger.parameters['body'] = {
|
|
766
|
-
in: 'body',
|
|
767
|
-
required: true,
|
|
768
|
-
type: 'object',
|
|
769
|
-
schema: { $ref: "#/definitions/BulkDeleteRowsRequestBody" }
|
|
770
|
-
}
|
|
771
639
|
*/
|
|
772
|
-
const { name: tableName } = req.params;
|
|
640
|
+
const { name: tableName, pks } = req.params;
|
|
773
641
|
const { _lookup_field } = req.query;
|
|
774
|
-
const { pks } = req.body;
|
|
775
642
|
|
|
776
643
|
let lookupField = _lookup_field;
|
|
777
644
|
|
|
@@ -801,9 +668,17 @@ const bulkDeleteRowsInTableByPK = async (req, res) => {
|
|
|
801
668
|
});
|
|
802
669
|
} else {
|
|
803
670
|
res.json({
|
|
804
|
-
message: '
|
|
671
|
+
message: 'Row deleted',
|
|
805
672
|
data,
|
|
806
673
|
});
|
|
674
|
+
req.broadcast = {
|
|
675
|
+
type: 'DELETE',
|
|
676
|
+
_lookup_field: lookupField,
|
|
677
|
+
data: {
|
|
678
|
+
pks: pks.split(','),
|
|
679
|
+
},
|
|
680
|
+
};
|
|
681
|
+
next();
|
|
807
682
|
}
|
|
808
683
|
} catch (error) {
|
|
809
684
|
res.status(400).json({
|
|
@@ -818,7 +693,5 @@ module.exports = {
|
|
|
818
693
|
insertRowInTable,
|
|
819
694
|
getRowInTableByPK,
|
|
820
695
|
updateRowInTableByPK,
|
|
821
|
-
bulkUpdateRowsInTableByPK,
|
|
822
696
|
deleteRowInTableByPK,
|
|
823
|
-
bulkDeleteRowsInTableByPK,
|
|
824
697
|
};
|
|
@@ -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({
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const { websocketSubscribers } = require('../websocket');
|
|
2
|
+
|
|
3
|
+
const broadcast = (req, res, next) => {
|
|
4
|
+
const data = req.broadcast;
|
|
5
|
+
const { name: tableName } = req.params;
|
|
6
|
+
|
|
7
|
+
const subscribers = websocketSubscribers?.get(tableName);
|
|
8
|
+
|
|
9
|
+
if (subscribers) {
|
|
10
|
+
subscribers.forEach(({ ws }) => {
|
|
11
|
+
ws.send(JSON.stringify(data));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
broadcast,
|
|
18
|
+
};
|
package/src/routes/rows.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
|
|
3
3
|
const controllers = require('../controllers/rows');
|
|
4
|
+
const { broadcast } = require('../middlewares/broadcast');
|
|
4
5
|
const { validator } = require('../middlewares/validation');
|
|
5
6
|
const schema = require('../schemas/rows');
|
|
6
7
|
|
|
@@ -14,32 +15,25 @@ router.get(
|
|
|
14
15
|
router.post(
|
|
15
16
|
'/:name/rows',
|
|
16
17
|
validator(schema.insertRowInTable),
|
|
17
|
-
controllers.insertRowInTable
|
|
18
|
+
controllers.insertRowInTable,
|
|
19
|
+
broadcast
|
|
18
20
|
);
|
|
19
21
|
router.get(
|
|
20
|
-
'/:name/rows/:
|
|
22
|
+
'/:name/rows/:pks',
|
|
21
23
|
validator(schema.getRowInTableByPK),
|
|
22
24
|
controllers.getRowInTableByPK
|
|
23
25
|
);
|
|
24
26
|
router.put(
|
|
25
|
-
'/:name/rows
|
|
26
|
-
validator(schema.bulkUpdateRowsInTableByPK),
|
|
27
|
-
controllers.bulkUpdateRowsInTableByPK
|
|
28
|
-
);
|
|
29
|
-
router.put(
|
|
30
|
-
'/:name/rows/:pk',
|
|
27
|
+
'/:name/rows/:pks',
|
|
31
28
|
validator(schema.updateRowInTableByPK),
|
|
32
|
-
controllers.updateRowInTableByPK
|
|
33
|
-
|
|
34
|
-
router.delete(
|
|
35
|
-
'/:name/rows/bulk',
|
|
36
|
-
validator(schema.bulkDeleteRowsInTableByPK),
|
|
37
|
-
controllers.bulkDeleteRowsInTableByPK
|
|
29
|
+
controllers.updateRowInTableByPK,
|
|
30
|
+
broadcast
|
|
38
31
|
);
|
|
39
32
|
router.delete(
|
|
40
|
-
'/:name/rows/:
|
|
33
|
+
'/:name/rows/:pks',
|
|
41
34
|
validator(schema.deleteRowInTableByPK),
|
|
42
|
-
controllers.deleteRowInTableByPK
|
|
35
|
+
controllers.deleteRowInTableByPK,
|
|
36
|
+
broadcast
|
|
43
37
|
);
|
|
44
38
|
|
|
45
39
|
module.exports = router;
|
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,27 +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
|
-
|
|
30
|
-
fields: Joi.object().required(),
|
|
31
|
-
_lookup_field: Joi.string().min(3).max(30),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const bulkUpdateRowsInTableByPK = Joi.object({
|
|
35
|
-
name: Joi.string().min(3).max(30).required(),
|
|
36
|
-
pks: Joi.array().items(Joi.required()).required(),
|
|
29
|
+
pks: Joi.string().required(),
|
|
37
30
|
fields: Joi.object().required(),
|
|
38
31
|
_lookup_field: Joi.string().min(3).max(30),
|
|
39
32
|
});
|
|
40
33
|
|
|
41
34
|
const deleteRowInTableByPK = Joi.object({
|
|
42
35
|
name: Joi.string().min(3).max(30).required(),
|
|
43
|
-
|
|
44
|
-
_lookup_field: Joi.string().min(3).max(30),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const bulkDeleteRowsInTableByPK = Joi.object({
|
|
48
|
-
name: Joi.string().min(3).max(30).required(),
|
|
49
|
-
pks: Joi.array().items(Joi.required()).required(),
|
|
36
|
+
pks: Joi.string().required(),
|
|
50
37
|
_lookup_field: Joi.string().min(3).max(30),
|
|
51
38
|
});
|
|
52
39
|
|
|
@@ -55,7 +42,5 @@ module.exports = {
|
|
|
55
42
|
insertRowInTable,
|
|
56
43
|
getRowInTableByPK,
|
|
57
44
|
updateRowInTableByPK,
|
|
58
|
-
bulkUpdateRowsInTableByPK,
|
|
59
45
|
deleteRowInTableByPK,
|
|
60
|
-
bulkDeleteRowsInTableByPK,
|
|
61
46
|
};
|
package/src/server.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
const http = require('http');
|
|
4
|
+
|
|
3
5
|
const app = require('./index');
|
|
6
|
+
const { wss } = require('./websocket');
|
|
4
7
|
const config = require('./config/index');
|
|
5
8
|
|
|
9
|
+
const server = http.createServer(app);
|
|
10
|
+
|
|
6
11
|
const port = config.port;
|
|
7
|
-
|
|
12
|
+
server.listen(port, () => {
|
|
8
13
|
console.log(`Soul is running on port ${port}...`);
|
|
9
14
|
});
|
|
15
|
+
|
|
16
|
+
server.on('upgrade', (request, socket, head) => {
|
|
17
|
+
wss.handleUpgrade(request, socket, head, (socket) => {
|
|
18
|
+
wss.emit('connection', socket, request);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
module.exports = {};
|
package/src/swagger/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const doc = {
|
|
|
11
11
|
version: version,
|
|
12
12
|
title: 'Soul API',
|
|
13
13
|
description:
|
|
14
|
-
'API Documentation for <b>Soul</b>, a
|
|
14
|
+
'API Documentation for <b>Soul</b>, a SQLite REST and realtime server. ',
|
|
15
15
|
},
|
|
16
16
|
host: `localhost:${config.port}`,
|
|
17
17
|
basePath: '/',
|
package/src/swagger/swagger.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"title": "Soul API",
|
|
6
|
-
"description": "API Documentation for <b>Soul</b>, a simple
|
|
6
|
+
"description": "API Documentation for <b>Soul</b>, a simple REST and realtime server. "
|
|
7
7
|
},
|
|
8
8
|
"host": "localhost:8000",
|
|
9
9
|
"basePath": "/",
|
|
@@ -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",
|
|
@@ -459,129 +459,6 @@
|
|
|
459
459
|
}
|
|
460
460
|
}
|
|
461
461
|
}
|
|
462
|
-
},
|
|
463
|
-
"/api/tables/{name}/rows/bulk": {
|
|
464
|
-
"put": {
|
|
465
|
-
"tags": [
|
|
466
|
-
"Rows"
|
|
467
|
-
],
|
|
468
|
-
"summary": "Bulk Update Rows",
|
|
469
|
-
"description": "Bulk Update rows by primary key",
|
|
470
|
-
"parameters": [
|
|
471
|
-
{
|
|
472
|
-
"name": "name",
|
|
473
|
-
"in": "path",
|
|
474
|
-
"required": true,
|
|
475
|
-
"type": "string",
|
|
476
|
-
"description": "Table name"
|
|
477
|
-
},
|
|
478
|
-
{
|
|
479
|
-
"name": "body",
|
|
480
|
-
"in": "body",
|
|
481
|
-
"required": true,
|
|
482
|
-
"schema": {
|
|
483
|
-
"$ref": "#/definitions/BulkUpdateRowsRequestBody"
|
|
484
|
-
}
|
|
485
|
-
},
|
|
486
|
-
{
|
|
487
|
-
"name": "_lookup_field",
|
|
488
|
-
"in": "query",
|
|
489
|
-
"description": "If you want to update row by any other field than primary key, use this parameter",
|
|
490
|
-
"required": false,
|
|
491
|
-
"type": "string"
|
|
492
|
-
},
|
|
493
|
-
{
|
|
494
|
-
"name": "in",
|
|
495
|
-
"in": "query",
|
|
496
|
-
"type": "string"
|
|
497
|
-
},
|
|
498
|
-
{
|
|
499
|
-
"name": "required",
|
|
500
|
-
"in": "query",
|
|
501
|
-
"type": "string"
|
|
502
|
-
},
|
|
503
|
-
{
|
|
504
|
-
"name": "type",
|
|
505
|
-
"in": "query",
|
|
506
|
-
"type": "string"
|
|
507
|
-
},
|
|
508
|
-
{
|
|
509
|
-
"name": "schema",
|
|
510
|
-
"in": "query",
|
|
511
|
-
"type": "string"
|
|
512
|
-
}
|
|
513
|
-
],
|
|
514
|
-
"responses": {
|
|
515
|
-
"200": {
|
|
516
|
-
"description": "OK"
|
|
517
|
-
},
|
|
518
|
-
"400": {
|
|
519
|
-
"description": "Bad Request"
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
},
|
|
523
|
-
"delete": {
|
|
524
|
-
"tags": [
|
|
525
|
-
"Rows"
|
|
526
|
-
],
|
|
527
|
-
"summary": "Bulk Delete Rows",
|
|
528
|
-
"description": "Bulks Delete rows by primary key",
|
|
529
|
-
"parameters": [
|
|
530
|
-
{
|
|
531
|
-
"name": "name",
|
|
532
|
-
"in": "path",
|
|
533
|
-
"required": true,
|
|
534
|
-
"type": "string",
|
|
535
|
-
"description": "Table name"
|
|
536
|
-
},
|
|
537
|
-
{
|
|
538
|
-
"name": "_lookup_field",
|
|
539
|
-
"in": "query",
|
|
540
|
-
"description": "If you want to delete row by any other field than primary key, use this parameter",
|
|
541
|
-
"required": false,
|
|
542
|
-
"type": "string"
|
|
543
|
-
},
|
|
544
|
-
{
|
|
545
|
-
"name": "body",
|
|
546
|
-
"in": "body",
|
|
547
|
-
"required": true,
|
|
548
|
-
"schema": {
|
|
549
|
-
"$ref": "#/definitions/BulkDeleteRowsRequestBody"
|
|
550
|
-
}
|
|
551
|
-
},
|
|
552
|
-
{
|
|
553
|
-
"name": "in",
|
|
554
|
-
"in": "query",
|
|
555
|
-
"type": "string"
|
|
556
|
-
},
|
|
557
|
-
{
|
|
558
|
-
"name": "required",
|
|
559
|
-
"in": "query",
|
|
560
|
-
"type": "string"
|
|
561
|
-
},
|
|
562
|
-
{
|
|
563
|
-
"name": "type",
|
|
564
|
-
"in": "query",
|
|
565
|
-
"type": "string"
|
|
566
|
-
},
|
|
567
|
-
{
|
|
568
|
-
"name": "schema",
|
|
569
|
-
"in": "query",
|
|
570
|
-
"type": "string"
|
|
571
|
-
}
|
|
572
|
-
],
|
|
573
|
-
"responses": {
|
|
574
|
-
"200": {
|
|
575
|
-
"description": "OK"
|
|
576
|
-
},
|
|
577
|
-
"400": {
|
|
578
|
-
"description": "Bad Request"
|
|
579
|
-
},
|
|
580
|
-
"404": {
|
|
581
|
-
"description": "Not Found"
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
462
|
}
|
|
586
463
|
},
|
|
587
464
|
"definitions": {
|
package/src/websocket.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const { WebSocketServer } = require('ws');
|
|
2
|
+
|
|
3
|
+
const db = require('./db/index');
|
|
4
|
+
|
|
5
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
6
|
+
|
|
7
|
+
const websocketSubscribers = new Map();
|
|
8
|
+
|
|
9
|
+
wss.on('connection', function (ws, request) {
|
|
10
|
+
const [_path, params] = request?.url?.split('?');
|
|
11
|
+
|
|
12
|
+
if (!_path.startsWith('/ws')) {
|
|
13
|
+
ws.close();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const tableName = _path.replace('/ws/tables/', '').replace('/', '');
|
|
18
|
+
|
|
19
|
+
// if table does not exists close the connection
|
|
20
|
+
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`;
|
|
21
|
+
try {
|
|
22
|
+
const table = db.prepare(query).get();
|
|
23
|
+
|
|
24
|
+
if (!table) {
|
|
25
|
+
ws.send(
|
|
26
|
+
JSON.stringify({
|
|
27
|
+
message: `Table ${tableName} does not exist`,
|
|
28
|
+
})
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
ws.close();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
ws.send(
|
|
36
|
+
JSON.stringify({
|
|
37
|
+
message: error.message,
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
ws.close();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!websocketSubscribers.has(tableName)) {
|
|
45
|
+
websocketSubscribers.set(tableName, new Set());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const subscriber = {
|
|
49
|
+
ws,
|
|
50
|
+
params: new URLSearchParams(params),
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
console.log(`New subscriber for table ${tableName}`);
|
|
54
|
+
websocketSubscribers.get(tableName).add(subscriber);
|
|
55
|
+
|
|
56
|
+
ws.send(JSON.stringify({ message: `Subscribed to table "${tableName}"` }));
|
|
57
|
+
|
|
58
|
+
ws.on('message', function (message) {
|
|
59
|
+
console.log('received: %s', message);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
ws.on('close', function () {
|
|
63
|
+
websocketSubscribers.get(tableName).delete(subscriber);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
module.exports = {
|
|
68
|
+
wss,
|
|
69
|
+
websocketSubscribers,
|
|
70
|
+
};
|