soul-cli 0.1.0 → 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
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "soul-cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "A SQLite REST and Realtime server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -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
  };
@@ -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;
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "swagger": "2.0",
3
3
  "info": {
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "title": "Soul API",
6
- "description": "API Documentation for <b>Soul</b>, a simple REST and realtime 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": [