soul-cli 0.6.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soul-cli",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A SQLite REST and Realtime server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
@@ -15,7 +15,7 @@ const operators = {
15
15
  gte: '>=',
16
16
  neq: '!=',
17
17
  null: 'IS NULL',
18
- notnull: 'IS NOT NULL',
18
+ notnull: 'IS NOT NULL'
19
19
  };
20
20
 
21
21
  // Return paginated rows of a table
@@ -80,10 +80,16 @@ const listTableRows = async (req, res) => {
80
80
  let filters = [];
81
81
 
82
82
  // split the filters by comma(,) except when in an array
83
- const re = /(\w+:?(\[.*?\]|\w+)?)/g;
83
+ const re = /,(?![^\[]*?\])/;
84
84
  try {
85
- filters = _filters.match(re)?.map((filter) => {
86
- let [key, value] = filter.split(':');
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
+ }
87
93
 
88
94
  let field = key.split('__')[0];
89
95
  let fieldOperator = key.split('__')[1];
@@ -285,7 +291,7 @@ const listTableRows = async (req, res) => {
285
291
  orderString,
286
292
  limit,
287
293
  page: limit * (page - 1),
288
- whereStringValues,
294
+ whereStringValues
289
295
  });
290
296
 
291
297
  // parse json extended files
@@ -305,7 +311,7 @@ const listTableRows = async (req, res) => {
305
311
  const total = rowService.getCount({
306
312
  tableName,
307
313
  whereString,
308
- whereStringValues,
314
+ whereStringValues
309
315
  });
310
316
 
311
317
  const next =
@@ -377,14 +383,14 @@ const insertRowInTable = async (req, res, next) => {
377
383
  */
378
384
  res.status(201).json({
379
385
  message: 'Row inserted',
380
- data,
386
+ data
381
387
  });
382
388
  req.broadcast = {
383
389
  type: 'INSERT',
384
390
  data: {
385
391
  pk: data.lastInsertRowid,
386
- ...fields,
387
- },
392
+ ...fields
393
+ }
388
394
  };
389
395
  next();
390
396
  } catch (error) {
@@ -398,7 +404,7 @@ const insertRowInTable = async (req, res, next) => {
398
404
  */
399
405
  res.status(400).json({
400
406
  message: error.message,
401
- error: error,
407
+ error: error
402
408
  });
403
409
  }
404
410
  };
@@ -459,7 +465,7 @@ const getRowInTableByPK = async (req, res) => {
459
465
  } catch (error) {
460
466
  return res.status(400).json({
461
467
  message: error.message,
462
- error: error,
468
+ error: error
463
469
  });
464
470
  }
465
471
  }
@@ -551,7 +557,7 @@ const getRowInTableByPK = async (req, res) => {
551
557
  } catch (error) {
552
558
  return res.status(400).json({
553
559
  message: error.message,
554
- error: error,
560
+ error: error
555
561
  });
556
562
  }
557
563
  });
@@ -563,7 +569,7 @@ const getRowInTableByPK = async (req, res) => {
563
569
  tableName,
564
570
  extendString,
565
571
  lookupField,
566
- pks,
572
+ pks
567
573
  });
568
574
 
569
575
  // parse json extended files
@@ -582,17 +588,17 @@ const getRowInTableByPK = async (req, res) => {
582
588
  if (data.length === 0) {
583
589
  return res.status(404).json({
584
590
  message: 'Row not found',
585
- error: 'not_found',
591
+ error: 'not_found'
586
592
  });
587
593
  } else {
588
594
  res.json({
589
- data,
595
+ data
590
596
  });
591
597
  }
592
598
  } catch (error) {
593
599
  return res.status(400).json({
594
600
  message: error.message,
595
- error: error,
601
+ error: error
596
602
  });
597
603
  }
598
604
  };
@@ -646,7 +652,7 @@ const updateRowInTableByPK = async (req, res, next) => {
646
652
  } catch (error) {
647
653
  return res.status(400).json({
648
654
  message: error.message,
649
- error: error,
655
+ error: error
650
656
  });
651
657
  }
652
658
  }
@@ -665,7 +671,7 @@ const updateRowInTableByPK = async (req, res, next) => {
665
671
  if (fieldsString === '') {
666
672
  return res.status(400).json({
667
673
  message: 'No fields provided',
668
- error: 'no_fields_provided',
674
+ error: 'no_fields_provided'
669
675
  });
670
676
  }
671
677
 
@@ -674,26 +680,26 @@ const updateRowInTableByPK = async (req, res, next) => {
674
680
  tableName,
675
681
  fieldsString,
676
682
  lookupField,
677
- pks,
683
+ pks
678
684
  });
679
685
 
680
686
  res.json({
681
687
  message: 'Row updated',
682
- data,
688
+ data
683
689
  });
684
690
  req.broadcast = {
685
691
  type: 'UPDATE',
686
692
  _lookup_field: lookupField,
687
693
  data: {
688
694
  pks: pks.split(','),
689
- ...fields,
690
- },
695
+ ...fields
696
+ }
691
697
  };
692
698
  next();
693
699
  } catch (error) {
694
700
  res.status(400).json({
695
701
  message: error.message,
696
- error: error,
702
+ error: error
697
703
  });
698
704
  }
699
705
  };
@@ -738,7 +744,7 @@ const deleteRowInTableByPK = async (req, res, next) => {
738
744
  } catch (error) {
739
745
  return res.status(400).json({
740
746
  message: error.message,
741
- error: error,
747
+ error: error
742
748
  });
743
749
  }
744
750
  }
@@ -748,26 +754,26 @@ const deleteRowInTableByPK = async (req, res, next) => {
748
754
 
749
755
  if (data.changes === 0) {
750
756
  res.status(404).json({
751
- error: 'not_found',
757
+ error: 'not_found'
752
758
  });
753
759
  } else {
754
760
  res.json({
755
761
  message: 'Row deleted',
756
- data,
762
+ data
757
763
  });
758
764
  req.broadcast = {
759
765
  type: 'DELETE',
760
766
  _lookup_field: lookupField,
761
767
  data: {
762
- pks: pks.split(','),
763
- },
768
+ pks: pks.split(',')
769
+ }
764
770
  };
765
771
  next();
766
772
  }
767
773
  } catch (error) {
768
774
  res.status(400).json({
769
775
  message: error.message,
770
- error: error,
776
+ error: error
771
777
  });
772
778
  }
773
779
  };
@@ -777,5 +783,5 @@ module.exports = {
777
783
  insertRowInTable,
778
784
  getRowInTableByPK,
779
785
  updateRowInTableByPK,
780
- deleteRowInTableByPK,
786
+ deleteRowInTableByPK
781
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')
@@ -103,16 +191,14 @@ describe('Rows Endpoints', () => {
103
191
  });
104
192
 
105
193
  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
- });
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
+ });
116
202
  expect(res.status).toEqual(201);
117
203
  expect(res.type).toEqual(expect.stringContaining('json'));
118
204
  expect(res.body).toHaveProperty('data');
@@ -137,5 +223,4 @@ describe('Rows Endpoints', () => {
137
223
  expect(res.body.data).toEqual(expect.any(Array));
138
224
  expect(res.body.data.length).toEqual(1);
139
225
  });
140
-
141
226
  });
@@ -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, email TEXT, username 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
  };
@@ -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
- { 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' },
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 };