prostgles-server 2.0.146 → 2.0.149

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 (76) hide show
  1. package/dist/AuthHandler.d.ts +15 -13
  2. package/dist/AuthHandler.d.ts.map +1 -1
  3. package/dist/AuthHandler.js +43 -44
  4. package/dist/AuthHandler.js.map +1 -1
  5. package/dist/DBEventsManager.d.ts +6 -5
  6. package/dist/DBEventsManager.d.ts.map +1 -1
  7. package/dist/DBEventsManager.js +8 -2
  8. package/dist/DBEventsManager.js.map +1 -1
  9. package/dist/DboBuilder.d.ts +54 -50
  10. package/dist/DboBuilder.d.ts.map +1 -1
  11. package/dist/DboBuilder.js +242 -206
  12. package/dist/DboBuilder.js.map +1 -1
  13. package/dist/FileManager.d.ts +5 -5
  14. package/dist/FileManager.d.ts.map +1 -1
  15. package/dist/FileManager.js +59 -31
  16. package/dist/FileManager.js.map +1 -1
  17. package/dist/Filtering.d.ts.map +1 -1
  18. package/dist/Filtering.js +17 -14
  19. package/dist/Filtering.js.map +1 -1
  20. package/dist/PostgresNotifListenManager.d.ts +3 -3
  21. package/dist/PostgresNotifListenManager.d.ts.map +1 -1
  22. package/dist/PostgresNotifListenManager.js +7 -5
  23. package/dist/PostgresNotifListenManager.js.map +1 -1
  24. package/dist/Prostgles.d.ts +6 -9
  25. package/dist/Prostgles.d.ts.map +1 -1
  26. package/dist/Prostgles.js +125 -86
  27. package/dist/Prostgles.js.map +1 -1
  28. package/dist/PubSubManager.d.ts +8 -8
  29. package/dist/PubSubManager.d.ts.map +1 -1
  30. package/dist/PubSubManager.js +58 -52
  31. package/dist/PubSubManager.js.map +1 -1
  32. package/dist/QueryBuilder.d.ts +6 -5
  33. package/dist/QueryBuilder.d.ts.map +1 -1
  34. package/dist/QueryBuilder.js +124 -89
  35. package/dist/QueryBuilder.js.map +1 -1
  36. package/dist/SyncReplication.js +38 -35
  37. package/dist/SyncReplication.js.map +1 -1
  38. package/dist/TableConfig.d.ts +0 -1
  39. package/dist/TableConfig.d.ts.map +1 -1
  40. package/dist/TableConfig.js +36 -28
  41. package/dist/TableConfig.js.map +1 -1
  42. package/dist/shortestPath.d.ts.map +1 -1
  43. package/dist/shortestPath.js +2 -1
  44. package/dist/shortestPath.js.map +1 -1
  45. package/dist/utils.d.ts +2 -1
  46. package/dist/utils.d.ts.map +1 -1
  47. package/dist/utils.js +3 -1
  48. package/dist/utils.js.map +1 -1
  49. package/lib/AuthHandler.ts +50 -40
  50. package/lib/DBEventsManager.ts +14 -7
  51. package/lib/DboBuilder.ts +231 -195
  52. package/lib/FileManager.ts +30 -21
  53. package/lib/Filtering.ts +19 -16
  54. package/lib/PostgresNotifListenManager.ts +11 -10
  55. package/lib/Prostgles.ts +89 -73
  56. package/lib/PubSubManager.ts +13 -11
  57. package/lib/QueryBuilder.ts +53 -29
  58. package/lib/SyncReplication.ts +10 -10
  59. package/lib/TableConfig.ts +23 -15
  60. package/lib/shortestPath.ts +6 -4
  61. package/lib/utils.ts +3 -2
  62. package/package.json +4 -3
  63. package/tests/client/PID.txt +1 -1
  64. package/tests/client/index.js +10 -7
  65. package/tests/client/index.ts +12 -8
  66. package/tests/client/package-lock.json +14 -14
  67. package/tests/client/package.json +2 -2
  68. package/tests/client/tsconfig.json +2 -2
  69. package/tests/client_only_queries.js +127 -104
  70. package/tests/client_only_queries.ts +43 -17
  71. package/tests/isomorphic_queries.js +22 -6
  72. package/tests/isomorphic_queries.ts +18 -6
  73. package/tests/server/package-lock.json +21 -19
  74. package/tests/server/package.json +2 -2
  75. package/tests/server/tsconfig.json +2 -2
  76. package/tsconfig.json +3 -2
@@ -6,18 +6,18 @@
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.makeQuery = exports.getNewQuery = exports.SelectItemBuilder = exports.COMPUTED_FIELDS = exports.FUNCTIONS = exports.parseFunction = exports.parseFunctionObject = exports.asNameAlias = void 0;
8
8
  const DboBuilder_1 = require("./DboBuilder");
9
- const Prostgles_1 = require("./Prostgles");
10
9
  const prostgles_types_1 = require("prostgles-types");
11
10
  const utils_1 = require("./utils");
