prostgles-server 3.0.64 → 3.0.66
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/AuthHandler.d.ts +11 -11
- package/dist/AuthHandler.d.ts.map +1 -1
- package/dist/DBSchemaBuilder.d.ts +3 -3
- package/dist/DBSchemaBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/Functions.d.ts +3 -3
- package/dist/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts +3 -3
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler.d.ts +1 -1
- package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler.d.ts +5 -4
- package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler.js +7 -134
- package/dist/DboBuilder/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/delete.js +1 -1
- package/dist/DboBuilder/delete.js.map +1 -1
- package/dist/DboBuilder/insert.js +1 -1
- package/dist/DboBuilder/insert.js.map +1 -1
- package/dist/DboBuilder/insertDataParse.js +1 -1
- package/dist/DboBuilder/insertDataParse.js.map +1 -1
- package/dist/DboBuilder/runSQL.js +1 -1
- package/dist/DboBuilder/runSQL.js.map +1 -1
- package/dist/DboBuilder/subscribe.d.ts +11 -0
- package/dist/DboBuilder/subscribe.d.ts.map +1 -0
- package/dist/DboBuilder/subscribe.js +190 -0
- package/dist/DboBuilder/subscribe.js.map +1 -0
- package/dist/DboBuilder/update.js +1 -1
- package/dist/DboBuilder/update.js.map +1 -1
- package/dist/DboBuilder.d.ts +24 -24
- package/dist/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder.js +1 -1
- package/dist/DboBuilder.js.map +1 -1
- package/dist/FileManager.d.ts +6 -6
- package/dist/FileManager.d.ts.map +1 -1
- package/dist/FileManager.js +17 -17
- package/dist/FileManager.js.map +1 -1
- package/dist/Filtering.d.ts +1 -1
- package/dist/Filtering.d.ts.map +1 -1
- package/dist/PostgresNotifListenManager.d.ts +1 -1
- package/dist/PostgresNotifListenManager.d.ts.map +1 -1
- package/dist/Prostgles.d.ts +14 -14
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +12 -12
- package/dist/Prostgles.js.map +1 -1
- package/{lib → dist/PubSubManager}/PubSubManager.d.ts +24 -19
- package/dist/PubSubManager/PubSubManager.d.ts.map +1 -0
- package/dist/PubSubManager/PubSubManager.js +770 -0
- package/dist/PubSubManager/PubSubManager.js.map +1 -0
- package/dist/PubSubManager/initPubSubManager.d.ts +3 -0
- package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -0
- package/dist/PubSubManager/initPubSubManager.js +616 -0
- package/dist/PubSubManager/initPubSubManager.js.map +1 -0
- package/dist/PublishParser.d.ts +32 -32
- package/dist/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser.js +1 -1
- package/dist/PublishParser.js.map +1 -1
- package/dist/SchemaWatch.d.ts +1 -1
- package/dist/SchemaWatch.d.ts.map +1 -1
- package/dist/SyncReplication.d.ts +6 -6
- package/dist/SyncReplication.d.ts.map +1 -1
- package/dist/SyncReplication.js +1 -1
- package/dist/SyncReplication.js.map +1 -1
- package/dist/TableConfig.d.ts +21 -21
- package/dist/TableConfig.d.ts.map +1 -1
- package/dist/TableConfig.js +13 -13
- package/dist/TableConfig.js.map +1 -1
- package/dist/shortestPath.d.ts +1 -1
- package/dist/shortestPath.d.ts.map +1 -1
- package/dist/validation.d.ts +9 -9
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +1 -1
- package/dist/validation.js.map +1 -1
- package/lib/DboBuilder/ViewHandler.d.ts +4 -3
- package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/lib/DboBuilder/ViewHandler.js +7 -134
- package/lib/DboBuilder/ViewHandler.ts +15 -164
- package/lib/DboBuilder/delete.js +1 -1
- package/lib/DboBuilder/delete.ts +1 -1
- package/lib/DboBuilder/insert.js +1 -1
- package/lib/DboBuilder/insert.ts +1 -1
- package/lib/DboBuilder/insertDataParse.js +1 -1
- package/lib/DboBuilder/insertDataParse.ts +1 -1
- package/lib/DboBuilder/runSQL.js +1 -1
- package/lib/DboBuilder/runSQL.ts +1 -1
- package/lib/DboBuilder/subscribe.d.ts +11 -0
- package/lib/DboBuilder/subscribe.d.ts.map +1 -0
- package/lib/DboBuilder/subscribe.js +189 -0
- package/lib/DboBuilder/subscribe.ts +230 -0
- package/lib/DboBuilder/update.js +1 -1
- package/lib/DboBuilder/update.ts +1 -1
- package/lib/DboBuilder.d.ts +1 -1
- package/lib/DboBuilder.d.ts.map +1 -1
- package/lib/DboBuilder.js +1 -1
- package/lib/DboBuilder.ts +1 -1
- package/lib/Prostgles.js +1 -1
- package/lib/Prostgles.ts +1 -1
- package/{dist → lib/PubSubManager}/PubSubManager.d.ts +19 -14
- package/lib/PubSubManager/PubSubManager.d.ts.map +1 -0
- package/lib/PubSubManager/PubSubManager.js +792 -0
- package/lib/PubSubManager/PubSubManager.ts +1016 -0
- package/lib/PubSubManager/initPubSubManager.d.ts +3 -0
- package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -0
- package/lib/PubSubManager/initPubSubManager.js +615 -0
- package/lib/PubSubManager/initPubSubManager.ts +630 -0
- package/lib/PublishParser.js +1 -1
- package/lib/PublishParser.ts +1 -1
- package/lib/SyncReplication.d.ts +1 -1
- package/lib/SyncReplication.d.ts.map +1 -1
- package/lib/SyncReplication.js +1 -1
- package/lib/SyncReplication.ts +1 -1
- package/lib/TableConfig.js +1 -1
- package/lib/TableConfig.ts +1 -1
- package/lib/validation.js +1 -1
- package/lib/validation.ts +1 -1
- package/package.json +5 -4
- package/tests/client/PID.txt +1 -1
- package/tests/server/package-lock.json +7 -5
- package/dist/PubSubManager.d.ts.map +0 -1
- package/dist/PubSubManager.js +0 -1398
- package/dist/PubSubManager.js.map +0 -1
- package/lib/PubSubManager.d.ts.map +0 -1
- package/lib/PubSubManager.js +0 -1420
- package/lib/PubSubManager.ts +0 -1655
|
@@ -0,0 +1,770 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*---------------------------------------------------------------------------------------------
|
|
3
|
+
* Copyright (c) Stefan L. All rights reserved.
|
|
4
|
+
* Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
5
|
+
*--------------------------------------------------------------------------------------------*/
|
|
6
|
+
var _a;
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.pickKeys = exports.omitKeys = exports.PubSubManager = exports.log = exports.DEFAULT_SYNC_BATCH_SIZE = exports.asValue = void 0;
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
const DboBuilder_1 = require("../DboBuilder");
|
|
11
|
+
const Prostgles_1 = require("../Prostgles");
|
|
12
|
+
const initPubSubManager_1 = require("./initPubSubManager");
|
|
13
|
+
const Bluebird = require("bluebird");
|
|
14
|
+
const pgPromise = require("pg-promise");
|
|
15
|
+
const prostgles_types_1 = require("prostgles-types");
|
|
16
|
+
const SyncReplication_1 = require("../SyncReplication");
|
|
17
|
+
let pgp = pgPromise({
|
|
18
|
+
promiseLib: Bluebird
|
|
19
|
+
});
|
|
20
|
+
const asValue = (v) => pgp.as.format("$1", [v]);
|
|
21
|
+
exports.asValue = asValue;
|
|
22
|
+
exports.DEFAULT_SYNC_BATCH_SIZE = 50;
|
|
23
|
+
const log = (...args) => {
|
|
24
|
+
if (process.env.TEST_TYPE) {
|
|
25
|
+
console.log(...args);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
exports.log = log;
|
|
29
|
+
class PubSubManager {
|
|
30
|
+
get db() {
|
|
31
|
+
return this.dboBuilder.db;
|
|
32
|
+
}
|
|
33
|
+
get dbo() {
|
|
34
|
+
return this.dboBuilder.dbo;
|
|
35
|
+
}
|
|
36
|
+
constructor(options) {
|
|
37
|
+
this.onSchemaChange = undefined;
|
|
38
|
+
this.NOTIF_TYPE = {
|
|
39
|
+
data: "data_has_changed",
|
|
40
|
+
schema: "schema_has_changed"
|
|
41
|
+
};
|
|
42
|
+
this.NOTIF_CHANNEL = {
|
|
43
|
+
preffix: 'prostgles_',
|
|
44
|
+
getFull: (appID) => {
|
|
45
|
+
const finalAppId = appID ?? this.appID;
|
|
46
|
+
if (!finalAppId)
|
|
47
|
+
throw "No appID";
|
|
48
|
+
return this.NOTIF_CHANNEL.preffix + finalAppId;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
this.appCheckFrequencyMS = 10 * 1000;
|
|
52
|
+
this.destroyed = false;
|
|
53
|
+
this.destroy = () => {
|
|
54
|
+
this.destroyed = true;
|
|
55
|
+
if (this.appCheck) {
|
|
56
|
+
clearInterval(this.appCheck);
|
|
57
|
+
}
|
|
58
|
+
this.onSocketDisconnected();
|
|
59
|
+
// if(this.postgresNotifListenManager){
|
|
60
|
+
// this.postgresNotifListenManager.stopListening();
|
|
61
|
+
// }
|
|
62
|
+
if (!this.postgresNotifListenManager)
|
|
63
|
+
throw "this.postgresNotifListenManager missing";
|
|
64
|
+
this.postgresNotifListenManager.destroy();
|
|
65
|
+
};
|
|
66
|
+
this.canContinue = () => {
|
|
67
|
+
if (this.destroyed) {
|
|
68
|
+
console.trace("Could not start destroyed instance");
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
this.appChecking = false;
|
|
74
|
+
this.init = initPubSubManager_1.initPubSubManager.bind(this);
|
|
75
|
+
this.DB_OBJ_NAMES = {
|
|
76
|
+
trigger_add_remove_func: "prostgles.trigger_add_remove_func",
|
|
77
|
+
data_watch_func: "prostgles.prostgles_trigger_function",
|
|
78
|
+
schema_watch_func: "prostgles.schema_watch_func",
|
|
79
|
+
schema_watch_trigger: "prostgles_schema_watch_trigger_new"
|
|
80
|
+
};
|
|
81
|
+
this.prepareTriggers = async () => {
|
|
82
|
+
// SELECT * FROM pg_catalog.pg_event_trigger WHERE evtname
|
|
83
|
+
if (!this.appID)
|
|
84
|
+
throw "prepareTriggers failed: this.appID missing";
|
|
85
|
+
if (this.dboBuilder.prostgles.opts.watchSchema && !(await (0, Prostgles_1.isSuperUser)(this.db))) {
|
|
86
|
+
console.warn("prostgles watchSchema requires superuser db user. Will not watch using event triggers");
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await this.db.any(`
|
|
90
|
+
BEGIN;-- ISOLATION LEVEL SERIALIZABLE;
|
|
91
|
+
|
|
92
|
+
/** ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
|
|
93
|
+
* Drop stale triggers
|
|
94
|
+
* */
|
|
95
|
+
DO
|
|
96
|
+
$do$
|
|
97
|
+
DECLARE trg RECORD;
|
|
98
|
+
q TEXT;
|
|
99
|
+
ev_trg_needed BOOLEAN := FALSE;
|
|
100
|
+
ev_trg_exists BOOLEAN := FALSE;
|
|
101
|
+
is_super_user BOOLEAN := FALSE;
|
|
102
|
+
BEGIN
|
|
103
|
+
--SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
|
104
|
+
|
|
105
|
+
LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
|
|
106
|
+
EXECUTE format(
|
|
107
|
+
$q$
|
|
108
|
+
|
|
109
|
+
CREATE TEMP TABLE %1$I AS --ON COMMIT DROP AS
|
|
110
|
+
SELECT * FROM prostgles.app_triggers;
|
|
111
|
+
|
|
112
|
+
DELETE FROM prostgles.app_triggers;
|
|
113
|
+
|
|
114
|
+
INSERT INTO prostgles.app_triggers
|
|
115
|
+
SELECT * FROM %1$I;
|
|
116
|
+
|
|
117
|
+
DROP TABLE IF EXISTS %1$I;
|
|
118
|
+
$q$,
|
|
119
|
+
${(0, exports.asValue)('triggers_' + this.appID)}
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
is_super_user := EXISTS (select 1 from pg_user where usename = CURRENT_USER AND usesuper IS TRUE);
|
|
123
|
+
/**
|
|
124
|
+
* Delete stale app records
|
|
125
|
+
* */
|
|
126
|
+
DELETE FROM prostgles.apps
|
|
127
|
+
WHERE last_check < NOW() - 8 * check_frequency_ms * interval '1 millisecond';
|
|
128
|
+
|
|
129
|
+
DELETE FROM prostgles.app_triggers
|
|
130
|
+
WHERE app_id NOT IN (SELECT id FROM prostgles.apps);
|
|
131
|
+
|
|
132
|
+
/* DROP the old buggy schema watch trigger */
|
|
133
|
+
IF EXISTS (
|
|
134
|
+
SELECT 1 FROM pg_catalog.pg_event_trigger
|
|
135
|
+
WHERE evtname = 'prostgles_schema_watch_trigger'
|
|
136
|
+
) AND is_super_user IS TRUE
|
|
137
|
+
THEN
|
|
138
|
+
DROP EVENT TRIGGER IF EXISTS prostgles_schema_watch_trigger;
|
|
139
|
+
END IF;
|
|
140
|
+
|
|
141
|
+
ev_trg_needed := EXISTS (SELECT 1 FROM prostgles.apps WHERE watching_schema IS TRUE);
|
|
142
|
+
ev_trg_exists := EXISTS (
|
|
143
|
+
SELECT 1 FROM pg_catalog.pg_event_trigger
|
|
144
|
+
WHERE evtname = ${(0, exports.asValue)(this.DB_OBJ_NAMES.schema_watch_trigger)}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
-- RAISE NOTICE ' ev_trg_needed %, ev_trg_exists %', ev_trg_needed, ev_trg_exists;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* DROP stale event trigger
|
|
151
|
+
* */
|
|
152
|
+
IF is_super_user IS TRUE AND ev_trg_needed IS FALSE AND ev_trg_exists IS TRUE THEN
|
|
153
|
+
|
|
154
|
+
SELECT format(
|
|
155
|
+
$$ DROP EVENT TRIGGER IF EXISTS %I ; $$
|
|
156
|
+
, ${(0, exports.asValue)(this.DB_OBJ_NAMES.schema_watch_trigger)}
|
|
157
|
+
)
|
|
158
|
+
INTO q;
|
|
159
|
+
|
|
160
|
+
--RAISE NOTICE ' DROP EVENT TRIGGER %', q;
|
|
161
|
+
|
|
162
|
+
EXECUTE q;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* CREATE event trigger
|
|
166
|
+
* */
|
|
167
|
+
ELSIF
|
|
168
|
+
is_super_user IS TRUE
|
|
169
|
+
AND ev_trg_needed IS TRUE
|
|
170
|
+
AND ev_trg_exists IS FALSE
|
|
171
|
+
THEN
|
|
172
|
+
|
|
173
|
+
DROP EVENT TRIGGER IF EXISTS ${this.DB_OBJ_NAMES.schema_watch_trigger};
|
|
174
|
+
CREATE EVENT TRIGGER ${this.DB_OBJ_NAMES.schema_watch_trigger} ON ddl_command_end
|
|
175
|
+
WHEN TAG IN ('COMMENT', 'CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW', 'CREATE TABLE AS', 'SELECT INTO')
|
|
176
|
+
--WHEN TAG IN ('CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE TRIGGER', 'DROP TRIGGER')
|
|
177
|
+
EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.schema_watch_func}();
|
|
178
|
+
|
|
179
|
+
--RAISE NOTICE ' CREATED EVENT TRIGGER %', q;
|
|
180
|
+
END IF;
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
END
|
|
184
|
+
$do$;
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
COMMIT;
|
|
188
|
+
`).catch(e => {
|
|
189
|
+
console.error("prepareTriggers failed: ", e);
|
|
190
|
+
throw e;
|
|
191
|
+
});
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
catch (e) {
|
|
195
|
+
console.error("prepareTriggers failed: ", e);
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
/* Relay relevant data to relevant subscriptions */
|
|
200
|
+
this.notifListener = async (data) => {
|
|
201
|
+
const str = data.payload;
|
|
202
|
+
if (!str) {
|
|
203
|
+
console.error("Empty notif?");
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const dataArr = str.split(PubSubManager.DELIMITER), notifType = dataArr[0];
|
|
207
|
+
(0, exports.log)(str);
|
|
208
|
+
if (notifType === this.NOTIF_TYPE.schema) {
|
|
209
|
+
if (this.onSchemaChange) {
|
|
210
|
+
const command = dataArr[1], event_type = dataArr[2], query = dataArr[3];
|
|
211
|
+
if (query) {
|
|
212
|
+
this.onSchemaChange({ command, query });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (notifType !== this.NOTIF_TYPE.data) {
|
|
218
|
+
console.error("Unexpected notif type: ", notifType);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const table_name = dataArr[1], op_name = dataArr[2], condition_ids_str = dataArr[3];
|
|
222
|
+
// const triggers = await this.db.any("SELECT * FROM prostgles.triggers WHERE table_name = $1 AND id IN ($2:csv)", [table_name, condition_ids_str.split(",").map(v => +v)]);
|
|
223
|
+
// const conditions: string[] = triggers.map(t => t.condition);
|
|
224
|
+
(0, exports.log)("PG Trigger ->", dataArr.join("__"));
|
|
225
|
+
if (condition_ids_str && condition_ids_str.startsWith("error") &&
|
|
226
|
+
this._triggers && this._triggers[table_name] && this._triggers[table_name].length) {
|
|
227
|
+
const pref = "INTERNAL ERROR. Schema might have changed";
|
|
228
|
+
console.error(`${pref}: ${condition_ids_str}`);
|
|
229
|
+
this._triggers[table_name].map(c => {
|
|
230
|
+
const subs = this.getSubs(table_name, c);
|
|
231
|
+
subs.map(s => {
|
|
232
|
+
this.pushSubData(s, pref + ". Check server logs");
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
else if (condition_ids_str?.split(",").length &&
|
|
237
|
+
condition_ids_str?.split(",").every((c) => Number.isInteger(+c)) &&
|
|
238
|
+
this._triggers?.[table_name]?.length) {
|
|
239
|
+
const idxs = condition_ids_str.split(",").map(v => +v);
|
|
240
|
+
const conditions = this._triggers[table_name].filter((c, i) => idxs.includes(i));
|
|
241
|
+
(0, exports.log)("PG Trigger -> ", { table_name, op_name, condition_ids_str, conditions }, this._triggers[table_name]);
|
|
242
|
+
conditions.map(condition => {
|
|
243
|
+
const subs = this.getSubs(table_name, condition);
|
|
244
|
+
const syncs = this.getSyncs(table_name, condition);
|
|
245
|
+
syncs.map((s) => {
|
|
246
|
+
this.syncData(s, undefined, "trigger");
|
|
247
|
+
});
|
|
248
|
+
if (!subs) {
|
|
249
|
+
// console.error(`sub missing for ${table_name} ${condition}`, this.triggers);
|
|
250
|
+
// console.log(this.subs)
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
/* Throttle the subscriptions */
|
|
254
|
+
for (var i = 0; i < subs.length; i++) {
|
|
255
|
+
var sub = subs[i];
|
|
256
|
+
if (this.dbo[sub.table_name] &&
|
|
257
|
+
sub.is_ready &&
|
|
258
|
+
(sub.socket_id && this.sockets[sub.socket_id]) || sub.func) {
|
|
259
|
+
const throttle = sub.throttle || 0;
|
|
260
|
+
if (sub.last_throttled <= Date.now() - throttle) {
|
|
261
|
+
/* It is assumed the policy was checked before this point */
|
|
262
|
+
this.pushSubData(sub);
|
|
263
|
+
// sub.last_throttled = Date.now();
|
|
264
|
+
}
|
|
265
|
+
else if (!sub.is_throttling) {
|
|
266
|
+
(0, exports.log)("throttling sub");
|
|
267
|
+
sub.is_throttling = setTimeout(() => {
|
|
268
|
+
(0, exports.log)("throttling finished. pushSubData...");
|
|
269
|
+
sub.is_throttling = null;
|
|
270
|
+
this.pushSubData(sub);
|
|
271
|
+
}, throttle); // sub.throttle);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
// if(!this._triggers || !this._triggers[table_name] || !this._triggers[table_name].length){
|
|
279
|
+
// console.warn(190, "Trigger sub not found. DROPPING TRIGGER", table_name, condition_ids_str, this._triggers);
|
|
280
|
+
// this.dropTrigger(table_name);
|
|
281
|
+
// } else {
|
|
282
|
+
// }
|
|
283
|
+
console.warn(190, "Trigger sub issue: ", table_name, condition_ids_str, this._triggers);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
this.getActiveListeners = () => {
|
|
287
|
+
let result = [];
|
|
288
|
+
const upsert = (t, c) => {
|
|
289
|
+
if (!result.find(r => r.table_name === t && r.condition === c)) {
|
|
290
|
+
result.push({ table_name: t, condition: c });
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
(this.syncs || []).map(s => {
|
|
294
|
+
upsert(s.table_name, s.condition);
|
|
295
|
+
});
|
|
296
|
+
Object.keys(this.subs || {}).map(table_name => {
|
|
297
|
+
Object.keys(this.subs[table_name] || {}).map(condition => {
|
|
298
|
+
if (this.subs[table_name][condition].subs.length) {
|
|
299
|
+
upsert(table_name, condition);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
return result;
|
|
304
|
+
};
|
|
305
|
+
this.checkIfTimescaleBug = async (table_name) => {
|
|
306
|
+
const schema = "_timescaledb_catalog", res = await this.db.oneOrNone("SELECT EXISTS( \
|
|
307
|
+
SELECT * \
|
|
308
|
+
FROM information_schema.tables \
|
|
309
|
+
WHERE 1 = 1 \
|
|
310
|
+
AND table_schema = ${schema} \
|
|
311
|
+
AND table_name = 'hypertable' \
|
|
312
|
+
);", { schema });
|
|
313
|
+
if (res.exists) {
|
|
314
|
+
let isHyperTable = await this.db.any("SELECT * FROM " + (0, prostgles_types_1.asName)(schema) + ".hypertable WHERE table_name = ${table_name};", { table_name, schema });
|
|
315
|
+
if (isHyperTable && isHyperTable.length) {
|
|
316
|
+
throw "Triggers do not work on timescaledb hypertables due to bug:\nhttps://github.com/timescale/timescaledb/issues/1084";
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return true;
|
|
320
|
+
};
|
|
321
|
+
/*
|
|
322
|
+
A table will only have a trigger with all conditions (for different subs)
|
|
323
|
+
conditions = ["user_id = 1"]
|
|
324
|
+
fields = ["user_id"]
|
|
325
|
+
*/
|
|
326
|
+
this.getMyTriggerQuery = async () => {
|
|
327
|
+
return pgp.as.format(`
|
|
328
|
+
SELECT * --, ROW_NUMBER() OVER(PARTITION BY table_name ORDER BY table_name, condition ) - 1 as id
|
|
329
|
+
FROM prostgles.v_triggers
|
|
330
|
+
WHERE app_id = $1
|
|
331
|
+
ORDER BY table_name, condition
|
|
332
|
+
`, [this.appID]);
|
|
333
|
+
};
|
|
334
|
+
this.addTriggerPool = undefined;
|
|
335
|
+
const { wsChannelNamePrefix, pgChannelName, onSchemaChange, dboBuilder } = options;
|
|
336
|
+
if (!dboBuilder.db || !dboBuilder.dbo) {
|
|
337
|
+
throw 'MISSING: db_pg, db';
|
|
338
|
+
}
|
|
339
|
+
this.onSchemaChange = onSchemaChange;
|
|
340
|
+
this.dboBuilder = dboBuilder;
|
|
341
|
+
this.sockets = {};
|
|
342
|
+
this.subs = {};
|
|
343
|
+
this.syncs = [];
|
|
344
|
+
this.socketChannelPreffix = wsChannelNamePrefix || "_psqlWS_";
|
|
345
|
+
(0, exports.log)("Created PubSubManager");
|
|
346
|
+
}
|
|
347
|
+
isReady() {
|
|
348
|
+
if (!this.postgresNotifListenManager)
|
|
349
|
+
throw "this.postgresNotifListenManager missing";
|
|
350
|
+
return this.postgresNotifListenManager.isListening();
|
|
351
|
+
}
|
|
352
|
+
getSubs(table_name, condition) {
|
|
353
|
+
return this.subs?.[table_name]?.[condition]?.subs;
|
|
354
|
+
}
|
|
355
|
+
getSyncs(table_name, condition) {
|
|
356
|
+
return (this.syncs || [])
|
|
357
|
+
.filter((s) => s.table_name === table_name && s.condition === condition);
|
|
358
|
+
}
|
|
359
|
+
pushSubData(sub, err) {
|
|
360
|
+
if (!sub)
|
|
361
|
+
throw "pushSubData: invalid sub";
|
|
362
|
+
const { table_name, filter, params, table_rules, socket_id, channel_name, func } = sub; //, subOne = false
|
|
363
|
+
sub.last_throttled = Date.now();
|
|
364
|
+
if (err) {
|
|
365
|
+
if (socket_id) {
|
|
366
|
+
this.sockets[socket_id].emit(channel_name, { err });
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
return new Promise(async (resolve, reject) => {
|
|
371
|
+
/* TODO: Retire subOne -> it's redundant */
|
|
372
|
+
// this.dbo[table_name][subOne? "findOne" : "find"](filter, params, null, table_rules)
|
|
373
|
+
if (!this.dbo?.[table_name]?.find) {
|
|
374
|
+
throw new Error(`1107 this.dbo.${table_name}.find`);
|
|
375
|
+
}
|
|
376
|
+
this.dbo?.[table_name]?.find?.(filter, params, undefined, table_rules)
|
|
377
|
+
.then(data => {
|
|
378
|
+
if (socket_id && this.sockets[socket_id]) {
|
|
379
|
+
(0, exports.log)("Pushed " + data.length + " records to sub");
|
|
380
|
+
this.sockets[socket_id].emit(channel_name, { data }, () => {
|
|
381
|
+
resolve(data);
|
|
382
|
+
});
|
|
383
|
+
/* TO DO: confirm receiving data or server will unsubscribe
|
|
384
|
+
{ data }, (cb)=> { console.log(cb) });
|
|
385
|
+
*/
|
|
386
|
+
}
|
|
387
|
+
else if (func) {
|
|
388
|
+
func(data);
|
|
389
|
+
resolve(data);
|
|
390
|
+
}
|
|
391
|
+
sub.last_throttled = Date.now();
|
|
392
|
+
}).catch(err => {
|
|
393
|
+
const errObj = { _err_msg: err.toString(), err };
|
|
394
|
+
if (socket_id && this.sockets[socket_id]) {
|
|
395
|
+
this.sockets[socket_id].emit(channel_name, { err: errObj });
|
|
396
|
+
}
|
|
397
|
+
else if (func) {
|
|
398
|
+
func({ err: errObj });
|
|
399
|
+
}
|
|
400
|
+
reject(errObj);
|
|
401
|
+
});
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
upsertSocket(socket, channel_name) {
|
|
405
|
+
if (socket && !this.sockets[socket.id]) {
|
|
406
|
+
this.sockets[socket.id] = socket;
|
|
407
|
+
socket.on("disconnect", () => this.onSocketDisconnected(socket));
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async syncData(sync, clientData, source) {
|
|
411
|
+
return await (0, SyncReplication_1.syncData)(this, sync, clientData, source);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Returns a sync channel
|
|
415
|
+
* A sync channel is unique per socket for each filter
|
|
416
|
+
*/
|
|
417
|
+
async addSync(syncParams) {
|
|
418
|
+
const { socket = null, table_info = null, table_rules, synced_field = null, allow_delete = false, id_fields = [], filter = {}, params, condition = "", throttle = 0 } = syncParams || {};
|
|
419
|
+
let conditionParsed = parseCondition(condition);
|
|
420
|
+
if (!socket || !table_info)
|
|
421
|
+
throw "socket or table_info missing";
|
|
422
|
+
const { name: table_name } = table_info, channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
|
|
423
|
+
if (!synced_field)
|
|
424
|
+
throw "synced_field missing from table_rules";
|
|
425
|
+
this.upsertSocket(socket, channel_name);
|
|
426
|
+
const upsertSync = () => {
|
|
427
|
+
let newSync = {
|
|
428
|
+
channel_name,
|
|
429
|
+
table_name,
|
|
430
|
+
filter,
|
|
431
|
+
condition: conditionParsed,
|
|
432
|
+
synced_field,
|
|
433
|
+
id_fields,
|
|
434
|
+
allow_delete,
|
|
435
|
+
table_rules,
|
|
436
|
+
throttle: Math.max(throttle || 0, table_rules?.sync?.throttle || 0),
|
|
437
|
+
batch_size: (0, utils_1.get)(table_rules, "sync.batch_size") || exports.DEFAULT_SYNC_BATCH_SIZE,
|
|
438
|
+
last_throttled: 0,
|
|
439
|
+
socket_id: socket.id,
|
|
440
|
+
is_sync: true,
|
|
441
|
+
last_synced: 0,
|
|
442
|
+
lr: undefined,
|
|
443
|
+
table_info,
|
|
444
|
+
is_syncing: false,
|
|
445
|
+
wal: undefined,
|
|
446
|
+
socket,
|
|
447
|
+
params
|
|
448
|
+
};
|
|
449
|
+
/* Only a sync per socket per table per condition allowed */
|
|
450
|
+
this.syncs = this.syncs || [];
|
|
451
|
+
let existing = this.syncs.find(s => s.socket_id === socket.id && s.channel_name === channel_name);
|
|
452
|
+
if (!existing) {
|
|
453
|
+
this.syncs.push(newSync);
|
|
454
|
+
// console.log("Added SYNC");
|
|
455
|
+
socket.removeAllListeners(channel_name + "unsync");
|
|
456
|
+
socket.once(channel_name + "unsync", (_data, cb) => {
|
|
457
|
+
this.onSocketDisconnected(socket, channel_name);
|
|
458
|
+
cb(null, { res: "ok" });
|
|
459
|
+
});
|
|
460
|
+
socket.removeAllListeners(channel_name);
|
|
461
|
+
socket.on(channel_name, (data, cb) => {
|
|
462
|
+
if (!data) {
|
|
463
|
+
cb({ err: "Unexpected request. Need data or onSyncRequest" });
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
/*
|
|
467
|
+
*/
|
|
468
|
+
/* Server will:
|
|
469
|
+
1. Ask for last_synced emit(onSyncRequest)
|
|
470
|
+
2. Ask for data >= server_synced emit(onPullRequest)
|
|
471
|
+
-> Upsert that data
|
|
472
|
+
2. Push data >= last_synced emit(data.data)
|
|
473
|
+
|
|
474
|
+
Client will:
|
|
475
|
+
1. Send last_synced on(onSyncRequest)
|
|
476
|
+
2. Send data >= server_synced on(onPullRequest)
|
|
477
|
+
3. Send data on CRUD emit(data.data | data.deleted)
|
|
478
|
+
4. Upsert data.data | deleted on(data.data | data.deleted)
|
|
479
|
+
*/
|
|
480
|
+
// if(data.data){
|
|
481
|
+
// console.error("THIS SHOUKD NEVER FIRE !! NEW DATA FROM SYNC");
|
|
482
|
+
// this.upsertClientData(newSync, data.data);
|
|
483
|
+
// } else
|
|
484
|
+
if (data.onSyncRequest) {
|
|
485
|
+
// console.log("syncData from socket")
|
|
486
|
+
this.syncData(newSync, data.onSyncRequest, "client");
|
|
487
|
+
// console.log("onSyncRequest ", socket._user)
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
console.error("Unexpected sync request data from client: ", data);
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
// socket.emit(channel_name, { onSyncRequest: true }, (response) => {
|
|
494
|
+
// console.log(response)
|
|
495
|
+
// });
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
console.error("UNCLOSED DUPLICATE SYNC FOUND");
|
|
499
|
+
}
|
|
500
|
+
return newSync;
|
|
501
|
+
};
|
|
502
|
+
// const { min_id, max_id, count, max_synced } = params;
|
|
503
|
+
let sync = upsertSync();
|
|
504
|
+
await this.addTrigger({ table_name, condition: conditionParsed });
|
|
505
|
+
return channel_name;
|
|
506
|
+
}
|
|
507
|
+
/* Must return a channel for socket */
|
|
508
|
+
/* The distinct list of channel names must have a corresponding trigger in the database */
|
|
509
|
+
async addSub(subscriptionParams) {
|
|
510
|
+
const { socket, func = null, table_info = null, table_rules, filter = {}, params = {}, condition = "", throttle = 0, //subOne = false,
|
|
511
|
+
viewOptions } = subscriptionParams || {};
|
|
512
|
+
let validated_throttle = subscriptionParams.throttle || 10;
|
|
513
|
+
if ((!socket && !func) || !table_info)
|
|
514
|
+
throw "socket/func or table_info missing";
|
|
515
|
+
const pubThrottle = (0, utils_1.get)(table_rules, ["subscribe", "throttle"]) || 0;
|
|
516
|
+
if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
|
|
517
|
+
validated_throttle = pubThrottle;
|
|
518
|
+
}
|
|
519
|
+
if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
|
|
520
|
+
validated_throttle = throttle;
|
|
521
|
+
}
|
|
522
|
+
const channel_name = `${this.socketChannelPreffix}.${table_info.name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
|
|
523
|
+
this.upsertSocket(socket, channel_name);
|
|
524
|
+
const upsertSub = (newSubData) => {
|
|
525
|
+
const { table_name, condition: _cond, is_ready = false, parentSubParams } = newSubData, condition = parseCondition(_cond), newSub = {
|
|
526
|
+
socket,
|
|
527
|
+
table_name: table_info.name,
|
|
528
|
+
table_info,
|
|
529
|
+
filter,
|
|
530
|
+
params,
|
|
531
|
+
table_rules,
|
|
532
|
+
channel_name,
|
|
533
|
+
parentSubParams,
|
|
534
|
+
func: func ?? undefined,
|
|
535
|
+
socket_id: socket?.id,
|
|
536
|
+
throttle: validated_throttle,
|
|
537
|
+
is_throttling: null,
|
|
538
|
+
last_throttled: 0,
|
|
539
|
+
is_ready,
|
|
540
|
+
};
|
|
541
|
+
this.subs[table_name] = this.subs[table_name] ?? {};
|
|
542
|
+
this.subs[table_name][condition] = this.subs[table_name][condition] ?? { subs: [] };
|
|
543
|
+
this.subs[table_name][condition].subs = this.subs[table_name][condition].subs ?? [];
|
|
544
|
+
// console.log("1034 upsertSub", this.subs)
|
|
545
|
+
const sub_idx = this.subs[table_name][condition].subs.findIndex(s => s.channel_name === channel_name &&
|
|
546
|
+
(socket && s.socket_id === socket.id ||
|
|
547
|
+
func && s.func === func));
|
|
548
|
+
if (sub_idx < 0) {
|
|
549
|
+
this.subs[table_name][condition].subs.push(newSub);
|
|
550
|
+
if (socket) {
|
|
551
|
+
const chnUnsub = channel_name + "unsubscribe";
|
|
552
|
+
socket.removeAllListeners(chnUnsub);
|
|
553
|
+
socket.once(chnUnsub, (_data, cb) => {
|
|
554
|
+
const res = this.onSocketDisconnected(socket, channel_name);
|
|
555
|
+
cb(null, { res });
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
this.subs[table_name][condition].subs[sub_idx] = newSub;
|
|
561
|
+
}
|
|
562
|
+
if (is_ready) {
|
|
563
|
+
this.pushSubData(newSub);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
if (table_info.is_view) {
|
|
567
|
+
if (viewOptions?.relatedTables.length) {
|
|
568
|
+
viewOptions?.relatedTables.map(async (relatedTable) => {
|
|
569
|
+
const params = {
|
|
570
|
+
table_name: relatedTable.tableName,
|
|
571
|
+
condition: relatedTable.condition,
|
|
572
|
+
parentSubParams: {
|
|
573
|
+
...subscriptionParams,
|
|
574
|
+
channel_name
|
|
575
|
+
},
|
|
576
|
+
};
|
|
577
|
+
upsertSub({
|
|
578
|
+
...params,
|
|
579
|
+
is_ready: false
|
|
580
|
+
});
|
|
581
|
+
await this.addTrigger(params);
|
|
582
|
+
upsertSub({
|
|
583
|
+
...params,
|
|
584
|
+
is_ready: true
|
|
585
|
+
});
|
|
586
|
+
});
|
|
587
|
+
return channel_name;
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
throw "PubSubManager: view parent_tables missing";
|
|
591
|
+
}
|
|
592
|
+
/* */
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
/* Just a table, add table + condition trigger */
|
|
596
|
+
// console.log(table_info, 202);
|
|
597
|
+
upsertSub({
|
|
598
|
+
table_name: table_info.name,
|
|
599
|
+
condition: parseCondition(condition),
|
|
600
|
+
parentSubParams: undefined,
|
|
601
|
+
is_ready: false
|
|
602
|
+
});
|
|
603
|
+
await this.addTrigger({
|
|
604
|
+
table_name: table_info.name,
|
|
605
|
+
condition: parseCondition(condition),
|
|
606
|
+
});
|
|
607
|
+
upsertSub({
|
|
608
|
+
table_name: table_info.name,
|
|
609
|
+
condition: parseCondition(condition),
|
|
610
|
+
parentSubParams: undefined,
|
|
611
|
+
is_ready: true
|
|
612
|
+
});
|
|
613
|
+
return channel_name;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
removeLocalSub(table_name, condition, func) {
|
|
617
|
+
let cond = parseCondition(condition);
|
|
618
|
+
if ((0, utils_1.get)(this.subs, [table_name, cond, "subs"])) {
|
|
619
|
+
this.subs[table_name][cond].subs.map((sub, i) => {
|
|
620
|
+
if (sub.func && sub.func === func) {
|
|
621
|
+
this.subs[table_name][cond].subs.splice(i, 1);
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
console.error("Could not unsubscribe. Subscription might not have initialised yet");
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
onSocketDisconnected(socket, channel_name) {
|
|
630
|
+
// process.on('warning', e => {
|
|
631
|
+
// console.warn(e.stack)
|
|
632
|
+
// });
|
|
633
|
+
// console.log("onSocketDisconnected", channel_name, this.syncs)
|
|
634
|
+
if (this.subs) {
|
|
635
|
+
Object.keys(this.subs).map(table_name => {
|
|
636
|
+
Object.keys(this.subs[table_name]).map(condition => {
|
|
637
|
+
this.subs[table_name][condition].subs.map((sub, i) => {
|
|
638
|
+
/**
|
|
639
|
+
* If a channel name is specified then delete triggers
|
|
640
|
+
*/
|
|
641
|
+
if ((socket && sub.socket_id === socket.id) &&
|
|
642
|
+
(!channel_name || sub.channel_name === channel_name)) {
|
|
643
|
+
this.subs[table_name][condition].subs.splice(i, 1);
|
|
644
|
+
if (!this.subs[table_name][condition].subs.length) {
|
|
645
|
+
delete this.subs[table_name][condition];
|
|
646
|
+
if ((0, prostgles_types_1.isEmpty)(this.subs[table_name])) {
|
|
647
|
+
delete this.subs[table_name];
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
if (this.syncs) {
|
|
656
|
+
this.syncs = this.syncs.filter(s => {
|
|
657
|
+
const matchesSocket = Boolean(socket && s.socket_id !== socket.id);
|
|
658
|
+
if (channel_name) {
|
|
659
|
+
return matchesSocket || s.channel_name !== channel_name;
|
|
660
|
+
}
|
|
661
|
+
return matchesSocket;
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
if (!socket) {
|
|
665
|
+
}
|
|
666
|
+
else if (!channel_name) {
|
|
667
|
+
delete this.sockets[socket.id];
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
socket.removeAllListeners(channel_name);
|
|
671
|
+
socket.removeAllListeners(channel_name + "unsync");
|
|
672
|
+
socket.removeAllListeners(channel_name + "unsubscribe");
|
|
673
|
+
}
|
|
674
|
+
return "ok";
|
|
675
|
+
}
|
|
676
|
+
async addTrigger(params, viewOptions) {
|
|
677
|
+
try {
|
|
678
|
+
let { table_name, condition } = { ...params };
|
|
679
|
+
if (!table_name)
|
|
680
|
+
throw "MISSING table_name";
|
|
681
|
+
if (!this.appID)
|
|
682
|
+
throw "MISSING appID";
|
|
683
|
+
if (!condition || !condition.trim().length) {
|
|
684
|
+
condition = "TRUE";
|
|
685
|
+
}
|
|
686
|
+
// console.log(1623, { app_id, addTrigger: { table_name, condition } });
|
|
687
|
+
await this.checkIfTimescaleBug(table_name);
|
|
688
|
+
const trgVals = {
|
|
689
|
+
tbl: (0, exports.asValue)(table_name),
|
|
690
|
+
cond: (0, exports.asValue)(condition),
|
|
691
|
+
};
|
|
692
|
+
await this.db.any(`
|
|
693
|
+
BEGIN WORK;
|
|
694
|
+
LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
|
|
695
|
+
|
|
696
|
+
INSERT INTO prostgles.app_triggers (table_name, condition, app_id, related_view_name, related_view_def)
|
|
697
|
+
VALUES (${trgVals.tbl}, ${trgVals.cond}, ${(0, exports.asValue)(this.appID)}, ${viewOptions?.viewName ?? null}, ${viewOptions?.definition ?? null})
|
|
698
|
+
ON CONFLICT DO NOTHING;
|
|
699
|
+
|
|
700
|
+
COMMIT WORK;
|
|
701
|
+
`);
|
|
702
|
+
(0, exports.log)("addTrigger.. ", { table_name, condition });
|
|
703
|
+
const triggers = await this.db.any(await this.getMyTriggerQuery());
|
|
704
|
+
this._triggers = {};
|
|
705
|
+
triggers.map(t => {
|
|
706
|
+
this._triggers = this._triggers || {};
|
|
707
|
+
this._triggers[t.table_name] = this._triggers[t.table_name] || [];
|
|
708
|
+
if (!this._triggers[t.table_name].includes(t.condition)) {
|
|
709
|
+
this._triggers[t.table_name].push(t.condition);
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
(0, exports.log)("trigger added.. ", { table_name, condition });
|
|
713
|
+
return true;
|
|
714
|
+
// console.log("1612", JSON.stringify(triggers, null, 2))
|
|
715
|
+
// console.log("1613",JSON.stringify(this._triggers, null, 2))
|
|
716
|
+
}
|
|
717
|
+
catch (e) {
|
|
718
|
+
console.trace("Failed adding trigger", e);
|
|
719
|
+
// throw e
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
exports.PubSubManager = PubSubManager;
|
|
724
|
+
_a = PubSubManager;
|
|
725
|
+
PubSubManager.DELIMITER = '|$prstgls$|';
|
|
726
|
+
// ,datname
|
|
727
|
+
// ,usename
|
|
728
|
+
// ,client_hostname
|
|
729
|
+
// ,client_port
|
|
730
|
+
// ,backend_start
|
|
731
|
+
// ,query_start
|
|
732
|
+
// ,query
|
|
733
|
+
// ,state
|
|
734
|
+
// console.log(await _db.any(`
|
|
735
|
+
// SELECT pid, application_name, state
|
|
736
|
+
// FROM pg_stat_activity
|
|
737
|
+
// WHERE application_name IS NOT NULL AND application_name != '' -- state = 'active';
|
|
738
|
+
// `))
|
|
739
|
+
PubSubManager.canCreate = async (db) => {
|
|
740
|
+
const canExecute = await (0, DboBuilder_1.canEXECUTE)(db);
|
|
741
|
+
const isSuperUs = await (0, Prostgles_1.isSuperUser)(db);
|
|
742
|
+
return { canExecute, isSuperUs, yes: canExecute && isSuperUs };
|
|
743
|
+
};
|
|
744
|
+
PubSubManager.create = async (options) => {
|
|
745
|
+
const res = new PubSubManager(options);
|
|
746
|
+
return await res.init();
|
|
747
|
+
};
|
|
748
|
+
PubSubManager.SCHEMA_ALTERING_QUERIES = ['CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW', 'CREATE TABLE AS', 'SELECT INTO'];
|
|
749
|
+
PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID = "prostgles internal query that should be excluded from ";
|
|
750
|
+
const parseCondition = (condition) => Boolean(condition && condition.trim().length) ? condition : "TRUE";
|
|
751
|
+
function omitKeys(obj, exclude) {
|
|
752
|
+
return pickKeys(obj, (0, prostgles_types_1.getKeys)(obj).filter(k => !exclude.includes(k)));
|
|
753
|
+
}
|
|
754
|
+
exports.omitKeys = omitKeys;
|
|
755
|
+
function pickKeys(obj, include = []) {
|
|
756
|
+
let keys = include;
|
|
757
|
+
if (!keys.length) {
|
|
758
|
+
return {};
|
|
759
|
+
}
|
|
760
|
+
if (obj && keys.length) {
|
|
761
|
+
let res = {};
|
|
762
|
+
keys.forEach(k => {
|
|
763
|
+
res[k] = obj[k];
|
|
764
|
+
});
|
|
765
|
+
return res;
|
|
766
|
+
}
|
|
767
|
+
return obj;
|
|
768
|
+
}
|
|
769
|
+
exports.pickKeys = pickKeys;
|
|
770
|
+
//# sourceMappingURL=PubSubManager.js.map
|