@saltcorn/postgres 1.6.0-alpha.7 → 1.6.0-alpha.8

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/postgres.js +38 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/postgres",
3
- "version": "1.6.0-alpha.7",
3
+ "version": "1.6.0-alpha.8",
4
4
  "description": "Postgres structures for Saltcorn, open-source no-code platform",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "main": "index.js",
14
14
  "dependencies": {
15
- "@saltcorn/db-common": "1.6.0-alpha.7",
15
+ "@saltcorn/db-common": "1.6.0-alpha.8",
16
16
  "@saltcorn/plain-date": "0.2.5",
17
17
  "pg": "^8.13.1",
18
18
  "pg-copy-streams": "^6.0.6",
package/postgres.js CHANGED
@@ -106,13 +106,44 @@ const getMyClient = (selopts) => {
106
106
  const select = async (tbl, whereObj, selectopts = Object.create(null)) => {
107
107
  const { where, values } = mkWhere(whereObj);
108
108
  const schema = selectopts.schema || getTenantSchema();
109
- const sql = `SELECT ${
110
- selectopts.fields ? selectopts.fields.join(", ") : `*`
111
- } FROM "${schema}"."${sqlsanitize(tbl)}" ${where} ${mkSelectOptions(
112
- selectopts,
113
- values,
114
- false
115
- )}`;
109
+ let sql;
110
+ if (selectopts.tree_field)
111
+ sql = `WITH RECURSIVE _tree AS (
112
+ SELECT ${
113
+ selectopts.fields ? selectopts.fields.join(", ") : `*`
114
+ }, 0 as _level
115
+ ${selectopts.orderBy ? `, ARRAY[row_number() over (ORDER BY "${sqlsanitize(selectopts.orderBy)}"${selectopts.orderDesc ? " DESC" : ""})] as _sort_path` : ""}
116
+ FROM "${schema}"."${sqlsanitize(tbl)}"
117
+ WHERE "${selectopts.tree_field}" IS NULL
118
+
119
+ UNION ALL
120
+
121
+ SELECT ${
122
+ selectopts.fields
123
+ ? selectopts.fields.map((f) => `p."${f}"`).join(", ")
124
+ : `p.*`
125
+ }, pt._level+1
126
+ ${selectopts.orderBy ? `, pt._sort_path || row_number() OVER (PARTITION BY p."${selectopts.tree_field}" ORDER BY p."${selectopts.orderBy}"${selectopts.orderDesc ? " DESC" : ""})` : ""}
127
+ FROM "${schema}"."${sqlsanitize(tbl)}" p
128
+ JOIN _tree pt ON p."${selectopts.tree_field}" = pt."${selectopts.pk_name || "id"}"
129
+ )
130
+ SELECT ${
131
+ selectopts.fields ? selectopts.fields.join(", ") : `*`
132
+ }, _level FROM _tree ${where} ${mkSelectOptions(
133
+ selectopts.orderBy
134
+ ? { ...selectopts, orderBy: "_sort_path", orderDesc: false }
135
+ : selectopts,
136
+ values,
137
+ false
138
+ )}`;
139
+ else
140
+ sql = `SELECT ${
141
+ selectopts.fields ? selectopts.fields.join(", ") : `*`
142
+ } FROM "${schema}"."${sqlsanitize(tbl)}" ${where} ${mkSelectOptions(
143
+ selectopts,
144
+ values,
145
+ false
146
+ )}`;
116
147
  sql_log(sql, values);
117
148
  const tq = await getMyClient(selectopts).query(sql, values);
118
149