prostgles-server 3.0.151 → 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.
- package/dist/DboBuilder/getSubscribeRelatedTables.d.ts +20 -0
- package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -0
- package/dist/DboBuilder/getSubscribeRelatedTables.js +153 -0
- package/dist/DboBuilder/getSubscribeRelatedTables.js.map +1 -0
- package/dist/DboBuilder/subscribe.d.ts.map +1 -1
- package/dist/DboBuilder/subscribe.js +27 -173
- package/dist/DboBuilder/subscribe.js.map +1 -1
- package/lib/DboBuilder/getSubscribeRelatedTables.d.ts +20 -0
- package/lib/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -0
- package/lib/DboBuilder/getSubscribeRelatedTables.js +152 -0
- package/lib/DboBuilder/getSubscribeRelatedTables.ts +183 -0
- package/lib/DboBuilder/subscribe.d.ts.map +1 -1
- package/lib/DboBuilder/subscribe.js +27 -173
- package/lib/DboBuilder/subscribe.ts +38 -200
- package/package.json +1 -1
- package/tests/client/PID.txt +1 -1
- package/tests/server/package-lock.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AnyObject, SubscribeParams } from "prostgles-types";
|
|
2
|
+
import { ExistsFilterConfig, Filter, LocalParams } from "../DboBuilder";
|
|
3
|
+
import { TableRule } from "../PublishParser";
|
|
4
|
+
import { ViewSubscriptionOptions } from "../PubSubManager/PubSubManager";
|
|
5
|
+
import { ViewHandler } from "./ViewHandler";
|
|
6
|
+
type Args = {
|
|
7
|
+
selectParams: Omit<SubscribeParams<any>, "throttle">;
|
|
8
|
+
filter: Filter;
|
|
9
|
+
table_rules: TableRule<AnyObject, void> | undefined;
|
|
10
|
+
localParams: LocalParams | undefined;
|
|
11
|
+
condition: string;
|
|
12
|
+
filterOpts: {
|
|
13
|
+
where: string;
|
|
14
|
+
filter: AnyObject;
|
|
15
|
+
exists: ExistsFilterConfig[];
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare function getSubscribeRelatedTables(this: ViewHandler, { selectParams, filter, localParams, table_rules, condition, filterOpts }: Args): Promise<ViewSubscriptionOptions | undefined>;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=getSubscribeRelatedTables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSubscribeRelatedTables.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/getSubscribeRelatedTables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,WAAW,EAAwB,MAAM,eAAe,CAAC;AAC9F,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAO,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAE9E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,KAAK,IAAI,GAAG;IACV,YAAY,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,CAAC;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC;IACpD,WAAW,EAAE,WAAW,GAAG,SAAS,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE;QACV,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,EAAE,kBAAkB,EAAE,CAAC;KAC9B,CAAC;CACH,CAAA;AACD,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,IAAI,gDAmKjJ"}
|
|
@@ -0,0 +1,153 @@
|
|
|
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;
|
|
153
|
+
//# sourceMappingURL=getSubscribeRelatedTables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSubscribeRelatedTables.js","sourceRoot":"","sources":["../../lib/DboBuilder/getSubscribeRelatedTables.ts"],"names":[],"mappings":";;;AAAA,qDAAqE;AACrE,8CAA8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,MAAM,CAAC;gBAChC,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC,YAAa,CAAC;oBAC3D,MAAM,EAAE;wBACN,aAAa,EAAE;4BACb,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,eAAe;yBACnF;qBACF;oBACD,WAAW,EAAE,KAAK;oBAClB,WAAW,EAAE,SAAS;oBACtB,SAAS,EAAE,SAAS;iBACrB,CAAC,CAAC,CAAC,KAAK;aACV,CAAC,CAAC;SACJ;QACD,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,MAAM,EAAE;YACrC,WAAW,GAAG,SAAS,CAAC;SACzB;KACF;IAED,OAAO,WAAW,CAAC;AACrB,CAAC;AAnKD,8DAmKC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/subscribe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"subscribe.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribe.js","sourceRoot":"","sources":["../../lib/DboBuilder/subscribe.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"subscribe.js","sourceRoot":"","sources":["../../lib/DboBuilder/subscribe.ts"],"names":[],"mappings":";;;AACA,8CAAgE;AAEhE,kEAA0D;AAE1D,2EAAwE;AAMxE,KAAK,UAAU,SAAS,CAAoB,MAAc,EAAE,MAAuB,EAAE,SAAqB,EAAE,WAAuB,EAAE,WAAyB;IAG5J,IAAI;QACF,wDAAwD;QAExD,IAAI,IAAI,CAAC,C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SAAS,EAAE,SAAS,CAAC,CAAA;YAC/D,CAAC,CAAC;YACF,MAAM,GAAG,GAA+B,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,CAAC,CAAA;YACtE,OAAO,GAAG,CAAC;SACZ;KACF;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,WAAW,IAAI,WAAW,CAAC,QAAQ;YAAE,MAAM,CAAC,CAAC;QACjD,MAAM,IAAA,uBAAU,EAAC,CAAC,EAAE,OAAO,IAAI,CAAC,IAAI,cAAc,CAAC,CAAC;KACrD;AACH,CAAC;AAEQ,8BAAS"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AnyObject, SubscribeParams } from "prostgles-types";
|
|
2
|
+
import { ExistsFilterConfig, Filter, LocalParams } from "../DboBuilder";
|
|
3
|
+
import { TableRule } from "../PublishParser";
|
|
4
|
+
import { ViewSubscriptionOptions } from "../PubSubManager/PubSubManager";
|
|
5
|
+
import { ViewHandler } from "./ViewHandler";
|
|
6
|
+
type Args = {
|
|
7
|
+
selectParams: Omit<SubscribeParams<any>, "throttle">;
|
|
8
|
+
filter: Filter;
|
|
9
|
+
table_rules: TableRule<AnyObject, void> | undefined;
|
|
10
|
+
localParams: LocalParams | undefined;
|
|
11
|
+
condition: string;
|
|
12
|
+
filterOpts: {
|
|
13
|
+
where: string;
|
|
14
|
+
filter: AnyObject;
|
|
15
|
+
exists: ExistsFilterConfig[];
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare function getSubscribeRelatedTables(this: ViewHandler, { selectParams, filter, localParams, table_rules, condition, filterOpts }: Args): Promise<ViewSubscriptionOptions>;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=getSubscribeRelatedTables.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSubscribeRelatedTables.d.ts","sourceRoot":"","sources":["getSubscribeRelatedTables.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAU,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,WAAW,EAAwB,MAAM,eAAe,CAAC;AAC9F,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAO,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAE9E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,KAAK,IAAI,GAAG;IACV,YAAY,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,CAAC;IACrD,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC;IACpD,WAAW,EAAE,WAAW,GAAG,SAAS,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE;QACV,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,SAAS,CAAC;QAClB,MAAM,EAAE,kBAAkB,EAAE,CAAC;KAC9B,CAAC;CACH,CAAA;AACD,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,IAAI,oCAmKjJ"}
|
|
@@ -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;
|