soul-cli 0.0.9 → 0.2.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
File without changes
@@ -0,0 +1,63 @@
1
+ const hello = {
2
+ method: 'GET',
3
+ path: '/api/hello-soul',
4
+ handler: (req, res, db) => {
5
+ res.status(200).json({
6
+ message: 'Hello Soul!',
7
+ });
8
+ },
9
+ };
10
+
11
+ const timestamp = {
12
+ method: 'GET',
13
+ path: '/api/timestamp',
14
+ handler: (req, res, db) => {
15
+ res.status(200).json({
16
+ timestamp: Date.now(),
17
+ });
18
+ },
19
+ };
20
+
21
+ const greetings = {
22
+ method: 'POST',
23
+ path: '/api/greetings/:name',
24
+ handler: (req, res, db) => {
25
+ const { name } = req.params;
26
+ const { greeting } = req.body;
27
+ res.status(200).json({
28
+ message: `${greeting} ${name}!`,
29
+ });
30
+ },
31
+ };
32
+
33
+ const searchTables = {
34
+ method: 'GET',
35
+ path: '/api/search-tables',
36
+ handler: (req, res, db) => {
37
+ const { q } = req.query;
38
+ const sql = `
39
+ SELECT name FROM sqlite_master
40
+ WHERE type='table'
41
+ AND name LIKE $searchQuery
42
+ `;
43
+ try {
44
+ const tables = db.prepare(sql).all({
45
+ searchQuery: `%${q}%`,
46
+ });
47
+ res.status(200).json({
48
+ tables,
49
+ });
50
+ } catch (error) {
51
+ res.status(500).json({
52
+ error: error.message,
53
+ });
54
+ }
55
+ },
56
+ };
57
+
58
+ module.exports = {
59
+ hello,
60
+ timestamp,
61
+ greetings,
62
+ searchTables,
63
+ };
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.2.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
 
@@ -34,6 +34,12 @@ if (process.env.NO_CLI !== 'true') {
34
34
  demandOption: false,
35
35
  choices: ['console', null],
36
36
  })
37
+ .options('e', {
38
+ alias: 'extensions',
39
+ describe: 'Extensions directory path to load',
40
+ type: 'string',
41
+ demandOption: false,
42
+ })
37
43
  .help(true).argv;
38
44
  }
39
45
 
@@ -24,6 +24,8 @@ const envVarsSchema = Joi.object()
24
24
  RATE_LIMIT_ENABLED: Joi.boolean().default(false),
25
25
  RATE_LIMIT_WINDOW_MS: Joi.number().positive().default(1000),
26
26
  RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().default(10),
27
+
28
+ EXTENSIONS: Joi.string().default(null),
27
29
  })
28
30
  .unknown();
29
31
 
@@ -76,4 +78,8 @@ module.exports = {
76
78
  windowMs: envVars.RATE_LIMIT_WINDOW,
77
79
  max: envVars.RATE_LIMIT_MAX,
78
80
  },
81
+
82
+ extensions: {
83
+ path: argv.extensions || envVars.EXTENSIONS,
84
+ },
79
85
  };
@@ -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,47 @@
1
+ const fs = require('fs');
2
+
3
+ const { extensions: extensionsConfig } = require('./config');
4
+
5
+ const { path: extensionsPath } = extensionsConfig;
6
+
7
+ const setupExtensions = async (app, db) => {
8
+ if (extensionsPath) {
9
+ const extensions = fs.readdirSync(extensionsPath);
10
+ extensions.forEach((extension) => {
11
+ if (extension === 'api.js') {
12
+ const apiExtensions = require(`${extensionsPath}/${extension}`);
13
+
14
+ console.log('API extensions loaded');
15
+
16
+ Object.keys(apiExtensions).forEach((key) => {
17
+ const api = apiExtensions[key];
18
+ switch (api.method) {
19
+ case 'GET':
20
+ app.get(api.path, (req, res) => api.handler(req, res, db));
21
+ break;
22
+ case 'POST':
23
+ app.post(api.path, api.handler);
24
+ break;
25
+ case 'PUT':
26
+ app.put(api.path, api.handler);
27
+ break;
28
+ case 'DELETE':
29
+ app.delete(api.path, api.handler);
30
+ break;
31
+
32
+ default:
33
+ break;
34
+ }
35
+ console.log(' >', api.path);
36
+ });
37
+ console.log('\n');
38
+ }
39
+ });
40
+ } else {
41
+ console.log('No extensions directory provided');
42
+ }
43
+ };
44
+
45
+ module.exports = {
46
+ setupExtensions,
47
+ };
package/src/index.js CHANGED
@@ -14,9 +14,14 @@ const rootRoutes = require('./routes/index');
14
14
  const tablesRoutes = require('./routes/tables');
15
15
  const rowsRoutes = require('./routes/rows');
16
16
  const swaggerFile = require('./swagger/swagger.json');
17
+ const { setupExtensions } = require('./extensions');
17
18
 
18
19
  const app = express();
19
20
 
21
+ app.get('/health', (req, res) => {
22
+ res.send('OK');
23
+ });
24
+
20
25
  app.use(bodyParser.json());
21
26
 
22
27
  // Activate wal mode
@@ -63,4 +68,6 @@ app.use('/api', rootRoutes);
63
68
  app.use('/api/tables', tablesRoutes);
64
69
  app.use('/api/tables', rowsRoutes);
65
70
 
71
+ setupExtensions(app, db);
72
+
66
73
  module.exports = app;
@@ -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.2.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 SQLite REST and realtime server. "
7
7
  },
8
8
  "host": "localhost:8000",
9
9
  "basePath": "/",
@@ -33,6 +33,17 @@
33
33
  "application/json"
34
34
  ],
35
35
  "paths": {
36
+ "/health": {
37
+ "get": {
38
+ "description": "",
39
+ "parameters": [],
40
+ "responses": {
41
+ "200": {
42
+ "description": "OK"
43
+ }
44
+ }
45
+ }
46
+ },
36
47
  "/api/": {
37
48
  "get": {
38
49
  "tags": [
@@ -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
+ };