12
- exports.asNameAlias = (field, tableAlias) => {
13
- let result = prostgles_types_1.asName(field);
11
+ const asNameAlias = (field, tableAlias) => {
12
+ let result = (0, prostgles_types_1.asName)(field);
14
13
  if (tableAlias)
15
- return prostgles_types_1.asName(tableAlias) + "." + result;
14
+ return (0, prostgles_types_1.asName)(tableAlias) + "." + result;
16
15
  return result;
17
16
  };
18
- exports.parseFunctionObject = (funcData) => {
17
+ exports.asNameAlias = asNameAlias;
18
+ const parseFunctionObject = (funcData) => {
19
19
  const makeErr = (msg) => `Function not specified correctly. Expecting { $funcName: ["columnName",...] } object but got: ${JSON.stringify(funcData)} \n ${msg}`;
20
- if (!utils_1.isObject(funcData))
20
+ if (!(0, utils_1.isObject)(funcData))
21
21
  throw makeErr("");
22
22
  const keys = Object.keys(funcData);
23
23
  if (keys.length !== 1)
@@ -29,8 +29,8 @@ exports.parseFunctionObject = (funcData) => {
29
29
  }
30
30
  return { funcName, args };
31
31
  };
32
- exports.parseFunction = (funcData) => {
33
- var _a;
32
+ exports.parseFunctionObject = parseFunctionObject;
33
+ const parseFunction = (funcData) => {
34
34
  const { func, args, functions, allowedFields } = funcData;
35
35
  /* Function is computed column. No checks needed */
36
36
  if (typeof func !== "string") {
@@ -58,7 +58,7 @@ exports.parseFunction = (funcData) => {
58
58
  throw makeErr(`getFields() => field name ${JSON.stringify(fieldKey)} is invalid or disallowed`);
59
59
  }
60
60
  });
61
- if (((_a = funcDef.minCols) !== null && _a !== void 0 ? _a : 0) > fields.length) {
61
+ if ((funcDef.minCols ?? 0) > fields.length) {
62
62
  throw makeErr(`Less columns provided than necessary (minCols=${funcDef.minCols})`);
63
63
  }
64
64
  }
@@ -67,6 +67,7 @@ exports.parseFunction = (funcData) => {
67
67
  }
68
68
  return funcDef;
69
69
  };
70
+ exports.parseFunction = parseFunction;
70
71
  const MAX_COL_NUM = 1600;
71
72
  const asValue = (v, castAs = "") => DboBuilder_1.pgp.as.format("$1" + castAs, [v]);
72
73
  const FTS_Funcs =
@@ -88,12 +89,12 @@ const FTS_Funcs =
88
89
  numArgs: 2,
89
90
  getFields: (args) => [args[0]],
90
91
  getQuery: ({ allColumns, args, tableAlias }) => {
91
- const col = prostgles_types_1.asName(args[0]);
92
+ const col = (0, prostgles_types_1.asName)(args[0]);
92
93
  let qVal = args[1], qType = "to_tsquery";
93
94
  let _type = type ? (asValue(type) + ",") : "";
94
95
  const searchTypes = prostgles_types_1.TextFilter_FullTextSearchFilterKeys;
95
96
  /* { to_tsquery: 'search term' } */
96
- if (DboBuilder_1.isPlainObject(qVal)) {
97
+ if ((0, DboBuilder_1.isPlainObject)(qVal)) {
97
98
  const keys = Object.keys(qVal);
98
99
  if (!keys.length)
99
100
  throw "Bad arg";
@@ -151,9 +152,11 @@ let PostGIS_Funcs = [
151
152
  getFields: (args) => [args[0]],
152
153
  getQuery: ({ allColumns, args, tableAlias }) => {
153
154
  const arg2 = args[1], mErr = () => { throw `${fname}: Expecting a second argument like: { lat?: number; lng?: number; geojson?: object; srid?: number; use_spheroid?: boolean }`; };
154
- if (!DboBuilder_1.isPlainObject(arg2))
155
+ if (!(0, DboBuilder_1.isPlainObject)(arg2))
155
156
  mErr();
156
157
  const col = allColumns.find(c => c.name === args[0]);
158
+ if (!col)
159
+ throw new Error("Col not found: " + args[0]);
157
160
  const { lat, lng, srid = 4326, geojson, text, use_spheroid, distance, spheroid = 'SPHEROID["WGS 84",6378137,298.257223563]', debug } = arg2;
158
161
  let geomQ = "", extraParams = "";
159
162
  if (typeof text === "string") {
@@ -162,7 +165,7 @@ let PostGIS_Funcs = [
162
165
  else if ([lat, lng].every(v => Number.isFinite(v))) {
163
166
  geomQ = `ST_Point(${asValue(lng)}, ${asValue(lat)})`;
164
167
  }
165
- else if (DboBuilder_1.isPlainObject(geojson)) {
168
+ else if ((0, DboBuilder_1.isPlainObject)(geojson)) {
166
169
  geomQ = `ST_GeomFromGeoJSON(${geojson})`;
167
170
  }
168
171
  else
@@ -220,12 +223,12 @@ let PostGIS_Funcs = [
220
223
  else if (fname === "<->") {
221
224
  colCast = colIsGeog ? "::geography" : "::geometry";
222
225
  geomQCast = colIsGeog ? "::geography" : "::geometry";
223
- const q = DboBuilder_1.pgp.as.format(`${exports.asNameAlias(args[0], tableAlias)}${colCast} <-> ${geomQ}${geomQCast}`);
226
+ const q = DboBuilder_1.pgp.as.format(`${(0, exports.asNameAlias)(args[0], tableAlias)}${colCast} <-> ${geomQ}${geomQCast}`);
224
227
  if (debug)
225
228
  throw q;
226
229
  return q;
227
230
  }
228
- const q = DboBuilder_1.pgp.as.format(`${fname}(${exports.asNameAlias(args[0], tableAlias)}${colCast} , ${geomQ}${geomQCast} ${extraParams})`);
231
+ const q = DboBuilder_1.pgp.as.format(`${fname}(${(0, exports.asNameAlias)(args[0], tableAlias)}${colCast} , ${geomQ}${geomQCast} ${extraParams})`);
229
232
  if (debug)
230
233
  throw q;
231
234
  return q;
@@ -249,7 +252,7 @@ PostGIS_Funcs = PostGIS_Funcs.concat([
249
252
  const otherArgs = args.slice(1);
250
253
  if (otherArgs.length)
251
254
  secondArg = ", " + otherArgs.map(arg => asValue(arg)).join(", ");
252
- const escTabelName = exports.asNameAlias(args[0], tableAlias) + "::geometry";
255
+ const escTabelName = (0, exports.asNameAlias)(args[0], tableAlias) + "::geometry";
253
256
  let result = DboBuilder_1.pgp.as.format(fname + "(" + escTabelName + secondArg + (fname === "ST_AsGeoJSON" ? ")::jsonb" : ")"));
254
257
  if (fname.startsWith("ST_SnapToGrid") || fname.startsWith("ST_Simplify")) {
255
258
  let r = `ST_AsGeoJSON(${result})::jsonb`;
@@ -272,7 +275,7 @@ PostGIS_Funcs = PostGIS_Funcs.concat(["ST_Extent", "ST_3DExtent", "ST_XMin_Agg",
272
275
  numArgs: 1,
273
276
  getFields: (args) => [args[0]],
274
277
  getQuery: ({ allowedFields, args, tableAlias }) => {
275
- const escTabelName = exports.asNameAlias(args[0], tableAlias) + "::geometry";
278
+ const escTabelName = (0, exports.asNameAlias)(args[0], tableAlias) + "::geometry";
276
279
  if (fname.includes("Extent")) {
277
280
  return `${fname}(${escTabelName})`;
278
281
  }
@@ -294,7 +297,7 @@ exports.FUNCTIONS = [
294
297
  numArgs: MAX_COL_NUM,
295
298
  getFields: (args) => args,
296
299
  getQuery: ({ allowedFields, args, tableAlias }) => {
297
- const q = DboBuilder_1.pgp.as.format("md5(" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + "::text, '' )").join(" || ") + ")");
300
+ const q = DboBuilder_1.pgp.as.format("md5(" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + "::text, '' )").join(" || ") + ")");
298
301
  return q;
299
302
  }
300
303
  },
@@ -306,7 +309,7 @@ exports.FUNCTIONS = [
306
309
  numArgs: MAX_COL_NUM,
307
310
  getFields: (args) => args,
308
311
  getQuery: ({ allowedFields, args, tableAlias }) => {
309
- const q = DboBuilder_1.pgp.as.format("md5(string_agg(" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + "::text, '' )").join(" || ") + ", ','))");
312
+ const q = DboBuilder_1.pgp.as.format("md5(string_agg(" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + "::text, '' )").join(" || ") + ", ','))");
310
313
  return q;
311
314
  }
312
315
  },
@@ -318,7 +321,7 @@ exports.FUNCTIONS = [
318
321
  numArgs: MAX_COL_NUM,
319
322
  getFields: (args) => args,
320
323
  getQuery: ({ allowedFields, args, tableAlias }) => {
321
- const q = DboBuilder_1.pgp.as.format("encode(sha256((" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + ", '' )").join(" || ") + ")::text::bytea), 'hex')");
324
+ const q = DboBuilder_1.pgp.as.format("encode(sha256((" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + ", '' )").join(" || ") + ")::text::bytea), 'hex')");
322
325
  return q;
323
326
  }
324
327
  },
@@ -330,7 +333,7 @@ exports.FUNCTIONS = [
330
333
  numArgs: MAX_COL_NUM,
331
334
  getFields: (args) => args,
332
335
  getQuery: ({ allowedFields, args, tableAlias }) => {
333
- const q = DboBuilder_1.pgp.as.format("encode(sha256(string_agg(" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + ", '' )").join(" || ") + ", ',')::text::bytea), 'hex')");
336
+ const q = DboBuilder_1.pgp.as.format("encode(sha256(string_agg(" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + ", '' )").join(" || ") + ", ',')::text::bytea), 'hex')");
334
337
  return q;
335
338
  }
336
339
  },
@@ -342,7 +345,7 @@ exports.FUNCTIONS = [
342
345
  numArgs: MAX_COL_NUM,
343
346
  getFields: (args) => args,
344
347
  getQuery: ({ allowedFields, args, tableAlias }) => {
345
- const q = DboBuilder_1.pgp.as.format("encode(sha512((" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + ", '' )").join(" || ") + ")::text::bytea), 'hex')");
348
+ const q = DboBuilder_1.pgp.as.format("encode(sha512((" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + ", '' )").join(" || ") + ")::text::bytea), 'hex')");
346
349
  return q;
347
350
  }
348
351
  },
@@ -354,7 +357,7 @@ exports.FUNCTIONS = [
354
357
  numArgs: MAX_COL_NUM,
355
358
  getFields: (args) => args,
356
359
  getQuery: ({ allowedFields, args, tableAlias }) => {
357
- const q = DboBuilder_1.pgp.as.format("encode(sha512(string_agg(" + args.map(fname => "COALESCE( " + exports.asNameAlias(fname, tableAlias) + ", '' )").join(" || ") + ", ',')::text::bytea), 'hex')");
360
+ const q = DboBuilder_1.pgp.as.format("encode(sha512(string_agg(" + args.map(fname => "COALESCE( " + (0, exports.asNameAlias)(fname, tableAlias) + ", '' )").join(" || ") + ", ',')::text::bytea), 'hex')");
358
361
  return q;
359
362
  }
360
363
  },
@@ -368,7 +371,7 @@ exports.FUNCTIONS = [
368
371
  singleColArg: false,
369
372
  getFields: (args) => [args[0]],
370
373
  getQuery: ({ allowedFields, args, tableAlias }) => {
371
- return DboBuilder_1.pgp.as.format("LEFT(" + exports.asNameAlias(args[0], tableAlias) + ", $1)", [args[1]]);
374
+ return DboBuilder_1.pgp.as.format("LEFT(" + (0, exports.asNameAlias)(args[0], tableAlias) + ", $1)", [args[1]]);
372
375
  }
373
376
  },
374
377
  {
@@ -379,7 +382,7 @@ exports.FUNCTIONS = [
379
382
  singleColArg: true,
380
383
  getFields: (args) => [args[0]],
381
384
  getQuery: ({ allowedFields, args, tableAlias }) => {
382
- return DboBuilder_1.pgp.as.format("unnest(string_to_array(" + exports.asNameAlias(args[0], tableAlias) + "::TEXT , ' '))"); //, [args[1]]
385
+ return DboBuilder_1.pgp.as.format("unnest(string_to_array(" + (0, exports.asNameAlias)(args[0], tableAlias) + "::TEXT , ' '))"); //, [args[1]]
383
386
  }
384
387
  },
385
388
  {
@@ -390,7 +393,7 @@ exports.FUNCTIONS = [
390
393
  singleColArg: false,
391
394
  getFields: (args) => [args[0]],
392
395
  getQuery: ({ allowedFields, args, tableAlias }) => {
393
- return DboBuilder_1.pgp.as.format("RIGHT(" + exports.asNameAlias(args[0], tableAlias) + ", $1)", [args[1]]);
396
+ return DboBuilder_1.pgp.as.format("RIGHT(" + (0, exports.asNameAlias)(args[0], tableAlias) + ", $1)", [args[1]]);
394
397
  }
395
398
  },
396
399
  {
@@ -402,9 +405,9 @@ exports.FUNCTIONS = [
402
405
  getFields: (args) => [args[0]],
403
406
  getQuery: ({ allowedFields, args, tableAlias }) => {
404
407
  if (args.length === 3) {
405
- return DboBuilder_1.pgp.as.format("to_char(" + exports.asNameAlias(args[0], tableAlias) + ", $2, $3)", [args[0], args[1], args[2]]);
408
+ return DboBuilder_1.pgp.as.format("to_char(" + (0, exports.asNameAlias)(args[0], tableAlias) + ", $2, $3)", [args[0], args[1], args[2]]);
406
409
  }
407
- return DboBuilder_1.pgp.as.format("to_char(" + exports.asNameAlias(args[0], tableAlias) + ", $2)", [args[0], args[1]]);
410
+ return DboBuilder_1.pgp.as.format("to_char(" + (0, exports.asNameAlias)(args[0], tableAlias) + ", $2)", [args[0], args[1]]);
408
411
  }
409
412
  },
410
413
  /**
@@ -464,7 +467,7 @@ exports.FUNCTIONS = [
464
467
  numArgs: 1,
465
468
  getFields: (args) => [args[0]],
466
469
  getQuery: ({ allowedFields, args, tableAlias }) => {
467
- const col = exports.asNameAlias(args[0], tableAlias);
470
+ const col = (0, exports.asNameAlias)(args[0], tableAlias);
468
471
  if (!val)
469
472
  return `date_trunc(${asValue(unit)}, ${col})`;
470
473
  const prevInt = {
@@ -487,7 +490,7 @@ exports.FUNCTIONS = [
487
490
  singleColArg: false,
488
491
  getFields: (args) => [args[1]],
489
492
  getQuery: ({ allowedFields, args, tableAlias }) => {
490
- return `${funcName}(${asValue(args[0])}, ${exports.asNameAlias(args[1], tableAlias)})`;
493
+ return `${funcName}(${asValue(args[0])}, ${(0, exports.asNameAlias)(args[1], tableAlias)})`;
491
494
  }
492
495
  })),
493
496
  /* Handy date funcs */
@@ -526,18 +529,42 @@ exports.FUNCTIONS = [
526
529
  numArgs: 1,
527
530
  getFields: (args) => [args[0]],
528
531
  getQuery: ({ allowedFields, args, tableAlias }) => {
529
- return DboBuilder_1.pgp.as.format("trim(to_char(" + exports.asNameAlias(args[0], tableAlias) + ", $2))", [args[0], txt]);
532
+ return DboBuilder_1.pgp.as.format("trim(to_char(" + (0, exports.asNameAlias)(args[0], tableAlias) + ", $2))", [args[0], txt]);
530
533
  }
531
534
  })),
532
535
  /* Basic 1 arg col funcs */
533
- ...["upper", "lower", "length", "reverse", "trim", "initcap", "round", "ceil", "floor", "sign", "age", "md5"].map(funcName => ({
536
+ ...["upper", "lower", "length", "reverse", "trim", "initcap", "round", "ceil", "floor", "sign", "md5"].map(funcName => ({
534
537
  name: "$" + funcName,
535
538
  type: "function",
536
539
  numArgs: 1,
537
540
  singleColArg: true,
538
541
  getFields: (args) => [args[0]],
539
542
  getQuery: ({ allowedFields, args, tableAlias }) => {
540
- return funcName + "(" + exports.asNameAlias(args[0], tableAlias) + ")";
543
+ return funcName + "(" + (0, exports.asNameAlias)(args[0], tableAlias) + ")";
544
+ }
545
+ })),
546
+ /* Interval funcs */
547
+ ...["age", "difference"].map(funcName => ({
548
+ name: "$" + funcName,
549
+ type: "function",
550
+ numArgs: 1,
551
+ singleColArg: true,
552
+ getFields: (args) => args,
553
+ getQuery: ({ allowedFields, args, tableAlias }) => {
554
+ const validCols = args.filter(a => typeof a === "string").length;
555
+ if (funcName === "difference" && validCols !== 2)
556
+ throw new Error("Must have two column names");
557
+ if (![1, 2].includes(validCols))
558
+ throw new Error("Must have one or two column names");
559
+ const [leftField, rightField] = args;
560
+ const leftQ = (0, exports.asNameAlias)(leftField, tableAlias);
561
+ const rightQ = rightField ? ("," + (0, exports.asNameAlias)(rightField, tableAlias)) : "";
562
+ if (funcName === "age") {
563
+ return `${funcName}(${leftQ}, ${rightQ})`;
564
+ }
565
+ else {
566
+ return `${leftQ} - ${rightQ}`;
567
+ }
541
568
  }
542
569
  })),
543
570
  /* pgcrypto funcs */
@@ -548,7 +575,7 @@ exports.FUNCTIONS = [
548
575
  singleColArg: false,
549
576
  getFields: (args) => [args[1]],
550
577
  getQuery: ({ allowedFields, args, tableAlias }) => {
551
- const value = asValue(args[0]) + "", seedColumnName = exports.asNameAlias(args[1], tableAlias);
578
+ const value = asValue(args[0]) + "", seedColumnName = (0, exports.asNameAlias)(args[1], tableAlias);
552
579
  return `crypt(${value}, ${seedColumnName}::text)`;
553
580
  }
554
581
  })),
@@ -560,7 +587,7 @@ exports.FUNCTIONS = [
560
587
  singleColArg: false,
561
588
  getFields: (args) => [args[1]],
562
589
  getQuery: ({ allowedFields, args, tableAlias }) => {
563
- let a1 = asValue(args[0]), a2 = exports.asNameAlias(args[1], tableAlias);
590
+ let a1 = asValue(args[0]), a2 = (0, exports.asNameAlias)(args[1], tableAlias);
564
591
  if (funcName === "position_lower") {
565
592
  a1 = `LOWER(${a1}::text)`;
566
593
  a2 = `LOWER(${a2}::text)`;
@@ -574,7 +601,7 @@ exports.FUNCTIONS = [
574
601
  numArgs: 1,
575
602
  minCols: 0,
576
603
  singleColArg: false,
577
- getFields: (args, allowedFields) => [],
604
+ getFields: (args) => [],
578
605
  getQuery: ({ allowedFields, args, tableAlias }) => {
579
606
  let value = asValue(args[0]);
580
607
  if (typeof value !== "string")
@@ -585,7 +612,7 @@ exports.FUNCTIONS = [
585
612
  });
586
613
  value = asValue(value);
587
614
  if (usedColumns.length) {
588
- return `format(${value}, ${usedColumns.map(c => `${exports.asNameAlias(c, tableAlias)}::TEXT`).join(", ")})`;
615
+ return `format(${value}, ${usedColumns.map(c => `${(0, exports.asNameAlias)(c, tableAlias)}::TEXT`).join(", ")})`;
589
616
  }
590
617
  return `format(${value})`;
591
618
  }
@@ -608,7 +635,7 @@ exports.FUNCTIONS = [
608
635
  let term = args[1];
609
636
  const rawTerm = args[1];
610
637
  let { edgeTruncate, noFields = false, returnType, matchCase = false } = args[2] || {};
611
- if (!prostgles_types_1.isEmpty(args[2])) {
638
+ if (!(0, prostgles_types_1.isEmpty)(args[2])) {
612
639
  const keys = Object.keys(args[2]);
613
640
  const validKeys = ["edgeTruncate", "noFields", "returnType", "matchCase"];
614
641
  const bad_keys = keys.filter(k => !validKeys.includes(k));
@@ -652,7 +679,7 @@ exports.FUNCTIONS = [
652
679
  END
653
680
  `;
654
681
  };
655
- let colRaw = "( " + cols.map(c => `${noFields ? "" : (asValue(c + ": ") + " || ")} COALESCE(${exports.asNameAlias(c, tableAlias)}::TEXT, '')`).join(" || ', ' || ") + " )";
682
+ let colRaw = "( " + cols.map(c => `${noFields ? "" : (asValue(c + ": ") + " || ")} COALESCE(${(0, exports.asNameAlias)(c, tableAlias)}::TEXT, '')`).join(" || ', ' || ") + " )";
656
683
  let col = colRaw;
657
684
  term = asValue(term);
658
685
  if (!matchCase) {
@@ -684,7 +711,7 @@ exports.FUNCTIONS = [
684
711
  let _cols = validCols.filter(c =>
685
712
  /** Exclude numeric columns when the search tern contains a character */
686
713
  !hasChars ||
687
- DboBuilder_1.postgresToTsType(c.colInfo.udt_name) !== "number");
714
+ (0, DboBuilder_1.postgresToTsType)(c.colInfo.udt_name) !== "number");
688
715
  /** This will break GROUP BY (non-integer constant in GROUP BY) */
689
716
  if (!_cols.length) {
690
717
  if (validCols.length && hasChars)
@@ -694,11 +721,10 @@ exports.FUNCTIONS = [
694
721
  res = `CASE
695
722
  ${_cols
696
723
  .map(c => {
697
- var _a, _b;
698
- const colNameEscaped = exports.asNameAlias(c.key, tableAlias);
724
+ const colNameEscaped = (0, exports.asNameAlias)(c.key, tableAlias);
699
725
  let colSelect = `${colNameEscaped}::TEXT`;
700
- const isTstamp = (_a = c.colInfo) === null || _a === void 0 ? void 0 : _a.udt_name.startsWith("timestamp");
701
- if (isTstamp || ((_b = c.colInfo) === null || _b === void 0 ? void 0 : _b.udt_name) === "date") {
726
+ const isTstamp = c.colInfo?.udt_name.startsWith("timestamp");
727
+ if (isTstamp || c.colInfo?.udt_name === "date") {
702
728
  colSelect = `( CASE WHEN ${colNameEscaped} IS NULL THEN ''
703
729
  ELSE concat_ws(' ',
704
730
  ${colNameEscaped}::TEXT,
@@ -753,7 +779,7 @@ exports.FUNCTIONS = [
753
779
  if (args.length > 1) {
754
780
  extraArgs = DboBuilder_1.pgp.as.format(", $1:csv", args.slice(1));
755
781
  }
756
- return aggName + "(" + exports.asNameAlias(args[0], tableAlias) + `${extraArgs})`;
782
+ return aggName + "(" + (0, exports.asNameAlias)(args[0], tableAlias) + `${extraArgs})`;
757
783
  }
758
784
  })),
759
785
  /* More aggs */
@@ -775,7 +801,7 @@ exports.FUNCTIONS = [
775
801
  singleColArg: true,
776
802
  getFields: (args) => [args[0]],
777
803
  getQuery: ({ allowedFields, args, tableAlias }) => {
778
- const col = exports.asNameAlias(args[0], tableAlias);
804
+ const col = (0, exports.asNameAlias)(args[0], tableAlias);
779
805
  return `round( ( ( MAX(${col}) - MIN(${col}) )::float/MIN(${col}) ) * 100, 2)`;
780
806
  }
781
807
  }
@@ -795,7 +821,7 @@ exports.COMPUTED_FIELDS = [
795
821
  /* CTID not available in AFTER trigger */
796
822
  // .concat(ctidField? [ctidField] : [])
797
823
  .sort()
798
- .map(f => exports.asNameAlias(f, tableAlias))
824
+ .map(f => (0, exports.asNameAlias)(f, tableAlias))
799
825
  .map(f => `md5(coalesce(${f}::text, 'dd'))`)
800
826
  .join(" || ") +
801
827
  `)`;
@@ -831,7 +857,7 @@ class SelectItemBuilder {
831
857
  this.select.push(item);
832
858
  };
833
859
  this.addFunction = (func, args, alias) => {
834
- const funcDef = exports.parseFunction({
860
+ const funcDef = (0, exports.parseFunction)({
835
861
  func, args, functions: this.functions,
836
862
  allowedFields: this.allowedFieldsIncludingComputed,
837
863
  });
@@ -841,6 +867,8 @@ class SelectItemBuilder {
841
867
  getFields: () => funcDef.getFields(args),
842
868
  getQuery: (tableAlias) => funcDef.getQuery({ allColumns: this.columns, allowedFields: this.allowedFields, args, tableAlias,
843
869
  ctidField: undefined,
870
+ /* CTID not available in AFTER trigger */
871
+ // ctidField: this.isView? undefined : "ctid"
844
872
  }),
845
873
  selected: true
846
874
  });
@@ -850,7 +878,13 @@ class SelectItemBuilder {
850
878
  if (selected) {
851
879
  const compCol = exports.COMPUTED_FIELDS.find(cf => cf.name === fieldName);
852
880
  if (compCol && !this.select.find(s => s.alias === fieldName)) {
853
- const cf = Object.assign(Object.assign({}, compCol), { type: "computed", numArgs: 0, singleColArg: false, getFields: (args) => [] });
881
+ const cf = {
882
+ ...compCol,
883
+ type: "computed",
884
+ numArgs: 0,
885
+ singleColArg: false,
886
+ getFields: (args) => []
887
+ };
854
888
  this.addFunction(cf, [], compCol.name);
855
889
  return;
856
890
  }
@@ -859,10 +893,10 @@ class SelectItemBuilder {
859
893
  let alias = selected ? fieldName : ("not_selected_" + fieldName);
860
894
  this.addItem({
861
895
  type: "column",
862
- columnPGDataType: colDef === null || colDef === void 0 ? void 0 : colDef.data_type,
863
- column_udt_type: colDef === null || colDef === void 0 ? void 0 : colDef.udt_name,
896
+ columnPGDataType: colDef?.data_type,
897
+ column_udt_type: colDef?.udt_name,
864
898
  alias,
865
- getQuery: () => prostgles_types_1.asName(fieldName),
899
+ getQuery: () => (0, prostgles_types_1.asName)(fieldName),
866
900
  getFields: () => [fieldName],
867
901
  selected
868
902
  });
@@ -887,7 +921,7 @@ class SelectItemBuilder {
887
921
  else if (userSelect === "*") {
888
922
  this.allowedFields.map(key => this.addColumn(key, true));
889
923
  }
890
- else if (DboBuilder_1.isPlainObject(userSelect) && !prostgles_types_1.isEmpty(userSelect)) {
924
+ else if ((0, DboBuilder_1.isPlainObject)(userSelect) && !(0, prostgles_types_1.isEmpty)(userSelect)) {
891
925
  const selectKeys = Object.keys(userSelect), selectValues = Object.values(userSelect);
892
926
  /* Cannot include and exclude at the same time */
893
927
  if (selectValues.filter(v => [0, false].includes(v)).length) {
@@ -913,12 +947,12 @@ class SelectItemBuilder {
913
947
  }
914
948
  /* Aggs and functions */
915
949
  }
916
- else if (typeof val === "string" || DboBuilder_1.isPlainObject(val)) {
950
+ else if (typeof val === "string" || (0, DboBuilder_1.isPlainObject)(val)) {
917
951
  /* Function shorthand notation
918
952
  { id: "$max" } === { id: { $max: ["id"] } } === SELECT MAX(id) AS id
919
953
  */
920
954
  if ((typeof val === "string" && val !== "*") ||
921
- DboBuilder_1.isPlainObject(val) && Object.keys(val).length === 1 && Array.isArray(Object.values(val)[0]) // !isPlainObject(Object.values(val)[0])
955
+ (0, DboBuilder_1.isPlainObject)(val) && Object.keys(val).length === 1 && Array.isArray(Object.values(val)[0]) // !isPlainObject(Object.values(val)[0])
922
956
  ) {
923
957
  // if(!Array.isArray(Object.values(val)[0])){
924
958
  // throw `Could not parse selected item: ${JSON.stringify(val)}\nFunction arguments must be in an array`;
@@ -937,7 +971,7 @@ class SelectItemBuilder {
937
971
  /** Function full notation { $funcName: ["colName", ...args] } */
938
972
  }
939
973
  else {
940
- ({ funcName, args } = exports.parseFunctionObject(val));
974
+ ({ funcName, args } = (0, exports.parseFunctionObject)(val));
941
975
  }
942
976
  this.addFunction(funcName, args, key);
943
977
  /* Join */
@@ -974,8 +1008,8 @@ class SelectItemBuilder {
974
1008
  }
975
1009
  }
976
1010
  exports.SelectItemBuilder = SelectItemBuilder;
977
- async function getNewQuery(_this, filter, selectParams, param3_unused = null, tableRules, localParams, columns) {
978
- if (((localParams === null || localParams === void 0 ? void 0 : localParams.socket) || (localParams === null || localParams === void 0 ? void 0 : localParams.httpReq)) && !utils_1.get(tableRules, "select.fields")) {
1011
+ async function getNewQuery(_this, filter, selectParams = {}, param3_unused = null, tableRules, localParams, columns) {
1012
+ if ((localParams?.socket || localParams?.httpReq) && !(0, utils_1.get)(tableRules, "select.fields")) {
979
1013
  throw `INTERNAL ERROR: publish.${_this.name}.select.fields rule missing`;
980
1014
  }
981
1015
  // const all_columns: SelectItem[] = _this.column_names.slice(0).map(fieldName => ({
@@ -995,11 +1029,10 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
995
1029
  // let select: SelectItem[] = [],
996
1030
  let joinQueries = [];
997
1031
  // const all_colnames = _this.column_names.slice(0).concat(COMPUTED_FIELDS.map(c => c.name));
998
- selectParams = selectParams || {};
999
1032
  const { select: userSelect = "*" } = selectParams,
1000
1033
  // allCols = _this.column_names.slice(0),
1001
1034
  // allFieldsIncludingComputed = allCols.concat(COMPUTED_FIELDS.map(c => c.name)),
1002
- allowedFields = _this.parseFieldFilter(utils_1.get(tableRules, "select.fields")) || _this.column_names.slice(0),
1035
+ allowedFields = _this.parseFieldFilter((0, utils_1.get)(tableRules, "select.fields")) || _this.column_names.slice(0),
1003
1036
  // allowedFieldsIncludingComputed = _this.parseFieldFilter(get(tableRules, "select.fields"), true, allFieldsIncludingComputed) || allFieldsIncludingComputed,
1004
1037
  sBuilder = new SelectItemBuilder({ allowedFields, computedFields: exports.COMPUTED_FIELDS, isView: _this.is_view, functions: exports.FUNCTIONS, allFields: _this.column_names.slice(0), columns });
1005
1038
  await sBuilder.parseUserSelect(userSelect, async (key, val, throwErr) => {
@@ -1040,6 +1073,8 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
1040
1073
  j_table = key;
1041
1074
  }
1042
1075
  }
1076
+ if (!j_table)
1077
+ throw "j_table missing";
1043
1078
  const _thisJoinedTable = _this.dboBuilder.dbo[j_table];
1044
1079
  if (!_thisJoinedTable) {
1045
1080
  throw `Joined table ${JSON.stringify(j_table)} is disallowed or inexistent \nOr you've forgot to put the function arguments into an array`;
@@ -1047,10 +1082,10 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
1047
1082
  let isLocal = true;
1048
1083
  if (localParams && (localParams.socket || localParams.httpReq)) {
1049
1084
  isLocal = false;
1050
- j_tableRules = await _this.dboBuilder.publishParser.getValidatedRequestRuleWusr({ tableName: j_table, command: "find", localParams });
1085
+ j_tableRules = await _this.dboBuilder.publishParser?.getValidatedRequestRuleWusr({ tableName: j_table, command: "find", localParams });
1051
1086
  }
1052
1087
  if (isLocal || j_tableRules) {
1053
- const joinQuery = await getNewQuery(_thisJoinedTable, j_filter, Object.assign(Object.assign({}, j_selectParams), { alias: j_alias }), param3_unused, j_tableRules, localParams, columns);
1088
+ const joinQuery = await getNewQuery(_thisJoinedTable, j_filter, { ...j_selectParams, alias: j_alias }, param3_unused, j_tableRules, localParams, columns);
1054
1089
  joinQuery.isLeftJoin = j_isLeftJoin;
1055
1090
  joinQuery.tableAlias = j_alias;
1056
1091
  joinQuery.$path = j_path;
@@ -1072,8 +1107,8 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
1072
1107
  const where = await _this.prepareWhere({
1073
1108
  filter,
1074
1109
  select,
1075
- forcedFilter: utils_1.get(tableRules, "select.forcedFilter"),
1076
- filterFields: utils_1.get(tableRules, "select.filterFields"),
1110
+ forcedFilter: (0, utils_1.get)(tableRules, "select.forcedFilter"),
1111
+ filterFields: (0, utils_1.get)(tableRules, "select.filterFields"),
1077
1112
  tableAlias: selectParams.alias,
1078
1113
  localParams,
1079
1114
  tableRule: tableRules
@@ -1087,7 +1122,7 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
1087
1122
  where,
1088
1123
  // having: cond.having,
1089
1124
  limit: _this.prepareLimitQuery(selectParams.limit, p),
1090
- orderBy: [_this.prepareSort(selectParams.orderBy, allowedFields, selectParams.alias, null, select)],
1125
+ orderBy: [_this.prepareSort(selectParams.orderBy, allowedFields, selectParams.alias, undefined, select)],
1091
1126
  offset: _this.prepareOffsetQuery(selectParams.offset)
1092
1127
  };
1093
1128
  // console.log(resQuery);
@@ -1096,24 +1131,24 @@ async function getNewQuery(_this, filter, selectParams, param3_unused = null, ta
1096
1131
  }
1097
1132
  exports.getNewQuery = getNewQuery;
1098
1133
  /* No validation/authorisation at this point */
1099
- function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1134
+ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams = {}) {
1100
1135
  const PREF = `prostgles`, joins = q.joins || [],
1101
1136
  // aggs = q.aggs || [],
1102
- makePref = (q) => !q.tableAlias ? q.table : `${q.tableAlias || ""}_${q.table}`, makePrefANON = (joinAlias, table) => prostgles_types_1.asName(!joinAlias ? table : `${joinAlias || ""}_${table}`), makePrefAN = (q) => prostgles_types_1.asName(makePref(q));
1137
+ makePref = (q) => !q.tableAlias ? q.table : `${q.tableAlias || ""}_${q.table}`, makePrefANON = (joinAlias, table) => (0, prostgles_types_1.asName)(!joinAlias ? table : `${joinAlias || ""}_${table}`), makePrefAN = (q) => (0, prostgles_types_1.asName)(makePref(q));
1103
1138
  const indentLine = (numInd, str, indentStr = " ") => new Array(numInd).fill(indentStr).join("") + str;
1104
1139
  const indStr = (numInd, str) => str.split("\n").map(s => indentLine(numInd, s)).join("\n");
1105
1140
  const indjArr = (numInd, strArr, indentStr = " ") => strArr.map(str => indentLine(numInd, str));
1106
1141
  const indJ = (numInd, strArr, separator = " \n ", indentStr = " ") => indjArr(numInd, strArr, indentStr).join(separator);
1107
1142
  const selectArrComma = (strArr) => strArr.map((s, i, arr) => s + (i < arr.length - 1 ? " , " : " "));
1108
- const prefJCAN = (q, str) => prostgles_types_1.asName(`${q.tableAlias || q.table}_${PREF}_${str}`);
1143
+ const prefJCAN = (q, str) => (0, prostgles_types_1.asName)(`${q.tableAlias || q.table}_${PREF}_${str}`);
1109
1144
  // const indent = (a, b) => a;
1110
1145
  const joinTables = (q1, q2) => {
1111
1146
  const joinInfo = _this.getJoins(q1.table, q2.table, q2.$path, true);
1112
1147
  const paths = joinInfo.paths;
1113
- return Prostgles_1.flat(paths.map(({ table, on }, i) => {
1148
+ return paths.flatMap(({ table, on }, i) => {
1114
1149
  const getColName = (col, q) => {
1115
1150
  if (table === q.table) {
1116
- const colFromSelect = q.select.find(s => s.getQuery() === prostgles_types_1.asName(col));
1151
+ const colFromSelect = q.select.find(s => s.getQuery() === (0, prostgles_types_1.asName)(col));
1117
1152
  if (!colFromSelect) {
1118
1153
  console.error(`${col} column might be missing in user publish `);
1119
1154
  throw `Could not find join column (${col}) in allowe select. Some join tables and columns might be invalid/dissallowed`;
@@ -1135,7 +1170,7 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1135
1170
  const prevAlias = i === 0 ? makePrefAN(q1) : makePrefANON(q2.tableAlias, prevTable);
1136
1171
  // If root then prev table is aliased from root query. Alias from join otherwise
1137
1172
  let iQ = [
1138
- prostgles_types_1.asName(table) + ` ${thisAlias}`
1173
+ (0, prostgles_types_1.asName)(table) + ` ${thisAlias}`
1139
1174
  ];
1140
1175
  /* If target table then add filters, options, etc */
1141
1176
  if (i === paths.length - 1) {
@@ -1149,10 +1184,10 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1149
1184
  const targetSelect = q2.select.filter(s => s.selected).map(s => {
1150
1185
  /* Rename aggs to avoid collision with join cols */
1151
1186
  if (s.type === "aggregation")
1152
- return prostgles_types_1.asName(`agg_${s.alias}`) + " AS " + prostgles_types_1.asName(s.alias);
1153
- return prostgles_types_1.asName(s.alias);
1154
- }).concat(q2.joins.map(j => prostgles_types_1.asName(j.table))).join(", ");
1155
- const _iiQ = makeQuery(_this, q2, depth + 1, on.map(([c1, c2]) => prostgles_types_1.asName(c2)), selectParams);
1187
+ return (0, prostgles_types_1.asName)(`agg_${s.alias}`) + " AS " + (0, prostgles_types_1.asName)(s.alias);
1188
+ return (0, prostgles_types_1.asName)(s.alias);
1189
+ }).concat(q2.joins?.map(j => (0, prostgles_types_1.asName)(j.table)) ?? []).join(", ");
1190
+ const _iiQ = makeQuery(_this, q2, depth + 1, on.map(([c1, c2]) => (0, prostgles_types_1.asName)(c2)), selectParams);
1156
1191
  // const iiQ = flat(_iiQ.split("\n")); // prettify for debugging
1157
1192
  // console.log(_iiQ)
1158
1193
  const iiQ = [_iiQ];
@@ -1165,7 +1200,7 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1165
1200
  `row_to_json((select x from (SELECT ${targetSelect}) as x)) AS ${prefJCAN(q2, `json`)}`,
1166
1201
  `FROM (`,
1167
1202
  ...iiQ,
1168
- `) ${prostgles_types_1.asName(q2.table)} `
1203
+ `) ${(0, prostgles_types_1.asName)(q2.table)} `
1169
1204
  ]),
1170
1205
  `) ${thisAlias}`
1171
1206
  ];
@@ -1173,10 +1208,10 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1173
1208
  let jres = [
1174
1209
  `${q2.isLeftJoin ? "LEFT" : "INNER"} JOIN `,
1175
1210
  ...iQ,
1176
- `ON ${on.map(([c1, c2]) => `${prevAlias}.${prostgles_types_1.asName(getPrevColName(c1))} = ${thisAlias}.${prostgles_types_1.asName(getThisColName(c2))} `).join(" AND ")}`
1211
+ `ON ${on.map(([c1, c2]) => `${prevAlias}.${(0, prostgles_types_1.asName)(getPrevColName(c1))} = ${thisAlias}.${(0, prostgles_types_1.asName)(getThisColName(c2))} `).join(" AND ")}`
1177
1212
  ];
1178
1213
  return jres;
1179
- }));
1214
+ });
1180
1215
  };
1181
1216
  const getGroupBy = (rootSelectItems, groupByItems) => {
1182
1217
  if (groupByItems.length) {
@@ -1198,7 +1233,7 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1198
1233
  let groupBy = "";
1199
1234
  const rootSelectItems = q.select.filter(s => joinFields.includes(s.getQuery()) || s.selected);
1200
1235
  /* If aggs exist need to set groupBy add joinFields into select */
1201
- if (aggs.length || (selectParams === null || selectParams === void 0 ? void 0 : selectParams.groupBy)) {
1236
+ if (aggs.length || selectParams?.groupBy) {
1202
1237
  // const missingFields = joinFields.filter(jf => !q.select.find(s => s.type === "column" && s.alias === jf));
1203
1238
  // if(depth && missingFields.length){
1204
1239
  // // select = Array.from(new Set(missingFields.concat(select)));
@@ -1220,11 +1255,11 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1220
1255
  // return s.getQuery() + ((s.type !== "column")? (" AS " + s.alias) : "")
1221
1256
  if (s.type === "aggregation") {
1222
1257
  /* Rename aggs to avoid collision with join cols */
1223
- return s.getQuery() + " AS " + prostgles_types_1.asName((depth ? "agg_" : "") + s.alias);
1258
+ return s.getQuery() + " AS " + (0, prostgles_types_1.asName)((depth ? "agg_" : "") + s.alias);
1224
1259
  }
1225
- return s.getQuery() + " AS " + prostgles_types_1.asName(s.alias);
1260
+ return s.getQuery() + " AS " + (0, prostgles_types_1.asName)(s.alias);
1226
1261
  }).join(", "),
1227
- `FROM ${prostgles_types_1.asName(q.table)} `,
1262
+ `FROM ${(0, prostgles_types_1.asName)(q.table)} `,
1228
1263
  q.where,
1229
1264
  groupBy //!aggs.length? "" : `GROUP BY ${nonAggs.map(sf => asName(sf.alias)).join(", ")}`,
1230
1265
  ,
@@ -1251,8 +1286,8 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1251
1286
  // console.log({ aggs, nonAggs, joins: q.joins })
1252
1287
  // rootGroupBy = getGroupBy(rootSelectItems, depth? rootSelectItems : nonAggs) + (aggs?.length? "" : ", ctid")
1253
1288
  rootGroupBy = `GROUP BY ${(depth ?
1254
- q.allFields.map(f => prostgles_types_1.asName(f)) :
1255
- nonAggs.map(s => s.type === "function" ? s.getQuery() : prostgles_types_1.asName(s.alias))).concat((aggs && aggs.length) ?
1289
+ q.allFields.map(f => (0, prostgles_types_1.asName)(f)) :
1290
+ nonAggs.map(s => s.type === "function" ? s.getQuery() : (0, prostgles_types_1.asName)(s.alias))).concat((aggs && aggs.length) ?
1256
1291
  [] :
1257
1292
  [`ctid`]).filter(s => s).join(", ")} `;
1258
1293
  }
@@ -1261,10 +1296,10 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1261
1296
  " \n",
1262
1297
  `-- 0. [joined root] `,
1263
1298
  "SELECT ",
1264
- ...selectArrComma(rootSelectItems.map(s => s.getQuery() + " AS " + prostgles_types_1.asName(s.alias)).concat(joins.map((j, i) => {
1299
+ ...selectArrComma(rootSelectItems.map(s => s.getQuery() + " AS " + (0, prostgles_types_1.asName)(s.alias)).concat(joins.map((j, i) => {
1265
1300
  /** Apply LIMIT to joined items */
1266
1301
  const jsq = `json_agg(${prefJCAN(j, `json`)}::jsonb ORDER BY ${prefJCAN(j, `rowid_sorted`)}) FILTER (WHERE ${prefJCAN(j, `limit`)} <= ${j.limit} AND ${prefJCAN(j, `dupes_rowid`)} = 1 AND ${prefJCAN(j, `json`)} IS NOT NULL)`;
1267
- const resAlias = prostgles_types_1.asName(j.tableAlias || j.table);
1302
+ const resAlias = (0, prostgles_types_1.asName)(j.tableAlias || j.table);
1268
1303
  // If limit = 1 then return a single json object (first one)
1269
1304
  return (j.limit === 1 ? `${jsq}->0 ` : `COALESCE(${jsq}, '[]') `) + ` AS ${resAlias}`;
1270
1305
  }))),
@@ -1281,18 +1316,18 @@ function makeQuery(_this, q, depth = 0, joinFields = [], selectParams) {
1281
1316
  "-- 2. [source full select + ctid to group by] ",
1282
1317
  "SELECT ",
1283
1318
  ...selectArrComma(q.allFields.concat(["ctid"])
1284
- .map(field => `${makePrefAN(q)}.${prostgles_types_1.asName(field)} `)
1319
+ .map(field => `${makePrefAN(q)}.${(0, prostgles_types_1.asName)(field)} `)
1285
1320
  .concat(joins.map((j, i) => makePrefAN(j) + "." + prefJCAN(j, `json`) + ", " + makePrefAN(j) + "." + prefJCAN(j, `rowid_sorted`)).concat(joins.map(j => `row_number() over(partition by ${makePrefAN(j)}.${prefJCAN(j, `rowid_sorted`)}, ${makePrefAN(q)}.ctid ) AS ${prefJCAN(j, `dupes_rowid`)}`)))),
1286
1321
  `FROM ( `,
1287
1322
  ...indjArr(depth + 1, [
1288
1323
  "-- 3. [source table] ",
1289
1324
  "SELECT ",
1290
1325
  "*, row_number() over() as ctid ",
1291
- `FROM ${prostgles_types_1.asName(q.table)} `,
1326
+ `FROM ${(0, prostgles_types_1.asName)(q.table)} `,
1292
1327
  `${q.where} `
1293
1328
  ]),
1294
1329
  `) ${makePrefAN(q)} `,
1295
- ...Prostgles_1.flat(joins.map((j, i) => joinTables(q, j)))
1330
+ ...joins.flatMap((j, i) => joinTables(q, j))
1296
1331
  ]),
1297
1332
  ") t1"
1298
1333
  ]),