soul-cli 0.0.1
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 +10 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/package.json +38 -0
- package/soul/cli.js +37 -0
- package/soul/config/index.js +57 -0
- package/soul/controllers/index.js +90 -0
- package/soul/controllers/rows.js +422 -0
- package/soul/controllers/tables.js +171 -0
- package/soul/db/index.js +17 -0
- package/soul/index.js +64 -0
- package/soul/middlewares/logger.js +13 -0
- package/soul/middlewares/validation.js +16 -0
- package/soul/routes/index.js +17 -0
- package/soul/routes/rows.js +35 -0
- package/soul/routes/tables.js +14 -0
- package/soul/schemas/index.js +19 -0
- package/soul/schemas/rows.js +46 -0
- package/soul/schemas/tables.js +52 -0
- package/soul/services/.gitkeep +0 -0
package/.env.sample
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Vahid Al
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "soul-cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A SQLite RESTful server",
|
|
5
|
+
"main": "soul/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"soul": "./soul/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node soul/index.js",
|
|
11
|
+
"dev": "NO_CLI=true nodemon soul/index.js"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/thevahidal/soul.git"
|
|
16
|
+
},
|
|
17
|
+
"author": "Vahid Al @thevahidal",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/thevahidal/soul/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/thevahidal/soul#readme",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"better-sqlite3": "^7.6.2",
|
|
25
|
+
"body-parser": "^1.20.0",
|
|
26
|
+
"cors": "^2.8.5",
|
|
27
|
+
"dotenv": "^16.0.3",
|
|
28
|
+
"express": "^4.18.1",
|
|
29
|
+
"express-rate-limit": "^6.6.0",
|
|
30
|
+
"express-winston": "^4.2.0",
|
|
31
|
+
"joi": "^17.6.2",
|
|
32
|
+
"winston": "^3.8.2",
|
|
33
|
+
"yargs": "^17.6.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"nodemon": "^2.0.20"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/soul/cli.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const yargs = require('yargs');
|
|
2
|
+
|
|
3
|
+
const usage = `
|
|
4
|
+
Soul | RESTful server for SQLite
|
|
5
|
+
Usage: soul [options]
|
|
6
|
+
`;
|
|
7
|
+
|
|
8
|
+
let options = undefined;
|
|
9
|
+
if (process.env.NO_CLI !== 'true') {
|
|
10
|
+
yargs
|
|
11
|
+
.usage(usage)
|
|
12
|
+
.option('d', {
|
|
13
|
+
alias: 'database',
|
|
14
|
+
describe: 'SQLite database file or :memory:',
|
|
15
|
+
type: 'string',
|
|
16
|
+
demandOption: true,
|
|
17
|
+
})
|
|
18
|
+
.option('p', {
|
|
19
|
+
alias: 'port',
|
|
20
|
+
describe: 'Port to listen on',
|
|
21
|
+
type: 'number',
|
|
22
|
+
demandOption: false,
|
|
23
|
+
})
|
|
24
|
+
.option('r', {
|
|
25
|
+
alias: 'rate-limit-enabled',
|
|
26
|
+
describe: 'Enable rate limiting',
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
demandOption: false,
|
|
29
|
+
})
|
|
30
|
+
.help(true).argv;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = {
|
|
34
|
+
yargs,
|
|
35
|
+
usage,
|
|
36
|
+
options,
|
|
37
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const dotenv = require('dotenv');
|
|
2
|
+
const Joi = require('joi');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const { yargs, usage, options } = require('../cli');
|
|
6
|
+
|
|
7
|
+
dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
8
|
+
|
|
9
|
+
const envVarsSchema = Joi.object()
|
|
10
|
+
.keys({
|
|
11
|
+
PORT: Joi.number().positive().required(),
|
|
12
|
+
|
|
13
|
+
NODE_ENV: Joi.string()
|
|
14
|
+
.valid('production', 'development', 'test')
|
|
15
|
+
.required(),
|
|
16
|
+
|
|
17
|
+
DB: Joi.string().required(),
|
|
18
|
+
|
|
19
|
+
CORS_ORIGIN_WHITELIST: Joi.string().required(),
|
|
20
|
+
|
|
21
|
+
RATE_LIMIT_ENABLED: Joi.boolean().required(),
|
|
22
|
+
RATE_LIMIT_WINDOW_MS: Joi.number().positive().required(),
|
|
23
|
+
RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().required(),
|
|
24
|
+
})
|
|
25
|
+
.unknown();
|
|
26
|
+
|
|
27
|
+
const { value: envVars, error } = envVarsSchema
|
|
28
|
+
.prefs({ errors: { label: 'key' } })
|
|
29
|
+
.validate(process.env);
|
|
30
|
+
|
|
31
|
+
if (error) {
|
|
32
|
+
throw new Error(`Config validation error: ${error.message}`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const { argv } = yargs;
|
|
36
|
+
|
|
37
|
+
module.exports = {
|
|
38
|
+
env: envVars.NODE_ENV,
|
|
39
|
+
|
|
40
|
+
production: envVars.NODE_ENV === 'production',
|
|
41
|
+
development: envVars.NODE_ENV === 'development',
|
|
42
|
+
test: envVars.NODE_ENV === 'test',
|
|
43
|
+
|
|
44
|
+
port: argv.port || envVars.PORT,
|
|
45
|
+
|
|
46
|
+
db: {
|
|
47
|
+
filename: argv.database || envVars.DB || ':memory:',
|
|
48
|
+
},
|
|
49
|
+
cors: {
|
|
50
|
+
origin: envVars.CORS_ORIGIN_WHITELIST.split(','),
|
|
51
|
+
},
|
|
52
|
+
rateLimit: {
|
|
53
|
+
enabled: argv['rate-limit-enabled'] || envVars.RATE_LIMIT_ENABLED,
|
|
54
|
+
windowMs: envVars.RATE_LIMIT_WINDOW,
|
|
55
|
+
max: envVars.RATE_LIMIT_MAX,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const e = require('express');
|
|
2
|
+
const db = require('../db/index');
|
|
3
|
+
|
|
4
|
+
// Root endpoint
|
|
5
|
+
const root = async (req, res) => {
|
|
6
|
+
res.json({
|
|
7
|
+
message: 'Soul is running...',
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// Run any query
|
|
12
|
+
const query = async (req, res) => {
|
|
13
|
+
const { query } = req.body;
|
|
14
|
+
try {
|
|
15
|
+
const data = db.prepare(query).all();
|
|
16
|
+
res.json({
|
|
17
|
+
data,
|
|
18
|
+
});
|
|
19
|
+
} catch (error) {
|
|
20
|
+
res.status(400).json({
|
|
21
|
+
message: error.message,
|
|
22
|
+
error: error,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Run any query transactions
|
|
28
|
+
// inspired by https://github.com/proofrock/ws4sqlite
|
|
29
|
+
// e.g. body:
|
|
30
|
+
// "transaction": [
|
|
31
|
+
// {
|
|
32
|
+
// "statement": "INSERT INTO users (id, firstName, lastName) VALUES (:id, :firstName, :lastName)",
|
|
33
|
+
// "values": { "id": 1, "firstName": "John", "lastName": "Doe" }
|
|
34
|
+
// },
|
|
35
|
+
// {
|
|
36
|
+
// "query": "SELECT * FROM users"
|
|
37
|
+
// }
|
|
38
|
+
// }
|
|
39
|
+
//
|
|
40
|
+
// response:
|
|
41
|
+
// "data": [
|
|
42
|
+
// {
|
|
43
|
+
// "changes": 1,
|
|
44
|
+
// "lastInsertRowid": 1
|
|
45
|
+
// },
|
|
46
|
+
// [
|
|
47
|
+
// {
|
|
48
|
+
// "id": 1,
|
|
49
|
+
// "createdAt": "2022-10-10 10:55:29",
|
|
50
|
+
// "updatedAt": "2022-10-10 10:55:29",
|
|
51
|
+
// "firstName": "John",
|
|
52
|
+
// "lastName": "Doe"
|
|
53
|
+
// }
|
|
54
|
+
// ]
|
|
55
|
+
// ]
|
|
56
|
+
//
|
|
57
|
+
|
|
58
|
+
const transaction = async (req, res) => {
|
|
59
|
+
const { transaction } = req.body;
|
|
60
|
+
const results = [];
|
|
61
|
+
try {
|
|
62
|
+
db.transaction(() => {
|
|
63
|
+
transaction.forEach((query) => {
|
|
64
|
+
if (query.statement) {
|
|
65
|
+
const { statement, values } = query;
|
|
66
|
+
const data = db.prepare(statement).run(values);
|
|
67
|
+
results.push(data);
|
|
68
|
+
} else if (query.query) {
|
|
69
|
+
const { query: queryString } = query;
|
|
70
|
+
const data = db.prepare(queryString).all();
|
|
71
|
+
results.push(data);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
})();
|
|
75
|
+
res.json({
|
|
76
|
+
data: results,
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
res.status(400).json({
|
|
80
|
+
message: error.message,
|
|
81
|
+
error: error,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
root,
|
|
88
|
+
query,
|
|
89
|
+
transaction,
|
|
90
|
+
};
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
const db = require('../db/index');
|
|
2
|
+
|
|
3
|
+
// Return paginated rows of a table
|
|
4
|
+
const listTableRows = async (req, res) => {
|
|
5
|
+
const { name } = req.params;
|
|
6
|
+
const {
|
|
7
|
+
_page = 1,
|
|
8
|
+
_limit = 10,
|
|
9
|
+
_search,
|
|
10
|
+
_ordering,
|
|
11
|
+
_schema,
|
|
12
|
+
_extend,
|
|
13
|
+
...filters
|
|
14
|
+
} = req.query;
|
|
15
|
+
|
|
16
|
+
const page = parseInt(_page);
|
|
17
|
+
const limit = parseInt(_limit);
|
|
18
|
+
|
|
19
|
+
// if filters are provided, filter rows by them
|
|
20
|
+
// filters consists of fields to filter by
|
|
21
|
+
// filtering must be case insensitive
|
|
22
|
+
// e.g. ?name=John&age=20
|
|
23
|
+
// will filter by name like '%John%' and age like '%20%'
|
|
24
|
+
|
|
25
|
+
let whereString = '';
|
|
26
|
+
if (Object.keys(filters).length > 0) {
|
|
27
|
+
whereString += ' WHERE ';
|
|
28
|
+
whereString += Object.keys(filters)
|
|
29
|
+
.map((key) => `${key} LIKE '%${filters[key]}%'`)
|
|
30
|
+
.join(' AND ');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// if _search is provided, search rows by it
|
|
34
|
+
// e.g. ?_search=John will search for John in all fields of the table
|
|
35
|
+
// searching must be case insensitive
|
|
36
|
+
if (_search) {
|
|
37
|
+
if (whereString) {
|
|
38
|
+
whereString += ' AND ';
|
|
39
|
+
} else {
|
|
40
|
+
whereString += ' WHERE ';
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
// get all fields of the table
|
|
44
|
+
const fields = db.prepare(`PRAGMA table_info(${name})`).all();
|
|
45
|
+
whereString += fields
|
|
46
|
+
.map((field) => `${field.name} LIKE '%${_search}%'`)
|
|
47
|
+
.join(' OR ');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return res.status(400).json({
|
|
50
|
+
message: error.message,
|
|
51
|
+
error: error,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// if _ordering is provided, order rows by it
|
|
57
|
+
// e.g. ?_ordering=name will order by name
|
|
58
|
+
// e.g. ?_ordering=-name will order by name descending
|
|
59
|
+
let orderString = '';
|
|
60
|
+
if (_ordering) {
|
|
61
|
+
orderString += ` ORDER BY ${_ordering.replace('-', '')} ${
|
|
62
|
+
_ordering.startsWith('-') ? 'DESC' : 'ASC'
|
|
63
|
+
}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// if _schema is provided, return only those fields
|
|
67
|
+
// e.g. ?_schema=name,age will return only name and age fields
|
|
68
|
+
// if _schema is not provided, return all fields
|
|
69
|
+
|
|
70
|
+
let schemaString = '';
|
|
71
|
+
if (_schema) {
|
|
72
|
+
const schemaFields = _schema.split(',');
|
|
73
|
+
schemaFields.forEach((field) => {
|
|
74
|
+
schemaString += `${name}.${field},`;
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
schemaString = `${name}.*`;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// remove trailing comma
|
|
81
|
+
schemaString = schemaString.replace(/,\s*$/, '');
|
|
82
|
+
|
|
83
|
+
// if _extend is provided, extend rows with related data using joins
|
|
84
|
+
// e.g. ?_extend=author_id will extend rows with author data
|
|
85
|
+
// e.g. ?_extend=author_id,book_id will extend rows with author and book data
|
|
86
|
+
// considering that author_id and book_id are foreign keys to authors and books tables
|
|
87
|
+
|
|
88
|
+
// e.g. original row is
|
|
89
|
+
// {
|
|
90
|
+
// id: 1,
|
|
91
|
+
// name: 'John',
|
|
92
|
+
// author_id: 1,
|
|
93
|
+
// book_id: 2
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
// e.g. extended row is
|
|
97
|
+
// {
|
|
98
|
+
// id: 1,
|
|
99
|
+
// name: 'John',
|
|
100
|
+
// author_id: 1,
|
|
101
|
+
// author_id_data: {
|
|
102
|
+
// name: 'John Doe'
|
|
103
|
+
// },
|
|
104
|
+
// book_id: 2
|
|
105
|
+
// book_id_data: {
|
|
106
|
+
// name: 'The Book'
|
|
107
|
+
// }
|
|
108
|
+
// }
|
|
109
|
+
|
|
110
|
+
let extendString = '';
|
|
111
|
+
if (_extend) {
|
|
112
|
+
const extendFields = _extend.split(',');
|
|
113
|
+
extendFields.forEach((field) => {
|
|
114
|
+
try {
|
|
115
|
+
const foreignKey = db
|
|
116
|
+
.prepare(`PRAGMA foreign_key_list(${name})`)
|
|
117
|
+
.all()
|
|
118
|
+
.find((fk) => fk.from === field);
|
|
119
|
+
|
|
120
|
+
if (!foreignKey) {
|
|
121
|
+
throw new Error('Foreign key not found');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const { table } = foreignKey;
|
|
125
|
+
|
|
126
|
+
const fields = db.prepare(`PRAGMA table_info(${table})`).all();
|
|
127
|
+
|
|
128
|
+
extendString += ` LEFT JOIN ${table} as ${table} ON ${table}.${foreignKey.to} = ${name}.${field}`;
|
|
129
|
+
|
|
130
|
+
// joined fields will be returned in a new object called {field}_data e.g. author_id_data
|
|
131
|
+
const extendFieldsString =
|
|
132
|
+
'json_object( ' +
|
|
133
|
+
fields
|
|
134
|
+
.map((field) => `'${field.name}', ${table}.${field.name}`)
|
|
135
|
+
.join(', ') +
|
|
136
|
+
' ) as ' +
|
|
137
|
+
field +
|
|
138
|
+
'_data';
|
|
139
|
+
|
|
140
|
+
if (schemaString) {
|
|
141
|
+
schemaString += ', ';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
schemaString += extendFieldsString;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return res.status(400).json({
|
|
147
|
+
message: error.message,
|
|
148
|
+
error: error,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// get paginated rows
|
|
155
|
+
const query = `SELECT ${schemaString} FROM ${name} ${extendString} ${whereString} ${orderString} LIMIT ${limit} OFFSET ${
|
|
156
|
+
limit * (page - 1)
|
|
157
|
+
}`;
|
|
158
|
+
|
|
159
|
+
console.log({ query });
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
const data = db.prepare(query).all();
|
|
163
|
+
|
|
164
|
+
// get total number of rows
|
|
165
|
+
const total = db
|
|
166
|
+
.prepare(`SELECT COUNT(*) as total FROM ${name} ${whereString}`)
|
|
167
|
+
.get().total;
|
|
168
|
+
|
|
169
|
+
const next =
|
|
170
|
+
data.length === limit ? `/tables/${name}?page=${page + 1}` : null;
|
|
171
|
+
const previous = page > 1 ? `/tables/${name}?page=${page - 1}` : null;
|
|
172
|
+
|
|
173
|
+
res.json({
|
|
174
|
+
data,
|
|
175
|
+
total,
|
|
176
|
+
next,
|
|
177
|
+
previous,
|
|
178
|
+
});
|
|
179
|
+
} catch (error) {
|
|
180
|
+
res.status(400).json({
|
|
181
|
+
message: error.message,
|
|
182
|
+
error: error,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// Insert a new row in a table
|
|
188
|
+
const insertRowInTable = async (req, res) => {
|
|
189
|
+
const { name } = req.params;
|
|
190
|
+
const { fields } = req.body;
|
|
191
|
+
const fieldsString = Object.keys(fields).join(', ');
|
|
192
|
+
|
|
193
|
+
// wrap text values in quotes
|
|
194
|
+
const valuesString = Object.values(fields)
|
|
195
|
+
.map((value) => {
|
|
196
|
+
if (typeof value === 'string') {
|
|
197
|
+
return `'${value}'`;
|
|
198
|
+
}
|
|
199
|
+
return value;
|
|
200
|
+
})
|
|
201
|
+
.join(', ');
|
|
202
|
+
|
|
203
|
+
const query = `INSERT INTO ${name} (${fieldsString}) VALUES (${valuesString})`;
|
|
204
|
+
try {
|
|
205
|
+
const data = db.prepare(query).run();
|
|
206
|
+
|
|
207
|
+
res.json({
|
|
208
|
+
message: 'Row inserted',
|
|
209
|
+
data,
|
|
210
|
+
});
|
|
211
|
+
} catch (error) {
|
|
212
|
+
res.status(400).json({
|
|
213
|
+
message: error.message,
|
|
214
|
+
error: error,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// Get a row by pk
|
|
220
|
+
const getRowInTableByPK = async (req, res) => {
|
|
221
|
+
const { name, pk } = req.params;
|
|
222
|
+
const { _field, _schema, _extend } = req.query;
|
|
223
|
+
|
|
224
|
+
let searchField = _field;
|
|
225
|
+
|
|
226
|
+
if (!_field) {
|
|
227
|
+
// find the primary key of the table
|
|
228
|
+
searchField = db
|
|
229
|
+
.prepare(`PRAGMA table_info(${name})`)
|
|
230
|
+
.all()
|
|
231
|
+
.find((field) => field.pk === 1).name;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// if _schema is provided, return only those fields
|
|
235
|
+
// e.g. ?_schema=name,age will return only name and age fields
|
|
236
|
+
// if _schema is not provided, return all fields
|
|
237
|
+
|
|
238
|
+
let schemaString = '';
|
|
239
|
+
if (_schema) {
|
|
240
|
+
const schemaFields = _schema.split(',');
|
|
241
|
+
schemaFields.forEach((field) => {
|
|
242
|
+
schemaString += `${name}.${field},`;
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
schemaString = `${name}.*`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// remove trailing comma
|
|
249
|
+
schemaString = schemaString.replace(/,\s*$/, '');
|
|
250
|
+
|
|
251
|
+
// if _extend is provided, extend rows with related data using joins
|
|
252
|
+
// e.g. ?_extend=author_id will extend rows with author data
|
|
253
|
+
// e.g. ?_extend=author_id,book_id will extend rows with author and book data
|
|
254
|
+
// considering that author_id and book_id are foreign keys to authors and books tables
|
|
255
|
+
|
|
256
|
+
// e.g. original row is
|
|
257
|
+
// {
|
|
258
|
+
// id: 1,
|
|
259
|
+
// name: 'John',
|
|
260
|
+
// author_id: 1,
|
|
261
|
+
// book_id: 2
|
|
262
|
+
// }
|
|
263
|
+
|
|
264
|
+
// e.g. extended row is
|
|
265
|
+
// {
|
|
266
|
+
// id: 1,
|
|
267
|
+
// name: 'John',
|
|
268
|
+
// author_id: 1,
|
|
269
|
+
// author_id_data: {
|
|
270
|
+
// name: 'John Doe'
|
|
271
|
+
// },
|
|
272
|
+
// book_id: 2
|
|
273
|
+
// book_id_data: {
|
|
274
|
+
// name: 'The Book'
|
|
275
|
+
// }
|
|
276
|
+
// }
|
|
277
|
+
|
|
278
|
+
let extendString = '';
|
|
279
|
+
if (_extend) {
|
|
280
|
+
const extendFields = _extend.split(',');
|
|
281
|
+
extendFields.forEach((field) => {
|
|
282
|
+
try {
|
|
283
|
+
const foreignKey = db
|
|
284
|
+
.prepare(`PRAGMA foreign_key_list(${name})`)
|
|
285
|
+
.all()
|
|
286
|
+
.find((fk) => fk.from === field);
|
|
287
|
+
|
|
288
|
+
if (!foreignKey) {
|
|
289
|
+
throw new Error('Foreign key not found');
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const { table } = foreignKey;
|
|
293
|
+
|
|
294
|
+
const fields = db.prepare(`PRAGMA table_info(${table})`).all();
|
|
295
|
+
|
|
296
|
+
extendString += ` LEFT JOIN ${table} as ${table} ON ${table}.${foreignKey.to} = ${name}.${field}`;
|
|
297
|
+
|
|
298
|
+
// joined fields will be returned in a new object called {field}_data e.g. author_id_data
|
|
299
|
+
const extendFieldsString =
|
|
300
|
+
'json_object( ' +
|
|
301
|
+
fields
|
|
302
|
+
.map((field) => `'${field.name}', ${table}.${field.name}`)
|
|
303
|
+
.join(', ') +
|
|
304
|
+
' ) as ' +
|
|
305
|
+
field +
|
|
306
|
+
'_data';
|
|
307
|
+
|
|
308
|
+
if (schemaString) {
|
|
309
|
+
schemaString += ', ';
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
schemaString += extendFieldsString;
|
|
313
|
+
} catch (error) {
|
|
314
|
+
return res.status(400).json({
|
|
315
|
+
message: error.message,
|
|
316
|
+
error: error,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const query = `SELECT ${schemaString} FROM ${name} ${extendString} WHERE ${name}.${searchField} = ${pk}`;
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
const data = db.prepare(query).get();
|
|
326
|
+
|
|
327
|
+
if (!data) {
|
|
328
|
+
res.status(404).json({
|
|
329
|
+
error: 'not_found',
|
|
330
|
+
});
|
|
331
|
+
} else {
|
|
332
|
+
res.json({
|
|
333
|
+
data,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
} catch (error) {
|
|
337
|
+
res.status(400).json({
|
|
338
|
+
message: error.message,
|
|
339
|
+
error: error,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// Update a row by pk
|
|
345
|
+
const updateRowInTableByPK = async (req, res) => {
|
|
346
|
+
const { name, pk } = req.params;
|
|
347
|
+
const { fields } = req.body;
|
|
348
|
+
const { _field } = req.query;
|
|
349
|
+
|
|
350
|
+
let searchField = _field;
|
|
351
|
+
|
|
352
|
+
if (!_field) {
|
|
353
|
+
// find the primary key of the table
|
|
354
|
+
searchField = db
|
|
355
|
+
.prepare(`PRAGMA table_info(${name})`)
|
|
356
|
+
.all()
|
|
357
|
+
.find((field) => field.pk === 1).name;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// wrap text values in quotes
|
|
361
|
+
const fieldsString = Object.keys(fields)
|
|
362
|
+
.map((key) => {
|
|
363
|
+
let value = fields[key];
|
|
364
|
+
if (typeof value === 'string') {
|
|
365
|
+
value = `'${value}'`;
|
|
366
|
+
}
|
|
367
|
+
return `${key} = ${value}`;
|
|
368
|
+
})
|
|
369
|
+
.join(', ');
|
|
370
|
+
|
|
371
|
+
const query = `UPDATE ${name} SET ${fieldsString} WHERE ${searchField} = '${pk}'`;
|
|
372
|
+
try {
|
|
373
|
+
db.prepare(query).run();
|
|
374
|
+
|
|
375
|
+
res.json({
|
|
376
|
+
message: 'Row updated',
|
|
377
|
+
});
|
|
378
|
+
} catch (error) {
|
|
379
|
+
res.status(400).json({
|
|
380
|
+
message: error.message,
|
|
381
|
+
error: error,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
// Delete a row by id
|
|
387
|
+
const deleteRowInTableByPK = async (req, res) => {
|
|
388
|
+
const { name, pk } = req.params;
|
|
389
|
+
const { _field } = req.query;
|
|
390
|
+
|
|
391
|
+
let searchField = _field;
|
|
392
|
+
|
|
393
|
+
if (!_field) {
|
|
394
|
+
// find the primary key of the table
|
|
395
|
+
searchField = db
|
|
396
|
+
.prepare(`PRAGMA table_info(${name})`)
|
|
397
|
+
.all()
|
|
398
|
+
.find((field) => field.pk === 1).name;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const query = `DELETE FROM ${name} WHERE ${searchField} = '${pk}'`;
|
|
402
|
+
const data = db.prepare(query).run();
|
|
403
|
+
|
|
404
|
+
if (data.changes === 0) {
|
|
405
|
+
res.status(404).json({
|
|
406
|
+
error: 'not_found',
|
|
407
|
+
});
|
|
408
|
+
} else {
|
|
409
|
+
res.json({
|
|
410
|
+
message: 'Row deleted',
|
|
411
|
+
data,
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
module.exports = {
|
|
417
|
+
listTableRows,
|
|
418
|
+
insertRowInTable,
|
|
419
|
+
getRowInTableByPK,
|
|
420
|
+
updateRowInTableByPK,
|
|
421
|
+
deleteRowInTableByPK,
|
|
422
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
const db = require('../db/index');
|
|
2
|
+
|
|
3
|
+
const createTable = async (req, res) => {
|
|
4
|
+
const { name, schema } = req.body;
|
|
5
|
+
|
|
6
|
+
let schemaString = schema
|
|
7
|
+
// support name, type, default, not null, unique, primary key, foreign key, index
|
|
8
|
+
// e.g. { name: 'id', type: 'INTEGER', primaryKey: true }
|
|
9
|
+
|
|
10
|
+
.map(
|
|
11
|
+
({
|
|
12
|
+
name,
|
|
13
|
+
type,
|
|
14
|
+
default: defaultValue,
|
|
15
|
+
notNull,
|
|
16
|
+
unique,
|
|
17
|
+
primaryKey,
|
|
18
|
+
foreignKey,
|
|
19
|
+
index,
|
|
20
|
+
}) => {
|
|
21
|
+
let column = `${name} ${type}`;
|
|
22
|
+
if (defaultValue) {
|
|
23
|
+
column += ` DEFAULT ${defaultValue}`;
|
|
24
|
+
}
|
|
25
|
+
if (notNull) {
|
|
26
|
+
column += ' NOT NULL';
|
|
27
|
+
}
|
|
28
|
+
if (unique) {
|
|
29
|
+
column += ' UNIQUE';
|
|
30
|
+
}
|
|
31
|
+
if (primaryKey) {
|
|
32
|
+
column += ' PRIMARY KEY';
|
|
33
|
+
}
|
|
34
|
+
if (foreignKey) {
|
|
35
|
+
column += ` REFERENCES ${foreignKey.table}(${foreignKey.column})`;
|
|
36
|
+
}
|
|
37
|
+
if (foreignKey && foreignKey.onDelete) {
|
|
38
|
+
column += ` ON DELETE ${foreignKey.onDelete}`;
|
|
39
|
+
}
|
|
40
|
+
if (foreignKey && foreignKey.onUpdate) {
|
|
41
|
+
column += ` ON UPDATE ${foreignKey.onUpdate}`;
|
|
42
|
+
}
|
|
43
|
+
if (index) {
|
|
44
|
+
column += ` INDEX ${index}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return column;
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
.join(', ');
|
|
51
|
+
|
|
52
|
+
// add id if primary key is not defined
|
|
53
|
+
if (!schema.find((field) => field.primaryKey)) {
|
|
54
|
+
schemaString = `
|
|
55
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
56
|
+
${schemaString}
|
|
57
|
+
`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// add createdAt and updatedAt fields to fieldsString
|
|
61
|
+
schemaString = `
|
|
62
|
+
createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
63
|
+
updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
64
|
+
${schemaString}
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
const query = `CREATE TABLE ${name} (${schemaString})`;
|
|
68
|
+
try {
|
|
69
|
+
db.prepare(query).run();
|
|
70
|
+
|
|
71
|
+
res.json({
|
|
72
|
+
message: 'Table created',
|
|
73
|
+
});
|
|
74
|
+
} catch (error) {
|
|
75
|
+
res.status(400).json({
|
|
76
|
+
message: error.message,
|
|
77
|
+
error: error,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// Return all tables
|
|
83
|
+
const listTables = async (req, res) => {
|
|
84
|
+
const { _search, _ordering, ...filters } = req.query;
|
|
85
|
+
|
|
86
|
+
// filters consist of fields to filter by
|
|
87
|
+
// e.g. ?name=users
|
|
88
|
+
|
|
89
|
+
// if filters are provided, filter the tables
|
|
90
|
+
// filtering must be case insensitive
|
|
91
|
+
// otherwise, return all tables
|
|
92
|
+
|
|
93
|
+
let query = `SELECT name FROM sqlite_master WHERE type = 'table'`;
|
|
94
|
+
|
|
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
|
+
// if search is provided, search the tables
|
|
103
|
+
// e.g. ?_search=users
|
|
104
|
+
if (_search) {
|
|
105
|
+
query += ` AND name LIKE '%${_search}%'`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// if ordering is provided, order the tables
|
|
109
|
+
// e.g. ?_ordering=name (ascending) or ?_ordering=-name (descending)
|
|
110
|
+
if (_ordering) {
|
|
111
|
+
query += ` ORDER BY ${_ordering.replace('-', '')} ${
|
|
112
|
+
_ordering.startsWith('-') ? 'DESC' : 'ASC'
|
|
113
|
+
}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const tables = db.prepare(query).all();
|
|
118
|
+
|
|
119
|
+
res.json({
|
|
120
|
+
tables,
|
|
121
|
+
});
|
|
122
|
+
} catch (error) {
|
|
123
|
+
res.status(400).json({
|
|
124
|
+
message: error.message,
|
|
125
|
+
error: error,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// TODO: Return the schema of a table by name
|
|
131
|
+
const getTableSchema = async (req, res) => {
|
|
132
|
+
const { name } = req.params;
|
|
133
|
+
const query = `PRAGMA table_info(${name})`;
|
|
134
|
+
try {
|
|
135
|
+
const schema = db.prepare(query).all();
|
|
136
|
+
|
|
137
|
+
res.json({
|
|
138
|
+
data: schema,
|
|
139
|
+
});
|
|
140
|
+
} catch (error) {
|
|
141
|
+
res.status(400).json({
|
|
142
|
+
message: error.message,
|
|
143
|
+
error: error,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Delete a table by name
|
|
149
|
+
const deleteTable = async (req, res) => {
|
|
150
|
+
const { name } = req.params;
|
|
151
|
+
const query = `DROP TABLE ${name}`;
|
|
152
|
+
try {
|
|
153
|
+
db.prepare(query).run();
|
|
154
|
+
|
|
155
|
+
res.json({
|
|
156
|
+
message: 'Table deleted',
|
|
157
|
+
});
|
|
158
|
+
} catch (error) {
|
|
159
|
+
res.status(400).json({
|
|
160
|
+
message: error.message,
|
|
161
|
+
error: error,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
module.exports = {
|
|
167
|
+
listTables,
|
|
168
|
+
createTable,
|
|
169
|
+
getTableSchema,
|
|
170
|
+
deleteTable,
|
|
171
|
+
};
|
package/soul/db/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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, { verbose: console.log, ...options });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get() {
|
|
11
|
+
return this.db;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
console.log(config.db);
|
|
16
|
+
|
|
17
|
+
module.exports = new Database(config.db.filename).get();
|
package/soul/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const express = require('express');
|
|
4
|
+
const bodyParser = require('body-parser');
|
|
5
|
+
const winston = require('winston');
|
|
6
|
+
const expressWinston = require('express-winston');
|
|
7
|
+
const cors = require('cors');
|
|
8
|
+
const rateLimit = require('express-rate-limit');
|
|
9
|
+
const dotenv = require('dotenv');
|
|
10
|
+
|
|
11
|
+
const config = require('./config/index');
|
|
12
|
+
const db = require('./db/index');
|
|
13
|
+
const rootRoutes = require('./routes/index');
|
|
14
|
+
const tablesRoutes = require('./routes/tables');
|
|
15
|
+
const rowsRoutes = require('./routes/rows');
|
|
16
|
+
|
|
17
|
+
const app = express();
|
|
18
|
+
|
|
19
|
+
app.use(bodyParser.json());
|
|
20
|
+
|
|
21
|
+
// Activate wal mode
|
|
22
|
+
db.pragma('journal_mode = WAL');
|
|
23
|
+
|
|
24
|
+
// Enable CORS
|
|
25
|
+
const corsOptions = {
|
|
26
|
+
origin: config.cors.origin,
|
|
27
|
+
};
|
|
28
|
+
app.use(cors(corsOptions));
|
|
29
|
+
|
|
30
|
+
// 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
|
+
);
|
|
44
|
+
|
|
45
|
+
if (config.rateLimit.enabled) {
|
|
46
|
+
const limiter = rateLimit({
|
|
47
|
+
windowMs: config.rateLimit.windowMs,
|
|
48
|
+
max: config.rateLimit.max, // Limit each IP to {max} requests per `window`
|
|
49
|
+
standardHeaders: true, // Return rate limit info in the `RateLimit*` headers
|
|
50
|
+
legacyHeaders: false, // Disable the `XRateLimit*` headers
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Apply the rate limiting middleware to all requests
|
|
54
|
+
app.use(limiter);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
app.use('/', rootRoutes);
|
|
58
|
+
app.use('/tables', tablesRoutes);
|
|
59
|
+
app.use('/tables', rowsRoutes);
|
|
60
|
+
|
|
61
|
+
const port = config.port;
|
|
62
|
+
app.listen(port, () => {
|
|
63
|
+
console.log(`Soul is running on port ${port}...`);
|
|
64
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// including the method, url, status code and response time
|
|
2
|
+
const logger = (req, res, next) => {
|
|
3
|
+
const start = Date.now();
|
|
4
|
+
res.on('finish', () => {
|
|
5
|
+
const delta = Date.now() - start;
|
|
6
|
+
console.log(`${req.method} ${req.url} ${res.statusCode} ${delta}ms`);
|
|
7
|
+
});
|
|
8
|
+
next();
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
logger,
|
|
13
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const validator = (schema) => (req, res, next) => {
|
|
2
|
+
const data = { ...req.body, ...req.params, ...req.query };
|
|
3
|
+
const { error } = schema.validate(data);
|
|
4
|
+
if (error) {
|
|
5
|
+
res.status(400).json({
|
|
6
|
+
message: error.message,
|
|
7
|
+
error: error,
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
next();
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
validator,
|
|
16
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/index');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const schema = require('../schemas/index');
|
|
6
|
+
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
|
|
9
|
+
router.get('/', controllers.root);
|
|
10
|
+
router.post('/query', controllers.query);
|
|
11
|
+
router.post(
|
|
12
|
+
'/transaction',
|
|
13
|
+
validator(schema.transaction),
|
|
14
|
+
controllers.transaction
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
module.exports = router;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/rows');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const schema = require('../schemas/rows');
|
|
6
|
+
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
|
|
9
|
+
router.get(
|
|
10
|
+
'/:name/rows',
|
|
11
|
+
validator(schema.listTableRows),
|
|
12
|
+
controllers.listTableRows
|
|
13
|
+
);
|
|
14
|
+
router.post(
|
|
15
|
+
'/:name/rows',
|
|
16
|
+
validator(schema.insertRowInTable),
|
|
17
|
+
controllers.insertRowInTable
|
|
18
|
+
);
|
|
19
|
+
router.get(
|
|
20
|
+
'/:name/rows/:pk',
|
|
21
|
+
validator(schema.getRowInTableByPK),
|
|
22
|
+
controllers.getRowInTableByPK
|
|
23
|
+
);
|
|
24
|
+
router.put(
|
|
25
|
+
'/:name/rows/:pk',
|
|
26
|
+
validator(schema.updateRowInTableByPK),
|
|
27
|
+
controllers.updateRowInTableByPK
|
|
28
|
+
);
|
|
29
|
+
router.delete(
|
|
30
|
+
'/:name/rows/:pk',
|
|
31
|
+
validator(schema.deleteRowInTableByPK),
|
|
32
|
+
controllers.deleteRowInTableByPK
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
module.exports = router;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
|
|
3
|
+
const controllers = require('../controllers/tables');
|
|
4
|
+
const { validator } = require('../middlewares/validation');
|
|
5
|
+
const schema = require('../schemas/tables');
|
|
6
|
+
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
|
|
9
|
+
router.get('/', controllers.listTables);
|
|
10
|
+
router.post('/', validator(schema.createTable), controllers.createTable);
|
|
11
|
+
router.get('/:name', controllers.getTableSchema);
|
|
12
|
+
router.delete('/:name', validator(schema.deleteTable), controllers.deleteTable);
|
|
13
|
+
|
|
14
|
+
module.exports = router;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
const transaction = Joi.object({
|
|
4
|
+
transaction: Joi.array()
|
|
5
|
+
.items(
|
|
6
|
+
Joi.object({
|
|
7
|
+
statement: Joi.string().required(),
|
|
8
|
+
values: Joi.object().required(),
|
|
9
|
+
}),
|
|
10
|
+
Joi.object({
|
|
11
|
+
query: Joi.string().required(),
|
|
12
|
+
})
|
|
13
|
+
)
|
|
14
|
+
.required(),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
transaction,
|
|
19
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
// allow other fields for filtering
|
|
4
|
+
const listTableRows = Joi.object({
|
|
5
|
+
name: Joi.string().min(3).max(30).required(),
|
|
6
|
+
_page: Joi.number().integer().min(1).default(1),
|
|
7
|
+
_limit: Joi.number().integer().min(1).default(10),
|
|
8
|
+
_search: Joi.string(),
|
|
9
|
+
_ordering: Joi.string(),
|
|
10
|
+
_schema: Joi.string(),
|
|
11
|
+
_extend: Joi.string(),
|
|
12
|
+
}).unknown(true);
|
|
13
|
+
|
|
14
|
+
const insertRowInTable = Joi.object({
|
|
15
|
+
name: Joi.string().min(3).max(30).required(),
|
|
16
|
+
fields: Joi.object().required(),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const getRowInTableByPK = Joi.object({
|
|
20
|
+
name: Joi.string().min(3).max(30).required(),
|
|
21
|
+
pk: Joi.string().required(),
|
|
22
|
+
_field: Joi.string().min(3).max(30),
|
|
23
|
+
_schema: Joi.string(),
|
|
24
|
+
_extend: Joi.string(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const updateRowInTableByPK = Joi.object({
|
|
28
|
+
name: Joi.string().min(3).max(30).required(),
|
|
29
|
+
pk: Joi.string().required(),
|
|
30
|
+
fields: Joi.object().required(),
|
|
31
|
+
_field: Joi.string().min(3).max(30),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const deleteRowInTableByPK = Joi.object({
|
|
35
|
+
name: Joi.string().min(3).max(30).required(),
|
|
36
|
+
pk: Joi.string().required(),
|
|
37
|
+
_field: Joi.string().min(3).max(30),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
module.exports = {
|
|
41
|
+
listTableRows,
|
|
42
|
+
insertRowInTable,
|
|
43
|
+
getRowInTableByPK,
|
|
44
|
+
updateRowInTableByPK,
|
|
45
|
+
deleteRowInTableByPK,
|
|
46
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const Joi = require('joi');
|
|
2
|
+
|
|
3
|
+
const createTable = Joi.object({
|
|
4
|
+
name: Joi.string().min(2).max(30).required(),
|
|
5
|
+
schema: Joi.array()
|
|
6
|
+
.items(
|
|
7
|
+
Joi.object({
|
|
8
|
+
name: Joi.string().min(2).max(30).required(),
|
|
9
|
+
// type one of sqlite3 types
|
|
10
|
+
type: Joi.string()
|
|
11
|
+
.valid(
|
|
12
|
+
'TEXT',
|
|
13
|
+
'NUMERIC',
|
|
14
|
+
'INTEGER',
|
|
15
|
+
'REAL',
|
|
16
|
+
'BLOB',
|
|
17
|
+
'BOOLEAN',
|
|
18
|
+
'DATE',
|
|
19
|
+
'DATETIME'
|
|
20
|
+
)
|
|
21
|
+
.insensitive()
|
|
22
|
+
.required(),
|
|
23
|
+
default: Joi.any(),
|
|
24
|
+
notNull: Joi.boolean(),
|
|
25
|
+
unique: Joi.boolean(),
|
|
26
|
+
primaryKey: Joi.boolean(),
|
|
27
|
+
foreignKey: Joi.object({
|
|
28
|
+
table: Joi.string().min(2).max(30).required(),
|
|
29
|
+
column: Joi.string().min(2).max(30).required(),
|
|
30
|
+
onDelete: Joi.string()
|
|
31
|
+
.valid('CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT')
|
|
32
|
+
.insensitive()
|
|
33
|
+
.default('CASCADE'),
|
|
34
|
+
onUpdate: Joi.string()
|
|
35
|
+
.valid('CASCADE', 'SET NULL', 'SET DEFAULT', 'RESTRICT')
|
|
36
|
+
.insensitive()
|
|
37
|
+
.default('RESTRICT'),
|
|
38
|
+
}),
|
|
39
|
+
index: Joi.boolean(),
|
|
40
|
+
})
|
|
41
|
+
)
|
|
42
|
+
.required(),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const deleteTable = Joi.object({
|
|
46
|
+
name: Joi.string().min(3).max(30).required(),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
module.exports = {
|
|
50
|
+
createTable,
|
|
51
|
+
deleteTable,
|
|
52
|
+
};
|
|
File without changes
|