soul-cli 0.5.2 → 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/package.json
CHANGED
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(',');
|
|
@@ -65,7 +66,7 @@ const listTableRows = async (req, res) => {
|
|
|
65
66
|
_ordering,
|
|
66
67
|
_schema,
|
|
67
68
|
_extend,
|
|
68
|
-
_filters = ''
|
|
69
|
+
_filters = ''
|
|
69
70
|
} = req.query;
|
|
70
71
|
|
|
71
72
|
const page = parseInt(_page);
|
|
@@ -77,8 +78,11 @@ const listTableRows = async (req, res) => {
|
|
|
77
78
|
// e.g. ?_filters=name:John,age:20
|
|
78
79
|
// will filter by name like '%John%' and age like '%20%'
|
|
79
80
|
let filters = [];
|
|
81
|
+
|
|
82
|
+
// split the filters by comma(,) except when in an array
|
|
83
|
+
const re = /(\w+:?(\[.*?\]|\w+)?)/g;
|
|
80
84
|
try {
|
|
81
|
-
filters = _filters.
|
|
85
|
+
filters = _filters.match(re)?.map((filter) => {
|
|
82
86
|
let [key, value] = filter.split(':');
|
|
83
87
|
|
|
84
88
|
let field = key.split('__')[0];
|
|
@@ -88,7 +92,7 @@ const listTableRows = async (req, res) => {
|
|
|
88
92
|
fieldOperator = 'eq';
|
|
89
93
|
} else if (!operators[fieldOperator]) {
|
|
90
94
|
throw new Error(
|
|
91
|
-
`Invalid field operator
|
|
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.`
|
|
92
96
|
);
|
|
93
97
|
}
|
|
94
98
|
|
|
@@ -102,19 +106,30 @@ const listTableRows = async (req, res) => {
|
|
|
102
106
|
} catch (error) {
|
|
103
107
|
return res.status(400).json({
|
|
104
108
|
message: error.message,
|
|
105
|
-
error: error
|
|
109
|
+
error: error
|
|
106
110
|
});
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
let whereString = '';
|
|
114
|
+
const whereStringValues = [];
|
|
115
|
+
|
|
110
116
|
if (_filters !== '') {
|
|
111
117
|
whereString += ' WHERE ';
|
|
112
118
|
whereString += filters
|
|
113
|
-
.map((filter) =>
|
|
114
|
-
filter.value
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
})
|
|
118
133
|
.join(' AND ');
|
|
119
134
|
params = `_filters=${_filters}&`;
|
|
120
135
|
}
|
|
@@ -140,7 +155,7 @@ const listTableRows = async (req, res) => {
|
|
|
140
155
|
} catch (error) {
|
|
141
156
|
return res.status(400).json({
|
|
142
157
|
message: error.message,
|
|
143
|
-
error: error
|
|
158
|
+
error: error
|
|
144
159
|
});
|
|
145
160
|
}
|
|
146
161
|
}
|
|
@@ -256,18 +271,22 @@ const listTableRows = async (req, res) => {
|
|
|
256
271
|
if (foreignKeyError.error) {
|
|
257
272
|
return res.status(400).json({
|
|
258
273
|
message: foreignKeyError.message,
|
|
259
|
-
error: foreignKeyError.error
|
|
274
|
+
error: foreignKeyError.error
|
|
260
275
|
});
|
|
261
276
|
}
|
|
262
277
|
}
|
|
263
278
|
|
|
264
|
-
// get paginated rows
|
|
265
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} ${whereString} ${orderString} LIMIT ${limit} OFFSET ${
|
|
266
|
-
limit * (page - 1)
|
|
267
|
-
}`;
|
|
268
|
-
|
|
269
279
|
try {
|
|
270
|
-
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
|
+
});
|
|
271
290
|
|
|
272
291
|
// parse json extended files
|
|
273
292
|
if (_extend) {
|
|
@@ -283,9 +302,11 @@ const listTableRows = async (req, res) => {
|
|
|
283
302
|
}
|
|
284
303
|
|
|
285
304
|
// get total number of rows
|
|
286
|
-
const total =
|
|
287
|
-
|
|
288
|
-
|
|
305
|
+
const total = rowService.getCount({
|
|
306
|
+
tableName,
|
|
307
|
+
whereString,
|
|
308
|
+
whereStringValues,
|
|
309
|
+
});
|
|
289
310
|
|
|
290
311
|
const next =
|
|
291
312
|
data.length === limit
|
|
@@ -304,12 +325,12 @@ const listTableRows = async (req, res) => {
|
|
|
304
325
|
data,
|
|
305
326
|
total,
|
|
306
327
|
next,
|
|
307
|
-
previous
|
|
328
|
+
previous
|
|
308
329
|
});
|
|
309
330
|
} catch (error) {
|
|
310
331
|
res.status(400).json({
|
|
311
332
|
message: error.message,
|
|
312
|
-
error: error
|
|
333
|
+
error: error
|
|
313
334
|
});
|
|
314
335
|
}
|
|
315
336
|
};
|
|
@@ -331,39 +352,26 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
331
352
|
in: 'body',
|
|
332
353
|
required: true,
|
|
333
354
|
type: 'object',
|
|
334
|
-
schema: { $ref:
|
|
355
|
+
schema: { $ref: '#/definitions/InsertRowRequestBody' }
|
|
335
356
|
}
|
|
336
357
|
*/
|
|
337
358
|
|
|
338
359
|
const { name: tableName } = req.params;
|
|
339
|
-
const { fields } = req.body;
|
|
340
|
-
const fieldsString = Object.keys(fields).join(', ');
|
|
341
|
-
|
|
342
|
-
// wrap text values in quotes
|
|
343
|
-
const valuesString = Object.values(fields)
|
|
344
|
-
.map((value) => {
|
|
345
|
-
if (typeof value === 'string') {
|
|
346
|
-
return `'${value}'`;
|
|
347
|
-
}
|
|
348
|
-
return value;
|
|
349
|
-
})
|
|
350
|
-
.join(', ');
|
|
351
|
-
|
|
352
|
-
let values = `(${fieldsString}) VALUES (${valuesString})`;
|
|
360
|
+
const { fields: queryFields } = req.body;
|
|
353
361
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
362
|
+
// Remove null values from fields for accurate query construction.
|
|
363
|
+
const fields = Object.fromEntries(
|
|
364
|
+
Object.entries(queryFields).filter(([_, value]) => value !== null)
|
|
365
|
+
);
|
|
357
366
|
|
|
358
|
-
const query = `INSERT INTO ${tableName} ${values}`;
|
|
359
367
|
try {
|
|
360
|
-
const data =
|
|
368
|
+
const data = rowService.save({ tableName, fields });
|
|
361
369
|
|
|
362
370
|
/*
|
|
363
371
|
#swagger.responses[201] = {
|
|
364
372
|
description: 'Row inserted successfully',
|
|
365
373
|
schema: {
|
|
366
|
-
$ref:
|
|
374
|
+
$ref: '#/definitions/InsertRowSuccessResponse'
|
|
367
375
|
}
|
|
368
376
|
}
|
|
369
377
|
*/
|
|
@@ -384,7 +392,7 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
384
392
|
#swagger.responses[400] = {
|
|
385
393
|
description: 'Bad request',
|
|
386
394
|
schema: {
|
|
387
|
-
$ref:
|
|
395
|
+
$ref: '#/definitions/InsertRowErrorResponse'
|
|
388
396
|
}
|
|
389
397
|
}
|
|
390
398
|
*/
|
|
@@ -549,12 +557,14 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
549
557
|
});
|
|
550
558
|
}
|
|
551
559
|
|
|
552
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${quotePrimaryKeys(
|
|
553
|
-
pks
|
|
554
|
-
)})`;
|
|
555
|
-
|
|
556
560
|
try {
|
|
557
|
-
let data =
|
|
561
|
+
let data = rowService.getById({
|
|
562
|
+
schemaString,
|
|
563
|
+
tableName,
|
|
564
|
+
extendString,
|
|
565
|
+
lookupField,
|
|
566
|
+
pks,
|
|
567
|
+
});
|
|
558
568
|
|
|
559
569
|
// parse json extended files
|
|
560
570
|
if (_extend) {
|
|
@@ -609,7 +619,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
609
619
|
in: 'body',
|
|
610
620
|
required: true,
|
|
611
621
|
type: 'object',
|
|
612
|
-
schema: { $ref:
|
|
622
|
+
schema: { $ref: '#/definitions/UpdateRowRequestBody' }
|
|
613
623
|
}
|
|
614
624
|
|
|
615
625
|
#swagger.parameters['_lookup_field'] = {
|
|
@@ -659,12 +669,13 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
659
669
|
});
|
|
660
670
|
}
|
|
661
671
|
|
|
662
|
-
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
663
|
-
pks
|
|
664
|
-
)})`;
|
|
665
|
-
|
|
666
672
|
try {
|
|
667
|
-
const data =
|
|
673
|
+
const data = rowService.update({
|
|
674
|
+
tableName,
|
|
675
|
+
fieldsString,
|
|
676
|
+
lookupField,
|
|
677
|
+
pks,
|
|
678
|
+
});
|
|
668
679
|
|
|
669
680
|
res.json({
|
|
670
681
|
message: 'Row updated',
|
|
@@ -732,12 +743,8 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
732
743
|
}
|
|
733
744
|
}
|
|
734
745
|
|
|
735
|
-
const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
736
|
-
pks
|
|
737
|
-
)})`;
|
|
738
|
-
|
|
739
746
|
try {
|
|
740
|
-
const data =
|
|
747
|
+
const data = rowService.delete({ tableName, lookupField, pks });
|
|
741
748
|
|
|
742
749
|
if (data.changes === 0) {
|
|
743
750
|
res.status(404).json({
|
|
@@ -101,4 +101,41 @@ describe('Rows Endpoints', () => {
|
|
|
101
101
|
expect(res.status).toEqual(200);
|
|
102
102
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
103
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
|
+
|
|
104
141
|
});
|
|
@@ -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/tests/index.js
CHANGED
|
@@ -27,7 +27,7 @@ const dropTestDatabase = async (path = 'test.db') => {
|
|
|
27
27
|
|
|
28
28
|
const createTestTable = (table = 'users') => {
|
|
29
29
|
db.prepare(
|
|
30
|
-
`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)`
|
|
31
31
|
).run();
|
|
32
32
|
};
|
|
33
33
|
|