soul-cli 0.0.9 → 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.
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.9",
4
- "description": "A SQLite RESTful server",
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
@@ -1,7 +1,7 @@
1
1
  const yargs = require('yargs');
2
2
 
3
3
  const usage = `
4
- Soul | RESTful server for SQLite
4
+ Soul | REST and realtime server for SQLite
5
5
  Usage: soul [options]
6
6
  `;
7
7
 
@@ -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] = {
@@ -508,7 +516,7 @@ const getRowInTableByPK = async (req, res) => {
508
516
  };
509
517
 
510
518
  // Update a row by pk
511
- const updateRowInTableByPK = async (req, res) => {
519
+ const updateRowInTableByPK = async (req, res, next) => {
512
520
  /*
513
521
  #swagger.tags = ['Rows']
514
522
  #swagger.summary = 'Update Row'
@@ -587,6 +595,15 @@ const updateRowInTableByPK = async (req, res) => {
587
595
  message: 'Row updated',
588
596
  data,
589
597
  });
598
+ req.broadcast = {
599
+ type: 'UPDATE',
600
+ _lookup_field: lookupField,
601
+ data: {
602
+ pks: pks.split(','),
603
+ ...fields,
604
+ },
605
+ };
606
+ next();
590
607
  } catch (error) {
591
608
  res.status(400).json({
592
609
  message: error.message,
@@ -596,7 +613,7 @@ const updateRowInTableByPK = async (req, res) => {
596
613
  };
597
614
 
598
615
  // Delete a row by id
599
- const deleteRowInTableByPK = async (req, res) => {
616
+ const deleteRowInTableByPK = async (req, res, next) => {
600
617
  /*
601
618
  #swagger.tags = ['Rows']
602
619
  #swagger.summary = 'Delete Row'
@@ -654,6 +671,14 @@ const deleteRowInTableByPK = async (req, res) => {
654
671
  message: 'Row deleted',
655
672
  data,
656
673
  });
674
+ req.broadcast = {
675
+ type: 'DELETE',
676
+ _lookup_field: lookupField,
677
+ data: {
678
+ pks: pks.split(','),
679
+ },
680
+ };
681
+ next();
657
682
  }
658
683
  } catch (error) {
659
684
  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
+ };
@@ -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,7 +15,8 @@ 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
22
  '/:name/rows/:pks',
@@ -24,12 +26,14 @@ router.get(
24
26
  router.put(
25
27
  '/:name/rows/:pks',
26
28
  validator(schema.updateRowInTableByPK),
27
- controllers.updateRowInTableByPK
29
+ controllers.updateRowInTableByPK,
30
+ broadcast
28
31
  );
29
32
  router.delete(
30
33
  '/:name/rows/:pks',
31
34
  validator(schema.deleteRowInTableByPK),
32
- controllers.deleteRowInTableByPK
35
+ controllers.deleteRowInTableByPK,
36
+ broadcast
33
37
  );
34
38
 
35
39
  module.exports = router;
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
- app.listen(port, () => {
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 = {};
@@ -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 simple SQLite RESTful server. ',
14
+ 'API Documentation for <b>Soul</b>, a SQLite REST and realtime server. ',
15
15
  },
16
16
  host: `localhost:${config.port}`,
17
17
  basePath: '/',
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "swagger": "2.0",
3
3
  "info": {
4
- "version": "0.0.9",
4
+ "version": "0.1.0",
5
5
  "title": "Soul API",
6
- "description": "API Documentation for <b>Soul</b>, a simple SQLite RESTful server. "
6
+ "description": "API Documentation for <b>Soul</b>, a simple REST and realtime server. "
7
7
  },
8
8
  "host": "localhost:8000",
9
9
  "basePath": "/",
@@ -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
+ };