prostgles-server 4.0.35 → 4.0.37

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 (47) hide show
  1. package/dist/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
  2. package/dist/DboBuilder/QueryBuilder/Functions.js +18 -11
  3. package/dist/DboBuilder/QueryBuilder/Functions.js.map +1 -1
  4. package/dist/DboBuilder/ViewHandler.d.ts +1 -1
  5. package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
  6. package/dist/DboBuilder/ViewHandler.js +2 -77
  7. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  8. package/dist/DboBuilder/delete.d.ts.map +1 -1
  9. package/dist/DboBuilder/delete.js +5 -2
  10. package/dist/DboBuilder/delete.js.map +1 -1
  11. package/dist/DboBuilder/find.d.ts +8 -0
  12. package/dist/DboBuilder/find.d.ts.map +1 -0
  13. package/dist/DboBuilder/find.js +89 -0
  14. package/dist/DboBuilder/find.js.map +1 -0
  15. package/dist/DboBuilder/insert.d.ts.map +1 -1
  16. package/dist/DboBuilder/insert.js +4 -2
  17. package/dist/DboBuilder/insert.js.map +1 -1
  18. package/dist/DboBuilder/update.d.ts.map +1 -1
  19. package/dist/DboBuilder/update.js +6 -1
  20. package/dist/DboBuilder/update.js.map +1 -1
  21. package/dist/JSONBValidation/validation.js +1 -1
  22. package/dist/JSONBValidation/validation.js.map +1 -1
  23. package/lib/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
  24. package/lib/DboBuilder/QueryBuilder/Functions.js +18 -11
  25. package/lib/DboBuilder/QueryBuilder/Functions.ts +18 -11
  26. package/lib/DboBuilder/ViewHandler.d.ts +1 -1
  27. package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
  28. package/lib/DboBuilder/ViewHandler.js +2 -77
  29. package/lib/DboBuilder/ViewHandler.ts +6 -80
  30. package/lib/DboBuilder/delete.d.ts.map +1 -1
  31. package/lib/DboBuilder/delete.js +5 -2
  32. package/lib/DboBuilder/delete.ts +7 -3
  33. package/lib/DboBuilder/find.d.ts +8 -0
  34. package/lib/DboBuilder/find.d.ts.map +1 -0
  35. package/lib/DboBuilder/find.js +88 -0
  36. package/lib/DboBuilder/find.ts +89 -0
  37. package/lib/DboBuilder/insert.d.ts.map +1 -1
  38. package/lib/DboBuilder/insert.js +4 -2
  39. package/lib/DboBuilder/insert.ts +4 -2
  40. package/lib/DboBuilder/update.d.ts.map +1 -1
  41. package/lib/DboBuilder/update.js +6 -1
  42. package/lib/DboBuilder/update.ts +7 -2
  43. package/lib/JSONBValidation/validation.js +1 -1
  44. package/lib/JSONBValidation/validation.ts +1 -1
  45. package/package.json +2 -2
  46. package/tests/client/PID.txt +1 -1
  47. package/tests/server/package-lock.json +3 -3
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ViewHandler = void 0;
4
- const makeSelectQuery_1 = require("../DboBuilder/QueryBuilder/makeSelectQuery");
5
4
  const getCondition_1 = require("./getCondition");
6
- const runSQL_1 = require("../DboBuilder/runSQL");
7
5
  const prostgles_types_1 = require("prostgles-types");
8
6
  const DboBuilder_1 = require("../DboBuilder");
9
7
  const PubSubManager_1 = require("../PubSubManager/PubSubManager");
@@ -11,6 +9,7 @@ const QueryBuilder_1 = require("./QueryBuilder/QueryBuilder");
11
9
  const Functions_1 = require("./QueryBuilder/Functions");
12
10
  const getColumns_1 = require("./getColumns");
13
11
  const subscribe_1 = require("./subscribe");
12
+ const find_1 = require("./find");
14
13
  class ColSet {
15
14
  opts;
16
15
  constructor(columns, tableName) {
@@ -497,81 +496,7 @@ class ViewHandler {
497
496
  };
498
497
  }
499
498
  }
