@galadrim/galadmin 0.1.217 → 0.1.219

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": "@galadrim/galadmin",
3
- "version": "0.1.217",
3
+ "version": "0.1.219",
4
4
  "main": "dist/index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
package/src/api.ts CHANGED
@@ -32,7 +32,7 @@ export default (db, config, user) => {
32
32
  next();
33
33
  })
34
34
 
35
- async function getRecords(params) {
35
+ async function getRecords(params, computeCount = false) {
36
36
  let queryString = "";
37
37
  if (params.filters) {
38
38
  queryString = " WHERE (" + params.filters + ")";
@@ -63,22 +63,37 @@ export default (db, config, user) => {
63
63
  queryString += ")"
64
64
  }
65
65
  const limit = params.allRecords ? 999999 : parseInt(params.limit)
66
- const query = params.customQuery || ("SELECT " + (params.selectExpression || "*") + " FROM ?? " + (params.join || "") + queryString + " " + (params.groupBy ? "GROUP BY " + params.groupBy : "") + (params.having ? " HAVING " + params.having : "") + (params.orderBy || params.primaryId ? " ORDER BY " + (params.orderBy || `${params.table}.${params.primaryId}`) : "") + " LIMIT ? OFFSET ?")
67
- const queryParams = [params.table, limit, Math.max((params.page - 1) * limit, 0)]
68
- const { err, rows } = await db.query(query, queryParams)
66
+ const query = params.customQuery || `SELECT
67
+ ${params.selectExpression || "*"}
68
+ FROM ${params.table}
69
+ ${params.join || ""}
70
+ ${queryString}
71
+ ${params.groupBy ? "GROUP BY " + params.groupBy : ""}
72
+ ${params.having ? " HAVING " + params.having : ""}
73
+ ${params.orderBy || params.primaryId ? " ORDER BY " + (params.orderBy || `${params.table}.${params.primaryId}`) : ""}`
74
+ const { err, rows } = await db.query(`${query} LIMIT ${limit} OFFSET ${Math.max((params.page - 1) * limit, 0)}`)
69
75
  if (err) {
70
76
  console.error(err)
71
77
  return {
72
78
  error: err
73
79
  }
74
80
  }
81
+ let count = undefined;
82
+ if (computeCount) {
83
+ const { err: countErr, rows: countRows } = await db.query(`SELECT COUNT(*) AS count FROM (${query}) s`)
84
+ if (countErr) {
85
+ console.error(countErr)
86
+ }
87
+ count = countRows?.[0]?.count || 0
88
+ }
75
89
  return {
76
90
  rows,
77
91
  queryString,
78
92
  input: {
79
93
  query,
80
94
  params
81
- }
95
+ },
96
+ count
82
97
  }
83
98
  }
84
99
 
@@ -203,7 +218,7 @@ export default (db, config, user) => {
203
218
  const isDesc = req.body.isDesc === null ? view.isDesc : req.body.isDesc
204
219
  const subview = view.subviews && req.body.subviewIndex !== undefined ? view.subviews[req.body.subviewIndex] : null
205
220
  const orderBy = req.body.orderBy || subview?.orderBy || view.orderBy
206
- const { rows: records, queryString, input, error } = await getRecords({
221
+ const { rows: records, input, error, count } = await getRecords({
207
222
  table: view.tableName,
208
223
  page: req.body.page,
209
224
  filters: sqlAnd([
@@ -222,7 +237,7 @@ export default (db, config, user) => {
222
237
  groupBy: view.groupBy,
223
238
  columns: view.columns,
224
239
  limit: view.limit || 10,
225
- })
240
+ }, true)
226
241
  if (error) {
227
242
  return res.status(500).send(error)
228
243
  }
@@ -234,21 +249,11 @@ export default (db, config, user) => {
234
249
  }
235
250
  }
236
251
  }
237
- const { err, rows: counts } = await db.query("SELECT COUNT(*) AS count FROM (SELECT 1 FROM ?? " + (view.join || "") + queryString + " " + (view.groupBy ? "GROUP BY " + view.groupBy : "") + (subview?.having ? " HAVING " + subview?.having : "") + ") s", [view.tableName])
238
- if (err) {
239
- res.send({
240
- records,
241
- input,
242
- count: "-",
243
- countErr: err
244
- })
245
- } else {
246
- res.send({
247
- records,
248
- input,
249
- count: counts[0].count
250
- })
251
- }
252
+ res.send({
253
+ records,
254
+ input,
255
+ count
256
+ })
252
257
  })
253
258
 
254
259
  const formattedValueToFunction = {}