prostgles-server 3.0.150 → 3.0.152

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.
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSubscribeRelatedTables = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const DboBuilder_1 = require("../DboBuilder");
6
+ const PubSubManager_1 = require("../PubSubManager/PubSubManager");
7
+ async function getSubscribeRelatedTables({ selectParams, filter, localParams, table_rules, condition, filterOpts }) {
8
+ let viewOptions = undefined;
9
+ if (this.is_view) {
10
+ const viewName = this.name;
11
+ const viewNameEscaped = this.escapedName;
12
+ const { current_schema } = await this.db.oneOrNone("SELECT current_schema");
13
+ /** Get list of used columns and their parent tables */
14
+ let { def } = (await this.db.oneOrNone("SELECT pg_get_viewdef(${viewName}) as def", { viewName }));
15
+ def = def.trim();
16
+ if (def.endsWith(";")) {
17
+ def = def.slice(0, -1);
18
+ }
19
+ if (!def || typeof def !== "string") {
20
+ throw (0, DboBuilder_1.makeErrorFromPGError)("Could get view definition");
21
+ }
22
+ const { fields } = await this.dboBuilder.dbo.sql(`SELECT * FROM ( \n ${def} \n ) prostgles_subscribe_view_definition LIMIT 0`, {});
23
+ const tableColumns = fields.filter(f => f.tableName && f.columnName);
24
+ /** Create exists filters for each table */
25
+ const tableIds = Array.from(new Set(tableColumns.map(tc => tc.tableID.toString())));
26
+ viewOptions = {
27
+ type: "view",
28
+ viewName,
29
+ definition: def,
30
+ relatedTables: []
31
+ };
32
+ viewOptions.relatedTables = await Promise.all(tableIds.map(async (tableID) => {
33
+ const table = this.dboBuilder.USER_TABLES.find(t => t.relid === +tableID);
34
+ let tableCols = tableColumns.filter(tc => tc.tableID.toString() === tableID);
35
+ /** If table has primary keys and they are all in this view then use only primary keys */
36
+ if (table?.pkey_columns?.every(pkey => tableCols.some(c => c.columnName === pkey))) {
37
+ tableCols = tableCols.filter(c => table?.pkey_columns?.includes(c.columnName));
38
+ }
39
+ else {
40
+ /** Exclude non comparable data types */
41
+ tableCols = tableCols.filter(c => !["json", "xml"].includes(c.udt_name));
42
+ }
43
+ const { relname: tableName, schemaname: tableSchema } = table;
44
+ if (tableCols.length) {
45
+ const tableNameEscaped = tableSchema === current_schema ? table.relname : [tableSchema, tableName].map(v => JSON.stringify(v)).join(".");
46
+ const fullCondition = `EXISTS (
47
+ SELECT 1
48
+ FROM ${viewNameEscaped}
49
+ WHERE ${tableCols.map(c => `${tableNameEscaped}.${JSON.stringify(c.columnName)} = ${viewNameEscaped}.${JSON.stringify(c.name)}`).join(" AND \n")}
50
+ AND ${condition || "TRUE"}
51
+ )`;
52
+ try {
53
+ const { count } = await this.db.oneOrNone(`
54
+ WITH ${(0, prostgles_types_1.asName)(tableName)} AS (
55
+ SELECT *
56
+ FROM ${(0, prostgles_types_1.asName)(tableName)}
57
+ LIMIT 0
58
+ )
59
+
60
+ SELECT COUNT(*) as count
61
+ FROM (
62
+ ${def}
63
+ ) prostgles_view_ref_table_test
64
+ `);
65
+ const relatedTableSubscription = {
66
+ tableName: tableName,
67
+ tableNameEscaped,
68
+ condition: fullCondition,
69
+ };
70
+ if (count.toString() === '0') {
71
+ return relatedTableSubscription;
72
+ }
73
+ }
74
+ catch (e) {
75
+ (0, PubSubManager_1.log)(`Could not not override subscribed view (${this.name}) table (${tableName}). Will not check condition`, e);
76
+ }
77
+ }
78
+ return {
79
+ tableName,
80
+ tableNameEscaped: JSON.stringify(tableName),
81
+ condition: "TRUE"
82
+ };
83
+ }));
84
+ /** Get list of remaining used inner tables */
85
+ const allUsedTables = await this.db.any("SELECT distinct table_name, table_schema FROM information_schema.view_column_usage WHERE view_name = ${viewName}", { viewName });
86
+ /** Remaining tables will have listeners on all records (condition = "TRUE") */
87
+ const remainingInnerTables = allUsedTables.filter(at => !tableColumns.some(dc => dc.tableName === at.table_name && dc.tableSchema === at.table_schema));
88
+ viewOptions.relatedTables = [
89
+ ...viewOptions.relatedTables,
90
+ ...remainingInnerTables.map(t => ({
91
+ tableName: t.table_name,
92
+ tableNameEscaped: [t.table_name, t.table_schema].map(v => JSON.stringify(v)).join("."),
93
+ condition: "TRUE"
94
+ }))
95
+ ];
96
+ if (!viewOptions.relatedTables.length) {
97
+ throw "Could not subscribe to this view: no related tables found";
98
+ }
99
+ /** Any joined table used within select or filter must also be added a trigger for this recordset */
100
+ }
101
+ else {
102
+ const newQuery = await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, { ...localParams, returnNewQuery: true });
103
+ viewOptions = {
104
+ type: "table",
105
+ relatedTables: []
106
+ };
107
+ /**
108
+ * Avoid nested exists error. Will affect performance
109
+ */
110
+ const nonExistsFilter = filterOpts.exists.length ? {} : filter;
111
+ for await (const j of (newQuery.joins ?? [])) {
112
+ if (!viewOptions.relatedTables.find(rt => rt.tableName === j.table)) {
113
+ viewOptions.relatedTables.push({
114
+ tableName: j.table,
115
+ tableNameEscaped: (0, prostgles_types_1.asName)(j.table),
116
+ condition: (await this.dboBuilder.dbo[j.table].prepareWhere({
117
+ filter: {
118
+ $existsJoined: {
119
+ [[this.name, ...j.$path ?? [].slice(0).reverse()].join(".")]: nonExistsFilter
120
+ }
121
+ },
122
+ addKeywords: false,
123
+ localParams: undefined,
124
+ tableRule: undefined
125
+ })).where
126
+ });
127
+ }
128
+ }
129
+ for await (const e of newQuery.whereOpts.exists) {
130
+ const eTable = e.tables.at(-1);
131
+ viewOptions.relatedTables.push({
132
+ tableName: eTable,
133
+ tableNameEscaped: (0, prostgles_types_1.asName)(eTable),
134
+ condition: (await this.dboBuilder.dbo[eTable].prepareWhere({
135
+ filter: {
136
+ $existsJoined: {
137
+ [[this.name, ...e.tables ?? [].slice(0, -1).reverse()].join(".")]: nonExistsFilter
138
+ }
139
+ },
140
+ addKeywords: false,
141
+ localParams: undefined,
142
+ tableRule: undefined
143
+ })).where
144
+ });
145
+ }
146
+ if (!viewOptions.relatedTables.length) {
147
+ viewOptions = undefined;
148
+ }
149
+ }
150
+ return viewOptions;
151
+ }
152
+ exports.getSubscribeRelatedTables = getSubscribeRelatedTables;
@@ -0,0 +1,183 @@
1
+ import { AnyObject, asName, SubscribeParams } from "prostgles-types";
2
+ import { ExistsFilterConfig, Filter, LocalParams, makeErrorFromPGError } from "../DboBuilder";
3
+ import { TableRule } from "../PublishParser";
4
+ import { log, ViewSubscriptionOptions } from "../PubSubManager/PubSubManager";
5
+ import { NewQuery } from "./QueryBuilder/QueryBuilder";
6
+ import { ViewHandler } from "./ViewHandler";
7
+
8
+ type Args = {
9
+ selectParams: Omit<SubscribeParams<any>, "throttle">;
10
+ filter: Filter;
11
+ table_rules: TableRule<AnyObject, void> | undefined;
12
+ localParams: LocalParams | undefined;
13
+ condition: string;
14
+ filterOpts: {
15
+ where: string;
16
+ filter: AnyObject;
17
+ exists: ExistsFilterConfig[];
18
+ };
19
+ }
20
+ export async function getSubscribeRelatedTables(this: ViewHandler, { selectParams, filter, localParams, table_rules, condition, filterOpts }: Args){
21
+
22
+ let viewOptions: ViewSubscriptionOptions | undefined = undefined;
23
+ if (this.is_view) {
24
+ const viewName = this.name;
25
+ const viewNameEscaped = this.escapedName;
26
+ const { current_schema } = await this.db.oneOrNone("SELECT current_schema")
27
+
28
+ /** Get list of used columns and their parent tables */
29
+ let { def } = (await this.db.oneOrNone("SELECT pg_get_viewdef(${viewName}) as def", { viewName })) as { def: string };
30
+ def = def.trim();
31
+ if (def.endsWith(";")) {
32
+ def = def.slice(0, -1);
33
+ }
34
+ if (!def || typeof def !== "string") {
35
+ throw makeErrorFromPGError("Could get view definition");
36
+ }
37
+ const { fields } = await this.dboBuilder.dbo.sql!(`SELECT * FROM ( \n ${def} \n ) prostgles_subscribe_view_definition LIMIT 0`, {});
38
+ const tableColumns = fields.filter(f => f.tableName && f.columnName);
39
+
40
+ /** Create exists filters for each table */
41
+ const tableIds: string[] = Array.from(new Set(tableColumns.map(tc => tc.tableID!.toString())));
42
+ viewOptions = {
43
+ type: "view",
44
+ viewName,
45
+ definition: def,
46
+ relatedTables: []
47
+ }
48
+ viewOptions.relatedTables = await Promise.all(tableIds.map(async tableID => {
49
+ const table = this.dboBuilder.USER_TABLES!.find(t => t.relid === +tableID)!;
50
+ let tableCols = tableColumns.filter(tc => tc.tableID!.toString() === tableID);
51
+
52
+ /** If table has primary keys and they are all in this view then use only primary keys */
53
+ if (table?.pkey_columns?.every(pkey => tableCols.some(c => c.columnName === pkey))) {
54
+ tableCols = tableCols.filter(c => table?.pkey_columns?.includes(c.columnName!))
55
+ } else {
56
+ /** Exclude non comparable data types */
57
+ tableCols = tableCols.filter(c => !["json", "xml"].includes(c.udt_name))
58
+ }
59
+
60
+ const { relname: tableName, schemaname: tableSchema } = table;
61
+
62
+ if (tableCols.length) {
63
+
64
+ const tableNameEscaped = tableSchema === current_schema ? table.relname : [tableSchema, tableName].map(v => JSON.stringify(v)).join(".");
65
+
66
+ const fullCondition = `EXISTS (
67
+ SELECT 1
68
+ FROM ${viewNameEscaped}
69
+ WHERE ${tableCols.map(c => `${tableNameEscaped}.${JSON.stringify(c.columnName)} = ${viewNameEscaped}.${JSON.stringify(c.name)}`).join(" AND \n")}
70
+ AND ${condition || "TRUE"}
71
+ )`;
72
+
73
+ try {
74
+ const { count } = await this.db.oneOrNone(`
75
+ WITH ${asName(tableName)} AS (
76
+ SELECT *
77
+ FROM ${asName(tableName)}
78
+ LIMIT 0
79
+ )
80
+
81
+ SELECT COUNT(*) as count
82
+ FROM (
83
+ ${def}
84
+ ) prostgles_view_ref_table_test
85
+ `);
86
+
87
+ const relatedTableSubscription = {
88
+ tableName: tableName!,
89
+ tableNameEscaped,
90
+ condition: fullCondition,
91
+ }
92
+
93
+ if (count.toString() === '0') {
94
+ return relatedTableSubscription;
95
+ }
96
+ } catch (e) {
97
+ log(`Could not not override subscribed view (${this.name}) table (${tableName}). Will not check condition`, e);
98
+ }
99
+ }
100
+
101
+ return {
102
+ tableName,
103
+ tableNameEscaped: JSON.stringify(tableName),// [table.schemaname, table.relname].map(v => JSON.stringify(v)).join("."),
104
+ condition: "TRUE"
105
+ }
106
+
107
+ }))
108
+
109
+ /** Get list of remaining used inner tables */
110
+ const allUsedTables: { table_name: string; table_schema: string; }[] = await this.db.any(
111
+ "SELECT distinct table_name, table_schema FROM information_schema.view_column_usage WHERE view_name = ${viewName}",
112
+ { viewName }
113
+ );
114
+
115
+ /** Remaining tables will have listeners on all records (condition = "TRUE") */
116
+ const remainingInnerTables = allUsedTables.filter(at => !tableColumns.some(dc => dc.tableName === at.table_name && dc.tableSchema === at.table_schema));
117
+ viewOptions.relatedTables = [
118
+ ...viewOptions.relatedTables,
119
+ ...remainingInnerTables.map(t => ({
120
+ tableName: t.table_name,
121
+ tableNameEscaped: [t.table_name, t.table_schema].map(v => JSON.stringify(v)).join("."),
122
+ condition: "TRUE"
123
+ }))
124
+ ];
125
+
126
+ if (!viewOptions.relatedTables.length) {
127
+ throw "Could not subscribe to this view: no related tables found";
128
+ }
129
+
130
+ /** Any joined table used within select or filter must also be added a trigger for this recordset */
131
+ } else {
132
+ const newQuery = await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, { ...localParams, returnNewQuery: true }) as unknown as NewQuery;
133
+ viewOptions = {
134
+ type: "table",
135
+ relatedTables: []
136
+ }
137
+ /**
138
+ * Avoid nested exists error. Will affect performance
139
+ */
140
+ const nonExistsFilter = filterOpts.exists.length ? {} : filter;
141
+ for await (const j of (newQuery.joins ?? [])) {
142
+ if (!viewOptions!.relatedTables.find(rt => rt.tableName === j.table)) {
143
+ viewOptions.relatedTables.push({
144
+ tableName: j.table,
145
+ tableNameEscaped: asName(j.table),
146
+ condition: (await this.dboBuilder.dbo[j.table]!.prepareWhere!({
147
+ filter: {
148
+ $existsJoined: {
149
+
150
+ [[this.name, ...j.$path ?? [].slice(0).reverse()].join(".")]: nonExistsFilter
151
+ }
152
+ },
153
+ addKeywords: false,
154
+ localParams: undefined,
155
+ tableRule: undefined
156
+ })).where
157
+ })
158
+ }
159
+ }
160
+ for await (const e of newQuery.whereOpts.exists) {
161
+ const eTable = e.tables.at(-1)!
162
+ viewOptions.relatedTables.push({
163
+ tableName: eTable,
164
+ tableNameEscaped: asName(eTable),
165
+ condition: (await this.dboBuilder.dbo[eTable]!.prepareWhere!({
166
+ filter: {
167
+ $existsJoined: {
168
+ [[this.name, ...e.tables ?? [].slice(0, -1).reverse()].join(".")]: nonExistsFilter
169
+ }
170
+ },
171
+ addKeywords: false,
172
+ localParams: undefined,
173
+ tableRule: undefined
174
+ })).where
175
+ });
176
+ }
177
+ if (!viewOptions.relatedTables.length) {
178
+ viewOptions = undefined;
179
+ }
180
+ }
181
+
182
+ return viewOptions;
183
+ }
@@ -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,CAqL1N"}
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"}
@@ -77,7 +77,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
77
77
  fullReturning.concat(originalReturning.filter(s => !fullReturning.some(f => f.alias === s.alias)));
