prostgles-server 2.0.266 → 2.0.267

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/lib/DboBuilder.ts CHANGED
@@ -8,7 +8,7 @@ import * as Bluebird from "bluebird";
8
8
  // declare global { export interface Promise<T> extends Bluebird<T> {} }
9
9
 
10
10
  import * as pgPromise from 'pg-promise';
11
- const { ParameterizedQuery: PQ } = require('pg-promise');
11
+ import { canRunSQL, runSQL } from "./DboBuilder/runSQL";
12
12
  import pg = require('pg-promise/typescript/pg-subset');
13
13
  import {
14
14
  ColumnInfo, ValidatedColumnInfo, FieldFilter, SelectParams, SubscribeParams,
@@ -1091,6 +1091,14 @@ export class ViewHandler {
1091
1091
  }
1092
1092
 
1093
1093
  if(returnQuery) return (_query as unknown as any[]);
1094
+
1095
+ if(returnType === "statement"){
1096
+ if(!(await canRunSQL(this.dboBuilder.prostgles, localParams))){
1097
+ throw `Not allowed: {returnType: "statement"} requires sql privileges `
1098
+ }
1099
+ return _query as unknown as any[];
1100
+ }
1101
+
1094
1102
  if(["row", "value"].includes(returnType!)) {
1095
1103
  return (this.t || this.db).oneOrNone(_query).then(data => {
1096
1104
  return (data && returnType === "value")? Object.values(data)[0] : data;
@@ -2439,13 +2447,9 @@ export class TableHandler extends ViewHandler {
2439
2447
 
2440
2448
  }
2441
2449
 
2442
- let DATA_TYPES: {oid: string, typname: PG_COLUMN_UDT_DATA_TYPE }[] | undefined;
2443
- let USER_TABLES: { relid: string; relname: string; }[] | undefined;
2444
-
2445
2450
  import { JOIN_TYPES } from "./Prostgles";
2446
2451
  import { BasicSession } from "./AuthHandler";
2447
2452
  import { DBOFullyTyped, getDBSchema } from "./DBSchemaBuilder";
2448
- import { bool } from "aws-sdk/clients/signer";
2449
2453
 
2450
2454
  export class DboBuilder {
2451
2455
  tablesOrViews?: TableSchema[]; //TableSchema TableOrViewInfo
@@ -2651,107 +2655,9 @@ export class DboBuilder {
2651
2655
  return this.joinPaths;
2652
2656
  }
2653
2657
 
2654
- runSQL = async (query: string, params: any, options: SQLOptions | undefined, localParams?: LocalParams) => {
2655
-
2656
- /** Cache types */
2657
- DATA_TYPES ??= await this.db.any("SELECT oid, typname FROM pg_type") ?? [];
2658
- USER_TABLES ??= await this.db.any("SELECT relid, relname FROM pg_catalog.pg_statio_user_tables") ?? [];
2659
-
2660
- const canRunSQL = async (localParams?: LocalParams) => {
2661
- if(!localParams?.socket || !localParams?.httpReq) return true;
2662
-
2663
- const { socket } = localParams;
2664
- const publishParams = await this.prostgles.publishParser!.getPublishParams({ socket });
2665
- let res = await this.prostgles.opts.publishRawSQL?.(publishParams);
2666
- return Boolean(res && typeof res === "boolean" || res === "*");
2667
- }
2668
-
2669
- if(!(await canRunSQL(localParams))) throw "Not allowed to run SQL";
2670
-
2671
- const { returnType, allowListen }: SQLOptions = options || ({} as any);
2672
- const { socket } = localParams || {};
2673
-
2674
- const db = localParams?.tx?.t || this.db;
2675
- if(returnType === "noticeSubscription"){
2676
- if(!socket) throw "Only allowed with client socket"
2677
- return await this.prostgles.dbEventsManager?.addNotice(socket);
2678
- } else if(returnType === "statement"){
2679
- try {
2680
- return pgp.as.format(query, params);
2681
- } catch (err){
2682
- throw (err as any).toString();
2683
- }
2684
- } else if(db) {
2685
-
2686
- let finalQuery = query + "";
2687
- if(returnType === "arrayMode" && !["listen ", "notify "].find(c => query.toLowerCase().trim().startsWith(c))){
2688
- finalQuery = new PQ({ text: pgp.as.format(query, params), rowMode: "array" });
2689
- }
2690
-
2691
- let _qres = await db.result(finalQuery, params)
2692
- const { fields, rows, command } = _qres;
2693
-
2694
- /**
2695
- * Fallback for watchSchema in case not superuser and cannot add db event listener
2696
- */
2697
- const { watchSchema, watchSchemaType } = this.prostgles?.opts || {};
2698
-
2699
- if(
2700
- watchSchema &&
2701
- (!this.prostgles.isSuperUser || watchSchemaType === "prostgles_queries")
2702
- ){
2703
- if(["CREATE", "ALTER", "DROP"].includes(command)){
2704
- this.prostgles.onSchemaChange({ command, query })
2705
- } else if(query) {
2706
- const cleanedQuery = query.toLowerCase().replace(/\s\s+/g, ' ');
2707
- if(PubSubManager.SCHEMA_ALTERING_QUERIES.some(q => cleanedQuery.includes(q.toLowerCase()))){
2708
- this.prostgles.onSchemaChange({ command, query })
2709
- }
2710
- }
2711
- }
2712
-
2713
- if(command === "LISTEN"){
2714
- if(!allowListen) throw new Error(`Your query contains a LISTEN command. Set { allowListen: true } to get subscription hooks. Or ignore this message`)
2715
- if(!socket) throw "Only allowed with client socket"
2716
- return await this.prostgles.dbEventsManager?.addNotify(query, socket);
2717
-
2718
- } else if(returnType === "rows") {
2719
- return rows;
2720
-
2721
- } else if(returnType === "row") {
2722
- return rows[0];
2723
-
2724
- } else if(returnType === "value") {
2725
- return Object.values(rows?.[0] || {})?.[0];
2726
-
2727
- } else if(returnType === "values") {
2728
- return rows.map(r => Object.values(r[0]));
2729
-
2730
- } else {
2731
-
2732
- let qres: SQLResult<typeof returnType> = {
2733
- duration: 0,
2734
- ..._qres,
2735
- fields: fields?.map(f => {
2736
- const dataType = DATA_TYPES!.find(dt => +dt.oid === +f.dataTypeID)?.typname ?? "text",
2737
- tableName = USER_TABLES!.find(t => +t.relid === +f.tableID),
2738
- tsDataType = postgresToTsType(dataType);
2739
-
2740
- return {
2741
- ...f,
2742
- tsDataType,
2743
- dataType,
2744
- udt_name: dataType,
2745
- tableName: tableName?.relname
2746
- }
2747
- }) ?? []
2748
- };
2749
- return qres;
2750
- }
2751
-
2752
- } else console.error("db missing");
2658
+ private runSQL = async (query: string, params: any, options: SQLOptions | undefined, localParams?: LocalParams) => {
2659
+ return runSQL.bind(this)(query, params, options, localParams);
2753
2660
  }
2754
-
2755
2661
  async build(): Promise<DBHandlerServer>{
2756
2662
 
2757
2663
  this.tablesOrViews = await getTablesForSchemaPostgresSQL(this.db);
@@ -2820,101 +2726,6 @@ export class DboBuilder {
2820
2726
  if(!this.dbo.sql){
2821
2727
 
2822
2728
  this.dbo.sql = this.runSQL;
2823
- // this.dbo.sql = async (query: string, params: any, options: SQLOptions | undefined, localParams?: LocalParams) => {
2824
-
2825
- // const canRunSQL = async (localParams?: LocalParams) => {
2826
- // if(!localParams?.socket || !localParams?.httpReq) return true;
2827
-
2828
- // const { socket } = localParams;
2829
- // const publishParams = await this.prostgles.publishParser!.getPublishParams({ socket });
2830
- // let res = await this.prostgles.opts.publishRawSQL?.(publishParams);
2831
- // return Boolean(res && typeof res === "boolean" || res === "*");
2832
- // }
2833
-
2834
- // if(!(await canRunSQL(localParams))) throw "Not allowed to run SQL";
2835
-
2836
- // const { returnType, allowListen }: SQLOptions = options || ({} as any);
2837
- // const { socket } = localParams || {};
2838
-
2839
- // if(returnType === "noticeSubscription"){
2840
- // if(!socket) throw "Only allowed with client socket"
2841
- // return await this.prostgles.dbEventsManager?.addNotice(socket);
2842
- // } else if(returnType === "statement"){
2843
- // try {
2844
- // return pgp.as.format(query, params);
2845
- // } catch (err){
2846
- // throw (err as any).toString();
2847
- // }
2848
- // } else if(this.db) {
2849
-
2850
- // let finalQuery = query + "";
2851
- // if(returnType === "arrayMode" && !["listen ", "notify "].find(c => query.toLowerCase().trim().startsWith(c))){
2852
- // finalQuery = new PQ({ text: pgp.as.format(query, params), rowMode: "array" });
2853
- // }
2854
-
2855
- // let _qres = await this.db.result(finalQuery, params)
2856
- // const { fields, rows, command } = _qres;
2857
-
2858
- // /**
2859
- // * Fallback for watchSchema in case not superuser and cannot add db event listener
2860
- // */
2861
- // const { watchSchema, watchSchemaType } = this.prostgles?.opts || {};
2862
-
2863
- // if(
2864
- // watchSchema &&
2865
- // (!this.prostgles.isSuperUser || watchSchemaType === "prostgles_queries")
2866
- // ){
2867
- // if(["CREATE", "ALTER", "DROP"].includes(command)){
2868
- // this.prostgles.onSchemaChange({ command, query })
2869
- // } else if(query) {
2870
- // const cleanedQuery = query.toLowerCase().replace(/\s\s+/g, ' ');
2871
- // if(PubSubManager.SCHEMA_ALTERING_QUERIES.some(q => cleanedQuery.includes(q.toLowerCase()))){
2872
- // this.prostgles.onSchemaChange({ command, query })
2873
- // }
2874
- // }
2875
- // }
2876
-
2877
- // if(command === "LISTEN"){
2878
- // if(!allowListen) throw new Error(`Your query contains a LISTEN command. Set { allowListen: true } to get subscription hooks. Or ignore this message`)
2879
- // if(!socket) throw "Only allowed with client socket"
2880
- // return await this.prostgles.dbEventsManager?.addNotify(query, socket);
2881
-
2882
- // } else if(returnType === "rows") {
2883
- // return rows;
2884
-
2885
- // } else if(returnType === "row") {
2886
- // return rows[0];
2887
-
2888
- // } else if(returnType === "value") {
2889
- // return Object.values(rows?.[0] || {})?.[0];
2890
-
2891
- // } else if(returnType === "values") {
2892
- // return rows.map(r => Object.values(r[0]));
2893
-
2894
- // } else {
2895
-
2896
- // let qres: SQLResult<typeof returnType> = {
2897
- // duration: 0,
2898
- // ..._qres,
2899
- // fields: fields?.map(f => {
2900
- // const dataType = DATA_TYPES.find(dt => +dt.oid === +f.dataTypeID)?.typname ?? "text",
2901
- // tableName = USER_TABLES.find(t => +t.relid === +f.tableID),
2902
- // tsDataType = postgresToTsType(dataType);
2903
-
2904
- // return {
2905
- // ...f,
2906
- // tsDataType,
2907
- // dataType,
2908
- // udt_name: dataType,
2909
- // tableName: tableName?.relname
2910
- // }
2911
- // }) ?? []
2912
- // };
2913
- // return qres;
2914
- // }
2915
-
2916
- // } else console.error("db missing");
2917
- // }
2918
2729
  } else {
2919
2730
  console.warn(`Could not create dbo.sql handler because there is already a table named "sql"`)
2920
2731
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "2.0.266",
3
+ "version": "2.0.267",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -31,7 +31,7 @@
31
31
  "check-disk-space": "^3.3.1",
32
32
  "file-type": "^17.1.4",
33
33
  "pg-promise": "^10.11.1",
34
- "prostgles-types": "^1.5.171",
34
+ "prostgles-types": "^1.5.174",
35
35
  "sharp": "^0.30.7"
36
36
  },
37
37
  "devDependencies": {
@@ -1 +1 @@
1
- 20976
1
+ 14047
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "2.0.265",
24
+ "version": "2.0.266",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.121.0",
@@ -31,7 +31,7 @@
31
31
  "check-disk-space": "^3.3.1",
32
32
  "file-type": "^17.1.4",
33
33
  "pg-promise": "^10.11.1",
34
- "prostgles-types": "^1.5.171",
34
+ "prostgles-types": "^1.5.174",
35
35
  "sharp": "^0.30.7"
36
36
  },
37
37
  "devDependencies": {
@@ -1375,7 +1375,7 @@
1375
1375
  "check-disk-space": "^3.3.1",
1376
1376
  "file-type": "^17.1.4",
1377
1377
  "pg-promise": "^10.11.1",
1378
- "prostgles-types": "^1.5.171",
1378
+ "prostgles-types": "^1.5.174",
1379
1379
  "sharp": "^0.30.7",
1380
1380
  "typescript": "^4.7.4"
1381
1381
  }