soul-cli 0.0.8 → 0.0.9

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.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "A SQLite RESTful server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
@@ -330,9 +330,9 @@ const getRowInTableByPK = async (req, res) => {
330
330
  type: 'string'
331
331
  }
332
332
 
333
- #swagger.parameters['pk'] = {
333
+ #swagger.parameters['pks'] = {
334
334
  in: 'path',
335
- description: 'Primary key',
335
+ description: 'Primary key (comma-separated for bulk retrieve)',
336
336
  required: true,
337
337
  }
338
338
 
@@ -358,7 +358,7 @@ const getRowInTableByPK = async (req, res) => {
358
358
  }
359
359
 
360
360
  */
361
- const { name: tableName, pk } = req.params;
361
+ const { name: tableName, pks } = req.params;
362
362
  const { _lookup_field, _schema, _extend } = req.query;
363
363
 
364
364
  let lookupField = _lookup_field;
@@ -471,22 +471,25 @@ const getRowInTableByPK = async (req, res) => {
471
471
  });
472
472
  }
473
473
 
474
- const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} = '${pk}'`;
474
+ const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${pks})`;
475
475
 
476
476
  try {
477
- let data = db.prepare(query).get();
477
+ let data = db.prepare(query).all();
478
+
478
479
  // parse json extended files
479
480
  if (_extend) {
480
481
  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]);
482
+ data = data.map((row) => {
483
+ Object.keys(row).forEach((key) => {
484
+ if (extendFields.includes(key.replace('_data', ''))) {
485
+ row[key] = JSON.parse(row[key]);
486
+ }
486
487
  });
488
+ return row;
489
+ });
487
490
  }
488
491
 
489
- if (!data) {
492
+ if (data.length === 0) {
490
493
  return res.status(404).json({
491
494
  message: 'Row not found',
492
495
  error: 'not_found',
@@ -516,9 +519,9 @@ const updateRowInTableByPK = async (req, res) => {
516
519
  required: true,
517
520
  type: 'string'
518
521
  }
519
- #swagger.parameters['pk'] = {
522
+ #swagger.parameters['pks'] = {
520
523
  in: 'path',
521
- description: 'Primary key',
524
+ description: 'Primary key (comma-separated for bulk update)',
522
525
  required: true,
523
526
  }
524
527
 
@@ -537,7 +540,7 @@ const updateRowInTableByPK = async (req, res) => {
537
540
  }
538
541
  */
539
542
 
540
- const { name: tableName, pk } = req.params;
543
+ const { name: tableName, pks } = req.params;
541
544
  const { fields } = req.body;
542
545
  const { _lookup_field } = req.query;
543
546
 
@@ -576,95 +579,12 @@ const updateRowInTableByPK = async (req, res) => {
576
579
  });
577
580
  }
578
581
 
579
- const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} = '${pk}'`;
580
- try {
581
- const data = db.prepare(query).run();
582
-
583
- res.json({
584
- message: 'Row updated',
585
- data,
586
- });
587
- } catch (error) {
588
- res.status(400).json({
589
- message: error.message,
590
- error: error,
591
- });
592
- }
593
- };
594
-
595
- // Update a row by pk
596
- const bulkUpdateRowsInTableByPK = async (req, res) => {
597
- /*
598
- #swagger.tags = ['Rows']
599
- #swagger.summary = 'Bulk Update Rows'
600
- #swagger.description = 'Bulk Update rows by primary key'
601
- #swagger.parameters['name'] = {
602
- in: 'path',
603
- description: 'Table name',
604
- required: true,
605
- type: 'string'
606
- }
607
-
608
- #swagger.parameters['body'] = {
609
- in: 'body',
610
- required: true,
611
- type: 'object',
612
- schema: { $ref: "#/definitions/BulkUpdateRowsRequestBody" }
613
- }
614
-
615
- #swagger.parameters['_lookup_field'] = {
616
- in: 'query',
617
- description: 'If you want to update row by any other field than primary key, use this parameter',
618
- required: false,
619
- type: 'string'
620
- }
621
- */
622
-
623
- const { name: tableName } = req.params;
624
- const { pks, fields } = req.body;
625
- const { _lookup_field } = req.query;
626
-
627
- let lookupField = _lookup_field;
628
-
629
- if (!_lookup_field) {
630
- // find the primary key of the table
631
- try {
632
- lookupField = db
633
- .prepare(`PRAGMA table_info(${tableName})`)
634
- .all()
635
- .find((field) => field.pk === 1).name;
636
- } catch (error) {
637
- return res.status(400).json({
638
- message: error.message,
639
- error: error,
640
- });
641
- }
642
- }
643
-
644
- // wrap text values in quotes
645
- const fieldsString = Object.keys(fields)
646
- .map((key) => {
647
- let value = fields[key];
648
- if (typeof value === 'string') {
649
- value = `'${value}'`;
650
- }
651
- return `${key} = ${value}`;
652
- })
653
- .join(', ');
654
-
655
- if (fieldsString === '') {
656
- return res.status(400).json({
657
- message: 'No fields provided',
658
- error: 'no_fields_provided',
659
- });
660
- }
661
-
662
582
  const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${pks})`;