78
78
  const finalSelect = tableRules?.[ACTION]?.postValidate ? fullReturning : originalReturning;
79
79
  const returningSelect = this.makeReturnQuery(finalSelect);
80
- const makeQuery = async (_row, isOne = false) => {
80
+ const makeQuery = async (_row) => {
81
81
  const row = { ..._row };
82
82
  if (!(0, prostgles_types_1.isObject)(row)) {
83
83
  console.trace(row);
@@ -85,9 +85,6 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
85
85
  }
86
86
  const { data, allowedCols } = this.validateNewData({ row, forcedData, allowedFields: fields, tableRules, fixIssues });
87
87
  const _data = { ...data };
88
- if (Array.isArray(_data) && !_data.length) {
89
- throw "Emprty insert. Provide data";
90
- }
91
88
  let insertQ = "";
92
89
  if (!Array.isArray(_data) && !(0, prostgles_types_1.getKeys)(_data).length || Array.isArray(_data) && !_data.length) {
93
90
  await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
@@ -110,6 +107,9 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
110
107
  return insertResult;
111
108
  }
112
109
  if (Array.isArray(data)) {
110
+ if (!data.length) {
111
+ throw "Empty insert. Provide data";
112
+ }
113
113
  const queries = await Promise.all(data.map(async (p) => {
114
114
  const q = await makeQuery(p);
115
115
  return q;
@@ -119,7 +119,7 @@ async function insert(rowOrRows, param2, param3_unused, tableRules, localParams)
119
119
  queryType = "many";
120
120
  }
121
121
  else {
122
- query = await makeQuery(data, true);
122
+ query = await makeQuery(data);
123
123
  if (returningSelect)
124
124
  queryType = "one";
125
125
  }
@@ -91,7 +91,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
91
91
  const finalSelect = tableRules?.[ACTION]?.postValidate? fullReturning : originalReturning;
92
92
  const returningSelect = this.makeReturnQuery(finalSelect);
93
93
 
94
- const makeQuery = async (_row: AnyObject | undefined, isOne = false) => {
94
+ const makeQuery = async (_row: AnyObject | undefined) => {
95
95
  const row = { ..._row };
96
96
 
97
97
  if (!isObject(row)) {
@@ -102,10 +102,6 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
102
102
  const { data, allowedCols } = this.validateNewData({ row, forcedData, allowedFields: fields, tableRules, fixIssues });
103
103
  const _data = { ...data };
104
104
 
105
- if(Array.isArray(_data) && !_data.length){
106
- throw "Emprty insert. Provide data";
107
- }
108
-
109
105
  let insertQ = "";
110
106
  if (!Array.isArray(_data) && !getKeys(_data).length || Array.isArray(_data) && !_data.length) {
111
107
  await tableRules?.[ACTION]?.validate?.(_data, this.dbTX || this.dboBuilder.dbo);
@@ -130,6 +126,11 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
130
126
  }
131
127
 
132
128
  if (Array.isArray(data)) {
129
+
130
+ if(!data.length){
131
+ throw "Empty insert. Provide data";
132
+ }
133
+
133
134
  const queries = await Promise.all(data.map(async p => {
134
135
  const q = await makeQuery(p);
135
136
  return q;
@@ -138,7 +139,7 @@ export async function insert(this: TableHandler, rowOrRows: (AnyObject | AnyObje
138
139
  query = pgp.helpers.concat(queries);
139
140
  if (returningSelect) queryType = "many";
140
141
  } else {
141
- query = await makeQuery(data, true);
142
+ query = await makeQuery(data);
142
143
  if (returningSelect) queryType = "one";
143
144
  }
144
145
 
@@ -1 +1 @@
1
- {"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAoC,MAAM,eAAe,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAG7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,GAAG,CAAC;AAEpD,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,GAAG,CAAA;CAAE,CAAC,CAAA;AAC/I,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,GAAG,SAAS,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AAwPzL,OAAO,EAAE,SAAS,EAAE,CAAA"}
1
+ {"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,WAAW,EAAc,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,MAAM,MAAM,SAAS,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,GAAG,CAAC;AAEpD,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,GAAG,CAAA;CAAE,CAAC,CAAA;AAC/I,iBAAe,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,GAAG,SAAS,EAAE,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;AAsFzL,OAAO,EAAE,SAAS,EAAE,CAAA"}
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.subscribe = void 0;
4
- const prostgles_types_1 = require("prostgles-types");
5
4
  const DboBuilder_1 = require("../DboBuilder");
6
5
  const PubSubManager_1 = require("../PubSubManager/PubSubManager");
6
+ const getSubscribeRelatedTables_1 = require("./getSubscribeRelatedTables");
7
7
  async function subscribe(filter, params, localFunc, table_rules, localParams) {
8
8
  try {
9
9
  // if (this.is_view) throw "Cannot subscribe to a view";
@@ -25,186 +25,40 @@ async function subscribe(filter, params, localFunc, table_rules, localParams) {
25
25
  if (filterSize * 4 > 2704) {
26
26
  throw "filter too big. Might exceed the btree version 4 maximum 2704. Use a primary key or a $rowhash filter instead";
27
27
  }
28
+ if (!this.dboBuilder.prostgles.isSuperUser) {
29
+ throw "Subscribe not possible. Must be superuser to add triggers 1856";
30
+ }
31
+ /** Ensure request is valid */
32
+ await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, localParams);
33
+ const viewOptions = await getSubscribeRelatedTables_1.getSubscribeRelatedTables.bind(this)({ filter, selectParams, table_rules, localParams, condition, filterOpts });
34
+ const commonSubOpts = {
35
+ table_info: this.tableOrViewInfo,
36
+ viewOptions,
37
+ table_rules,
38
+ condition,
39
+ table_name: this.name,
40
+ filter: { ...filter },
41
+ params: { ...selectParams },
42
+ throttle,
43
+ last_throttled: 0,
44
+ };
28
45
  if (!localFunc) {
29
- if (!this.dboBuilder.prostgles.isSuperUser) {
30
- throw "Subscribe not possible. Must be superuser to add triggers 1856";
31
- }
32
- return await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, localParams)
33
- .then(async (_isValid) => {
34
- let viewOptions = undefined;
35
- if (this.is_view) {
36
- const viewName = this.name;
37
- const viewNameEscaped = this.escapedName;
38
- const { current_schema } = await this.db.oneOrNone("SELECT current_schema");
39
- /** Get list of used columns and their parent tables */
40
- let { def } = (await this.db.oneOrNone("SELECT pg_get_viewdef(${viewName}) as def", { viewName }));
41
- def = def.trim();
42
- if (def.endsWith(";")) {
43
- def = def.slice(0, -1);
44
- }
45
- if (!def || typeof def !== "string") {
46
- throw (0, DboBuilder_1.makeErrorFromPGError)("Could get view definition");
47
- }
48
- const { fields } = await this.dboBuilder.dbo.sql(`SELECT * FROM ( \n ${def} \n ) prostgles_subscribe_view_definition LIMIT 0`, {});
49
- const tableColumns = fields.filter(f => f.tableName && f.columnName);
50
- /** Create exists filters for each table */
51
- const tableIds = Array.from(new Set(tableColumns.map(tc => tc.tableID.toString())));
52
- viewOptions = {
53
- type: "view",
54
- viewName,
55
- definition: def,
56
- relatedTables: []
57
- };
58
- viewOptions.relatedTables = await Promise.all(tableIds.map(async (tableID) => {
59
- const table = this.dboBuilder.USER_TABLES.find(t => t.relid === +tableID);
60
- let tableCols = tableColumns.filter(tc => tc.tableID.toString() === tableID);
61
- /** If table has primary keys and they are all in this view then use only primary keys */
62
- if (table?.pkey_columns?.every(pkey => tableCols.some(c => c.columnName === pkey))) {
63
- tableCols = tableCols.filter(c => table?.pkey_columns?.includes(c.columnName));
64
- }
65
- else {
66
- /** Exclude non comparable data types */
67
- tableCols = tableCols.filter(c => !["json", "xml"].includes(c.udt_name));
68
- }
69
- const { relname: tableName, schemaname: tableSchema } = table;
70
- if (tableCols.length) {
71
- const tableNameEscaped = tableSchema === current_schema ? table.relname : [tableSchema, tableName].map(v => JSON.stringify(v)).join(".");
72
- const fullCondition = `EXISTS (
73
- SELECT 1
74
- FROM ${viewNameEscaped}
75
- WHERE ${tableCols.map(c => `${tableNameEscaped}.${JSON.stringify(c.columnName)} = ${viewNameEscaped}.${JSON.stringify(c.name)}`).join(" AND \n")}
76
- AND ${condition || "TRUE"}
77
- )`;
78
- try {
79
- const { count } = await this.db.oneOrNone(`
80
- WITH ${(0, prostgles_types_1.asName)(tableName)} AS (
81
- SELECT *
82
- FROM ${(0, prostgles_types_1.asName)(tableName)}
83
- LIMIT 0
84
- )
85
-
86
- SELECT COUNT(*) as count
87
- FROM (
88
- ${def}
89
- ) prostgles_view_ref_table_test
90
- `);
91
- const relatedTableSubscription = {
92
- tableName: tableName,
93
- tableNameEscaped,
94
- condition: fullCondition,
95
- };
96
- if (count.toString() === '0') {
97
- return relatedTableSubscription;
98
- }
99
- }
100
- catch (e) {
101
- (0, PubSubManager_1.log)(`Could not not override subscribed view (${this.name}) table (${tableName}). Will not check condition`, e);
102
- }
103
- }
104
- return {
105
- tableName,
106
- tableNameEscaped: JSON.stringify(tableName),
107
- condition: "TRUE"
108
- };
109
- }));
110
- /** Get list of remaining used inner tables */
111
- const allUsedTables = await this.db.any("SELECT distinct table_name, table_schema FROM information_schema.view_column_usage WHERE view_name = ${viewName}", { viewName });
112
- /** Remaining tables will have listeners on all records (condition = "TRUE") */
113
- const remainingInnerTables = allUsedTables.filter(at => !tableColumns.some(dc => dc.tableName === at.table_name && dc.tableSchema === at.table_schema));
114
- viewOptions.relatedTables = [
115
- ...viewOptions.relatedTables,
116
- ...remainingInnerTables.map(t => ({
117
- tableName: t.table_name,
118
- tableNameEscaped: [t.table_name, t.table_schema].map(v => JSON.stringify(v)).join("."),
119
- condition: "TRUE"
120
- }))
121
- ];
122
- if (!viewOptions.relatedTables.length) {
123
- throw "Could not subscribe to this view: no related tables found";
124
- }
125
- }
126
- /** Any joined table used within select or filter must also be added a trigger for this recordset */
127
- if (!this.is_view) {
128
- const newQuery = await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, { ...localParams, returnNewQuery: true });
129
- viewOptions = {
130
- type: "table",
131
- relatedTables: []
132
- };
133
- /**
134
- * Avoid nested exists error. Will affect performance
135
- */
136
- const nonExistsFilter = filterOpts.exists.length ? {} : filter;
137
- for await (const j of (newQuery.joins ?? [])) {
138
- if (!viewOptions.relatedTables.find(rt => rt.tableName === j.table)) {
139
- viewOptions.relatedTables.push({
140
- tableName: j.table,
141
- tableNameEscaped: (0, prostgles_types_1.asName)(j.table),
142
- condition: (await this.dboBuilder.dbo[j.table].prepareWhere({
143
- filter: {
144
- $existsJoined: {
145
- [[this.name, ...j.$path ?? [].slice(0).reverse()].join(".")]: nonExistsFilter
146
- }
147
- },
148
- addKeywords: false,
149
- localParams: undefined,
150
- tableRule: undefined
151
- })).where
152
- });
153
- }
154
- }
155
- for await (const e of newQuery.whereOpts.exists) {
156
- const eTable = e.tables.at(-1);
157
- viewOptions.relatedTables.push({
158
- tableName: eTable,
159
- tableNameEscaped: (0, prostgles_types_1.asName)(eTable),
160
- condition: (await this.dboBuilder.dbo[eTable].prepareWhere({
161
- filter: {
162
- $existsJoined: {
163
- [[this.name, ...e.tables ?? [].slice(0, -1).reverse()].join(".")]: nonExistsFilter
164
- }
165
- },
166
- addKeywords: false,
167
- localParams: undefined,
168
- tableRule: undefined
169
- })).where
170
- });
171
- }
172
- if (!viewOptions.relatedTables.length) {
173
- viewOptions = undefined;
174
- }
175
- }
176
- const { socket } = localParams ?? {};
177
- const pubSubManager = await this.dboBuilder.getPubSubManager();
178
- return pubSubManager.addSub({
179
- table_info: this.tableOrViewInfo,
180
- socket,
181
- table_rules,
182
- table_name: this.name,
183
- condition: condition,
184
- viewOptions,
185
- func: undefined,
186
- filter: { ...filter },
187
- params: { ...selectParams },
188
- socket_id: socket?.id,
189
- throttle,
190
- last_throttled: 0,
191
- }).then(channelName => ({ channelName }));
192
- });
46
+ const { socket } = localParams ?? {};
47
+ const pubSubManager = await this.dboBuilder.getPubSubManager();
48
+ return pubSubManager.addSub({
49
+ ...commonSubOpts,
50
+ socket,
51
+ func: undefined,
52
+ socket_id: socket?.id,
53
+ }).then(channelName => ({ channelName }));
193
54
  }
194
55
  else {
195
56
  const pubSubManager = await this.dboBuilder.getPubSubManager();
196
57
  pubSubManager.addSub({
197
- table_info: this.tableOrViewInfo,
58
+ ...commonSubOpts,
198
59
  socket: undefined,
199
- table_rules,
200
- condition,
201
60
  func: localFunc,
202
- filter: { ...filter },
203
- params: { ...selectParams },
204
61
  socket_id: undefined,
205
- table_name: this.name,
206
- throttle,
207
- last_throttled: 0,
208
62
  }).then(channelName => ({ channelName }));
209
63
  const unsubscribe = async () => {
210
64
  const pubSubManager = await this.dboBuilder.getPubSubManager();