soul-cli 0.5.2 → 0.6.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/package.json +1 -1
- package/src/controllers/rows.js +97 -84
- package/src/controllers/rows.test.js +125 -3
- package/src/services/index.js +6 -0
- package/src/services/rowService.js +69 -0
- package/src/services/tableService.js +5 -0
- package/src/tests/index.js +4 -4
- package/src/tests/testData.js +58 -26
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(',');
|
|
@@ -14,7 +15,7 @@ const operators = {
|
|
|
14
15
|
gte: '>=',
|
|
15
16
|
neq: '!=',
|
|
16
17
|
null: 'IS NULL',
|
|
17
|
-
notnull: 'IS NOT NULL'
|
|
18
|
+
notnull: 'IS NOT NULL'
|
|
18
19
|
};
|
|
19
20
|
|
|
20
21
|
// Return paginated rows of a table
|
|
@@ -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,9 +78,18 @@ 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 = /,(?![^\[]*?\])/;
|
|
80
84
|
try {
|
|
81
|
-
filters = _filters.split(
|
|
82
|
-
|
|
85
|
+
filters = _filters.split(re).map((filter) => {
|
|
86
|
+
//NOTE: When using the _filter parameter, the values are split using the ":" sign, like this (_filters=Total__eq:1). However, if the user sends a date value, such as (_filters=InvoiceDate__eq:2010-01-08 00:00:00), there will be additional colon (":") signs present.
|
|
87
|
+
let [key, ...value] = filter.split(':');
|
|
88
|
+
if (value.length === 1) {
|
|
89
|
+
value = value[0];
|
|
90
|
+
} else {
|
|
91
|
+
value = value.map((element) => element).join(':');
|
|
92
|
+
}
|
|
83
93
|
|
|
84
94
|
let field = key.split('__')[0];
|
|
85
95
|
let fieldOperator = key.split('__')[1];
|
|
@@ -88,7 +98,7 @@ const listTableRows = async (req, res) => {
|
|
|
88
98
|
fieldOperator = 'eq';
|
|
89
99
|
} else if (!operators[fieldOperator]) {
|
|
90
100
|
throw new Error(
|
|
91
|
-
`Invalid field operator
|
|
101
|
+
`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
102
|
);
|
|
93
103
|
}
|
|
94
104
|
|
|
@@ -102,19 +112,30 @@ const listTableRows = async (req, res) => {
|
|
|
102
112
|
} catch (error) {
|
|
103
113
|
return res.status(400).json({
|
|
104
114
|
message: error.message,
|
|
105
|
-
error: error
|
|
115
|
+
error: error
|
|
106
116
|
});
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
let whereString = '';
|
|
120
|
+
const whereStringValues = [];
|
|
121
|
+
|
|
110
122
|
if (_filters !== '') {
|
|
111
123
|
whereString += ' WHERE ';
|
|
112
124
|
whereString += filters
|
|
113
|
-
.map((filter) =>
|
|
114
|
-
filter.value
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
125
|
+
.map((filter) => {
|
|
126
|
+
if (filter.value) {
|
|
127
|
+
if (filter.value.startsWith('[') && filter.value.endsWith(']')) {
|
|
128
|
+
const arrayValues = filter.value.slice(1, -1).split(',');
|
|
129
|
+
return `${tableName}.${filter.field} IN (${arrayValues
|
|
130
|
+
.map((val) => `'${val}'`)
|
|
131
|
+
.join(', ')})`;
|
|
132
|
+
} else {
|
|
133
|
+
return `${tableName}.${filter.field} ${filter.operator} '${filter.value}'`;
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
return `${tableName}.${filter.field} ${filter.operator}`;
|
|
137
|
+
}
|
|
138
|
+
})
|
|
118
139
|
.join(' AND ');
|
|
119
140
|
params = `_filters=${_filters}&`;
|
|
120
141
|
}
|
|
@@ -140,7 +161,7 @@ const listTableRows = async (req, res) => {
|
|
|
140
161
|
} catch (error) {
|
|
141
162
|
return res.status(400).json({
|
|
142
163
|
message: error.message,
|
|
143
|
-
error: error
|
|
164
|
+
error: error
|
|
144
165
|
});
|
|
145
166
|
}
|
|
146
167
|
}
|
|
@@ -256,18 +277,22 @@ const listTableRows = async (req, res) => {
|
|
|
256
277
|
if (foreignKeyError.error) {
|
|
257
278
|
return res.status(400).json({
|
|
258
279
|
message: foreignKeyError.message,
|
|
259
|
-
error: foreignKeyError.error
|
|
280
|
+
error: foreignKeyError.error
|
|
260
281
|
});
|
|
261
282
|
}
|
|
262
283
|
}
|
|
263
284
|
|
|
264
|
-
// get paginated rows
|
|
265
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} ${whereString} ${orderString} LIMIT ${limit} OFFSET ${
|
|
266
|
-
limit * (page - 1)
|
|
267
|
-
}`;
|
|
268
|
-
|
|
269
285
|
try {
|
|
270
|
-
let data =
|
|
286
|
+
let data = rowService.get({
|
|
287
|
+
schemaString,
|
|
288
|
+
tableName,
|
|
289
|
+
extendString,
|
|
290
|
+
whereString,
|
|
291
|
+
orderString,
|
|
292
|
+
limit,
|
|
293
|
+
page: limit * (page - 1),
|
|
294
|
+
whereStringValues
|
|
295
|
+
});
|
|
271
296
|
|
|
272
297
|
// parse json extended files
|
|
273
298
|
if (_extend) {
|
|
@@ -283,9 +308,11 @@ const listTableRows = async (req, res) => {
|
|
|
283
308
|
}
|
|
284
309
|
|
|
285
310
|
// get total number of rows
|
|
286
|
-
const total =
|
|
287
|
-
|
|
288
|
-
|
|
311
|
+
const total = rowService.getCount({
|
|
312
|
+
tableName,
|
|
313
|
+
whereString,
|
|
314
|
+
whereStringValues
|
|
315
|
+
});
|
|
289
316
|
|
|
290
317
|
const next =
|
|
291
318
|
data.length === limit
|
|
@@ -304,12 +331,12 @@ const listTableRows = async (req, res) => {
|
|
|
304
331
|
data,
|
|
305
332
|
total,
|
|
306
333
|
next,
|
|
307
|
-
previous
|
|
334
|
+
previous
|
|
308
335
|
});
|
|
309
336
|
} catch (error) {
|
|
310
337
|
res.status(400).json({
|
|
311
338
|
message: error.message,
|
|
312
|
-
error: error
|
|
339
|
+
error: error
|
|
313
340
|
});
|
|
314
341
|
}
|
|
315
342
|
};
|
|
@@ -331,52 +358,39 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
331
358
|
in: 'body',
|
|
332
359
|
required: true,
|
|
333
360
|
type: 'object',
|
|
334
|
-
schema: { $ref:
|
|
361
|
+
schema: { $ref: '#/definitions/InsertRowRequestBody' }
|
|
335
362
|
}
|
|
336
363
|
*/
|
|
337
364
|
|
|
338
365
|
const { name: tableName } = req.params;
|
|
339
|
-
const { fields } = req.body;
|
|
340
|
-
const fieldsString = Object.keys(fields).join(', ');
|
|
366
|
+
const { fields: queryFields } = req.body;
|
|
341
367
|
|
|
342
|
-
//
|
|
343
|
-
const
|
|
344
|
-
.
|
|
345
|
-
|
|
346
|
-
return `'${value}'`;
|
|
347
|
-
}
|
|
348
|
-
return value;
|
|
349
|
-
})
|
|
350
|
-
.join(', ');
|
|
368
|
+
// Remove null values from fields for accurate query construction.
|
|
369
|
+
const fields = Object.fromEntries(
|
|
370
|
+
Object.entries(queryFields).filter(([_, value]) => value !== null)
|
|
371
|
+
);
|
|
351
372
|
|
|
352
|
-
let values = `(${fieldsString}) VALUES (${valuesString})`;
|
|
353
|
-
|
|
354
|
-
if (valuesString === '') {
|
|
355
|
-
values = 'DEFAULT VALUES';
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const query = `INSERT INTO ${tableName} ${values}`;
|
|
359
373
|
try {
|
|
360
|
-
const data =
|
|
374
|
+
const data = rowService.save({ tableName, fields });
|
|
361
375
|
|
|
362
376
|
/*
|
|
363
377
|
#swagger.responses[201] = {
|
|
364
378
|
description: 'Row inserted successfully',
|
|
365
379
|
schema: {
|
|
366
|
-
$ref:
|
|
380
|
+
$ref: '#/definitions/InsertRowSuccessResponse'
|
|
367
381
|
}
|
|
368
382
|
}
|
|
369
383
|
*/
|
|
370
384
|
res.status(201).json({
|
|
371
385
|
message: 'Row inserted',
|
|
372
|
-
data
|
|
386
|
+
data
|
|
373
387
|
});
|
|
374
388
|
req.broadcast = {
|
|
375
389
|
type: 'INSERT',
|
|
376
390
|
data: {
|
|
377
391
|
pk: data.lastInsertRowid,
|
|
378
|
-
...fields
|
|
379
|
-
}
|
|
392
|
+
...fields
|
|
393
|
+
}
|
|
380
394
|
};
|
|
381
395
|
next();
|
|
382
396
|
} catch (error) {
|
|
@@ -384,13 +398,13 @@ const insertRowInTable = async (req, res, next) => {
|
|
|
384
398
|
#swagger.responses[400] = {
|
|
385
399
|
description: 'Bad request',
|
|
386
400
|
schema: {
|
|
387
|
-
$ref:
|
|
401
|
+
$ref: '#/definitions/InsertRowErrorResponse'
|
|
388
402
|
}
|
|
389
403
|
}
|
|
390
404
|
*/
|
|
391
405
|
res.status(400).json({
|
|
392
406
|
message: error.message,
|
|
393
|
-
error: error
|
|
407
|
+
error: error
|
|
394
408
|
});
|
|
395
409
|
}
|
|
396
410
|
};
|
|
@@ -451,7 +465,7 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
451
465
|
} catch (error) {
|
|
452
466
|
return res.status(400).json({
|
|
453
467
|
message: error.message,
|
|
454
|
-
error: error
|
|
468
|
+
error: error
|
|
455
469
|
});
|
|
456
470
|
}
|
|
457
471
|
}
|
|
@@ -543,18 +557,20 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
543
557
|
} catch (error) {
|
|
544
558
|
return res.status(400).json({
|
|
545
559
|
message: error.message,
|
|
546
|
-
error: error
|
|
560
|
+
error: error
|
|
547
561
|
});
|
|
548
562
|
}
|
|
549
563
|
});
|
|
550
564
|
}
|
|
551
565
|
|
|
552
|
-
const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${quotePrimaryKeys(
|
|
553
|
-
pks
|
|
554
|
-
)})`;
|
|
555
|
-
|
|
556
566
|
try {
|
|
557
|
-
let data =
|
|
567
|
+
let data = rowService.getById({
|
|
568
|
+
schemaString,
|
|
569
|
+
tableName,
|
|
570
|
+
extendString,
|
|
571
|
+
lookupField,
|
|
572
|
+
pks
|
|
573
|
+
});
|
|
558
574
|
|
|
559
575
|
// parse json extended files
|
|
560
576
|
if (_extend) {
|
|
@@ -572,17 +588,17 @@ const getRowInTableByPK = async (req, res) => {
|
|
|
572
588
|
if (data.length === 0) {
|
|
573
589
|
return res.status(404).json({
|
|
574
590
|
message: 'Row not found',
|
|
575
|
-
error: 'not_found'
|
|
591
|
+
error: 'not_found'
|
|
576
592
|
});
|
|
577
593
|
} else {
|
|
578
594
|
res.json({
|
|
579
|
-
data
|
|
595
|
+
data
|
|
580
596
|
});
|
|
581
597
|
}
|
|
582
598
|
} catch (error) {
|
|
583
599
|
return res.status(400).json({
|
|
584
600
|
message: error.message,
|
|
585
|
-
error: error
|
|
601
|
+
error: error
|
|
586
602
|
});
|
|
587
603
|
}
|
|
588
604
|
};
|
|
@@ -609,7 +625,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
609
625
|
in: 'body',
|
|
610
626
|
required: true,
|
|
611
627
|
type: 'object',
|
|
612
|
-
schema: { $ref:
|
|
628
|
+
schema: { $ref: '#/definitions/UpdateRowRequestBody' }
|
|
613
629
|
}
|
|
614
630
|
|
|
615
631
|
#swagger.parameters['_lookup_field'] = {
|
|
@@ -636,7 +652,7 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
636
652
|
} catch (error) {
|
|
637
653
|
return res.status(400).json({
|
|
638
654
|
message: error.message,
|
|
639
|
-
error: error
|
|
655
|
+
error: error
|
|
640
656
|
});
|
|
641
657
|
}
|
|
642
658
|
}
|
|
@@ -655,34 +671,35 @@ const updateRowInTableByPK = async (req, res, next) => {
|
|
|
655
671
|
if (fieldsString === '') {
|
|
656
672
|
return res.status(400).json({
|
|
657
673
|
message: 'No fields provided',
|
|
658
|
-
error: 'no_fields_provided'
|
|
674
|
+
error: 'no_fields_provided'
|
|
659
675
|
});
|
|
660
676
|
}
|
|
661
677
|
|
|
662
|
-
const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
663
|
-
pks
|
|
664
|
-
)})`;
|
|
665
|
-
|
|
666
678
|
try {
|
|
667
|
-
const data =
|
|
679
|
+
const data = rowService.update({
|
|
680
|
+
tableName,
|
|
681
|
+
fieldsString,
|
|
682
|
+
lookupField,
|
|
683
|
+
pks
|
|
684
|
+
});
|
|
668
685
|
|
|
669
686
|
res.json({
|
|
670
687
|
message: 'Row updated',
|
|
671
|
-
data
|
|
688
|
+
data
|
|
672
689
|
});
|
|
673
690
|
req.broadcast = {
|
|
674
691
|
type: 'UPDATE',
|
|
675
692
|
_lookup_field: lookupField,
|
|
676
693
|
data: {
|
|
677
694
|
pks: pks.split(','),
|
|
678
|
-
...fields
|
|
679
|
-
}
|
|
695
|
+
...fields
|
|
696
|
+
}
|
|
680
697
|
};
|
|
681
698
|
next();
|
|
682
699
|
} catch (error) {
|
|
683
700
|
res.status(400).json({
|
|
684
701
|
message: error.message,
|
|
685
|
-
error: error
|
|
702
|
+
error: error
|
|
686
703
|
});
|
|
687
704
|
}
|
|
688
705
|
};
|
|
@@ -727,40 +744,36 @@ const deleteRowInTableByPK = async (req, res, next) => {
|
|
|
727
744
|
} catch (error) {
|
|
728
745
|
return res.status(400).json({
|
|
729
746
|
message: error.message,
|
|
730
|
-
error: error
|
|
747
|
+
error: error
|
|
731
748
|
});
|
|
732
749
|
}
|
|
733
750
|
}
|
|
734
751
|
|
|
735
|
-
const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${quotePrimaryKeys(
|
|
736
|
-
pks
|
|
737
|
-
)})`;
|
|
738
|
-
|
|
739
752
|
try {
|
|
740
|
-
const data =
|
|
753
|
+
const data = rowService.delete({ tableName, lookupField, pks });
|
|
741
754
|
|
|
742
755
|
if (data.changes === 0) {
|
|
743
756
|
res.status(404).json({
|
|
744
|
-
error: 'not_found'
|
|
757
|
+
error: 'not_found'
|
|
745
758
|
});
|
|
746
759
|
} else {
|
|
747
760
|
res.json({
|
|
748
761
|
message: 'Row deleted',
|
|
749
|
-
data
|
|
762
|
+
data
|
|
750
763
|
});
|
|
751
764
|
req.broadcast = {
|
|
752
765
|
type: 'DELETE',
|
|
753
766
|
_lookup_field: lookupField,
|
|
754
767
|
data: {
|
|
755
|
-
pks: pks.split(',')
|
|
756
|
-
}
|
|
768
|
+
pks: pks.split(',')
|
|
769
|
+
}
|
|
757
770
|
};
|
|
758
771
|
next();
|
|
759
772
|
}
|
|
760
773
|
} catch (error) {
|
|
761
774
|
res.status(400).json({
|
|
762
775
|
message: error.message,
|
|
763
|
-
error: error
|
|
776
|
+
error: error
|
|
764
777
|
});
|
|
765
778
|
}
|
|
766
779
|
};
|
|
@@ -770,5 +783,5 @@ module.exports = {
|
|
|
770
783
|
insertRowInTable,
|
|
771
784
|
getRowInTableByPK,
|
|
772
785
|
updateRowInTableByPK,
|
|
773
|
-
deleteRowInTableByPK
|
|
786
|
+
deleteRowInTableByPK
|
|
774
787
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const { not } = require('joi');
|
|
1
2
|
const supertest = require('supertest');
|
|
2
3
|
|
|
3
4
|
const app = require('../index');
|
|
@@ -30,7 +31,7 @@ describe('Rows Endpoints', () => {
|
|
|
30
31
|
_ordering: '-firstName',
|
|
31
32
|
_schema: 'firstName,lastName',
|
|
32
33
|
_limit: 8,
|
|
33
|
-
_page: 2
|
|
34
|
+
_page: 2
|
|
34
35
|
};
|
|
35
36
|
const query = queryString(params);
|
|
36
37
|
const res = await requestWithSupertest.get(
|
|
@@ -47,14 +48,14 @@ describe('Rows Endpoints', () => {
|
|
|
47
48
|
expect(res.body.next).toEqual(
|
|
48
49
|
`/tables/users/rows?${queryString({
|
|
49
50
|
...params,
|
|
50
|
-
_page: params._page + 1
|
|
51
|
+
_page: params._page + 1
|
|
51
52
|
}).toString()}`
|
|
52
53
|
);
|
|
53
54
|
|
|
54
55
|
expect(res.body.previous).toEqual(
|
|
55
56
|
`/tables/users/rows?${queryString({
|
|
56
57
|
...params,
|
|
57
|
-
_page: params._page - 1
|
|
58
|
+
_page: params._page - 1
|
|
58
59
|
}).toString()}`
|
|
59
60
|
);
|
|
60
61
|
});
|
|
@@ -69,6 +70,93 @@ describe('Rows Endpoints', () => {
|
|
|
69
70
|
expect(res.body.data[0].lastName).not.toBeNull();
|
|
70
71
|
});
|
|
71
72
|
|
|
73
|
+
it('GET /tables/:name/rows: should successfully retrieve users created after 2010-01-01 00:00:00.', async () => {
|
|
74
|
+
const date = '2010-01-01 00:00:00';
|
|
75
|
+
const res = await requestWithSupertest.get(
|
|
76
|
+
`/api/tables/users/rows?_filters=createdAt__gte:${date}`
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
res.body.data.map((user) => {
|
|
80
|
+
const createdAt = new Date(user.createdAt);
|
|
81
|
+
const referenceDate = new Date(date);
|
|
82
|
+
expect(createdAt.getTime()).toBeGreaterThan(referenceDate.getTime());
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(res.status).toEqual(200);
|
|
86
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
87
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
88
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
89
|
+
expect(res.body.data[0]).toHaveProperty('createdAt');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('GET /tables/:name/rows: should successfully retrieve users created before 2008-01-20 00:00:00.', async () => {
|
|
93
|
+
const date = '2008-01-20 00:00:00';
|
|
94
|
+
const res = await requestWithSupertest.get(
|
|
95
|
+
`/api/tables/users/rows?_filters=createdAt__lte:${date}`
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
res.body.data.map((user) => {
|
|
99
|
+
const createdAt = new Date(user.createdAt);
|
|
100
|
+
const referenceDate = new Date(date);
|
|
101
|
+
expect(createdAt.getTime()).toBeLessThan(referenceDate.getTime());
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(res.status).toEqual(200);
|
|
105
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
106
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
107
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
108
|
+
expect(res.body.data[0]).toHaveProperty('createdAt');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('GET /tables/:name/rows: should successfully retrieve users created at 2013-01-08 00:00:00', async () => {
|
|
112
|
+
const date = '2013-01-08 00:00:00';
|
|
113
|
+
const res = await requestWithSupertest.get(
|
|
114
|
+
`/api/tables/users/rows?_filters=createdAt__eq:${date}`
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
res.body.data.map((user) => {
|
|
118
|
+
const createdAt = new Date(user.createdAt);
|
|
119
|
+
const referenceDate = new Date(date);
|
|
120
|
+
expect(createdAt.getTime()).toEqual(referenceDate.getTime());
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(res.status).toEqual(200);
|
|
124
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
125
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
126
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
127
|
+
expect(res.body.data[0]).toHaveProperty('createdAt');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('GET /tables/:name/rows: should successfully retrieve users created at 2007-01-08 00:00:00', async () => {
|
|
131
|
+
const date = '2007-01-08 00:00:00';
|
|
132
|
+
const res = await requestWithSupertest.get(
|
|
133
|
+
`/api/tables/users/rows?_filters=createdAt__eq:${date}`
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
//There are no users that are created at 2007-01-08 00:00:00 so the API should return empty data
|
|
137
|
+
expect(res.body.data).toHaveLength(0);
|
|
138
|
+
expect(res.status).toEqual(200);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('GET /tables/:name/rows: should successfully retrieve users that are not created at 2021-01-08 00:00:00', async () => {
|
|
142
|
+
const date = '2021-01-08 00:00:00';
|
|
143
|
+
const res = await requestWithSupertest.get(
|
|
144
|
+
`/api/tables/users/rows?_filters=createdAt__neq:${date}`
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
res.body.data.map((user) => {
|
|
148
|
+
const createdAt = new Date(user.createdAt);
|
|
149
|
+
const referenceDate = new Date(date);
|
|
150
|
+
expect(createdAt.getTime()).not.toEqual(referenceDate.getTime());
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(res.status).toEqual(200);
|
|
154
|
+
expect(res.body.data[0]).toHaveProperty('id');
|
|
155
|
+
expect(res.body.data[0]).toHaveProperty('firstName');
|
|
156
|
+
expect(res.body.data[0]).toHaveProperty('lastName');
|
|
157
|
+
expect(res.body.data[0]).toHaveProperty('createdAt');
|
|
158
|
+
});
|
|
159
|
+
|
|
72
160
|
it('POST /tables/:name/rows should insert a new row and return the lastInsertRowid', async () => {
|
|
73
161
|
const res = await requestWithSupertest
|
|
74
162
|
.post('/api/tables/users/rows')
|
|
@@ -101,4 +189,38 @@ describe('Rows Endpoints', () => {
|
|
|
101
189
|
expect(res.status).toEqual(200);
|
|
102
190
|
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
103
191
|
});
|
|
192
|
+
|
|
193
|
+
it('POST /tables/:name/rows should insert a new row if any of the value of the object being inserted is null', async () => {
|
|
194
|
+
const res = await requestWithSupertest.post('/api/tables/users/rows').send({
|
|
195
|
+
fields: {
|
|
196
|
+
firstName: null,
|
|
197
|
+
lastName: 'Doe',
|
|
198
|
+
email: null,
|
|
199
|
+
username: 'Jane'
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
expect(res.status).toEqual(201);
|
|
203
|
+
expect(res.type).toEqual(expect.stringContaining('json'));
|
|
204
|
+
expect(res.body).toHaveProperty('data');
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('GET /tables/:name/rows should return values if any of the IDs from the array match the user ID.', async () => {
|
|
208
|
+
const res = await requestWithSupertest.get(
|
|
209
|
+
'/api/tables/users/rows?_filters=id:[2,3]'
|
|
210
|
+
);
|
|
211
|
+
expect(res.status).toEqual(200);
|
|
212
|
+
expect(res.body).toHaveProperty('data');
|
|
213
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
214
|
+
expect(res.body.data.length).toEqual(2);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('GET /tables/:name/rows should return values if the provided ID matches the user ID.', async () => {
|
|
218
|
+
const res = await requestWithSupertest.get(
|
|
219
|
+
'/api/tables/users/rows?_filters=id:2,firstName:Michael,lastName:Lee'
|
|
220
|
+
);
|
|
221
|
+
expect(res.status).toEqual(200);
|
|
222
|
+
expect(res.body).toHaveProperty('data');
|
|
223
|
+
expect(res.body.data).toEqual(expect.any(Array));
|
|
224
|
+
expect(res.body.data.length).toEqual(1);
|
|
225
|
+
});
|
|
104
226
|
});
|
|
@@ -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,17 +27,17 @@ 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, createdAt TEXT)`
|
|
31
31
|
).run();
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
const insertIntoTestTable = (table = 'users') => {
|
|
35
35
|
const statement = db.prepare(
|
|
36
|
-
`INSERT INTO ${table} (firstName, lastName) VALUES (?, ?)`
|
|
36
|
+
`INSERT INTO ${table} (firstName, lastName, createdAt) VALUES (?, ?, ?)`
|
|
37
37
|
);
|
|
38
38
|
|
|
39
39
|
for (const user of testNames) {
|
|
40
|
-
statement.run(user.firstName, user.lastName);
|
|
40
|
+
statement.run(user.firstName, user.lastName, user.createdAt);
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
|
|
@@ -45,5 +45,5 @@ module.exports = {
|
|
|
45
45
|
dropTestTable,
|
|
46
46
|
dropTestDatabase,
|
|
47
47
|
createTestTable,
|
|
48
|
-
insertIntoTestTable
|
|
48
|
+
insertIntoTestTable
|
|
49
49
|
};
|
package/src/tests/testData.js
CHANGED
|
@@ -1,30 +1,62 @@
|
|
|
1
1
|
const testNames = [
|
|
2
|
-
{ firstName: 'Emily', lastName: 'William' },
|
|
3
|
-
{ firstName: 'Michael', lastName: 'Lee' },
|
|
4
|
-
{ firstName: 'Sarah', lastName: 'Johnson' },
|
|
5
|
-
{ firstName: 'David', lastName: 'Chen' },
|
|
6
|
-
{
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
{ firstName: '
|
|
12
|
-
{ firstName: '
|
|
13
|
-
{
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{ firstName: '
|
|
19
|
-
{
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
{
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
{ firstName: 'Emily', lastName: 'William', createdAt: '2008-01-08 00:00:00' },
|
|
3
|
+
{ firstName: 'Michael', lastName: 'Lee', createdAt: '2009-01-08 00:00:00' },
|
|
4
|
+
{ firstName: 'Sarah', lastName: 'Johnson', createdAt: '2010-01-08 00:00:00' },
|
|
5
|
+
{ firstName: 'David', lastName: 'Chen', createdAt: '2011-01-08 00:00:00' },
|
|
6
|
+
{
|
|
7
|
+
firstName: 'Olivia',
|
|
8
|
+
lastName: 'William',
|
|
9
|
+
createdAt: '2012-01-08 00:00:00'
|
|
10
|
+
},
|
|
11
|
+
{ firstName: 'William', lastName: 'Kim', createdAt: '2013-01-08 00:00:00' },
|
|
12
|
+
{ firstName: 'Sophia', lastName: 'Singh', createdAt: '2013-02-08 00:00:00' },
|
|
13
|
+
{
|
|
14
|
+
firstName: 'James',
|
|
15
|
+
lastName: 'Rodriguez',
|
|
16
|
+
createdAt: '2013-03-08 00:00:00'
|
|
17
|
+
},
|
|
18
|
+
{ firstName: 'Ava', lastName: 'Patel', createdAt: '2013-01-04 00:00:00' },
|
|
19
|
+
{
|
|
20
|
+
firstName: 'Benjamin',
|
|
21
|
+
lastName: 'Garcia',
|
|
22
|
+
createdAt: '2015-01-08 00:00:00'
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
firstName: 'Isabella',
|
|
26
|
+
lastName: 'Nguyen',
|
|
27
|
+
createdAt: '2014-01-08 00:00:00'
|
|
28
|
+
},
|
|
29
|
+
{ firstName: 'Ethan', lastName: 'Lee', createdAt: '2016-01-08 00:00:00' },
|
|
30
|
+
{ firstName: 'Mia', lastName: 'Wilson', createdAt: '2017-01-08 00:00:00' },
|
|
31
|
+
{
|
|
32
|
+
firstName: 'Alexander',
|
|
33
|
+
lastName: 'William',
|
|
34
|
+
createdAt: '2018-01-08 00:00:00'
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
firstName: 'Charlotte',
|
|
38
|
+
lastName: 'Hernandez',
|
|
39
|
+
createdAt: '2019-01-08 00:00:00'
|
|
40
|
+
},
|
|
41
|
+
{ firstName: 'Liam', lastName: 'Gonzalez', createdAt: '2020-01-08 00:00:00' },
|
|
42
|
+
{ firstName: 'Emma', lastName: 'Gomez', createdAt: '2021-01-08 00:00:00' },
|
|
43
|
+
{ firstName: 'Noah', lastName: 'Perez', createdAt: '2021-01-08 00:00:00' },
|
|
44
|
+
{ firstName: 'Avery', lastName: 'Ramirez', createdAt: '2023-02-08 00:00:00' },
|
|
45
|
+
{ firstName: 'Jacob', lastName: 'Turner', createdAt: '2023-03-08 00:00:00' },
|
|
46
|
+
{
|
|
47
|
+
firstName: 'Abigail',
|
|
48
|
+
lastName: 'Williams',
|
|
49
|
+
createdAt: '2023-02-10 00:00:00'
|
|
50
|
+
},
|
|
51
|
+
{ firstName: 'Elijah', lastName: 'Hall', createdAt: '2023-04-02 00:00:00' },
|
|
52
|
+
{ firstName: 'Mila', lastName: 'Flores', createdAt: '2023-05-13 00:00:00' },
|
|
53
|
+
{
|
|
54
|
+
firstName: 'Evelyn',
|
|
55
|
+
lastName: 'Morales',
|
|
56
|
+
createdAt: '2023-06-05 00:00:00'
|
|
57
|
+
},
|
|
58
|
+
{ firstName: 'Logan', lastName: 'Collins', createdAt: '2023-06-07 00:00:00' },
|
|
59
|
+
{ firstName: null, lastName: 'Flores', createdAt: '2023-06-09 00:00:00' }
|
|
28
60
|
];
|
|
29
61
|
|
|
30
62
|
module.exports = { testNames };
|