soul-cli 0.0.1 → 0.0.2

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.
@@ -0,0 +1,62 @@
1
+ const supertest = require('supertest');
2
+
3
+ const app = require('../index');
4
+ const requestWithSupertest = supertest(app);
5
+
6
+ describe('Tables Endpoints', () => {
7
+ it('GET /tables should return a list of all tables', async () => {
8
+ const res = await requestWithSupertest.get('/api/tables');
9
+ expect(res.status).toEqual(200);
10
+ expect(res.type).toEqual(expect.stringContaining('json'));
11
+ expect(res.body).toHaveProperty('data');
12
+ expect(res.body.data).toEqual(expect.any(Array));
13
+ expect(res.body.data[0]).toHaveProperty('name');
14
+ });
15
+
16
+ it('POST /tables should create a new table and return generated schema', async () => {
17
+ const res = await requestWithSupertest.post('/api/tables').send({
18
+ name: 'pets',
19
+ autoAddCreatedAt: true,
20
+ autoAddUpdatedAt: false,
21
+ schema: [
22
+ {
23
+ name: 'owner',
24
+ type: 'INTEGER',
25
+ foreignKey: {
26
+ table: 'users',
27
+ column: 'id',
28
+ onDelete: 'CASCADE',
29
+ onUpdate: 'CASCADE',
30
+ },
31
+ },
32
+ {
33
+ name: 'name',
34
+ type: 'TEXT',
35
+ notNull: true,
36
+ },
37
+ {
38
+ name: 'petId',
39
+ unique: true,
40
+ type: 'INTEGER',
41
+ },
42
+ ],
43
+ });
44
+
45
+ expect(res.status).toEqual(201);
46
+ expect(res.type).toEqual(expect.stringContaining('json'));
47
+ expect(res.body).toHaveProperty('data');
48
+ expect(res.body.data).toHaveProperty('name');
49
+ expect(res.body.data).toHaveProperty('schema');
50
+ expect(res.body.data.schema).toEqual(expect.any(Array));
51
+ expect(res.body.data.schema[0]).toHaveProperty('name');
52
+ expect(res.body.data.schema[0]).toHaveProperty('cid');
53
+ });
54
+
55
+ it('GET /tables/:name should return schema of the table', async () => {
56
+ const res = await requestWithSupertest.get('/api/tables/users');
57
+ expect(res.status).toEqual(200);
58
+ expect(res.type).toEqual(expect.stringContaining('json'));
59
+ expect(res.body).toHaveProperty('data');
60
+ expect(res.body.data).toEqual(expect.any(Array));
61
+ });
62
+ });
@@ -0,0 +1,26 @@
1
+ const BS3Database = require('better-sqlite3');
2
+
3
+ const config = require('../config/index');
4
+
5
+ class Database {
6
+ constructor(filename, options) {
7
+ this.db = new BS3Database(filename, {
8
+ verbose: this.getVerbose(),
9
+ ...options,
10
+ });
11
+ }
12
+
13
+ getVerbose() {
14
+ if (config.verbose === 'console') {
15
+ return console.log;
16
+ } else if (config.verbose === null) {
17
+ return null;
18
+ }
19
+ }
20
+
21
+ get() {
22
+ return this.db;
23
+ }
24
+ }
25
+
26
+ module.exports = new Database(config.db.filename).get();
@@ -6,13 +6,14 @@ const winston = require('winston');
6
6
  const expressWinston = require('express-winston');
7
7
  const cors = require('cors');
8
8
  const rateLimit = require('express-rate-limit');
9
- const dotenv = require('dotenv');
9
+ const swaggerUi = require('swagger-ui-express');
10
10
 
11
11
  const config = require('./config/index');
12
12
  const db = require('./db/index');
13
13
  const rootRoutes = require('./routes/index');
14
14
  const tablesRoutes = require('./routes/tables');
15
15
  const rowsRoutes = require('./routes/rows');