663
583
  try {
664
584
  const data = db.prepare(query).run();
665
585
 
666
586
  res.json({
667
- message: 'Rows updated',
587
+ message: 'Row updated',
668
588
  data,
669
589
  });
670
590
  } catch (error) {
@@ -687,73 +607,10 @@ const deleteRowInTableByPK = async (req, res) => {
687
607
  required: true,
688
608
  type: 'string'
689
609
  }
690
- #swagger.parameters['pk'] = {
691
- in: 'path',
692
- description: 'Primary key',
693
- required: true,
694
- }
695
- #swagger.parameters['_lookup_field'] = {
696
- in: 'query',
697
- description: 'If you want to delete row by any other field than primary key, use this parameter',
698
- required: false,
699
- type: 'string'
700
- }
701
-
702
- */
703
- const { name: tableName, pk } = req.params;
704
- const { _lookup_field } = req.query;
705
-
706
- let lookupField = _lookup_field;
707
-
708
- if (!_lookup_field) {
709
- // find the primary key of the table
710
- try {
711
- lookupField = db
712
- .prepare(`PRAGMA table_info(${tableName})`)
713
- .all()
714
- .find((field) => field.pk === 1).name;
715
- } catch (error) {
716
- return res.status(400).json({
717
- message: error.message,
718
- error: error,
719
- });
720
- }
721
- }
722
-
723
- const query = `DELETE FROM ${tableName} WHERE ${lookupField} = '${pk}'`;
724
-
725
- try {
726
- const data = db.prepare(query).run();
727
-
728
- if (data.changes === 0) {
729
- res.status(404).json({
730
- error: 'not_found',
731
- });
732
- } else {
733
- res.json({
734
- message: 'Row deleted',
735
- data,
736
- });
737
- }
738
- } catch (error) {
739
- res.status(400).json({
740
- message: error.message,
741
- error: error,
742
- });
743
- }
744
- };
745
-
746
- // Bulk Delete rows by id
747
- const bulkDeleteRowsInTableByPK = async (req, res) => {
748
- /*
749
- #swagger.tags = ['Rows']
750
- #swagger.summary = 'Bulk Delete Rows'
751
- #swagger.description = 'Bulks Delete rows by primary key'
752
- #swagger.parameters['name'] = {
610
+ #swagger.parameters['pks'] = {
753
611
  in: 'path',
754
- description: 'Table name',
612
+ description: 'Primary key (comma-separated for bulk delete)',
755
613
  required: true,
756
- type: 'string'
757
614
  }
758
615
  #swagger.parameters['_lookup_field'] = {
759
616
  in: 'query',
@@ -762,16 +619,9 @@ const bulkDeleteRowsInTableByPK = async (req, res) => {
762
619
  type: 'string'
763
620
  }
764
621
 
765
- #swagger.parameters['body'] = {
766
- in: 'body',
767
- required: true,
768
- type: 'object',
769
- schema: { $ref: "#/definitions/BulkDeleteRowsRequestBody" }
770
- }
771
622
  */
772
- const { name: tableName } = req.params;
623
+ const { name: tableName, pks } = req.params;
773
624
  const { _lookup_field } = req.query;
774
- const { pks } = req.body;
775
625
 
776
626
  let lookupField = _lookup_field;
777
627
 
@@ -801,7 +651,7 @@ const bulkDeleteRowsInTableByPK = async (req, res) => {
801
651
  });