500
- async find(filter, selectParams, param3_unused, tableRules, localParams) {
501
- try {
502
- filter = filter || {};
503
- const allowedReturnTypes = ["row", "value", "values", "statement"];
504
- const { returnType } = selectParams || {};
505
- if (returnType && !allowedReturnTypes.includes(returnType)) {
506
- throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`;
507
- }
508
- const { testRule = false, returnQuery = false, returnNewQuery } = localParams || {};
509
- if (testRule)
510
- return [];
511
- if (selectParams) {
512
- const good_params = ["select", "orderBy", "offset", "limit", "returnType", "groupBy"];
513
- const bad_params = Object.keys(selectParams).filter(k => !good_params.includes(k));
514
- if (bad_params && bad_params.length)
515
- throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
516
- }
517
- /* Validate publish */
518
- if (tableRules) {
519
- if (!tableRules.select)
520
- throw "select rules missing for " + this.name;
521
- const fields = tableRules.select.fields;
522
- const maxLimit = tableRules.select.maxLimit;
523
- if (tableRules.select !== "*" && typeof tableRules.select !== "boolean" && !(0, DboBuilder_1.isPlainObject)(tableRules.select))
524
- throw `\nINVALID publish.${this.name}.select\nExpecting any of: "*" | { fields: "*" } | true | false`;
525
- if (!fields)
526
- throw ` invalid ${this.name}.select rule -> fields (required) setting missing.\nExpecting any of: "*" | { col_name: false } | { col1: true, col2: true }`;
527
- if (maxLimit && !Number.isInteger(maxLimit))
528
- throw ` invalid publish.${this.name}.select.maxLimit -> expecting integer but got ` + maxLimit;
529
- }
530
- const q = await (0, QueryBuilder_1.getNewQuery)(this, filter, selectParams, param3_unused, tableRules, localParams, this.columns), _query = (0, makeSelectQuery_1.makeSelectQuery)(this, q, undefined, undefined, selectParams);
531
- // console.log(_query, JSON.stringify(q, null, 2))
532
- if (testRule) {
533
- try {
534
- await this.db.any("EXPLAIN " + _query);
535
- return [];
536
- }
537
- catch (e) {
538
- console.error(e);
539
- throw `INTERNAL ERROR: Publish config is not valid for publish.${this.name}.select `;
540
- }
541
- }
542
- /** Used for subscribe */
543
- if (returnNewQuery)
544
- return q;
545
- if (returnQuery)
546
- return _query;
547
- if (returnType === "statement") {
548
- if (!(await (0, runSQL_1.canRunSQL)(this.dboBuilder.prostgles, localParams))) {
549
- throw `Not allowed: {returnType: "statement"} requires sql privileges `;
550
- }
551
- return _query;
552
- }
553
- if (["row", "value"].includes(returnType)) {
554
- return (this.t || this.db).oneOrNone(_query).then(data => {
555
- return (data && returnType === "value") ? Object.values(data)[0] : data;
556
- }).catch(err => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this));
557
- }
558
- else {
559
- return (this.t || this.db).any(_query).then(data => {
560
- if (returnType === "values") {
561
- return data.map(d => Object.values(d)[0]);
562
- }
563
- return data;
564
- }).catch(err => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this));
565
- }
566
- }
567
- catch (e) {
568
- // console.trace(e)
569
- if (localParams && localParams.testRule)
570
- throw e;
571
- throw (0, DboBuilder_1.parseError)(e, `dbo.${this.name}.find()`);
572
- // throw { err: parseError(e), msg: `Issue with dbo.${this.name}.find()`, args: { filter, selectParams} };
573
- }
574
- }
499
+ find = find_1.find.bind(this);
575
500
  findOne(filter, selectParams, param3_unused, table_rules, localParams) {
576
501
  try {
577
502
  const { select = "*", orderBy, offset = 0 } = selectParams || {};
@@ -1,8 +1,6 @@
1
- import { makeSelectQuery } from "../DboBuilder/QueryBuilder/makeSelectQuery";
2
1
  import { getCondition } from "./getCondition";
3
2
 
4
3
  import * as pgPromise from 'pg-promise';
5
- import { canRunSQL } from "../DboBuilder/runSQL";
6
4
  import {
7
5
  ColumnInfo, FieldFilter, SelectParams,
8
6
  OrderBy,
@@ -16,17 +14,19 @@ import {
16
14
  import { DB, DBHandlerServer, Join } from "../Prostgles";
17
15
  import {
18
16
  DboBuilder, escapeTSNames, ExistsFilterConfig,Filter, isPlainObject,
19
- JoinInfo, LocalParams, makeErrorFromPGError, parseError, pgp, postgresToTsType, SortItem,
17
+ JoinInfo, LocalParams, parseError, pgp, postgresToTsType, SortItem,
20
18
  TableHandlers, TableSchema, ValidatedTableRules
21
19
  } from "../DboBuilder";
22
20
  import { Graph } from "../shortestPath";
23
21
  import { TableRule, UpdateRule, ValidateRow } from "../PublishParser";
24
22
  import { asValue } from "../PubSubManager/PubSubManager";
25
23
  import { TableHandler } from "./TableHandler";
26
- import { asNameAlias, getNewQuery, SelectItem, SelectItemValidated } from "./QueryBuilder/QueryBuilder";
24
+ import { asNameAlias, SelectItem, SelectItemValidated } from "./QueryBuilder/QueryBuilder";
27
25
  import { COMPUTED_FIELDS, FieldSpec } from "./QueryBuilder/Functions";
28
26
  import { getColumns } from "./getColumns";
29
27
  import { LocalFuncs, subscribe } from "./subscribe";
28
+ import { find } from "./find";
29
+
30
30
  export type JoinPaths = {
31
31
  t1: string;
32
32
  t2: string;
@@ -600,82 +600,8 @@ export class ViewHandler {
600
600
  }
601
601
 
602
602
  }
603
-
604
- async find(filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise<any[]> {
605
- try {
606
- filter = filter || {};
607
- const allowedReturnTypes: Array<SelectParams["returnType"]> = ["row", "value", "values", "statement"]
608
- const { returnType } = selectParams || {};
609
- if (returnType && !allowedReturnTypes.includes(returnType)) {
610
- throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`
611
- }
612
-
613
- const { testRule = false, returnQuery = false, returnNewQuery } = localParams || {};
614
-
615
- if (testRule) return [];
616
- if (selectParams) {
617
- const good_params: Array<keyof SelectParams> = ["select", "orderBy", "offset", "limit", "returnType", "groupBy"];
618
- const bad_params = Object.keys(selectParams).filter(k => !good_params.includes(k as any));
619
- if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
620
- }
621
-
622
- /* Validate publish */
623
- if (tableRules) {
624
-
625
- if (!tableRules.select) throw "select rules missing for " + this.name;
626
- const fields = tableRules.select.fields;
627
- const maxLimit = tableRules.select.maxLimit;
628
-
629
- if (<any>tableRules.select !== "*" && typeof tableRules.select !== "boolean" && !isPlainObject(tableRules.select)) throw `\nINVALID publish.${this.name}.select\nExpecting any of: "*" | { fields: "*" } | true | false`
630
- if (!fields) throw ` invalid ${this.name}.select rule -> fields (required) setting missing.\nExpecting any of: "*" | { col_name: false } | { col1: true, col2: true }`;
631
- if (maxLimit && !Number.isInteger(maxLimit)) throw ` invalid publish.${this.name}.select.maxLimit -> expecting integer but got ` + maxLimit;
632
- }
633
-
634
- const q = await getNewQuery(this as unknown as TableHandler, filter, selectParams, param3_unused, tableRules, localParams, this.columns),
635
- _query = makeSelectQuery(this as unknown as TableHandler, q, undefined, undefined, selectParams);
636
- // console.log(_query, JSON.stringify(q, null, 2))
637
- if (testRule) {
638
- try {
639
- await this.db.any("EXPLAIN " + _query);
640
- return [];
641
- } catch (e) {
642
- console.error(e);
643
- throw `INTERNAL ERROR: Publish config is not valid for publish.${this.name}.select `
644
- }
645
- }
646
-
647
- /** Used for subscribe */
648
- if(returnNewQuery) return (q as unknown as any);
649
- if (returnQuery) return (_query as unknown as any[]);
650
-
651
- if (returnType === "statement") {
652
- if (!(await canRunSQL(this.dboBuilder.prostgles, localParams))) {
653
- throw `Not allowed: {returnType: "statement"} requires sql privileges `
654
- }
655
- return _query as unknown as any[];
656
- }
657
-
658
- if (["row", "value"].includes(returnType!)) {
659
- return (this.t || this.db).oneOrNone(_query).then(data => {
660
- return (data && returnType === "value") ? Object.values(data)[0] : data;
661
- }).catch(err => makeErrorFromPGError(err, localParams, this));
662
- } else {
663
- return (this.t || this.db).any(_query).then(data => {
664
- if (returnType === "values") {
665
- return data.map(d => Object.values(d)[0]);
666
- }
667
- return data;
668
- }).catch(err => makeErrorFromPGError(err, localParams, this));
669
- }
670
-
671
- } catch (e) {
672
- // console.trace(e)
673
- if (localParams && localParams.testRule) throw e;
674
- throw parseError(e, `dbo.${this.name}.find()`);
675
- // throw { err: parseError(e), msg: `Issue with dbo.${this.name}.find()`, args: { filter, selectParams} };
676
- }
677
- }
678
-
603
+ find = find.bind(this);
604
+
679
605
  findOne(filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise<any> {
680
606
 
681
607
  try {
@@ -1 +1 @@
1
- {"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["delete.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,YAAY,EAAe,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,MAAM,EAAE,WAAW,EAAoC,MAAM,eAAe,CAAC;AACtF,OAAO,EAAc,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAsB,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,CA6HrL"}
1
+ {"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["delete.ts"],"names":[],"mappings":"AACA,OAAO,EAAqB,YAAY,EAAe,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,MAAM,EAAE,WAAW,EAAc,MAAM,eAAe,CAAC;AAChE,OAAO,EAAc,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,wBAAsB,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,CAgIrL"}
@@ -4,6 +4,7 @@ exports._delete = void 0;
4
4
  const prostgles_types_1 = require("prostgles-types");
5
5
  const DboBuilder_1 = require("../DboBuilder");
6
6
  const PubSubManager_1 = require("../PubSubManager/PubSubManager");
7
+ const find_1 = require("./find");
7
8
  async function _delete(filter, params, param3_unused, table_rules, localParams) {
8
9
  try {
9
10
  const { returning } = params || {};
@@ -32,7 +33,8 @@ async function _delete(filter, params, param3_unused, table_rules, localParams)
32
33
  }
33
34
  }
34
35
  if (params) {
35
- const good_params = ["returning"];
36
+ const good_paramsObj = { returning: 1, returnType: 1 };
37
+ const good_params = Object.keys(good_paramsObj);
36
38
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
37
39
  if (bad_params && bad_params.length)
38
40
  throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
@@ -112,7 +114,8 @@ async function _delete(filter, params, param3_unused, table_rules, localParams)
112
114
  }
113
115
  }
114
116
  }
115
- return dbHandler[queryType](_query).catch((err) => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams));
117
+ return (0, find_1.runQueryReturnType)(_query, params?.returnType, this, localParams);
118
+ // return dbHandler[queryType](_query).catch((err: any) => makeErrorFromPGError(err, localParams));
116
119
  }
117
120
  catch (e) {
118
121
  // console.trace(e)
@@ -1,9 +1,10 @@
1
1
  import pgPromise from "pg-promise";
2
2
  import { AnyObject, asName, DeleteParams, FieldFilter } from "prostgles-types";
3
- import { Filter, LocalParams, makeErrorFromPGError, parseError } from "../DboBuilder";
3
+ import { Filter, LocalParams, parseError } from "../DboBuilder";
4
4
  import { DeleteRule, TableRule } from "../PublishParser";
5
5
  import { pickKeys } from "../PubSubManager/PubSubManager";
6
6
  import { TableHandler } from "./TableHandler";
7
+ import { runQueryReturnType } from "./find";
7
8
 
8
9
  export async function _delete(this: TableHandler, filter?: Filter, params?: DeleteParams, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise<any> {
9
10
  try {
@@ -40,7 +41,8 @@ export async function _delete(this: TableHandler, filter?: Filter, params?: Dele
40
41
 
41
42
 
42
43
  if (params) {
43
- const good_params = ["returning"];
44
+ const good_paramsObj: Record<keyof DeleteParams, 1> = { returning: 1, returnType: 1 };
45
+ const good_params = Object.keys(good_paramsObj);
44
46
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
45
47
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
46
48
  }
@@ -124,7 +126,9 @@ export async function _delete(this: TableHandler, filter?: Filter, params?: Dele
124
126
  }
125
127
  }
126
128
 
127
- return dbHandler[queryType](_query).catch((err: any) => makeErrorFromPGError(err, localParams));
129
+ return runQueryReturnType(_query, params?.returnType, this, localParams);
130
+
131
+ // return dbHandler[queryType](_query).catch((err: any) => makeErrorFromPGError(err, localParams));
128
132
  } catch (e) {
129
133
  // console.trace(e)
130
134
  if (localParams && localParams.testRule) throw e;
@@ -0,0 +1,8 @@
1
+ import { SelectParams } from "prostgles-types";
2
+ import { Filter, LocalParams } from "../DboBuilder";
3
+ import { TableRule } from "../PublishParser";
4
+ import { TableHandler } from "./TableHandler";
5
+ import { ViewHandler } from "./ViewHandler";
6
+ export declare const find: (this: ViewHandler, filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams) => Promise<any[]>;
7
+ export declare const runQueryReturnType: (query: string, returnType: SelectParams["returnType"], handler: ViewHandler | TableHandler, localParams: LocalParams | undefined) => Promise<any>;
8
+ //# sourceMappingURL=find.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find.d.ts","sourceRoot":"","sources":["find.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAY,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,WAAW,EAAoC,MAAM,eAAe,CAAC;AAGtF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,eAAO,MAAM,IAAI,SAAwB,WAAW,WAAW,MAAM,iBAAiB,YAAY,kBAAkB,SAAS,eAAe,SAAS,gBAAgB,WAAW,KAAG,QAAQ,GAAG,EAAE,CAuD/L,CAAA;AAGD,eAAO,MAAM,kBAAkB,UAAiB,MAAM,cAAc,YAAY,CAAC,YAAY,CAAC,WAAW,WAAW,GAAG,YAAY,eAAe,WAAW,GAAG,SAAS,iBAoBxK,CAAA"}
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runQueryReturnType = exports.find = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const DboBuilder_1 = require("../DboBuilder");
6
+ const makeSelectQuery_1 = require("../DboBuilder/QueryBuilder/makeSelectQuery");
7
+ const runSQL_1 = require("../DboBuilder/runSQL");
8
+ const QueryBuilder_1 = require("./QueryBuilder/QueryBuilder");
9
+ const find = async function (filter, selectParams, param3_unused, tableRules, localParams) {
10
+ try {
11
+ filter = filter || {};
12
+ const allowedReturnTypes = ["row", "value", "values", "statement"];
13
+ const { returnType } = selectParams || {};
14
+ if (returnType && !allowedReturnTypes.includes(returnType)) {
15
+ throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`;
16
+ }
17
+ const { testRule = false, returnQuery = false, returnNewQuery } = localParams || {};
18
+ if (testRule)
19
+ return [];
20
+ if (selectParams) {
21
+ const good_params = ["select", "orderBy", "offset", "limit", "returnType", "groupBy"];
22
+ const bad_params = Object.keys(selectParams).filter(k => !good_params.includes(k));
23
+ if (bad_params && bad_params.length)
24
+ throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
25
+ }
26
+ /* Validate publish */
27
+ if (tableRules) {
28
+ if (!tableRules.select)
29
+ throw "select rules missing for " + this.name;
30
+ const fields = tableRules.select.fields;
31
+ const maxLimit = tableRules.select.maxLimit;
32
+ if (tableRules.select !== "*" && typeof tableRules.select !== "boolean" && !(0, prostgles_types_1.isObject)(tableRules.select))
33
+ throw `\nINVALID publish.${this.name}.select\nExpecting any of: "*" | { fields: "*" } | true | false`;
34
+ if (!fields)
35
+ throw ` invalid ${this.name}.select rule -> fields (required) setting missing.\nExpecting any of: "*" | { col_name: false } | { col1: true, col2: true }`;
36
+ if (maxLimit && !Number.isInteger(maxLimit))
37
+ throw ` invalid publish.${this.name}.select.maxLimit -> expecting integer but got ` + maxLimit;
38
+ }
39
+ const q = await (0, QueryBuilder_1.getNewQuery)(this, filter, selectParams, param3_unused, tableRules, localParams, this.columns), _query = (0, makeSelectQuery_1.makeSelectQuery)(this, q, undefined, undefined, selectParams);
40
+ // console.log(_query, JSON.stringify(q, null, 2))
41
+ if (testRule) {
42
+ try {
43
+ await this.db.any("EXPLAIN " + _query);
44
+ return [];
45
+ }
46
+ catch (e) {
47
+ console.error(e);
48
+ throw `INTERNAL ERROR: Publish config is not valid for publish.${this.name}.select `;
49
+ }
50
+ }
51
+ /** Used for subscribe */
52
+ if (returnNewQuery)
53
+ return q;
54
+ if (returnQuery)
55
+ return _query;
56
+ return (0, exports.runQueryReturnType)(_query, returnType, this, localParams);
57
+ }
58
+ catch (e) {
59
+ // console.trace(e)
60
+ if (localParams && localParams.testRule)
61
+ throw e;
62
+ throw (0, DboBuilder_1.parseError)(e, `dbo.${this.name}.find()`);
63
+ // throw { err: parseError(e), msg: `Issue with dbo.${this.name}.find()`, args: { filter, selectParams} };
64
+ }
65
+ };
66
+ exports.find = find;
67
+ const runQueryReturnType = async (query, returnType, handler, localParams) => {
68
+ if (returnType === "statement") {
69
+ if (!(await (0, runSQL_1.canRunSQL)(handler.dboBuilder.prostgles, localParams))) {
70
+ throw `Not allowed: {returnType: "statement"} requires sql privileges `;
71
+ }
72
+ return query;
73
+ }
74
+ else if (["row", "value"].includes(returnType)) {
75
+ return (handler.t || handler.db).oneOrNone(query).then(data => {
76
+ return (data && returnType === "value") ? Object.values(data)[0] : data;
77
+ }).catch(err => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this));
78
+ }
79
+ else {
80
+ return (handler.t || handler.db).any(query).then(data => {
81
+ if (returnType === "values") {
82
+ return data.map(d => Object.values(d)[0]);
83
+ }
84
+ return data;
85
+ }).catch(err => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this));
86
+ }
87
+ };
88
+ exports.runQueryReturnType = runQueryReturnType;
@@ -0,0 +1,89 @@
1
+
2
+ import { SelectParams, isObject } from "prostgles-types";
3
+ import { Filter, LocalParams, makeErrorFromPGError, parseError } from "../DboBuilder";
4
+ import { makeSelectQuery } from "../DboBuilder/QueryBuilder/makeSelectQuery";
5
+ import { canRunSQL } from "../DboBuilder/runSQL";
6
+ import { TableRule } from "../PublishParser";
7
+ import { TableHandler } from "./TableHandler";
8
+ import { getNewQuery } from "./QueryBuilder/QueryBuilder";
9
+ import { ViewHandler } from "./ViewHandler";
10
+
11
+ export const find = async function(this: ViewHandler, filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise<any[]> {
12
+ try {
13
+ filter = filter || {};
14
+ const allowedReturnTypes: Array<SelectParams["returnType"]> = ["row", "value", "values", "statement"]
15
+ const { returnType } = selectParams || {};
16
+ if (returnType && !allowedReturnTypes.includes(returnType)) {
17
+ throw `returnType (${returnType}) can only be ${allowedReturnTypes.join(" OR ")}`
18
+ }
19
+
20
+ const { testRule = false, returnQuery = false, returnNewQuery } = localParams || {};
21
+
22
+ if (testRule) return [];
23
+ if (selectParams) {
24
+ const good_params: Array<keyof SelectParams> = ["select", "orderBy", "offset", "limit", "returnType", "groupBy"];
25
+ const bad_params = Object.keys(selectParams).filter(k => !good_params.includes(k as any));
26
+ if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
27
+ }
28
+
29
+ /* Validate publish */
30
+ if (tableRules) {
31
+
32
+ if (!tableRules.select) throw "select rules missing for " + this.name;
33
+ const fields = tableRules.select.fields;
34
+ const maxLimit = tableRules.select.maxLimit;
35
+
36
+ if (<any>tableRules.select !== "*" && typeof tableRules.select !== "boolean" && !isObject(tableRules.select)) throw `\nINVALID publish.${this.name}.select\nExpecting any of: "*" | { fields: "*" } | true | false`
37
+ if (!fields) throw ` invalid ${this.name}.select rule -> fields (required) setting missing.\nExpecting any of: "*" | { col_name: false } | { col1: true, col2: true }`;
38
+ if (maxLimit && !Number.isInteger(maxLimit)) throw ` invalid publish.${this.name}.select.maxLimit -> expecting integer but got ` + maxLimit;
39
+ }
40
+
41
+ const q = await getNewQuery(this as unknown as TableHandler, filter, selectParams, param3_unused, tableRules, localParams, this.columns),
42
+ _query = makeSelectQuery(this as unknown as TableHandler, q, undefined, undefined, selectParams);
43
+ // console.log(_query, JSON.stringify(q, null, 2))
44
+ if (testRule) {
45
+ try {
46
+ await this.db.any("EXPLAIN " + _query);
47
+ return [];
48
+ } catch (e) {
49
+ console.error(e);
50
+ throw `INTERNAL ERROR: Publish config is not valid for publish.${this.name}.select `
51
+ }
52
+ }
53
+
54
+ /** Used for subscribe */
55
+ if(returnNewQuery) return (q as unknown as any);
56
+ if (returnQuery) return (_query as unknown as any[]);
57
+
58
+ return runQueryReturnType(_query, returnType, this, localParams);
59
+
60
+ } catch (e) {
61
+ // console.trace(e)
62
+ if (localParams && localParams.testRule) throw e;
63
+ throw parseError(e, `dbo.${this.name}.find()`);
64
+ // throw { err: parseError(e), msg: `Issue with dbo.${this.name}.find()`, args: { filter, selectParams} };
65
+ }
66
+ }
67
+
68
+
69
+ export const runQueryReturnType = async (query: string, returnType: SelectParams["returnType"], handler: ViewHandler | TableHandler, localParams: LocalParams | undefined) => {
70
+
71
+ if (returnType === "statement") {
72
+ if (!(await canRunSQL(handler.dboBuilder.prostgles, localParams))) {
73
+ throw `Not allowed: {returnType: "statement"} requires sql privileges `
74
+ }
75
+ return query as unknown as any[];
76
+
77
+ } else if (["row", "value"].includes(returnType!)) {
78
+ return (handler.t || handler.db).oneOrNone(query).then(data => {
79
+ return (data && returnType === "value") ? Object.values(data)[0] : data;
80
+ }).catch(err => makeErrorFromPGError(err, localParams, this));
81
+ } else {
82
+ return (handler.t || handler.db).any(query).then(data => {
83
+ if (returnType === "values") {
84
+ return data.map(d => Object.values(d)[0]);
85
+ }
86
+ return data;
87
+ }).catch(err => makeErrorFromPGError(err, localParams, this));
88
+ }
89
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["insert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAqC,YAAY,EAAY,MAAM,iBAAiB,CAAC;AACvG,OAAO,EAAE,WAAW,EAAyC,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC,CAsL1N"}
1
+ {"version":3,"file":"insert.d.ts","sourceRoot":"","sources":["insert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAqC,YAAY,EAAY,MAAM,iBAAiB,CAAC;AACvG,OAAO,EAAE,WAAW,EAAyC,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,SAAS,EAAE,CAAC,SAAS,GAAG,SAAS,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC,CAwL1N"}
@@ -63,8 +63,9 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
63
63
  conflict_query = " ON CONFLICT DO NOTHING ";
64
64
  }
65
65
  if (param2) {
66
- const good_params = ["returning", "multi", "onConflictDoNothing", "fixIssues"];
67
- const bad_params = (0, prostgles_types_1.getKeys)(param2).filter(k => !good_params.includes(k));
66
+ const good_paramsObj = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1 };
67
+ const good_params = Object.keys(good_paramsObj);
68
+ const bad_params = Object.keys(param2).filter(k => !good_params.includes(k));
68
69
  if (bad_params && bad_params.length)
69
70
  throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
70
71
  }
@@ -91,6 +92,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
91
92
  insertQ = `INSERT INTO ${(0, prostgles_types_1.asName)(this.name)} DEFAULT VALUES `;
92
93
  }
93
94
  else {
95
+ //@ts-ignore
94
96
  insertQ = await this.colSet.getInsertQuery(_data, allowedCols, this.dbTX || this.dboBuilder.dbo, tableRules?.[ACTION]?.validate); // pgp.helpers.insert(_data, columnSet);
95
97
  }
96
98
  return insertQ + conflict_query + returningSelect;
@@ -74,8 +74,9 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
74
74
  }
75
75
 
76
76
  if (param2) {
77
- const good_params = ["returning", "multi", "onConflictDoNothing", "fixIssues"];
78
- const bad_params = getKeys(param2).filter(k => !good_params.includes(k));
77
+ const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1 };
78
+ const good_params = Object.keys(good_paramsObj);
79
+ const bad_params = Object.keys(param2).filter(k => !good_params.includes(k));
79
80
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
80
81
  }
81
82
 
@@ -107,6 +108,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
107
108
  await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
108
109
  insertQ = `INSERT INTO ${asName(this.name)} DEFAULT VALUES `;
109
110
  } else {
111
+ //@ts-ignore
110
112
  insertQ = await this.colSet.getInsertQuery(_data, allowedCols, this.dbTX || this.dboBuilder.dbo, tableRules?.[ACTION]?.validate) // pgp.helpers.insert(_data, columnSet);
111
113
  }
112
114
  return insertQ + conflict_query + returningSelect;
@@ -1 +1 @@
1
- {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAiB,WAAW,EAA2C,MAAM,eAAe,CAAC;AAC5G,OAAO,EAAE,SAAS,EAAe,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CA+JzL"}
1
+ {"version":3,"file":"update.d.ts","sourceRoot":"","sources":["update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAkC,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAiB,WAAW,EAA2C,MAAM,eAAe,CAAC;AAC5G,OAAO,EAAE,SAAS,EAAe,MAAM,kBAAkB,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAI9C,wBAAsB,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC,CAmKzL"}
@@ -5,6 +5,7 @@ const prostgles_types_1 = require("prostgles-types");
5
5
  const DboBuilder_1 = require("../DboBuilder");
6
6
  const PubSubManager_1 = require("../PubSubManager/PubSubManager");
7
7
  const uploadFile_1 = require("./uploadFile");
8
+ const find_1 = require("./find");
8
9
  async function update(filter, _newData, params, tableRules, localParams) {
9
10
  const ACTION = "update";
10
11
  try {
@@ -51,7 +52,8 @@ async function update(filter, _newData, params, tableRules, localParams) {
51
52
  const { returning, multi = true, onConflictDoNothing = false, fixIssues = false } = params || {};
52
53
  const { returnQuery = false } = localParams ?? {};
53
54
  if (params) {
54
- const good_params = ["returning", "multi", "onConflictDoNothing", "fixIssues"];
55
+ const good_paramsObj = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1, multi: 1 };
56
+ const good_params = Object.keys(good_paramsObj);
55
57
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
56
58
  if (bad_params && bad_params.length)
57
59
  throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
@@ -106,6 +108,9 @@ async function update(filter, _newData, params, tableRules, localParams) {
106
108
  }
107
109
  if (returnQuery)
108
110
  return query;
111
+ if (params?.returnType) {
112
+ return (0, find_1.runQueryReturnType)(query, params.returnType, this, localParams);
113
+ }
109
114
  let result;
110
115
  if (this.t) {
111
116
  result = await (this.t)[qType](query).catch((err) => (0, DboBuilder_1.makeErrorFromPGError)(err, localParams, this, fields));
@@ -4,6 +4,7 @@ import { TableRule, ValidateRow } from "../PublishParser";
4
4
  import { omitKeys, pickKeys } from "../PubSubManager/PubSubManager";
5
5
  import { TableHandler } from "./TableHandler";
6
6
  import { isFile, uploadFile } from "./uploadFile"
7
+ import { runQueryReturnType } from "./find";
7
8
 
8
9
  export async function update(this: TableHandler, filter: Filter, _newData: AnyObject, params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise<AnyObject | void> {
9
10
  const ACTION = "update";
@@ -56,9 +57,9 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
56
57
  const { returning, multi = true, onConflictDoNothing = false, fixIssues = false } = params || {};
57
58
  const { returnQuery = false } = localParams ?? {};
58
59
 
59
-
60
60
  if (params) {
61
- const good_params = ["returning", "multi", "onConflictDoNothing", "fixIssues"];
61
+ const good_paramsObj: Record<keyof UpdateParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflictDoNothing: 1, multi: 1 };
62
+ const good_params = Object.keys(good_paramsObj);
62
63
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
63
64
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
64
65
  }
@@ -127,6 +128,10 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
127
128
 
128
129
  if (returnQuery) return query as unknown as void;
129
130
 
131
+ if(params?.returnType){
132
+ return runQueryReturnType(query, params.returnType, this, localParams);
133
+ }
134
+
130
135
  let result;
131
136
  if (this.t) {
132
137
  result = await (this.t)[qType](query).catch((err: any) => makeErrorFromPGError(err, localParams, this, fields));
@@ -120,7 +120,7 @@ function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "", tables) {
120
120
  type = !cols ? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable ? "null | " : ""} ${(0, DboBuilder_1.postgresToTsType)(c.udt_name)}; `).join(" ")} }`;
121
121
  }
122
122
  }
123
- return `${fieldType.nullable ? `null |` : ""} ${type}${l.isArray ? "[]" : ""}`;
123
+ return `${fieldType.nullable ? `null | ` : ""}${type}${l.isArray ? "[]" : ""}`;
124
124
  }
125
125
  else
126
126
  throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2);
@@ -126,7 +126,7 @@ export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpt
126
126
  type = !cols? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable? "null | " : "" } ${postgresToTsType(c.udt_name)}; `).join(" ")} }`
127
127
  }
128
128
  }
129
- return `${fieldType.nullable ? `null |` : ""} ${type}${l.isArray? "[]" : ""}`;
129
+ return `${fieldType.nullable ? `null | ` : ""}${type}${l.isArray? "[]" : ""}`;
130
130
 
131
131
  } else throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2)
132
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "4.0.35",
3
+ "version": "4.0.37",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "check-disk-space": "^3.3.1",
41
41
  "file-type": "^17.1.4",
42
42
  "pg-promise": "^11.3.0",
43
- "prostgles-types": "^4.0.15",
43
+ "prostgles-types": "^4.0.16",
44
44
  "sharp": "^0.31.0"
45
45
  },
46
46
  "devDependencies": {
@@ -1 +1 @@
1
- 26897
1
+ 38429