soul-cli 0.3.4 → 0.4.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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "soul-cli",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "A SQLite REST and Realtime server",
5
5
  "main": "src/server.js",
6
6
  "bin": {
@@ -1,16 +1,22 @@
1
1
  const db = require('../db/index');
2
2
 
3
+ const quotePrimaryKeys = (pks) => {
4
+ const primaryKeys = pks.split(',');
5
+ const quotedPks = primaryKeys.map((id) => `'${id}'`).join(',');
6
+ return quotedPks;
7
+ };
8
+
3
9
  // Return paginated rows of a table
4
10
  const listTableRows = async (req, res) => {
5
- /*
11
+ /*
6
12
  #swagger.tags = ['Rows']
7
- #swagger.summary = 'List Rows'
13
+ #swagger.summary = 'List Rows'
8
14
  #swagger.description = 'Endpoint to list rows of a table.'
9
- #swagger.parameters['name'] = {
15
+ #swagger.parameters['name'] = {
10
16
  description: 'Table name.',
11
17
  in: 'path',
12
18
  }
13
- #swagger.parameters['_page'] = {
19
+ #swagger.parameters['_page'] = {
14
20
  description: 'Page number.' ,
15
21
  in: 'query',
16
22
  type: 'number',
@@ -68,7 +74,7 @@ const listTableRows = async (req, res) => {
68
74
  if (_filters !== '') {
69
75
  whereString += ' WHERE ';
70
76
  whereString += filters
71
- .map((filter) => `${tableName}.${filter.field} LIKE '%${filter.value}%'`)
77
+ .map((filter) => `${tableName}.${filter.field} = '${filter.value}'`)
72
78
  .join(' AND ');
73
79
  }
74
80
 
@@ -479,7 +485,9 @@ const getRowInTableByPK = async (req, res) => {
479
485
  });
480
486
  }
481
487
 
482
- const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${pks})`;
488
+ const query = `SELECT ${schemaString} FROM ${tableName} ${extendString} WHERE ${tableName}.${lookupField} in (${quotePrimaryKeys(
489
+ pks
490
+ )})`;
483
491
 
484
492
  try {
485
493
  let data = db.prepare(query).all();
@@ -534,7 +542,7 @@ const updateRowInTableByPK = async (req, res, next) => {
534
542
  }
535
543
 
536
544
  #swagger.parameters['body'] = {
537
- in: 'body',
545
+ in: 'body',
538
546
  required: true,
539
547
  type: 'object',
540
548
  schema: { $ref: "#/definitions/UpdateRowRequestBody" }
@@ -587,7 +595,10 @@ const updateRowInTableByPK = async (req, res, next) => {
587
595
  });
588
596
  }
589
597
 
590
- const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${pks})`;
598
+ const query = `UPDATE ${tableName} SET ${fieldsString} WHERE ${lookupField} in (${quotePrimaryKeys(
599
+ pks
600
+ )})`;
601
+
591
602
  try {
592
603
  const data = db.prepare(query).run();
593
604
 
@@ -657,7 +668,9 @@ const deleteRowInTableByPK = async (req, res, next) => {
657
668
  }
658
669
  }
659
670
 
660
- const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${pks})`;
671
+ const query = `DELETE FROM ${tableName} WHERE ${lookupField} in (${quotePrimaryKeys(
672
+ pks
673
+ )})`;
661
674
 
662
675
  try {
663
676
  const data = db.prepare(query).run();
@@ -69,7 +69,7 @@ const createTable = async (req, res) => {
69
69
  // add id if primary key is not defined
70
70
  if (!schema.find((field) => field.primaryKey)) {
71
71
  schemaString = `
72
- id INTEGER PRIMARY KEY AUTOINCREMENT,
72
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
73
73
  ${schemaString}
74
74
  `;
75
75
  }
@@ -92,7 +92,7 @@ const createTable = async (req, res) => {
92
92
  let indicesString = indices
93
93
  .map((field) => {
94
94
  return `
95
- CREATE INDEX ${tableName}_${field}_index
95
+ CREATE INDEX ${tableName}_${field}_index
96
96
  ON ${tableName} (${field})
97
97
  `;
98
98
  })
@@ -162,7 +162,7 @@ const listTables = async (req, res) => {
162
162
  */
163
163
  const { _search, _ordering } = req.query;
164
164
 
165
- let query = `SELECT name FROM sqlite_master WHERE type = 'table'`;
165
+ let query = `SELECT name FROM sqlite_master WHERE type IN ('table', 'view')`;
166
166
 
167
167
  // if search is provided, search the tables
168
168
  // e.g. ?_search=users
@@ -237,7 +237,7 @@ const deleteTable = async (req, res) => {
237
237
  type: 'string',
238
238
  description: 'Name of the table'
239
239
  }
240
-
240
+
241
241
  */
242
242
  const { name: tableName } = req.params;
243
243
  const query = `DROP TABLE ${tableName}`;