802
652
  } else {
803
653
  res.json({
804
- message: 'Rows deleted',
654
+ message: 'Row deleted',
805
655
  data,
806
656
  });
807
657
  }
@@ -818,7 +668,5 @@ module.exports = {
818
668
  insertRowInTable,
819
669
  getRowInTableByPK,
820
670
  updateRowInTableByPK,
821
- bulkUpdateRowsInTableByPK,
822
671
  deleteRowInTableByPK,
823
- bulkDeleteRowsInTableByPK,
824
672
  };
@@ -25,17 +25,17 @@ describe('Rows Endpoints', () => {
25
25
  expect(res.body).toHaveProperty('data');
26
26
  });
27
27
 
28
- it('GET /tables/:name/rows/:pk should return a row by its primary key', async () => {
28
+ it('GET /tables/:name/rows/:pks should return a row by its primary key', async () => {
29
29
  const res = await requestWithSupertest.get('/api/tables/users/rows/1');
30
30
  expect(res.status).toEqual(200);
31
31
  expect(res.type).toEqual(expect.stringContaining('json'));
32
32
  expect(res.body).toHaveProperty('data');
33
- expect(res.body.data).toHaveProperty('id');
34
- expect(res.body.data).toHaveProperty('firstName');
35
- expect(res.body.data).toHaveProperty('lastName');
33
+ expect(res.body.data[0]).toHaveProperty('id');
34
+ expect(res.body.data[0]).toHaveProperty('firstName');
35
+ expect(res.body.data[0]).toHaveProperty('lastName');
36
36
  });
37
37
 
38
- it('PUT /tables/:name/rows/:pk should update a row by its primary key and return the number of changes', async () => {
38
+ it('PUT /tables/:name/rows/:pks should update a row by its primary key and return the number of changes', async () => {
39
39
  const res = await requestWithSupertest
40
40
  .put('/api/tables/users/rows/1')
41
41
  .send({ fields: { firstName: 'Jane', lastName: 'Doe' } });
@@ -43,7 +43,7 @@ describe('Rows Endpoints', () => {
43
43
  expect(res.type).toEqual(expect.stringContaining('json'));
44
44
  });
45
45
 
46
- it('DELETE /tables/:name/rows/:pk should delete a row by its primary key and return the number of changes', async () => {
46
+ it('DELETE /tables/:name/rows/:pks should delete a row by its primary key and return the number of changes', async () => {
47
47
  const res = await requestWithSupertest.delete('/api/tables/users/rows/1');
48
48
  expect(res.status).toEqual(200);
49
49
  expect(res.type).toEqual(expect.stringContaining('json'));
@@ -239,10 +239,11 @@ const deleteTable = async (req, res) => {
239
239
  const { name: tableName } = req.params;
240
240
  const query = `DROP TABLE ${tableName}`;
241
241
  try {
242
- db.prepare(query).run();
242
+ const data = db.prepare(query).run();
243
243
 
244
244
  res.json({
245
245
  message: 'Table deleted',
246
+ data,
246
247
  });
247
248
  } catch (error) {
248
249
  res.status(400).json({
@@ -17,27 +17,17 @@ router.post(
17
17
  controllers.insertRowInTable
18
18
  );
19
19
  router.get(
20
- '/:name/rows/:pk',
20
+ '/:name/rows/:pks',
21
21
  validator(schema.getRowInTableByPK),
22
22
  controllers.getRowInTableByPK
23
23
  );
24
24
  router.put(
25
- '/:name/rows/bulk',
26
- validator(schema.bulkUpdateRowsInTableByPK),
27
- controllers.bulkUpdateRowsInTableByPK
28
- );
29
- router.put(
30
- '/:name/rows/:pk',
25
+ '/:name/rows/:pks',
31
26
  validator(schema.updateRowInTableByPK),
32
27
  controllers.updateRowInTableByPK
33
28
  );
34
29
  router.delete(
35
- '/:name/rows/bulk',
36
- validator(schema.bulkDeleteRowsInTableByPK),
37
- controllers.bulkDeleteRowsInTableByPK
38
- );
39
- router.delete(
40
- '/:name/rows/:pk',
30
+ '/:name/rows/:pks',
41
31
  validator(schema.deleteRowInTableByPK),
42
32
  controllers.deleteRowInTableByPK
43
33
  );
@@ -18,7 +18,7 @@ const insertRowInTable = Joi.object({
18
18
 
19
19
  const getRowInTableByPK = Joi.object({
20
20
  name: Joi.string().min(3).max(30).required(),
21
- pk: Joi.string().required(),
21
+ pks: Joi.string().required(),
22
22
  _lookup_field: Joi.string().min(3).max(30),
23
23
  _schema: Joi.string(),
24
24
  _extend: Joi.string(),
@@ -26,27 +26,14 @@ const getRowInTableByPK = Joi.object({
26
26
 
27
27
  const updateRowInTableByPK = Joi.object({
28
28
  name: Joi.string().min(3).max(30).required(),
29
- pk: Joi.string().required(),
30
- fields: Joi.object().required(),
31
- _lookup_field: Joi.string().min(3).max(30),
32
- });
33
-
34
- const bulkUpdateRowsInTableByPK = Joi.object({
35
- name: Joi.string().min(3).max(30).required(),
36
- pks: Joi.array().items(Joi.required()).required(),
29
+ pks: Joi.string().required(),
37
30
  fields: Joi.object().required(),
38
31
  _lookup_field: Joi.string().min(3).max(30),
39
32
  });
40
33
 
41
34
  const deleteRowInTableByPK = Joi.object({
42
35
  name: Joi.string().min(3).max(30).required(),
43
- pk: Joi.string().required(),
44
- _lookup_field: Joi.string().min(3).max(30),
45
- });
46
-
47
- const bulkDeleteRowsInTableByPK = Joi.object({
48
- name: Joi.string().min(3).max(30).required(),
49
- pks: Joi.array().items(Joi.required()).required(),
36
+ pks: Joi.string().required(),
50
37
  _lookup_field: Joi.string().min(3).max(30),
51
38
  });
52
39
 
@@ -55,7 +42,5 @@ module.exports = {
55
42
  insertRowInTable,
56
43
  getRowInTableByPK,
57
44
  updateRowInTableByPK,
58
- bulkUpdateRowsInTableByPK,
59
45
  deleteRowInTableByPK,
60
- bulkDeleteRowsInTableByPK,
61
46
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "swagger": "2.0",
3
3
  "info": {
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "title": "Soul API",
6
6
  "description": "API Documentation for <b>Soul</b>, a simple SQLite RESTful server. "
7
7
  },
@@ -296,7 +296,7 @@
296
296
  }
297
297
  }
298
298
  },
299
- "/api/tables/{name}/rows/{pk}": {
299
+ "/api/tables/{name}/rows/{pks}": {
300
300
  "get": {
301
301
  "tags": [
302
302
  "Rows"
@@ -312,11 +312,11 @@
312
312
  "description": "Table name"
313
313
  },
314
314
  {
315
- "name": "pk",
315
+ "name": "pks",
316
316
  "in": "path",
317
317
  "required": true,
318
318
  "type": "string",
319
- "description": "Primary key"
319
+ "description": "Primary key (comma-separated for bulk retrieve)"
320
320
  },
321
321
  {
322
322
  "name": "_schema",
@@ -367,11 +367,11 @@
367
367
  "description": "Table name"
368
368
  },
369
369
  {
370
- "name": "pk",
370
+ "name": "pks",
371
371
  "in": "path",
372
372
  "required": true,
373
373
  "type": "string",
374
- "description": "Primary key"
374
+ "description": "Primary key (comma-separated for bulk update)"
375
375
  },
376
376
  {
377
377
  "name": "body",
@@ -433,11 +433,11 @@
433
433
  "description": "Table name"
434
434
  },
435
435
  {
436
- "name": "pk",
436
+ "name": "pks",
437
437
  "in": "path",
438
438
  "required": true,
439
439
  "type": "string",
440
- "description": "Primary key"
440
+ "description": "Primary key (comma-separated for bulk delete)"
441
441
  },
442
442
  {
443
443
  "name": "_lookup_field",
@@ -459,129 +459,6 @@
459
459
  }
460
460
  }
461
461
  }
462
- },
463
- "/api/tables/{name}/rows/bulk": {
464
- "put": {
465
- "tags": [
466
- "Rows"
467
- ],
468
- "summary": "Bulk Update Rows",
469
- "description": "Bulk Update rows by primary key",
470
- "parameters": [
471
- {
472
- "name": "name",
473
- "in": "path",
474
- "required": true,
475
- "type": "string",
476
- "description": "Table name"
477
- },
478
- {
479
- "name": "body",
480
- "in": "body",
481
- "required": true,
482
- "schema": {
483
- "$ref": "#/definitions/BulkUpdateRowsRequestBody"
484
- }
485
- },
486
- {
487
- "name": "_lookup_field",
488
- "in": "query",
489
- "description": "If you want to update row by any other field than primary key, use this parameter",
490
- "required": false,
491
- "type": "string"
492
- },
493
- {
494
- "name": "in",
495
- "in": "query",
496
- "type": "string"
497
- },
498
- {
499
- "name": "required",
500
- "in": "query",
501
- "type": "string"
502
- },
503
- {
504
- "name": "type",
505
- "in": "query",
506
- "type": "string"
507
- },
508
- {
509
- "name": "schema",
510
- "in": "query",
511
- "type": "string"
512
- }
513
- ],
514
- "responses": {
515
- "200": {
516
- "description": "OK"
517
- },
518
- "400": {
519
- "description": "Bad Request"
520
- }
521
- }
522
- },
523
- "delete": {
524
- "tags": [
525
- "Rows"
526
- ],
527
- "summary": "Bulk Delete Rows",
528
- "description": "Bulks Delete rows by primary key",
529
- "parameters": [
530
- {
531
- "name": "name",
532
- "in": "path",
533
- "required": true,
534
- "type": "string",
535
- "description": "Table name"
536
- },
537
- {
538
- "name": "_lookup_field",
539
- "in": "query",
540
- "description": "If you want to delete row by any other field than primary key, use this parameter",
541
- "required": false,
542
- "type": "string"
543
- },
544
- {
545
- "name": "body",
546
- "in": "body",
547
- "required": true,
548
- "schema": {
549
- "$ref": "#/definitions/BulkDeleteRowsRequestBody"
550
- }
551
- },
552
- {
553
- "name": "in",
554
- "in": "query",
555
- "type": "string"
556
- },
557
- {
558
- "name": "required",
559
- "in": "query",
560
- "type": "string"
561
- },
562
- {
563
- "name": "type",
564
- "in": "query",
565
- "type": "string"
566
- },
567
- {
568
- "name": "schema",
569
- "in": "query",
570
- "type": "string"
571
- }
572
- ],
573
- "responses": {
574
- "200": {
575
- "description": "OK"
576
- },
577
- "400": {
578
- "description": "Bad Request"
579
- },
580
- "404": {
581
- "description": "Not Found"
582
- }
583
- }
584
- }
585
462
  }
586
463
  },
587
464
  "definitions": {