16
+ const swaggerFile = require('./swagger/swagger.json');
16
17
 
17
18
  const app = express();
18
19
 
@@ -28,19 +29,22 @@ const corsOptions = {
28
29
  app.use(cors(corsOptions));
29
30
 
30
31
  // Log requests
31
- app.use(
32
- expressWinston.logger({
33
- transports: [new winston.transports.Console()],
34
- format: winston.format.combine(
35
- winston.format.colorize(),
36
- winston.format.json()
37
- ),
38
- meta: false,
39
- msg: 'HTTP {{req.method}} {{req.url}}',
40
- expressFormat: true,
41
- colorize: false,
42
- })
43
- );
32
+
33
+ if (config.verbose !== null) {
34
+ app.use(
35
+ expressWinston.logger({
36
+ transports: [new winston.transports.Console()],
37
+ format: winston.format.combine(
38
+ winston.format.colorize(),
39
+ winston.format.json()
40
+ ),
41
+ meta: false,
42
+ msg: 'HTTP {{req.method}} {{req.url}}',
43
+ expressFormat: true,
44
+ colorize: false,
45
+ })
46
+ );
47
+ }
44
48
 
45
49
  if (config.rateLimit.enabled) {
46
50
  const limiter = rateLimit({
@@ -54,11 +58,9 @@ if (config.rateLimit.enabled) {
54
58
  app.use(limiter);
55
59
  }
56
60
 
57
- app.use('/', rootRoutes);
58
- app.use('/tables', tablesRoutes);
59
- app.use('/tables', rowsRoutes);
61
+ app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerFile));
62
+ app.use('/api', rootRoutes);
63
+ app.use('/api/tables', tablesRoutes);
64
+ app.use('/api/tables', rowsRoutes);
60
65
 
61
- const port = config.port;
62
- app.listen(port, () => {
63
- console.log(`Soul is running on port ${port}...`);
64
- });
66
+ module.exports = app;
File without changes
File without changes
@@ -7,7 +7,6 @@ const schema = require('../schemas/index');
7
7
  const router = express.Router();
8
8
 
9
9
  router.get('/', controllers.root);
10
- router.post('/query', controllers.query);
11
10
  router.post(
12
11
  '/transaction',
13
12
  validator(schema.transaction),
File without changes
@@ -6,7 +6,7 @@ const schema = require('../schemas/tables');
6
6
 
7
7
  const router = express.Router();
8
8
 
9
- router.get('/', controllers.listTables);
9
+ router.get('/', validator(schema.listTables), controllers.listTables);
10
10
  router.post('/', validator(schema.createTable), controllers.createTable);
11
11
  router.get('/:name', controllers.getTableSchema);
12
12
  router.delete('/:name', validator(schema.deleteTable), controllers.deleteTable);
File without changes
File without changes
@@ -1,7 +1,14 @@
1
1
  const Joi = require('joi');
2
2
 
3
+ const listTables = Joi.object({
4
+ _search: Joi.string(),
5
+ _ordering: Joi.string(),
6
+ }).unknown(true);
7
+
3
8
  const createTable = Joi.object({
4
9
  name: Joi.string().min(2).max(30).required(),
10
+ autoAddCreatedAt: Joi.boolean().default(true),
11
+ autoAddUpdatedAt: Joi.boolean().default(true),
5
12
  schema: Joi.array()
6
13
  .items(
7
14
  Joi.object({
@@ -47,6 +54,7 @@ const deleteTable = Joi.object({
47
54
  });
48
55
 
49
56
  module.exports = {
57
+ listTables,
50
58
  createTable,
51
59
  deleteTable,
52
60
  };
package/src/server.js ADDED
@@ -0,0 +1,7 @@
1
+ const app = require('./index');
2
+ const config = require('./config/index');
3
+
4
+ const port = config.port;
5
+ app.listen(port, () => {
6
+ console.log(`Soul is running on port ${port}...`);
7
+ });
File without changes
@@ -0,0 +1,119 @@
1
+ const swaggerAutogen = require('swagger-autogen')();
2
+
3
+ const config = require('../config/index');
4
+ const version = require('../../package.json').version;
5
+
6
+ const outputFile = './swagger.json';
7
+ const endpointsFiles = ['../index.js'];
8
+
9
+ const doc = {
10
+ info: {
11
+ version: version,
12
+ title: 'Soul API',
13
+ description:
14
+ 'API Documentation for <b>Soul</b>, a simple SQLite RESTful server. ',
15
+ },
16
+ host: `localhost:${config.port}`,
17
+ basePath: '/',
18
+ schemes: ['http', 'https'],
19
+ consumes: ['application/json'],
20
+ produces: ['application/json'],
21
+ tags: [
22
+ {
23
+ name: 'Root',
24
+ description: 'Root endpoints',
25
+ },
26
+ {
27
+ name: 'Tables',
28
+ description: 'Tables endpoints',
29
+ },
30
+ {
31
+ name: 'Rows',
32
+ description: 'Rows endpoints',
33
+ },
34
+ ],
35
+ securityDefinitions: {},
36
+ definitions: {
37
+ Table: {
38
+ name: 'users',
39
+ },
40
+ Row: {},
41
+ Query: {
42
+ query: 'SELECT * FROM users',
43
+ },
44
+ Statement: {
45
+ statement:
46
+ 'INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)',
47
+ values: { id: 1, firstName: 'John', lastName: 'Doe' },
48
+ },
49
+ Transaction: {
50
+ transaction: [
51
+ { $ref: '#/definitions/Query' },
52
+ { $ref: '#/definitions/Statement' },
53
+ ],
54
+ },
55
+ ForeignKey: {
56
+ table: 'users',
57
+ column: 'id',
58
+ onDelete: 'CASCADE',
59
+ onUpdate: 'CASCADE',
60
+ },
61
+ Field: {
62
+ name: 'user_id',
63
+ type: 'INTEGER',
64
+ default: 1,
65
+ notNull: true,
66
+ unique: false,
67
+ primaryKey: false,
68
+ foreignKey: { $ref: '#/definitions/ForeignKey' },
69
+ index: false,
70
+ },
71
+ Schema: [{ $ref: '#/definitions/Field' }],
72
+
73
+ CreateTableRequestBody: {
74
+ name: 'users',
75
+ schema: { $ref: '#/definitions/Schema' },
76
+ autoAddCreatedAt: true,
77
+ autoAddUpdatedAt: true,
78
+ },
79
+ CreateTableSuccessResponse: {
80
+ message: 'Table created',
81
+ data: {
82
+ name: 'users',
83
+ fields: [{ $ref: '#/definitions/Field' }],
84
+ },
85
+ },
86
+ CreateTableErrorResponse: {
87
+ message: 'Table not created',
88
+ error: 'already_exists',
89
+ data: {},
90
+ },
91
+
92
+ InsertRowRequestBody: {
93
+ $ref: '#/definitions/Row',
94
+ },
95
+ InsertRowSuccessResponse: {
96
+ message: 'Row inserted',
97
+ data: {
98
+ id: 1,
99
+ createdAt: '2022-10-10 10:55:29',
100
+ updatedAt: '2022-10-10 10:55:29',
101
+ firstName: 'John',
102
+ },
103
+ },
104
+ InsertRowErrorResponse: {
105
+ message: 'Row not inserted',
106
+ error: 'not_found',
107
+ },
108
+
109
+ UpdateRowRequestBody: {
110
+ fields: [{ $ref: '#/definitions/Field' }],
111
+ },
112
+
113
+ TransactionRequestBody: {
114
+ $ref: '#/definitions/Transaction',
115
+ },
116
+ },
117
+ };
118
+
119
+ swaggerAutogen(outputFile, endpointsFiles, doc);