soul-cli 0.0.5 → 0.0.7
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 -0
- package/package.json +1 -1
- package/src/controllers/rows.js +170 -84
- package/src/controllers/tables.js +24 -9
- package/src/schemas/rows.js +4 -4
- package/src/swagger/swagger.json +10 -6
package/.env.sample
CHANGED
package/package.json
CHANGED
package/src/controllers/rows.js
CHANGED
|
@@ -34,8 +34,12 @@ const listTableRows = async (req, res) => {
|
|
|
34
34
|
description: 'Extend rows. e.g. ?_extend=user_id will return user data for each row.',
|
|
35
35
|
in: 'query',
|
|
36
36
|
}
|
|
37
|
+
#swagger.parameters['_filters'] = {
|
|
38
|
+
description: 'Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20.',
|
|
39
|
+
in: 'query',
|
|
40
|
+
}
|
|
37
41
|
*/
|
|
38
|
-
const { name } = req.params;
|
|
42
|
+
const { name: tableName } = req.params;
|
|
39
43
|
const {
|
|
40
44
|
_page = 1,
|
|
41
45
|
_limit = 10,
|
|
@@ -43,41 +47,48 @@ const listTableRows = async (req, res) => {
|
|
|
43
47
|
_ordering,
|
|
44
48
|
_schema,
|
|
45
49
|
_extend,
|
|
46
|
-
|
|
50
|
+
_filters = '',
|
|
47
51
|
} = req.query;
|
|
48
52
|
|
|
49
53
|
const page = parseInt(_page);
|
|
50
54
|
const limit = parseInt(_limit);
|
|
51
55
|
|
|
52
|
-
// if
|
|
53
|
-
//
|
|
54
|
-
// filtering
|
|
55
|
-
// e.g. ?name
|
|
56
|
+
// if _filters are provided, filter rows by them
|
|
57
|
+
// _filters consists of fields to filter by
|
|
58
|
+
// filtering is case insensitive
|
|
59
|
+
// e.g. ?_filters=name:John,age:20
|
|
56
60
|
// will filter by name like '%John%' and age like '%20%'
|
|
57
61
|
|
|
62
|
+
const filters = _filters.split(',').map((filter) => {
|
|
63
|
+
const [field, value] = filter.split(':');
|
|
64
|
+
return { field, value };
|
|
65
|
+
});
|
|
66
|
+
|
|
58
67
|
let whereString = '';
|
|
59
|
-
if (
|
|
68
|
+
if (_filters !== '') {
|
|
60
69
|
whereString += ' WHERE ';
|
|
61
|
-
whereString +=
|
|
62
|
-
.map((
|
|
70
|
+
whereString += filters
|
|
71
|
+
.map((filter) => `${tableName}.${filter.field} LIKE '%${filter.value}%'`)
|
|
63
72
|
.join(' AND ');
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
// if _search is provided, search rows by it
|
|
67
76
|
// e.g. ?_search=John will search for John in all fields of the table
|
|
68
|
-
// searching
|
|
77
|
+
// searching is case insensitive
|
|
69
78
|
if (_search) {
|
|
70
|
-
if (whereString) {
|
|
79
|
+
if (whereString !== '') {
|
|
71
80
|
whereString += ' AND ';
|
|
72
81
|
} else {
|
|
73
82
|
whereString += ' WHERE ';
|
|
74
83
|
}
|
|
75
84
|
try {
|
|
76
85
|
// get all fields of the table
|
|
77
|
-
const fields = db.prepare(`PRAGMA table_info(${
|
|
86
|
+
const fields = db.prepare(`PRAGMA table_info(${tableName})`).all();
|
|
87
|
+
whereString += '(';
|
|
78
88
|
whereString += fields
|
|
79
|
-
.map((field) => `${field.name} LIKE '%${_search}%'`)
|
|
89
|
+
.map((field) => `${tableName}.${field.name} LIKE '%${_search}%'`)
|
|
80
90
|
.join(' OR ');
|
|
91
|
+
whereString += ')';
|
|
81
92
|
} catch (error) {
|
|
82
93
|
return res.status(400).json({
|
|
83
94
|
message: error.message,
|
|
@@ -104,10 +115,10 @@ const listTableRows = async (req, res) => {
|
|
|
104
115
|
if (_schema) {
|
|
105
116
|
const schemaFields = _schema.split(',');
|
|
106
117
|
schemaFields.forEach((field) => {
|
|
107
|
-
schemaString += `${
|
|
118
|
+
schemaString += `${tableName}.${field},`;
|
|
108
119
|
});
|
|
109
120
|
} else {
|
|
110
|
-
schemaString = `${
|
|
121
|
+
schemaString = `${tableName}.*`;
|
|
111
122
|
}
|
|
112
123
|
|
|
113
124
|
// remove trailing comma
|
|
@@ -143,31 +154,36 @@ const listTableRows = async (req, res) => {
|
|
|
143
154
|
let extendString = '';
|
|
144
155
|
if (_extend) {
|
|
145
156
|
const extendFields = _extend.split(',');
|
|
146
|
-
extendFields.forEach((
|
|
157
|
+
extendFields.forEach((extendedField) => {
|
|
147
158
|
try {
|
|
148
159
|
const foreignKey = db
|
|
149
|
-
.prepare(`PRAGMA foreign_key_list(${
|
|
160
|
+
.prepare(`PRAGMA foreign_key_list(${tableName})`)
|
|
150
161
|
.all()
|
|
151
|
-
.find((fk) => fk.from ===
|
|
162
|
+
.find((fk) => fk.from === extendedField);
|
|
152
163
|
|
|
153
164
|
if (!foreignKey) {
|
|
154
165
|
throw new Error('Foreign key not found');
|
|
155
166
|
}
|
|
156
167
|
|
|
157
|
-
const { table } = foreignKey;
|
|
168
|
+
const { table: joinedTableName } = foreignKey;
|
|
158
169
|
|
|
159
|
-
const
|
|
170
|
+
const joinedTableFields = db
|
|
171
|
+
.prepare(`PRAGMA table_info(${joinedTableName})`)
|
|
172
|
+
.all();
|
|
160
173
|
|
|
161
|
-
extendString += ` LEFT JOIN ${
|
|
174
|
+
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
162
175
|
|
|
163
176
|
// joined fields will be returned in a new object called {field}_data e.g. author_id_data
|
|
164
177
|
const extendFieldsString =
|
|
165
178
|
'json_object( ' +
|
|
166
|
-
|
|
167
|
-
.map(
|
|
179
|
+
joinedTableFields
|
|
180
|
+
.map(
|
|
181
|
+
(joinedTableField) =>
|
|
182
|
+
`'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`
|
|
183
|
+
)
|
|
168
184
|
.join(', ') +
|
|
169
185
|
' ) as ' +
|
|
170
|
-
|
|
186
|
+
extendedField +
|
|
171
187
|
'_data';
|
|
172
188
|
|
|
173
189
|
if (schemaString) {
|
|
@@ -185,21 +201,34 @@ const listTableRows = async (req, res) => {
|
|
|
185
201
|
}
|
|
186
202
|
|
|
187
203
|
// get paginated rows
|
|
188
|
-
const query = `SELECT ${schemaString} FROM ${
|
|
204
|
+
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} ${whereString} ${orderString} LIMIT ${limit} OFFSET ${
|
|
189
205
|
limit * (page - 1)
|
|
190
206
|
}`;
|
|
191
207
|
|
|
192
208
|
try {
|
|
193
|
-
|
|
209
|
+
let data = db.prepare(query).all();
|
|
210
|
+
|
|
211
|
+
// parse json extended files
|
|
212
|
+
if (_extend) {
|
|
213
|
+
const extendFields = _extend.split(',');
|
|
214
|
+
data = data.map((row) => {
|
|
215
|
+
Object.keys(row).forEach((key) => {
|
|
216
|
+
if (extendFields.includes(key.replace('_data', ''))) {
|
|
217
|
+
row[key] = JSON.parse(row[key]);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
return row;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
194
223
|
|
|
195
224
|
// get total number of rows
|
|
196
225
|
const total = db
|
|
197
|
-
.prepare(`SELECT COUNT(*) as total FROM ${
|
|
226
|
+
.prepare(`SELECT COUNT(*) as total FROM ${tableName} ${whereString}`)
|
|
198
227
|
.get().total;
|
|
199
228
|
|
|
200
229
|
const next =
|
|
201
|
-
data.length === limit ? `/tables/${
|
|
202
|
-
const previous = page > 1 ? `/tables/${
|
|
230
|
+
data.length === limit ? `/tables/${tableName}?page=${page + 1}` : null;
|
|
231
|
+
const previous = page > 1 ? `/tables/${tableName}?page=${page - 1}` : null;
|
|
203
232
|
|
|
204
233
|
res.json({
|
|
205
234
|
data,
|
|
@@ -236,7 +265,7 @@ const insertRowInTable = async (req, res) => {
|
|
|
236
265
|
}
|
|
237
266
|
*/
|
|
238
267
|
|
|
239
|
-
const { name } = req.params;
|
|
268
|
+
const { name: tableName } = req.params;
|
|
240
269
|
const { fields } = req.body;
|
|
241
270
|
const fieldsString = Object.keys(fields).join(', ');
|
|
242
271
|
|
|
@@ -250,7 +279,13 @@ const insertRowInTable = async (req, res) => {
|
|
|
250
279
|
})
|
|
251
280
|
.join(', ');
|
|
252
281
|
|
|
253
|
-
|
|
282
|
+
let values = `(${fieldsString}) VALUES (${valuesString})`;
|
|
283
|
+
|
|
284
|
+
if (valuesString === '') {
|
|
285
|
+
values = 'DEFAULT VALUES';
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const query = `INSERT INTO ${tableName} ${values}`;
|
|
254
289
|
try {
|
|
255
290
|
const data = db.prepare(query).run();
|
|
256
291
|
|
|
@@ -315,7 +350,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
315
350
|
type: 'string'
|
|
316
351
|
}
|
|
317
352
|
|
|
318
|
-
#swagger.parameters['
|
|
353
|
+
#swagger.parameters['_lookup_field'] = {
|
|
319
354
|
in: 'query',
|
|
320
355
|
description: 'If you want to get field by any other field than primary key, use this parameter',
|
|
321
356
|
required: false,
|
|
@@ -323,17 +358,24 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
323
358
|
}
|
|
324
359
|
|
|
325
360
|
*/
|
|
326
|
-
const { name, pk } = req.params;
|
|
327
|
-
const {
|
|
361
|
+
const { name: tableName, pk } = req.params;
|
|
362
|
+
const { _lookup_field, _schema, _extend } = req.query;
|
|
328
363
|
|
|
329
|
-
let
|
|
364
|
+
let lookupField = _lookup_field;
|
|
330
365
|
|
|
331
|
-
if (!
|
|
366
|
+
if (!_lookup_field) {
|
|
332
367
|
// find the primary key of the table
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
368
|
+
try {
|
|
369
|
+
lookupField = db
|
|
370
|
+
.prepare(`PRAGMA table_info(${tableName})`)
|
|
371
|
+
.all()
|
|
372
|
+
.find((field) => field.pk === 1).name;
|
|
373
|
+
} catch (error) {
|
|
374
|
+
return res.status(400).json({
|
|
375
|
+
message: error.message,
|
|
376
|
+
error: error,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
337
379
|
}
|
|
338
380
|
|
|
339
381
|
// if _schema is provided, return only those fields
|
|
@@ -344,10 +386,10 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
344
386
|
if (_schema) {
|
|
345
387
|
const schemaFields = _schema.split(',');
|
|
346
388
|
schemaFields.forEach((field) => {
|
|
347
|
-
schemaString += `${
|
|
389
|
+
schemaString += `${tableName}.${field},`;
|
|
348
390
|
});
|
|
349
391
|
} else {
|
|
350
|
-
schemaString = `${
|
|
392
|
+
schemaString = `${tableName}.*`;
|
|
351
393
|
}
|
|
352
394
|
|
|
353
395
|
// remove trailing comma
|
|
@@ -383,31 +425,36 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
383
425
|
let extendString = '';
|
|
384
426
|
if (_extend) {
|
|
385
427
|
const extendFields = _extend.split(',');
|
|
386
|
-
extendFields.forEach((
|
|
428
|
+
extendFields.forEach((extendedField) => {
|
|
387
429
|
try {
|
|
388
430
|
const foreignKey = db
|
|
389
|
-
.prepare(`PRAGMA foreign_key_list(${
|
|
431
|
+
.prepare(`PRAGMA foreign_key_list(${tableName})`)
|
|
390
432
|
.all()
|
|
391
|
-
.find((fk) => fk.from ===
|
|
433
|
+
.find((fk) => fk.from === extendedField);
|
|
392
434
|
|
|
393
435
|
if (!foreignKey) {
|
|
394
436
|
throw new Error('Foreign key not found');
|
|
395
437
|
}
|
|
396
438
|
|
|
397
|
-
const { table } = foreignKey;
|
|
439
|
+
const { table: joinedTableName } = foreignKey;
|
|
398
440
|
|
|
399
|
-
const
|
|
441
|
+
const joinedTableFields = db
|
|
442
|
+
.prepare(`PRAGMA table_info(${joinedTableName})`)
|
|
443
|
+
.all();
|
|
400
444
|
|
|
401
|
-
extendString += ` LEFT JOIN ${
|
|
445
|
+
extendString += ` LEFT JOIN ${joinedTableName} ON ${joinedTableName}.${foreignKey.to} = ${tableName}.${extendedField}`;
|
|
402
446
|
|
|
403
447
|
// joined fields will be returned in a new object called {field}_data e.g. author_id_data
|
|
404
448
|
const extendFieldsString =
|
|
405
449
|
'json_object( ' +
|
|
406
|
-
|
|
407
|
-
.map(
|
|
450
|
+
joinedTableFields
|
|
451
|
+
.map(
|
|
452
|
+
(joinedTableField) =>
|
|
453
|
+
`'${joinedTableField.name}', ${joinedTableName}.${joinedTableField.name}`
|
|
454
|
+
)
|
|
408
455
|
.join(', ') +
|
|
409
456
|
' ) as ' +
|
|
410
|
-
|
|
457
|
+
extendedField +
|
|
411
458
|
'_data';
|
|
412
459
|
|
|
413
460
|
if (schemaString) {
|
|
@@ -424,13 +471,23 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
424
471
|
});
|
|
425
472
|
}
|
|
426
473
|
|
|
427
|
-
const query = `SELECT ${schemaString} FROM ${
|
|
474
|
+
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} = '${pk}'`;
|
|
428
475
|
|
|
429
476
|
try {
|
|
430
|
-
|
|
477
|
+
let data = db.prepare(query).get();
|
|
478
|
+
// parse json extended files
|
|
479
|
+
if (_extend) {
|
|
480
|
+
const extendFields = _extend.split(',');
|
|
481
|
+
|
|
482
|
+
Object.keys(data)
|
|
483
|
+
.filter((key) => extendFields.includes(key.replace('_data', '')))
|
|
484
|
+
.forEach((key) => {
|
|
485
|
+
data[key] = JSON.parse(data[key]);
|
|
486
|
+
});
|
|
487
|
+
}
|
|
431
488
|
|
|
432
489
|
if (!data) {
|
|
433
|
-
res.status(404).json({
|
|
490
|
+
return res.status(404).json({
|
|
434
491
|
message: 'Row not found',
|
|
435
492
|
error: 'not_found',
|
|
436
493
|
});
|
|
@@ -440,7 +497,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
440
497
|
});
|
|
441
498
|
}
|
|
442
499
|
} catch (error) {
|
|
443
|
-
res.status(400).json({
|
|
500
|
+
return res.status(400).json({
|
|
444
501
|
message: error.message,
|
|
445
502
|
error: error,
|
|
446
503
|
});
|
|
@@ -472,7 +529,7 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
472
529
|
schema: { $ref: "#/definitions/UpdateRowRequestBody" }
|
|
473
530
|
}
|
|
474
531
|
|
|
475
|
-
#swagger.parameters['
|
|
532
|
+
#swagger.parameters['_lookup_field'] = {
|
|
476
533
|
in: 'query',
|
|
477
534
|
description: 'If you want to update row by any other field than primary key, use this parameter',
|
|
478
535
|
required: false,
|
|
@@ -480,18 +537,25 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
480
537
|
}
|
|
481
538
|
*/
|
|
482
539
|
|
|
483
|
-
const { name, pk } = req.params;
|
|
540
|
+
const { name: tableName, pk } = req.params;
|
|
484
541
|
const { fields } = req.body;
|
|
485
|
-
const {
|
|
542
|
+
const { _lookup_field } = req.query;
|
|
486
543
|
|
|
487
|
-
let
|
|
544
|
+
let lookupField = _lookup_field;
|
|
488
545
|
|
|
489
|
-
if (!
|
|
546
|
+
if (!_lookup_field) {
|
|
490
547
|
// find the primary key of the table
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
548
|
+
try {
|
|
549
|
+
lookupField = db
|
|
550
|
+
.prepare(`PRAGMA table_info(${tableName})`)
|
|
551
|
+
.all()
|
|
552
|
+
.find((field) => field.pk === 1).name;
|
|
553
|
+
} catch (error) {
|
|
554
|
+
return res.status(400).json({
|
|
555
|
+
message: error.message,
|
|
556
|
+
error: error,
|
|
557
|
+
});
|
|
558
|
+
}
|
|
495
559
|
}
|
|
496
560
|
|
|
497
561
|
// wrap text values in quotes
|
|
@@ -505,7 +569,14 @@ const updateRowInTableByPK = async (req, res) => {
|
|
|
505
569
|
})
|
|
506
570
|
.join(', ');
|
|
507
571
|
|
|
508
|
-
|
|
572
|
+
if (fieldsString === '') {
|
|
573
|
+
return res.status(400).json({
|
|
574
|
+
message: 'No fields provided',
|
|
575
|
+
error: 'no_fields_provided',
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} = '${pk}'`;
|
|
509
580
|
try {
|
|
510
581
|
db.prepare(query).run();
|
|
511
582
|
|
|
@@ -537,7 +608,7 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
537
608
|
description: 'Primary key',
|
|
538
609
|
required: true,
|
|
539
610
|
}
|
|
540
|
-
#swagger.parameters['
|
|
611
|
+
#swagger.parameters['_lookup_field'] = {
|
|
541
612
|
in: 'query',
|
|
542
613
|
description: 'If you want to delete row by any other field than primary key, use this parameter',
|
|
543
614
|
required: false,
|
|
@@ -545,30 +616,45 @@ const deleteRowInTableByPK = async (req, res) => {
|
|
|
545
616
|
}
|
|
546
617
|
|
|
547
618
|
*/
|
|
548
|
-
const { name, pk } = req.params;
|
|
549
|
-
const {
|
|
619
|
+
const { name: tableName, pk } = req.params;
|
|
620
|
+
const { _lookup_field } = req.query;
|
|
550
621
|
|
|
551
|
-
let
|
|
622
|
+
let lookupField = _lookup_field;
|
|
552
623
|
|
|
553
|
-
if (!
|
|
624
|
+
if (!_lookup_field) {
|
|
554
625
|
// find the primary key of the table
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
626
|
+
try {
|
|
627
|
+
lookupField = db
|
|
628
|
+
.prepare(`PRAGMA table_info(${tableName})`)
|
|
629
|
+
.all()
|
|
630
|
+
.find((field) => field.pk === 1).name;
|
|
631
|
+
} catch (error) {
|
|
632
|
+
return res.status(400).json({
|
|
633
|
+
message: error.message,
|
|
634
|
+
error: error,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
559
637
|
}
|
|
560
638
|
|
|
561
|
-
const query = `DELETE FROM ${
|
|
562
|
-
const data = db.prepare(query).run();
|
|
639
|
+
const query = `DELETE FROM ${tableName} WHERE ${lookupField} = '${pk}'`;
|
|
563
640
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
641
|
+
try {
|
|
642
|
+
const data = db.prepare(query).run();
|
|
643
|
+
|
|
644
|
+
if (data.changes === 0) {
|
|
645
|
+
res.status(404).json({
|
|
646
|
+
error: 'not_found',
|
|
647
|
+
});
|
|
648
|
+
} else {
|
|
649
|
+
res.json({
|
|
650
|
+
message: 'Row deleted',
|
|
651
|
+
data,
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
} catch (error) {
|
|
655
|
+
res.status(400).json({
|
|
656
|
+
message: error.message,
|
|
657
|
+
error: error,
|
|
572
658
|
});
|
|
573
659
|
}
|
|
574
660
|
};
|
|
@@ -13,12 +13,13 @@ const createTable = async (req, res) => {
|
|
|
13
13
|
}
|
|
14
14
|
*/
|
|
15
15
|
const {
|
|
16
|
-
name,
|
|
16
|
+
name: tableName,
|
|
17
17
|
schema,
|
|
18
18
|
autoAddCreatedAt = true,
|
|
19
19
|
autoAddUpdatedAt = true,
|
|
20
20
|
} = req.body;
|
|
21
21
|
|
|
22
|
+
let indices = [];
|
|
22
23
|
let schemaString = schema
|
|
23
24
|
// support name, type, default, not null, unique, primary key, foreign key, index
|
|
24
25
|
// e.g. { name: 'id', type: 'INTEGER', primaryKey: true }
|
|
@@ -57,7 +58,7 @@ const createTable = async (req, res) => {
|
|
|
57
58
|
column += ` ON UPDATE ${foreignKey.onUpdate}`;
|
|
58
59
|
}
|
|
59
60
|
if (index) {
|
|
60
|
-
|
|
61
|
+
indices.push(name);
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
return column;
|
|
@@ -88,11 +89,25 @@ const createTable = async (req, res) => {
|
|
|
88
89
|
`;
|
|
89
90
|
}
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
let indicesString = indices
|
|
93
|
+
.map((field) => {
|
|
94
|
+
return `
|
|
95
|
+
CREATE INDEX ${tableName}_${field}_index
|
|
96
|
+
ON ${tableName} (${field})
|
|
97
|
+
`;
|
|
98
|
+
})
|
|
99
|
+
.join(';');
|
|
100
|
+
|
|
101
|
+
const query = `CREATE TABLE ${tableName} (${schemaString})`;
|
|
102
|
+
|
|
92
103
|
try {
|
|
93
104
|
db.prepare(query).run();
|
|
94
105
|
|
|
95
|
-
|
|
106
|
+
if (indicesString) {
|
|
107
|
+
db.prepare(indicesString).run();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const generatedSchema = db.prepare(`PRAGMA table_info(${tableName})`).all();
|
|
96
111
|
|
|
97
112
|
/*
|
|
98
113
|
#swagger.responses[201] = {
|
|
@@ -105,7 +120,7 @@ const createTable = async (req, res) => {
|
|
|
105
120
|
res.status(201).json({
|
|
106
121
|
message: 'Table created',
|
|
107
122
|
data: {
|
|
108
|
-
name,
|
|
123
|
+
name: tableName,
|
|
109
124
|
schema: generatedSchema,
|
|
110
125
|
},
|
|
111
126
|
});
|
|
@@ -191,8 +206,8 @@ const getTableSchema = async (req, res) => {
|
|
|
191
206
|
}
|
|
192
207
|
|
|
193
208
|
*/
|
|
194
|
-
const { name } = req.params;
|
|
195
|
-
const query = `PRAGMA table_info(${
|
|
209
|
+
const { name: tableName } = req.params;
|
|
210
|
+
const query = `PRAGMA table_info(${tableName})`;
|
|
196
211
|
try {
|
|
197
212
|
const schema = db.prepare(query).all();
|
|
198
213
|
|
|
@@ -221,8 +236,8 @@ const deleteTable = async (req, res) => {
|
|
|
221
236
|
}
|
|
222
237
|
|
|
223
238
|
*/
|
|
224
|
-
const { name } = req.params;
|
|
225
|
-
const query = `DROP TABLE ${
|
|
239
|
+
const { name: tableName } = req.params;
|
|
240
|
+
const query = `DROP TABLE ${tableName}`;
|
|
226
241
|
try {
|
|
227
242
|
db.prepare(query).run();
|
|
228
243
|
|
package/src/schemas/rows.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const Joi = require('joi');
|
|
2
2
|
|
|
3
|
-
// allow other fields for filtering
|
|
4
3
|
const listTableRows = Joi.object({
|
|
5
4
|
name: Joi.string().min(3).max(30).required(),
|
|
6
5
|
_page: Joi.number().integer().min(1).default(1),
|
|
@@ -9,6 +8,7 @@ const listTableRows = Joi.object({
|
|
|
9
8
|
_ordering: Joi.string(),
|
|
10
9
|
_schema: Joi.string(),
|
|
11
10
|
_extend: Joi.string(),
|
|
11
|
+
_filters: Joi.string(),
|
|
12
12
|
}).unknown(true);
|
|
13
13
|
|
|
14
14
|
const insertRowInTable = Joi.object({
|
|
@@ -19,7 +19,7 @@ const insertRowInTable = Joi.object({
|
|
|
19
19
|
const getRowInTableByPK = Joi.object({
|
|
20
20
|
name: Joi.string().min(3).max(30).required(),
|
|
21
21
|
pk: Joi.string().required(),
|
|
22
|
-
|
|
22
|
+
_lookup_field: Joi.string().min(3).max(30),
|
|
23
23
|
_schema: Joi.string(),
|
|
24
24
|
_extend: Joi.string(),
|
|
25
25
|
});
|
|
@@ -28,13 +28,13 @@ const updateRowInTableByPK = Joi.object({
|
|
|
28
28
|
name: Joi.string().min(3).max(30).required(),
|
|
29
29
|
pk: Joi.string().required(),
|
|
30
30
|
fields: Joi.object().required(),
|
|
31
|
-
|
|
31
|
+
_lookup_field: Joi.string().min(3).max(30),
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
const deleteRowInTableByPK = Joi.object({
|
|
35
35
|
name: Joi.string().min(3).max(30).required(),
|
|
36
36
|
pk: Joi.string().required(),
|
|
37
|
-
|
|
37
|
+
_lookup_field: Joi.string().min(3).max(30),
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
module.exports = {
|
package/src/swagger/swagger.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"swagger": "2.0",
|
|
3
3
|
"info": {
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.7",
|
|
5
5
|
"title": "Soul API",
|
|
6
6
|
"description": "API Documentation for <b>Soul</b>, a simple SQLite RESTful server. "
|
|
7
7
|
},
|
|
@@ -237,12 +237,13 @@
|
|
|
237
237
|
"type": "string"
|
|
238
238
|
},
|
|
239
239
|
{
|
|
240
|
-
"name": "
|
|
240
|
+
"name": "_filters",
|
|
241
|
+
"description": "Filter rows. e.g. ?_filters=name:John,age:20 will return rows where name is John and age is 20.",
|
|
241
242
|
"in": "query",
|
|
242
243
|
"type": "string"
|
|
243
244
|
},
|
|
244
245
|
{
|
|
245
|
-
"name": "
|
|
246
|
+
"name": "_search",
|
|
246
247
|
"in": "query",
|
|
247
248
|
"type": "string"
|
|
248
249
|
}
|
|
@@ -332,7 +333,7 @@
|
|
|
332
333
|
"type": "string"
|
|
333
334
|
},
|
|
334
335
|
{
|
|
335
|
-
"name": "
|
|
336
|
+
"name": "_lookup_field",
|
|
336
337
|
"in": "query",
|
|
337
338
|
"description": "If you want to get field by any other field than primary key, use this parameter",
|
|
338
339
|
"required": false,
|
|
@@ -381,7 +382,7 @@
|
|
|
381
382
|
}
|
|
382
383
|
},
|
|
383
384
|
{
|
|
384
|
-
"name": "
|
|
385
|
+
"name": "_lookup_field",
|
|
385
386
|
"in": "query",
|
|
386
387
|
"description": "If you want to update row by any other field than primary key, use this parameter",
|
|
387
388
|
"required": false,
|
|
@@ -439,7 +440,7 @@
|
|
|
439
440
|
"description": "Primary key"
|
|
440
441
|
},
|
|
441
442
|
{
|
|
442
|
-
"name": "
|
|
443
|
+
"name": "_lookup_field",
|
|
443
444
|
"in": "query",
|
|
444
445
|
"description": "If you want to delete row by any other field than primary key, use this parameter",
|
|
445
446
|
"required": false,
|
|
@@ -450,6 +451,9 @@
|
|
|
450
451
|
"200": {
|
|
451
452
|
"description": "OK"
|
|
452
453
|
},
|
|
454
|
+
"400": {
|
|
455
|
+
"description": "Bad Request"
|
|
456
|
+
},
|
|
453
457
|
"404": {
|
|
454
458
|
"description": "Not Found"
|
|
455
459
|
}
|