soul-cli 0.0.1 → 0.0.3
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/.env.sample +1 -1
- package/README.md +1 -3
- package/package.json +22 -6
- package/{soul → src}/cli.js +7 -0
- package/{soul → src}/config/index.js +18 -8
- package/{soul → src}/controllers/index.js +22 -18
- package/src/controllers/index.test.js +43 -0
- package/{soul → src}/controllers/rows.js +163 -3
- package/src/controllers/rows.test.js +51 -0
- package/{soul → src}/controllers/tables.js +98 -24
- package/src/controllers/tables.test.js +62 -0
- package/src/db/index.js +26 -0
- package/{soul → src}/index.js +23 -21
- package/{soul → src}/middlewares/logger.js +0 -0
- package/{soul → src}/middlewares/validation.js +0 -0
- package/{soul → src}/routes/index.js +0 -1
- package/{soul → src}/routes/rows.js +0 -0
- package/{soul → src}/routes/tables.js +1 -1
- package/{soul → src}/schemas/index.js +0 -0
- package/{soul → src}/schemas/rows.js +0 -0
- package/{soul → src}/schemas/tables.js +8 -0
- package/src/server.js +9 -0
- package/{soul → src}/services/.gitkeep +0 -0
- package/src/swagger/index.js +119 -0
- package/src/swagger/swagger.json +705 -0
- package/src/tests/globalSetup.js +11 -0
- package/src/tests/globalTeardown.js +9 -0
- package/src/tests/index.js +45 -0
- package/LICENSE +0 -21
- package/soul/db/index.js +0 -17
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
const db = require('../db/index');
|
|
2
2
|
|
|
3
3
|
const createTable = async (req, res) => {
|
|
4
|
-
|
|
4
|
+
/*
|
|
5
|
+
#swagger.tags = ['Tables']
|
|
6
|
+
#swagger.summary = 'Create Table'
|
|
7
|
+
#swagger.description = 'Endpoint to create a table'
|
|
8
|
+
#swagger.parameters['body'] = {
|
|
9
|
+
in: 'body',
|
|
10
|
+
required: true,
|
|
11
|
+
type: 'object',
|
|
12
|
+
schema: { $ref: "#/definitions/CreateTableRequestBody" }
|
|
13
|
+
}
|
|
14
|
+
*/
|
|
15
|
+
const {
|
|
16
|
+
name,
|
|
17
|
+
schema,
|
|
18
|
+
autoAddCreatedAt = true,
|
|
19
|
+
autoAddUpdatedAt = true,
|
|
20
|
+
} = req.body;
|
|
5
21
|
|
|
6
22
|
let schemaString = schema
|
|
7
23
|
// support name, type, default, not null, unique, primary key, foreign key, index
|
|
@@ -57,21 +73,51 @@ const createTable = async (req, res) => {
|
|
|
57
73
|
`;
|
|
58
74
|
}
|
|
59
75
|
|
|
60
|
-
// add
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
// add created at and updated at
|
|
77
|
+
if (autoAddCreatedAt) {
|
|
78
|
+
schemaString = `
|
|
79
|
+
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
80
|
+
${schemaString}
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (autoAddUpdatedAt) {
|
|
85
|
+
schemaString = `
|
|
86
|
+
updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
87
|
+
${schemaString}
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
66
90
|
|
|
67
91
|
const query = `CREATE TABLE ${name} (${schemaString})`;
|
|
68
92
|
try {
|
|
69
93
|
db.prepare(query).run();
|
|
70
94
|
|
|
71
|
-
|
|
95
|
+
const generatedSchema = db.prepare(`PRAGMA table_info(${name})`).all();
|
|
96
|
+
|
|
97
|
+
/*
|
|
98
|
+
#swagger.responses[201] = {
|
|
99
|
+
description: 'Table created',
|
|
100
|
+
schema: {
|
|
101
|
+
$ref: "#/definitions/CreateTableSuccessResponse"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
*/
|
|
105
|
+
res.status(201).json({
|
|
72
106
|
message: 'Table created',
|
|
107
|
+
data: {
|
|
108
|
+
name,
|
|
109
|
+
schema: generatedSchema,
|
|
110
|
+
},
|
|
73
111
|
});
|
|
74
112
|
} catch (error) {
|
|
113
|
+
/*
|
|
114
|
+
#swagger.responses[400] = {
|
|
115
|
+
description: 'Bad request',
|
|
116
|
+
schema: {
|
|
117
|
+
$ref: "#/definitions/CreateTableErrorResponse"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
*/
|
|
75
121
|
res.status(400).json({
|
|
76
122
|
message: error.message,
|
|
77
123
|
error: error,
|
|
@@ -81,24 +127,28 @@ const createTable = async (req, res) => {
|
|
|
81
127
|
|
|
82
128
|
// Return all tables
|
|
83
129
|
const listTables = async (req, res) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
130
|
+
/*
|
|
131
|
+
#swagger.tags = ['Tables']
|
|
132
|
+
#swagger.summary = 'List Tables'
|
|
133
|
+
#swagger.description = 'Endpoint to list all tables'
|
|
134
|
+
|
|
135
|
+
#swagger.parameters['_search'] = {
|
|
136
|
+
in: 'query',
|
|
137
|
+
required: false,
|
|
138
|
+
type: 'string',
|
|
139
|
+
description: 'Search term'
|
|
140
|
+
}
|
|
141
|
+
#swagger.parameters['_ordering'] = {
|
|
142
|
+
in: 'query',
|
|
143
|
+
required: false,
|
|
144
|
+
type: 'string',
|
|
145
|
+
description: 'Ordering term'
|
|
146
|
+
}
|
|
147
|
+
*/
|
|
148
|
+
const { _search, _ordering } = req.query;
|
|
92
149
|
|
|
93
150
|
let query = `SELECT name FROM sqlite_master WHERE type = 'table'`;
|
|
94
151
|
|
|
95
|
-
if (Object.keys(filters).length) {
|
|
96
|
-
query += ' AND ';
|
|
97
|
-
query += Object.keys(filters)
|
|
98
|
-
.map((key) => `${key} LIKE '%${filters[key]}%'`)
|
|
99
|
-
.join(' AND ');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
152
|
// if search is provided, search the tables
|
|
103
153
|
// e.g. ?_search=users
|
|
104
154
|
if (_search) {
|
|
@@ -117,7 +167,7 @@ const listTables = async (req, res) => {
|
|
|
117
167
|
const tables = db.prepare(query).all();
|
|
118
168
|
|
|
119
169
|
res.json({
|
|
120
|
-
tables,
|
|
170
|
+
data: tables,
|
|
121
171
|
});
|
|
122
172
|
} catch (error) {
|
|
123
173
|
res.status(400).json({
|
|
@@ -129,6 +179,18 @@ const listTables = async (req, res) => {
|
|
|
129
179
|
|
|
130
180
|
// TODO: Return the schema of a table by name
|
|
131
181
|
const getTableSchema = async (req, res) => {
|
|
182
|
+
/*
|
|
183
|
+
#swagger.tags = ['Tables']
|
|
184
|
+
#swagger.summary = 'Get Table Schema'
|
|
185
|
+
#swagger.description = 'Endpoint to get the schema of a table'
|
|
186
|
+
#swagger.parameters['name'] = {
|
|
187
|
+
in: 'path',
|
|
188
|
+
required: true,
|
|
189
|
+
type: 'string',
|
|
190
|
+
description: 'Name of the table'
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
*/
|
|
132
194
|
const { name } = req.params;
|
|
133
195
|
const query = `PRAGMA table_info(${name})`;
|
|
134
196
|
try {
|
|
@@ -147,6 +209,18 @@ const getTableSchema = async (req, res) => {
|
|
|
147
209
|
|
|
148
210
|
// Delete a table by name
|
|
149
211
|
const deleteTable = async (req, res) => {
|
|
212
|
+
/*
|
|
213
|
+
#swagger.tags = ['Tables']
|
|
214
|
+
#swagger.summary = 'Delete Table'
|
|
215
|
+
#swagger.description = 'Endpoint to delete a table'
|
|
216
|
+
#swagger.parameters['name'] = {
|
|
217
|
+
in: 'path',
|
|
218
|
+
required: true,
|
|
219
|
+
type: 'string',
|
|
220
|
+
description: 'Name of the table'
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
*/
|
|
150
224
|
const { name } = req.params;
|
|
151
225
|
const query = `DROP TABLE ${name}`;
|
|
152
226
|
try {
|
|
@@ -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
|
+
});
|
package/src/db/index.js
ADDED
|
@@ -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();
|
package/{soul → src}/index.js
RENAMED
|
@@ -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
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
winston.
|
|
36
|
-
winston.format.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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('/',
|
|
58
|
-
app.use('/
|
|
59
|
-
app.use('/tables',
|
|
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
|
-
|
|
62
|
-
app.listen(port, () => {
|
|
63
|
-
console.log(`Soul is running on port ${port}...`);
|
|
64
|
-
});
|
|
66
|
+
module.exports = app;
|
|
File without changes
|
|
File without changes
|
|
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
|
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);
|