soul-cli 0.4.0 → 0.6.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.
- package/.env.sample +4 -2
- package/package.json +3 -2
- package/src/cli.js +6 -0
- package/src/config/index.js +7 -3
- package/src/controllers/rows.js +134 -63
- package/src/controllers/rows.test.js +90 -0
- package/src/server.js +20 -1
- package/src/services/index.js +6 -0
- package/src/services/rowService.js +69 -0
- package/src/services/tableService.js +5 -0
- package/src/swagger/swagger.json +2 -2
- package/src/tests/index.js +8 -4
- package/src/tests/testData.js +30 -0
package/.env.sample
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
CORE_PORT=8000
|
|
2
2
|
|
|
3
3
|
NODE_ENV=development
|
|
4
4
|
|
|
5
5
|
CORS_ORIGIN_WHITELIST=http://localhost:3000,http://127.0.0.1:3000
|
|
6
6
|
|
|
7
|
-
RATE_LIMIT_ENABLED=
|
|
7
|
+
RATE_LIMIT_ENABLED=false
|
|
8
8
|
RATE_LIMIT_WINDOW_MS=1000
|
|
9
9
|
RATE_LIMIT_MAX_REQUESTS=10
|
|
10
10
|
|
|
11
11
|
DB=foobar.db
|
|
12
|
+
|
|
13
|
+
START_WITH_STUDIO=false
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A SQLite REST and Realtime server",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"dev": "npm run swagger-autogen && cross-env NO_CLI=true nodemon src/server.js",
|
|
12
12
|
"cli": "nodemon src/server.js --database foobar.db",
|
|
13
13
|
"swagger-autogen": "cross-env NO_CLI=true node src/swagger/index.js",
|
|
14
|
-
"test": "cross-env NODE_ENV=test NO_CLI=true DB=test.db
|
|
14
|
+
"test": "cross-env NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"express-rate-limit": "^6.7.0",
|
|
33
33
|
"express-winston": "^4.2.0",
|
|
34
34
|
"joi": "^17.8.3",
|
|
35
|
+
"soul-studio": "^0.0.1",
|
|
35
36
|
"swagger-ui-express": "^4.6.1",
|
|
36
37
|
"winston": "^3.8.2",
|
|
37
38
|
"ws": "^8.12.1",
|
package/src/cli.js
CHANGED
package/src/config/index.js
CHANGED
|
@@ -10,7 +10,7 @@ dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
|
10
10
|
|
|
11
11
|
const envVarsSchema = Joi.object()
|
|
12
12
|
.keys({
|
|
13
|
-
|
|
13
|
+
CORE_PORT: Joi.number().positive().default(8000),
|
|
14
14
|
|
|
15
15
|
NODE_ENV: Joi.string()
|
|
16
16
|
.valid('production', 'development', 'test')
|
|
@@ -26,6 +26,8 @@ const envVarsSchema = Joi.object()
|
|
|
26
26
|
RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().default(10),
|
|
27
27
|
|
|
28
28
|
EXTENSIONS: Joi.string().default(null),
|
|
29
|
+
|
|
30
|
+
START_WITH_STUDIO: Joi.boolean().default(false)
|
|
29
31
|
})
|
|
30
32
|
.unknown();
|
|
31
33
|
|
|
@@ -34,7 +36,7 @@ const env = {
|
|
|
34
36
|
};
|
|
35
37
|
|
|
36
38
|
if (argv.port) {
|
|
37
|
-
env.
|
|
39
|
+
env.CORE_PORT = argv.port;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
if (argv.verbose) {
|
|
@@ -68,7 +70,7 @@ module.exports = {
|
|
|
68
70
|
isDevelopment: envVars.NODE_ENV === 'development',
|
|
69
71
|
isTest: envVars.NODE_ENV === 'test',
|
|
70
72
|
|
|
71
|
-
port: argv.port || envVars.
|
|
73
|
+
port: argv.port || envVars.CORE_PORT,
|
|
72
74
|
verbose: argv['verbose'] || envVars.VERBOSE,
|
|
73
75
|
|
|
74
76
|
db: {
|
|
@@ -87,4 +89,6 @@ module.exports = {
|
|
|
87
89
|
extensions: {
|
|
88
90
|
path: argv.extensions || envVars.EXTENSIONS,
|
|
89
91
|
},
|
|
92
|
+
|
|
93
|
+
startWithStudio: argv.studio || envVars.START_WITH_STUDIO
|
|
90
94
|
};
|
package/src/controllers/rows.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const db = require('../db/index');
|
|
2
|
+
const { rowService } = require('../services');
|
|
2
3
|
|
|
3
4
|
const quotePrimaryKeys = (pks) => {
|
|
4
5
|
const primaryKeys = pks.split(',');
|
|
@@ -6,6 +7,17 @@ const quotePrimaryKeys = (pks) => {
|
|
|
6
7
|
return quotedPks;
|
|
7
8
|
};
|
|
8
9
|
|
|
10
|
+
const operators = {
|
|
11
|
+
eq: '=',
|
|
12
|
+
lt: '<',
|
|
13
|
+
gt: '>',
|
|
14
|
+
lte: '<=',
|
|
15
|
+
gte: '>=',
|
|
16
|
+
neq: '!=',
|
|
17
|
+
null: 'IS NULL',
|
|
18
|
+
notnull: 'IS NOT NULL',
|
|
19
|
+
};
|
|
20
|
+
|
|
9
21
|
// Return paginated rows of a table
|
|
10
22
|
const listTableRows = async (req, res) => {
|
|
11
23
|
/*
|
|
@@ -45,6 +57,7 @@ const listTableRows = async (req, res) => {
|
|
|
45
57
|
in: 'query',
|
|
46
58
|
}
|
|
47
59
|
*/
|
|
60
|
+
let params = '';
|
|
48
61
|
const { name: tableName } = req.params;
|
|
49
62
|
const {
|
|
50
63
|
_page = 1,
|
|
@@ -53,7 +66,7 @@ const listTableRows = async (req, res) => {
|
|
|
53
66
|
_ordering,
|
|
54
67
|
_schema,
|
|
55
68
|
_extend,
|
|
56
|
-
_filters = ''
|
|
69
|
+
_filters = ''
|
|
57
70
|
} = req.query;
|
|
58
71
|
|
|
59
72
|
const page = parseInt(_page);
|
|
@@ -64,18 +77,61 @@ const listTableRows = async (req, res) => {
|
|
|
64
77
|
// filtering is case insensitive
|
|
65
78
|
// e.g. ?_filters=name:John,age:20
|
|
66
79
|
// will filter by name like '%John%' and age like '%20%'
|
|
80
|
+
let filters = [];
|
|
81
|
+
|
|
82
|
+
// split the filters by comma(,) except when in an array
|
|
83
|
+
const re = /(\w+:?(\[.*?\]|\w+)?)/g;
|
|
84
|
+
try {
|
|
85
|
+
filters = _filters.match(re)?.map((filter) => {
|
|
86
|
+
let [key, value] = filter.split(':');
|
|
87
|
+
|
|
88
|
+
let field = key.split('__')[0];
|
|
89
|
+
let fieldOperator = key.split('__')[1];
|
|
90
|
+
|
|
91
|
+
if (!fieldOperator) {
|
|
92
|
+
fieldOperator = 'eq';
|
|
93
|
+
} else if (!operators[fieldOperator]) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`Invalid field operator '${fieldOperator}' for field '${field}'. You can only use the following operators after the '${field}' field: __lt, __gt, __lte, __gte, __eq, __neq.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
67
98
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
99
|
+
let operator = operators[fieldOperator];
|
|
100
|
+
if (['null', 'notnull'].includes(fieldOperator)) {
|
|
101
|
+
value = null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { field, operator, value };
|
|
105
|
+
});
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return res.status(400).json({
|
|
108
|
+
message: error.message,
|
|
109
|
+
error: error
|
|
110
|
+
});
|
|
111
|
+
}
|
|
72
112
|
|
|
73
113
|
let whereString = '';
|
|
114
|
+
const whereStringValues = [];
|
|
115
|
+
|
|
74
116
|
if (_filters !== '') {
|
|
75
117
|
whereString += ' WHERE ';
|
|
76
118
|
whereString += filters
|
|
77
|
-
.map((filter) =>
|
|
119
|
+
.map((filter) => {
|
|
120
|
+
if (filter.value) {
|
|
121
|
+
if (filter.value.startsWith('[') && filter.value.endsWith(']')) {
|
|
122
|
+
const arrayValues = filter.value.slice(1, -1).split(',');
|
|
123
|
+
return `${tableName}.${filter.field} IN (${arrayValues
|
|
124
|
+
.map((val) => `'${val}'`)
|
|
125
|
+
.join(', ')})`;
|
|
126
|
+
} else {
|
|
127
|
+
return `${tableName}.${filter.field} ${filter.operator} '${filter.value}'`;
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
return `${tableName}.${filter.field} ${filter.operator}`;
|
|
131
|
+
}
|
|
132
|
+
})
|
|
78
133
|
.join(' AND ');
|
|
134
|
+
params = `_filters=${_filters}&`;
|
|
79
135
|
}
|
|
80
136
|
|
|
81
137
|
// if _search is provided, search rows by it
|
|
@@ -95,10 +151,11 @@ const listTableRows = async (req, res) => {
|
|
|
95
151
|
.map((field) => `${tableName}.${field.name} LIKE '%${_search}%'`)
|
|
96
152
|
.join(' OR ');
|
|
97
153
|
whereString += ')';
|
|
154
|
+
params += `_search=${_search}&`;
|
|
98
155
|
} catch (error) {
|
|
99
156
|
return res.status(400).json({
|
|
100
157
|
message: error.message,
|
|
101
|
-
error: error
|
|
158
|
+
error: error
|
|
102
159
|
});
|
|
103
160
|
}
|
|
104
161
|
}
|
|
@@ -111,6 +168,7 @@ const listTableRows = async (req, res) => {
|
|
|
111
168
|
orderString += ` ORDER BY ${_ordering.replace('-', '')} ${
|
|
112
169
|
_ordering.startsWith('-') ? 'DESC' : 'ASC'
|
|
113
170
|
}`;
|
|
171
|
+
params += `_ordering=${_ordering}&`;
|
|
114
172
|
}
|
|
115
173
|
|
|
116
174
|
// if _schema is provided, return only those fields
|
|
@@ -123,6 +181,7 @@ const listTableRows = async (req, res) => {
|
|
|
123
181
|
schemaFields.forEach((field) => {
|
|
124
182
|
schemaString += `${tableName}.${field},`;
|
|
125
183
|
});
|
|
184
|
+
params += `_schema=${_schema}&`;
|
|
126
185
|
} else {
|
|
127
186
|
schemaString = `${tableName}.*`;
|
|
128
187
|
}
|
|
@@ -157,7 +216,9 @@ const listTableRows = async (req, res) => {
|
|
|
157
216
|
// }
|
|
158
217
|
// }
|
|
159
218
|
|
|
219
|
+
let foreignKeyError = { error: '', message: '' };
|
|
160
220
|
let extendString = '';
|
|
221
|
+
|
|
161
222
|
if (_extend) {
|
|
162
223
|
const extendFields = _extend.split(',');
|
|
163
224
|
extendFields.forEach((extendedField) => {
|
|
@@ -168,7 +229,9 @@ const listTableRows = async (req, res) => {
|
|
|
168
229
|
.find((fk) => fk.from === extendedField);
|
|
169
230
|
|
|
170
231
|
if (!foreignKey) {
|
|
171
|
-
throw new Error(
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Foreign key not found for extended field '${extendedField}'`
|
|
234
|
+
);
|
|
172
235
|
}
|
|
173
236
|
|
|
174
237
|
const { table: joinedTableName } = foreignKey;
|
|
@@ -198,21 +261,32 @@ const listTableRows = async (req, res) => {
|
|
|
198
261
|
|
|
199
262
|
schemaString += extendFieldsString;
|
|
200
263
|
} catch (error) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
error: error,
|
|
204
|
-
});
|
|
264
|
+
foreignKeyError.error = error;
|
|
265
|
+
foreignKeyError.message = error.message;
|
|
205
266
|
}
|
|
206
267
|
});
|
|
207
|
-
}
|
|
208
268
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
269
|
+
params += `_extend=${_extend}&`;
|
|
270
|
+
|
|
271
|
+
if (foreignKeyError.error) {
|
|
272
|
+
return res.status(400).json({
|
|
273
|
+
message: foreignKeyError.message,
|
|
274
|
+
error: foreignKeyError.error
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
213
278
|
|
|
214
279
|
try {
|
|
215
|
-
let data =
|
|
280
|
+
let data = rowService.get({
|
|
281
|
+
schemaString,
|
|
282
|
+
tableName,
|
|
283
|
+
extendString,
|
|
284
|
+
whereString,
|
|
285
|
+
orderString,
|
|
286
|
+
limit,
|
|
287
|
+
page: limit * (page - 1),
|
|
288
|
+
whereStringValues,
|
|
289
|
+
});
|
|
216
290
|
|
|
217
291
|
// parse json extended files
|
|
218
292
|
if (_extend) {
|
|
@@ -228,24 +302,35 @@ const listTableRows = async (req, res) => {
|
|
|
228
302
|
}
|
|
229
303
|
|
|
230
304
|
// get total number of rows
|
|
231
|
-
const total =
|
|
232
|
-
|
|
233
|
-
|
|
305
|
+
const total = rowService.getCount({
|
|
306
|
+
tableName,
|
|
307
|
+
whereString,
|
|
308
|
+
whereStringValues,
|
|
309
|
+
});
|
|
234
310
|
|
|
235
311
|
const next =
|
|
236
|
-
data.length === limit
|
|
237
|
-
|
|
312
|
+
data.length === limit
|
|
313
|
+
? `/tables/${tableName}/rows?${params}_limit=${_limit}&_page=${
|
|
314
|
+
page + 1
|
|
315
|
+
}`
|
|
316
|
+
: null;
|
|
317
|
+
const previous =
|
|
318
|
+
page > 1
|
|
319
|
+
? `/tables/${tableName}/rows?${params}_limit=${_limit}&_page=${
|
|
320
|
+
page - 1
|
|
321
|
+
}`
|
|
322
|
+
: null;
|
|
238
323
|
|
|
239
324
|
res.json({
|
|
240
325
|
data,
|
|
241
326
|
total,
|
|
242
327
|
next,
|
|
243
|
-
previous
|
|
328
|
+
previous
|
|
244
329
|
});
|
|
245
330
|
} catch (error) {
|
|
246
331
|
res.status(400).json({
|
|
247
332
|
message: error.message,
|
|
248
|
-
error: error
|
|
333
|
+
error: error
|
|
249
334
|
});
|
|
250
335
|
}
|
|
251
336
|
};
|
|
@@ -267,39 +352,26 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
267
352
|
in: 'body',
|
|
268
353
|
required: true,
|
|
269
354
|
type: 'object',
|
|
270
|
-
schema: { $ref:
|
|
355
|
+
schema: { $ref: '#/definitions/InsertRowRequestBody' }
|
|
271
356
|
}
|
|
272
357
|
*/
|
|
273
358
|
|
|
274
359
|
const { name: tableName } = req.params;
|
|
275
|
-
const { fields } = req.body;
|
|
276
|
-
const fieldsString = Object.keys(fields).join(', ');
|
|
360
|
+
const { fields: queryFields } = req.body;
|
|
277
361
|
|
|
278
|
-
//
|
|
279
|
-
const
|
|
280
|
-
.
|
|
281
|
-
|
|
282
|
-
return `'${value}'`;
|
|
283
|
-
}
|
|
284
|
-
return value;
|
|
285
|
-
})
|
|
286
|
-
.join(', ');
|
|
287
|
-
|
|
288
|
-
let values = `(${fieldsString}) VALUES (${valuesString})`;
|
|
289
|
-
|
|
290
|
-
if (valuesString === '') {
|
|
291
|
-
values = 'DEFAULT VALUES';
|
|
292
|
-
}
|
|
362
|
+
// Remove null values from fields for accurate query construction.
|
|
363
|
+
const fields = Object.fromEntries(
|
|
364
|
+
Object.entries(queryFields).filter(([_, value]) => value !== null)
|
|
365
|
+
);
|
|
293
366
|
|
|
294
|
-
const query = `INSERT INTO ${tableName} ${values}`;
|
|
295
367
|
try {
|
|
296
|
-
const data =
|
|
368
|
+
const data = rowService.save({ tableName, fields });
|
|
297
369
|
|
|
298
370
|
/*
|
|
299
371
|
#swagger.responses[201] = {
|
|
300
372
|
description: 'Row inserted successfully',
|
|
301
373
|
schema: {
|
|
302
|
-
$ref:
|
|
374
|
+
$ref: '#/definitions/InsertRowSuccessResponse'
|
|
303
375
|
}
|
|
304
376
|
}
|
|
305
377
|
*/
|
|
@@ -320,7 +392,7 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
320
392
|
#swagger.responses[400] = {
|
|
321
393
|
description: 'Bad request',
|
|
322
394
|
schema: {
|
|
323
|
-
$ref:
|
|
395
|
+
$ref: '#/definitions/InsertRowErrorResponse'
|
|
324
396
|
}
|
|
325
397
|
}
|
|
326
398
|
*/
|
|
@@ -485,12 +557,14 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
485
557
|
});
|
|
486
558
|
}
|
|
487
559
|
|
|
488
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${quotePrimaryKeys(
|
|
489
|
-
pks
|
|
490
|
-
)})`;
|
|
491
|
-
|
|
492
560
|
try {
|
|
493
|
-
let data =
|
|
561
|
+
let data = rowService.getById({
|
|
562
|
+
schemaString,
|
|
563
|
+
tableName,
|
|
564
|
+
extendString,
|
|
565
|
+
lookupField,
|
|
566
|
+
pks,
|
|
567
|
+
});
|
|
494
568
|
|
|
495
569
|
// parse json extended files
|
|
496
570
|
if (_extend) {
|
|
@@ -545,7 +619,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
545
619
|
in: 'body',
|
|
546
620
|
required: true,
|
|
547
621
|
type: 'object',
|
|
548
|
-
schema: { $ref:
|
|
622
|
+
schema: { $ref: '#/definitions/UpdateRowRequestBody' }
|
|
549
623
|
}
|
|
550
624
|
|
|
551
625
|
#swagger.parameters['_lookup_field'] = {
|
|
@@ -595,12 +669,13 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
595
669
|
});
|
|
596
670
|
}
|
|
597
671
|
|
|
598
|
-
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
599
|
-
pks
|
|
600
|
-
)})`;
|
|
601
|
-
|
|
602
672
|
try {
|
|
603
|
-
const data =
|
|
673
|
+
const data = rowService.update({
|
|
674
|
+
tableName,
|
|
675
|
+
fieldsString,
|
|
676
|
+
lookupField,
|
|
677
|
+
pks,
|
|
678
|
+
});
|
|
604
679
|
|
|
605
680
|
res.json({
|
|
606
681
|
message: 'Row updated',
|
|
@@ -668,12 +743,8 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
668
743
|
}
|
|
669
744
|
}
|
|
670
745
|
|
|
671
|
-
const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
672
|
-
pks
|
|
673
|
-
)})`;
|
|
674
|
-
|
|
675
746
|
try {
|
|
676
|
-
const data =
|
|
747
|
+
const data = rowService.delete({ tableName, lookupField, pks });
|
|
677
748
|
|
|
678
749
|
if (data.changes === 0) {
|
|
679
750
|
res.status(404).json({
|
|
@@ -3,6 +3,14 @@ const supertest = require('supertest');
|
|
|
3
3
|
const app = require('../index');
|
|
4
4
|
const requestWithSupertest = supertest(app);
|
|
5
5
|
|
|
6
|
+
function queryString(params) {
|
|
7
|
+
const queryString = Object.keys(params)
|
|
8
|
+
.map((key) => `${key}=${params[key]}`)
|
|
9
|
+
.join('&');
|
|
10
|
+
|
|
11
|
+
return queryString;
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
describe('Rows Endpoints', () => {
|
|
7
15
|
it('GET /tables/:name/rows should return a list of all rows', async () => {
|
|
8
16
|
const res = await requestWithSupertest.get('/api/tables/users/rows');
|
|
@@ -16,6 +24,51 @@ describe('Rows Endpoints', () => {
|
|
|
16
24
|
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
17
25
|
});
|
|
18
26
|
|
|
27
|
+
it('GET /tables/:name/rows?_limit=8&_schema=firstName,lastName&_ordering:-firstName&_page=2: should query the rows by the provided query params', async () => {
|
|
28
|
+
const params = {
|
|
29
|
+
_search: 'a',
|
|
30
|
+
_ordering: '-firstName',
|
|
31
|
+
_schema: 'firstName,lastName',
|
|
32
|
+
_limit: 8,
|
|
33
|
+
_page: 2,
|
|
34
|
+
};
|
|
35
|
+
const query = queryString(params);
|
|
36
|
+
const res = await requestWithSupertest.get(
|
|
37
|
+
`/api/tables/users/rows?${query}`
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(res.status).toEqual(200);
|
|
41
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
42
|
+
expect(res.body).toHaveProperty('data');
|
|
43
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
44
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
45
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
46
|
+
|
|
47
|
+
expect(res.body.next).toEqual(
|
|
48
|
+
`/tables/users/rows?${queryString({
|
|
49
|
+
...params,
|
|
50
|
+
_page: params._page + 1,
|
|
51
|
+
}).toString()}`
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(res.body.previous).toEqual(
|
|
55
|
+
`/tables/users/rows?${queryString({
|
|
56
|
+
...params,
|
|
57
|
+
_page: params._page - 1,
|
|
58
|
+
}).toString()}`
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('GET /tables/:name/rows: should return a null field', async () => {
|
|
63
|
+
const res = await requestWithSupertest.get(
|
|
64
|
+
'/api/tables/users/rows?_filters=firstName__null,lastName__notnull'
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(res.status).toEqual(200);
|
|
68
|
+
expect(res.body.data[0].firstName).toBeNull();
|
|
69
|
+
expect(res.body.data[0].lastName).not.toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
19
72
|
it('POST /tables/:name/rows should insert a new row and return the lastInsertRowid', async () => {
|
|
20
73
|
const res = await requestWithSupertest
|
|
21
74
|
.post('/api/tables/users/rows')
|
|
@@ -48,4 +101,41 @@ describe('Rows Endpoints', () => {
|
|
|
48
101
|
expect(res.status).toEqual(200);
|
|
49
102
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
50
103
|
});
|
|
104
|
+
|
|
105
|
+
it('POST /tables/:name/rows should insert a new row if any of the value of the object being inserted is null', async () => {
|
|
106
|
+
const res = await requestWithSupertest
|
|
107
|
+
.post('/api/tables/users/rows')
|
|
108
|
+
.send({
|
|
109
|
+
fields: {
|
|
110
|
+
firstName: null,
|
|
111
|
+
lastName: 'Doe',
|
|
112
|
+
email: null,
|
|
113
|
+
username: 'Jane'
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
expect(res.status).toEqual(201);
|
|
117
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
118
|
+
expect(res.body).toHaveProperty('data');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('GET /tables/:name/rows should return values if any of the IDs from the array match the user ID.', async () => {
|
|
122
|
+
const res = await requestWithSupertest.get(
|
|
123
|
+
'/api/tables/users/rows?_filters=id:[2,3]'
|
|
124
|
+
);
|
|
125
|
+
expect(res.status).toEqual(200);
|
|
126
|
+
expect(res.body).toHaveProperty('data');
|
|
127
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
128
|
+
expect(res.body.data.length).toEqual(2);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('GET /tables/:name/rows should return values if the provided ID matches the user ID.', async () => {
|
|
132
|
+
const res = await requestWithSupertest.get(
|
|
133
|
+
'/api/tables/users/rows?_filters=id:2,firstName:Michael,lastName:Lee'
|
|
134
|
+
);
|
|
135
|
+
expect(res.status).toEqual(200);
|
|
136
|
+
expect(res.body).toHaveProperty('data');
|
|
137
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
138
|
+
expect(res.body.data.length).toEqual(1);
|
|
139
|
+
});
|
|
140
|
+
|
|
51
141
|
});
|
package/src/server.js
CHANGED
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
const http = require('http');
|
|
4
|
+
const express = require('express');
|
|
4
5
|
|
|
5
6
|
const app = require('./index');
|
|
6
7
|
const { wss } = require('./websocket');
|
|
7
8
|
const config = require('./config/index');
|
|
8
9
|
|
|
10
|
+
if (config.startWithStudio) {
|
|
11
|
+
(async () => {
|
|
12
|
+
const { handler: soulStudioHandler } = await import('soul-studio/build/handler.js');
|
|
13
|
+
app.use('/studio', soulStudioHandler);
|
|
14
|
+
})();
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
const server = http.createServer(app);
|
|
10
18
|
|
|
11
19
|
const port = config.port;
|
|
20
|
+
|
|
21
|
+
const baseURL = `http://localhost:${port}`;
|
|
22
|
+
const coreURL = `${baseURL}/api/`;
|
|
23
|
+
const studioURL = `${baseURL}/studio/`;
|
|
24
|
+
|
|
12
25
|
server.listen(port, () => {
|
|
13
|
-
console.log(`Soul is running
|
|
26
|
+
console.log(`Soul is running...`);
|
|
27
|
+
console.log(` > Core API at ${coreURL}`);
|
|
28
|
+
|
|
29
|
+
if (config.startWithStudio) {
|
|
30
|
+
console.log(` > Studio at ${studioURL}`);
|
|
31
|
+
require('child_process').exec(`open ${studioURL}`);
|
|
32
|
+
}
|
|
14
33
|
});
|
|
15
34
|
|
|
16
35
|
server.on('upgrade', (request, socket, head) => {
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module.exports = (db) => {
|
|
2
|
+
return {
|
|
3
|
+
get(data) {
|
|
4
|
+
const query = `SELECT ${data.schemaString} FROM ${data.tableName} ${data.extendString} ${data.whereString} ${data.orderString} LIMIT ? OFFSET ?`;
|
|
5
|
+
const statement = db.prepare(query);
|
|
6
|
+
const result = statement.all(
|
|
7
|
+
...data.whereStringValues,
|
|
8
|
+
data.limit,
|
|
9
|
+
data.page
|
|
10
|
+
);
|
|
11
|
+
return result;
|
|
12
|
+
},
|
|
13
|
+
|
|
14
|
+
getById(data) {
|
|
15
|
+
const pks = data.pks.split(',');
|
|
16
|
+
const placeholders = pks.map((pk) => '?').join(',');
|
|
17
|
+
const query = `SELECT ${data.schemaString} FROM ${data.tableName} ${data.extendString} WHERE ${data.tableName}.${data.lookupField} in (${placeholders})`;
|
|
18
|
+
const statement = db.prepare(query);
|
|
19
|
+
const result = statement.all(...pks);
|
|
20
|
+
return result;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
getCount(data) {
|
|
24
|
+
const query = `SELECT COUNT(*) as total FROM ${data.tableName} ${data.whereString}`;
|
|
25
|
+
const statement = db.prepare(query);
|
|
26
|
+
const result = statement.get(...data.whereStringValues).total;
|
|
27
|
+
return result;
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
save(data) {
|
|
31
|
+
// wrap text values in quotes
|
|
32
|
+
const fieldsString = Object.keys(data.fields).join(', ');
|
|
33
|
+
|
|
34
|
+
// wrap text values in quotes
|
|
35
|
+
const valuesString = Object.values(data.fields).map((value) => value);
|
|
36
|
+
const placeholders = Object.values(data.fields)
|
|
37
|
+
.map(() => '?')
|
|
38
|
+
.join(',');
|
|
39
|
+
|
|
40
|
+
let values = `(${fieldsString}) VALUES (${placeholders})`;
|
|
41
|
+
if (valuesString === '') {
|
|
42
|
+
values = 'DEFAULT VALUES';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const query = `INSERT INTO ${data.tableName} ${values}`;
|
|
46
|
+
const statement = db.prepare(query);
|
|
47
|
+
const result = statement.run(...valuesString);
|
|
48
|
+
return result;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
update(data) {
|
|
52
|
+
const pks = data.pks.split(',');
|
|
53
|
+
const placeholders = pks.map((pk) => '?').join(',');
|
|
54
|
+
const query = `UPDATE ${data.tableName} SET ${data.fieldsString} WHERE ${data.lookupField} in (${placeholders})`;
|
|
55
|
+
const statement = db.prepare(query);
|
|
56
|
+
const result = statement.run(...pks);
|
|
57
|
+
return result;
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
delete(data) {
|
|
61
|
+
const pks = data.pks.split(',');
|
|
62
|
+
const placeholders = pks.map((pk) => '?').join(',');
|
|
63
|
+
const query = `DELETE FROM ${data.tableName} WHERE ${data.lookupField} in (${placeholders})`;
|
|
64
|
+
const statement = db.prepare(query);
|
|
65
|
+
const result = statement.run(...pks);
|
|
66
|
+
return result;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
};
|
package/src/swagger/swagger.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.2
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"title": "Soul API",
|
|
6
6
|
"description": "API Documentation for <b>Soul</b>, a SQLite REST and realtime server. "
|
|
7
7
|
},
|
|
@@ -755,4 +755,4 @@
|
|
|
755
755
|
"$ref": "#/definitions/Transaction"
|
|
756
756
|
}
|
|
757
757
|
}
|
|
758
|
-
}
|
|
758
|
+
}
|
package/src/tests/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const fs = require('fs');
|
|
|
2
2
|
const { unlink } = require('fs/promises');
|
|
3
3
|
|
|
4
4
|
const db = require('../db/index');
|
|
5
|
+
const { testNames } = require('./testData');
|
|
5
6
|
|
|
6
7
|
const dropTestTable = (table = 'users') => {
|
|
7
8
|
db.prepare(`DROP TABLE IF EXISTS ${table}`).run();
|
|
@@ -26,15 +27,18 @@ const dropTestDatabase = async (path = 'test.db') => {
|
|
|
26
27
|
|
|
27
28
|
const createTestTable = (table = 'users') => {
|
|
28
29
|
db.prepare(
|
|
29
|
-
`CREATE TABLE ${table} (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT)`
|
|
30
|
+
`CREATE TABLE ${table} (id INTEGER PRIMARY KEY, firstName TEXT, lastName TEXT, email TEXT, username TEXT)`
|
|
30
31
|
).run();
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
const insertIntoTestTable = (table = 'users') => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
'Doe'
|
|
35
|
+
const statement = db.prepare(
|
|
36
|
+
`INSERT INTO ${table} (firstName, lastName) VALUES (?, ?)`
|
|
37
37
|
);
|
|
38
|
+
|
|
39
|
+
for (const user of testNames) {
|
|
40
|
+
statement.run(user.firstName, user.lastName);
|
|
41
|
+
}
|
|
38
42
|
};
|
|
39
43
|
|
|
40
44
|
module.exports = {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const testNames = [
|
|
2
|
+
{ firstName: 'Emily', lastName: 'William' },
|
|
3
|
+
{ firstName: 'Michael', lastName: 'Lee' },
|
|
4
|
+
{ firstName: 'Sarah', lastName: 'Johnson' },
|
|
5
|
+
{ firstName: 'David', lastName: 'Chen' },
|
|
6
|
+
{ firstName: 'Olivia', lastName: 'William' },
|
|
7
|
+
{ firstName: 'William', lastName: 'Kim' },
|
|
8
|
+
{ firstName: 'Sophia', lastName: 'Singh' },
|
|
9
|
+
{ firstName: 'James', lastName: 'Rodriguez' },
|
|
10
|
+
{ firstName: 'Ava', lastName: 'Patel' },
|
|
11
|
+
{ firstName: 'Benjamin', lastName: 'Garcia' },
|
|
12
|
+
{ firstName: 'Isabella', lastName: 'Nguyen' },
|
|
13
|
+
{ firstName: 'Ethan', lastName: 'Lee' },
|
|
14
|
+
{ firstName: 'Mia', lastName: 'Wilson' },
|
|
15
|
+
{ firstName: 'Alexander', lastName: 'William' },
|
|
16
|
+
{ firstName: 'Charlotte', lastName: 'Hernandez' },
|
|
17
|
+
{ firstName: 'Liam', lastName: 'Gonzalez' },
|
|
18
|
+
{ firstName: 'Emma', lastName: 'Gomez' },
|
|
19
|
+
{ firstName: 'Noah', lastName: 'Perez' },
|
|
20
|
+
{ firstName: 'Avery', lastName: 'Ramirez' },
|
|
21
|
+
{ firstName: 'Jacob', lastName: 'Turner' },
|
|
22
|
+
{ firstName: 'Abigail', lastName: 'Williams' },
|
|
23
|
+
{ firstName: 'Elijah', lastName: 'Hall' },
|
|
24
|
+
{ firstName: 'Mila', lastName: 'Flores' },
|
|
25
|
+
{ firstName: 'Evelyn', lastName: 'Morales' },
|
|
26
|
+
{ firstName: 'Logan', lastName: 'Collins' },
|
|
27
|
+
{ firstName: null, lastName: 'Flores' },
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
module.exports = { testNames };
|