prostgles-server 3.0.64 → 3.0.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/dist/AuthHandler.d.ts +11 -11
  2. package/dist/AuthHandler.d.ts.map +1 -1
  3. package/dist/DBSchemaBuilder.d.ts +3 -3
  4. package/dist/DBSchemaBuilder.d.ts.map +1 -1
  5. package/dist/DboBuilder/QueryBuilder/Functions.d.ts +3 -3
  6. package/dist/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
  7. package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts +3 -3
  8. package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler.d.ts +1 -1
  10. package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
  11. package/dist/DboBuilder/ViewHandler.d.ts +5 -4
  12. package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
  13. package/dist/DboBuilder/ViewHandler.js +7 -134
  14. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  15. package/dist/DboBuilder/delete.js +1 -1
  16. package/dist/DboBuilder/delete.js.map +1 -1
  17. package/dist/DboBuilder/insert.js +1 -1
  18. package/dist/DboBuilder/insert.js.map +1 -1
  19. package/dist/DboBuilder/insertDataParse.js +1 -1
  20. package/dist/DboBuilder/insertDataParse.js.map +1 -1
  21. package/dist/DboBuilder/runSQL.js +1 -1
  22. package/dist/DboBuilder/runSQL.js.map +1 -1
  23. package/dist/DboBuilder/subscribe.d.ts +11 -0
  24. package/dist/DboBuilder/subscribe.d.ts.map +1 -0
  25. package/dist/DboBuilder/subscribe.js +190 -0
  26. package/dist/DboBuilder/subscribe.js.map +1 -0
  27. package/dist/DboBuilder/update.js +1 -1
  28. package/dist/DboBuilder/update.js.map +1 -1
  29. package/dist/DboBuilder.d.ts +24 -24
  30. package/dist/DboBuilder.d.ts.map +1 -1
  31. package/dist/DboBuilder.js +1 -1
  32. package/dist/DboBuilder.js.map +1 -1
  33. package/dist/FileManager.d.ts +6 -6
  34. package/dist/FileManager.d.ts.map +1 -1
  35. package/dist/FileManager.js +17 -17
  36. package/dist/FileManager.js.map +1 -1
  37. package/dist/Filtering.d.ts +1 -1
  38. package/dist/Filtering.d.ts.map +1 -1
  39. package/dist/PostgresNotifListenManager.d.ts +1 -1
  40. package/dist/PostgresNotifListenManager.d.ts.map +1 -1
  41. package/dist/Prostgles.d.ts +14 -14
  42. package/dist/Prostgles.d.ts.map +1 -1
  43. package/dist/Prostgles.js +12 -12
  44. package/dist/Prostgles.js.map +1 -1
  45. package/dist/{PubSubManager.d.ts → PubSubManager/PubSubManager.d.ts} +24 -21
  46. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -0
  47. package/dist/PubSubManager/PubSubManager.js +754 -0
  48. package/dist/PubSubManager/PubSubManager.js.map +1 -0
  49. package/dist/PubSubManager/initPubSubManager.d.ts +3 -0
  50. package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -0
  51. package/dist/PubSubManager/initPubSubManager.js +616 -0
  52. package/dist/PubSubManager/initPubSubManager.js.map +1 -0
  53. package/dist/PublishParser.d.ts +32 -32
  54. package/dist/PublishParser.d.ts.map +1 -1
  55. package/dist/PublishParser.js +1 -1
  56. package/dist/PublishParser.js.map +1 -1
  57. package/dist/SchemaWatch.d.ts +1 -1
  58. package/dist/SchemaWatch.d.ts.map +1 -1
  59. package/dist/SyncReplication.d.ts +6 -6
  60. package/dist/SyncReplication.d.ts.map +1 -1
  61. package/dist/SyncReplication.js +1 -1
  62. package/dist/SyncReplication.js.map +1 -1
  63. package/dist/TableConfig.d.ts +21 -21
  64. package/dist/TableConfig.d.ts.map +1 -1
  65. package/dist/TableConfig.js +13 -13
  66. package/dist/TableConfig.js.map +1 -1
  67. package/dist/shortestPath.d.ts +1 -1
  68. package/dist/shortestPath.d.ts.map +1 -1
  69. package/dist/validation.d.ts +9 -9
  70. package/dist/validation.d.ts.map +1 -1
  71. package/dist/validation.js +1 -1
  72. package/dist/validation.js.map +1 -1
  73. package/lib/DboBuilder/ViewHandler.d.ts +4 -3
  74. package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
  75. package/lib/DboBuilder/ViewHandler.js +7 -134
  76. package/lib/DboBuilder/ViewHandler.ts +15 -164
  77. package/lib/DboBuilder/delete.js +1 -1
  78. package/lib/DboBuilder/delete.ts +1 -1
  79. package/lib/DboBuilder/insert.js +1 -1
  80. package/lib/DboBuilder/insert.ts +1 -1
  81. package/lib/DboBuilder/insertDataParse.js +1 -1
  82. package/lib/DboBuilder/insertDataParse.ts +1 -1
  83. package/lib/DboBuilder/runSQL.js +1 -1
  84. package/lib/DboBuilder/runSQL.ts +1 -1
  85. package/lib/DboBuilder/subscribe.d.ts +11 -0
  86. package/lib/DboBuilder/subscribe.d.ts.map +1 -0
  87. package/lib/DboBuilder/subscribe.js +189 -0
  88. package/lib/DboBuilder/subscribe.ts +230 -0
  89. package/lib/DboBuilder/update.js +1 -1
  90. package/lib/DboBuilder/update.ts +1 -1
  91. package/lib/DboBuilder.d.ts +1 -1
  92. package/lib/DboBuilder.d.ts.map +1 -1
  93. package/lib/DboBuilder.js +1 -1
  94. package/lib/DboBuilder.ts +1 -1
  95. package/lib/Prostgles.js +1 -1
  96. package/lib/Prostgles.ts +1 -1
  97. package/lib/{PubSubManager.d.ts → PubSubManager/PubSubManager.d.ts} +20 -17
  98. package/lib/PubSubManager/PubSubManager.d.ts.map +1 -0
  99. package/lib/PubSubManager/PubSubManager.js +776 -0
  100. package/lib/PubSubManager/PubSubManager.ts +998 -0
  101. package/lib/PubSubManager/initPubSubManager.d.ts +3 -0
  102. package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -0
  103. package/lib/PubSubManager/initPubSubManager.js +615 -0
  104. package/lib/PubSubManager/initPubSubManager.ts +630 -0
  105. package/lib/PublishParser.js +1 -1
  106. package/lib/PublishParser.ts +1 -1
  107. package/lib/SyncReplication.d.ts +1 -1
  108. package/lib/SyncReplication.d.ts.map +1 -1
  109. package/lib/SyncReplication.js +1 -1
  110. package/lib/SyncReplication.ts +1 -1
  111. package/lib/TableConfig.js +1 -1
  112. package/lib/TableConfig.ts +1 -1
  113. package/lib/validation.js +1 -1
  114. package/lib/validation.ts +1 -1
  115. package/package.json +6 -5
  116. package/tests/client/PID.txt +1 -1
  117. package/tests/server/package-lock.json +9 -7
  118. package/dist/PubSubManager.d.ts.map +0 -1
  119. package/dist/PubSubManager.js +0 -1398
  120. package/dist/PubSubManager.js.map +0 -1
  121. package/lib/PubSubManager.d.ts.map +0 -1
  122. package/lib/PubSubManager.js +0 -1420
  123. package/lib/PubSubManager.ts +0 -1655
@@ -1,1398 +0,0 @@
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 PostgresNotifListenManager_1 = require("./PostgresNotifListenManager");
10
- const utils_1 = require("./utils");
11
- const DboBuilder_1 = require("./DboBuilder");
12
- const Prostgles_1 = require("./Prostgles");
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
- const REALTIME_TRIGGER_CHECK_QUERY = "prostgles-server internal query used to manage realtime triggers";
18
- let pgp = pgPromise({
19
- promiseLib: Bluebird
20
- });
21
- const asValue = (v) => pgp.as.format("$1", [v]);
22
- exports.asValue = asValue;
23
- exports.DEFAULT_SYNC_BATCH_SIZE = 50;
24
- const log = (...args) => {
25
- if (process.env.TEST_TYPE) {
26
- console.log(...args);
27
- }
28
- };
29
- exports.log = log;
30
- class PubSubManager {
31
- constructor(options) {
32
- this.onSchemaChange = undefined;
33
- this.NOTIF_TYPE = {
34
- data: "data_has_changed",
35
- schema: "schema_has_changed"
36
- };
37
- this.NOTIF_CHANNEL = {
38
- preffix: 'prostgles_',
39
- getFull: (appID) => {
40
- const finalAppId = appID ?? this.appID;
41
- if (!finalAppId)
42
- throw "No appID";
43
- return this.NOTIF_CHANNEL.preffix + finalAppId;
44
- }
45
- };
46
- this.appCheckFrequencyMS = 10 * 1000;
47
- this.destroyed = false;
48
- this.destroy = () => {
49
- this.destroyed = true;
50
- if (this.appCheck) {
51
- clearInterval(this.appCheck);
52
- }
53
- this.onSocketDisconnected();
54
- // if(this.postgresNotifListenManager){
55
- // this.postgresNotifListenManager.stopListening();
56
- // }
57
- if (!this.postgresNotifListenManager)
58
- throw "this.postgresNotifListenManager missing";
59
- this.postgresNotifListenManager.destroy();
60
- };
61
- this.canContinue = () => {
62
- if (this.destroyed) {
63
- console.trace("Could not start destroyed instance");
64
- return false;
65
- }
66
- return true;
67
- };
68
- this.appChecking = false;
69
- this.init = async () => {
70
- if (!this.canContinue())
71
- return undefined;
72
- try {
73
- const schema_version = 5;
74
- const q = `
75
- BEGIN; -- ISOLATION LEVEL SERIALIZABLE;-- TRANSACTION ISOLATION LEVEL SERIALIZABLE;
76
-
77
- --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
78
-
79
- /*
80
- * ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
81
- */
82
-
83
- DO
84
- $do$
85
- BEGIN
86
-
87
- /* Reduce deadlocks */
88
- PERFORM pg_sleep(random());
89
-
90
- /* Drop older version */
91
- IF EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles') THEN
92
-
93
- IF
94
- NOT EXISTS (
95
- SELECT 1
96
- FROM information_schema.tables
97
- WHERE table_schema = 'prostgles'
98
- AND table_name = 'versions'
99
- )
100
- THEN
101
- DROP SCHEMA IF EXISTS prostgles CASCADE;
102
- ELSE
103
- IF NOT EXISTS(SELECT 1 FROM prostgles.versions WHERE version >= ${schema_version}) THEN
104
- DROP SCHEMA IF EXISTS prostgles CASCADE;
105
- END IF;
106
- END IF;
107
-
108
- END IF;
109
-
110
-
111
- IF NOT EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles')
112
- THEN
113
- --RAISE NOTICE 'CREATE SCHEMA IF NOT EXISTS prostgles';
114
-
115
- CREATE SCHEMA IF NOT EXISTS prostgles;
116
-
117
- CREATE TABLE IF NOT EXISTS prostgles.versions(
118
- version NUMERIC PRIMARY KEY
119
- );
120
- INSERT INTO prostgles.versions(version) VALUES(${schema_version}) ON CONFLICT DO NOTHING;
121
-
122
- CREATE OR REPLACE FUNCTION prostgles.random_string(length INTEGER DEFAULT 33) RETURNS TEXT AS $$
123
- DECLARE
124
- chars TEXT[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
125
- result TEXT := '';
126
- i INTEGER := 0;
127
- BEGIN
128
- IF length < 0 THEN
129
- RAISE exception 'Given length cannot be less than 0';
130
- END IF;
131
- FOR i IN 1..length LOOP
132
- result := result || chars[1+random()*(array_length(chars, 1)-1)];
133
- END LOOP;
134
- RETURN result;
135
- END;
136
- $$ language plpgsql;
137
- COMMENT ON FUNCTION prostgles.random_string IS 'UUIDs without installing pgcrypto';
138
-
139
-
140
- CREATE OR REPLACE FUNCTION prostgles.debug(VARIADIC args TEXT[]) RETURNS VOID AS $$
141
- BEGIN
142
-
143
- --PERFORM pg_notify('debug', concat_ws(' ', args));
144
- IF
145
- NOT EXISTS (
146
- SELECT 1
147
- FROM information_schema.tables
148
- WHERE table_schema = 'prostgles'
149
- AND table_name = 'debug'
150
- )
151
- THEN
152
- CREATE TABLE IF NOT EXISTS prostgles.debug(m TEXT);
153
- END IF;
154
-
155
- INSERT INTO prostgles.debug(m) VALUES(concat_ws(' ', args));
156
-
157
- END;
158
- $$ LANGUAGE plpgsql;
159
- COMMENT ON FUNCTION prostgles.debug IS 'Used for internal debugging';
160
-
161
-
162
- CREATE TABLE IF NOT EXISTS prostgles.apps (
163
- id TEXT PRIMARY KEY DEFAULT prostgles.random_string(),
164
- added TIMESTAMP DEFAULT NOW(),
165
- application_name TEXT,
166
- last_check TIMESTAMP NOT NULL DEFAULT NOW(),
167
- last_check_ended TIMESTAMP NOT NULL DEFAULT NOW(),
168
- watching_schema BOOLEAN DEFAULT FALSE,
169
- check_frequency_ms INTEGER NOT NULL
170
- );
171
- COMMENT ON TABLE prostgles.apps IS 'Keep track of prostgles server apps connected to db to combine common triggers. Heartbeat used due to no logout triggers in postgres';
172
-
173
- CREATE TABLE IF NOT EXISTS prostgles.app_triggers (
174
- app_id TEXT NOT NULL,
175
- table_name TEXT NOT NULL,
176
- condition TEXT NOT NULL,
177
- inserted TIMESTAMP NOT NULL DEFAULT NOW(),
178
- last_used TIMESTAMP NOT NULL DEFAULT NOW(),
179
- PRIMARY KEY (app_id, table_name, condition) -- This unqique index limits the condition column value to be less than 'SELECT current_setting('block_size');'
180
- );
181
- COMMENT ON TABLE prostgles.app_triggers IS 'Tables and conditions that are currently subscribed/synced';
182
-
183
-
184
- CREATE OR REPLACE VIEW prostgles.v_triggers AS
185
- SELECT *
186
- , (ROW_NUMBER() OVER( ORDER BY table_name, condition ))::text AS id
187
- -- , concat_ws('-', table_name, condition) AS id
188
- , ROW_NUMBER() OVER(PARTITION BY app_id, table_name ORDER BY table_name, condition ) - 1 AS c_id
189
- FROM prostgles.app_triggers;
190
- COMMENT ON VIEW prostgles.v_triggers IS 'Augment trigger table with natural IDs and per app IDs';
191
-
192
-
193
- /*
194
- CREATE OR REPLACE VIEW prostgles.v_triggers_unnested AS
195
- SELECT *
196
- , ROW_NUMBER() OVER(PARTITION BY app_id, table_name ORDER BY table_name, condition ) - 1 AS c_id
197
- FROM (
198
- SELECT *, unnest(app_ids) as app_id
199
- FROM prostgles.v_triggers
200
- ) t;
201
-
202
- -- Force table into cache
203
- IF EXISTS (select * from pg_extension where extname = 'pg_prewarm') THEN
204
- CREATE EXTENSION IF NOT EXISTS pg_prewarm;
205
- PERFORM pg_prewarm('prostgles.app_triggers');
206
- END IF;
207
- */
208
-
209
-
210
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.data_watch_func}() RETURNS TRIGGER
211
- AS $$
212
-
213
- DECLARE t_ids TEXT[];
214
- DECLARE c_ids INTEGER[];
215
- DECLARE err_c_ids INTEGER[];
216
- DECLARE unions TEXT := '';
217
- DECLARE query TEXT := '';
218
- DECLARE nrw RECORD;
219
- DECLARE erw RECORD;
220
- DECLARE has_errors BOOLEAN := FALSE;
221
-
222
- DECLARE err_text TEXT;
223
- DECLARE err_detail TEXT;
224
- DECLARE err_hint TEXT;
225
-
226
- BEGIN
227
-
228
- -- PERFORM pg_notify('debug', concat_ws(' ', 'TABLE', TG_TABLE_NAME, TG_OP));
229
-
230
- SELECT string_agg(
231
- concat_ws(
232
- E' UNION \n ',
233
- CASE WHEN (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN (p1 || ' old_table ' || p2) END,
234
- CASE WHEN (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN (p1 || ' new_table ' || p2) END
235
- ),
236
- E' UNION \n '::text
237
- )
238
- INTO unions
239
- FROM (
240
- SELECT
241
- $z$
242
- SELECT CASE WHEN EXISTS( SELECT 1 FROM
243
- $z$ AS p1,
244
- format(
245
- $c$
246
- as %I WHERE %s ) THEN %s::text END AS t_ids
247
- $c$,
248
- table_name, condition, id
249
- ) AS p2
250
- FROM prostgles.v_triggers
251
- WHERE table_name = TG_TABLE_NAME
252
- ) t;
253
-
254
- /*
255
- PERFORM pg_notify(
256
- ${(0, exports.asValue)(this.NOTIF_CHANNEL.preffix)} || (SELECT id FROM prostgles.apps LIMIT 1) ,
257
- concat_ws(
258
- ${(0, exports.asValue)(PubSubManager.DELIMITER)},
259
-
260
- ${(0, exports.asValue)(this.NOTIF_TYPE.data)},
261
- COALESCE(TG_TABLE_NAME, 'MISSING'),
262
- COALESCE(TG_OP, 'MISSING'),
263
- unions
264
- )
265
- );
266
- RAISE 'unions: % , cids: %', unions, c_ids;
267
- */
268
-
269
- IF unions IS NOT NULL THEN
270
- query = format(
271
- $s$
272
- SELECT ARRAY_AGG(DISTINCT t.t_ids)
273
- FROM ( %s ) t
274
- $s$,
275
- unions
276
- );
277
-
278
- BEGIN
279
- EXECUTE query INTO t_ids;
280
-
281
- --RAISE NOTICE 'trigger fired ok';
282
-
283
- EXCEPTION WHEN OTHERS THEN
284
-
285
- has_errors := TRUE;
286
-
287
- GET STACKED DIAGNOSTICS
288
- err_text = MESSAGE_TEXT,
289
- err_detail = PG_EXCEPTION_DETAIL,
290
- err_hint = PG_EXCEPTION_HINT;
291
-
292
-
293
- END;
294
-
295
- --RAISE NOTICE 'has_errors: % ', has_errors;
296
- --RAISE NOTICE 'unions: % , cids: %', unions, c_ids;
297
-
298
- IF (t_ids IS NOT NULL OR has_errors) THEN
299
-
300
- FOR nrw IN
301
- SELECT app_id, string_agg(c_id::text, ',') as cids
302
- FROM prostgles.v_triggers
303
- WHERE id = ANY(t_ids)
304
- OR has_errors
305
- GROUP BY app_id
306
- LOOP
307
-
308
- PERFORM pg_notify(
309
- ${(0, exports.asValue)(this.NOTIF_CHANNEL.preffix)} || nrw.app_id ,
310
- concat_ws(
311
- ${(0, exports.asValue)(PubSubManager.DELIMITER)},
312
-
313
- ${(0, exports.asValue)(this.NOTIF_TYPE.data)},
314
- COALESCE(TG_TABLE_NAME, 'MISSING'),
315
- COALESCE(TG_OP, 'MISSING'),
316
- CASE WHEN has_errors
317
- THEN concat_ws('; ', 'error', err_text, err_detail, err_hint )
318
- ELSE COALESCE(nrw.cids, '')
319
- END
320
- ${this.dboBuilder.prostgles.opts.DEBUG_MODE ? (", (select json_agg(t)::TEXT FROM (SELECT * from old_table) t), query") : ""}
321
- )
322
- );
323
- END LOOP;
324
-
325
-
326
- IF has_errors THEN
327
-
328
- DELETE FROM prostgles.app_triggers;
329
- RAISE NOTICE 'trigger dropped due to exception: % % %', err_text, err_detail, err_hint;
330
-
331
- END IF;
332
-
333
-
334
- END IF;
335
- END IF;
336
-
337
-
338
- RETURN NULL;
339
-
340
- /*
341
- EXCEPTION WHEN OTHERS THEN
342
- DELETE FROM prostgles.app_triggers; -- delete all or will need to loop through all conditions to find issue;
343
- RAISE NOTICE 'trigger dropped due to exception';
344
- ${"--EXCEPTION_WHEN_COLUMN_WAS_RENAMED_THEN_DROP_TRIGGER"};
345
-
346
-
347
-
348
- RETURN NULL;
349
- */
350
- END;
351
-
352
- --COMMIT;
353
- $$ LANGUAGE plpgsql;
354
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.data_watch_func} IS 'Prostgles internal function used to notify when data in the table changed';
355
-
356
-
357
-
358
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.trigger_add_remove_func}() RETURNS TRIGGER
359
- AS $$
360
-
361
- DECLARE operations TEXT[] := ARRAY['insert', 'update', 'delete'];
362
- DECLARE op TEXT;
363
- DECLARE query TEXT;
364
- DECLARE trw RECORD;
365
-
366
- BEGIN
367
-
368
-
369
- --RAISE NOTICE 'prostgles.app_triggers % ', TG_OP;
370
-
371
- /* If no other listeners on table then DROP triggers */
372
- IF TG_OP = 'DELETE' THEN
373
-
374
- --RAISE NOTICE 'DELETE trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.app_triggers) , ' 0 ');
375
- --RAISE NOTICE 'DELETE trigger_add_remove_func old_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM old_table), ' 0 ');
376
-
377
-
378
- /* Drop actual triggers if needed */
379
- FOR trw IN
380
- SELECT DISTINCT table_name FROM old_table ot
381
- WHERE NOT EXISTS (
382
- SELECT 1 FROM prostgles.app_triggers t
383
- WHERE t.table_name = ot.table_name
384
- )
385
- LOOP
386
-
387
- FOREACH op IN ARRAY operations
388
- LOOP
389
- --RAISE NOTICE ' DROP DATA TRIGGER FOR: % ', trw.table_name;
390
- EXECUTE format(' DROP TRIGGER IF EXISTS %I ON %I ;' , 'prostgles_triggers_' || trw.table_name || '_' || op, trw.table_name);
391
- END LOOP;
392
-
393
- END LOOP;
394
-
395
- /* If newly added listeners on table then CREATE triggers */
396
- ELSIF TG_OP = 'INSERT' THEN
397
-
398
-
399
- --RAISE NOTICE 'INSERT trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.triggers) , ' 0 ');
400
- --RAISE NOTICE 'INSERT trigger_add_remove_func new_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM new_table), ' 0 ');
401
-
402
- /* Loop through newly added tables */
403
- FOR trw IN
404
-
405
- SELECT DISTINCT table_name
406
- FROM new_table nt
407
-
408
- /* Table did not exist prior to this insert */
409
- WHERE NOT EXISTS (
410
- SELECT 1
411
- FROM prostgles.app_triggers t
412
- WHERE t.table_name = nt.table_name
413
- AND t.inserted < nt.inserted -- exclude current record (this is an after trigger). Turn into before trigger?
414
- )
415
-
416
- /* Table is valid */
417
- AND EXISTS (
418
- SELECT 1
419
- FROM information_schema.tables
420
- WHERE table_schema = 'public'
421
- AND table_name = nt.table_name
422
- )
423
- LOOP
424
-
425
- /*
426
- RAISE NOTICE ' CREATE DATA TRIGGER FOR: % TABLE EXISTS?', trw.table_name, SELECT EXISTS (
427
- SELECT 1
428
- FROM information_schema.tables
429
- WHERE table_schema = 'public'
430
- AND table_name = nt.table_name
431
- );
432
- */
433
-
434
- query := format(
435
- $q$
436
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
437
- CREATE TRIGGER %1$I
438
- AFTER INSERT ON %2$I
439
- REFERENCING NEW TABLE AS new_table
440
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
441
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
442
- $q$,
443
- 'prostgles_triggers_' || trw.table_name || '_insert', trw.table_name
444
- ) || format(
445
- $q$
446
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
447
- CREATE TRIGGER %1$I
448
- AFTER UPDATE ON %2$I
449
- REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
450
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
451
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
452
- $q$,
453
- 'prostgles_triggers_' || trw.table_name || '_update', trw.table_name
454
- ) || format(
455
- $q$
456
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
457
- CREATE TRIGGER %1$I
458
- AFTER DELETE ON %2$I
459
- REFERENCING OLD TABLE AS old_table
460
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
461
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
462
- $q$,
463
- 'prostgles_triggers_' || trw.table_name || '_delete', trw.table_name
464
- );
465
-
466
- --RAISE NOTICE ' % ', query;
467
-
468
-
469
- query := format(
470
- $q$
471
- DO $e$
472
- BEGIN
473
-
474
- IF EXISTS (
475
- SELECT 1
476
- FROM information_schema.tables
477
- WHERE table_schema = 'public'
478
- AND table_name = %L
479
- ) THEN
480
-
481
- %s
482
-
483
- END IF;
484
-
485
- END $e$;
486
- $q$,
487
- trw.table_name,
488
- query
489
- ) ;
490
-
491
-
492
- EXECUTE query;
493
-
494
- END LOOP;
495
-
496
- END IF;
497
-
498
-
499
- RETURN NULL;
500
- END;
501
-
502
- $$ LANGUAGE plpgsql;
503
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.trigger_add_remove_func} IS 'Used to add/remove table watch triggers concurrently ';
504
-
505
- DROP TRIGGER IF EXISTS prostgles_triggers_insert ON prostgles.app_triggers;
506
- CREATE TRIGGER prostgles_triggers_insert
507
- AFTER INSERT ON prostgles.app_triggers
508
- REFERENCING NEW TABLE AS new_table
509
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.trigger_add_remove_func}();
510
-
511
- DROP TRIGGER IF EXISTS prostgles_triggers_delete ON prostgles.app_triggers;
512
- CREATE TRIGGER prostgles_triggers_delete
513
- AFTER DELETE ON prostgles.app_triggers
514
- REFERENCING OLD TABLE AS old_table
515
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.trigger_add_remove_func}();
516
-
517
-
518
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.schema_watch_func}() RETURNS event_trigger AS $$
519
-
520
- DECLARE curr_query TEXT := '';
521
- DECLARE arw RECORD;
522
-
523
- BEGIN
524
-
525
- --RAISE NOTICE 'SCHEMA_WATCH: %', tg_tag;
526
-
527
- /*
528
- This event trigger will outlive a prostgles app instance.
529
- Must ensure it only fires if an app instance is running
530
- */
531
- IF
532
- EXISTS (
533
- SELECT 1
534
- FROM information_schema.tables
535
- WHERE table_schema = 'prostgles'
536
- AND table_name = 'apps'
537
- )
538
- THEN
539
-
540
- SELECT LEFT(COALESCE(current_query(), ''), 5000)
541
- INTO curr_query;
542
-
543
- FOR arw IN
544
- SELECT * FROM prostgles.apps WHERE watching_schema IS TRUE
545
-
546
- LOOP
547
- PERFORM pg_notify(
548
- ${(0, exports.asValue)(this.NOTIF_CHANNEL.preffix)} || arw.id,
549
- concat_ws(
550
- ${(0, exports.asValue)(PubSubManager.DELIMITER)},
551
- ${(0, exports.asValue)(this.NOTIF_TYPE.schema)}, tg_tag , TG_event, curr_query
552
- )
553
- );
554
- END LOOP;
555
-
556
- END IF;
557
-
558
- END;
559
- $$ LANGUAGE plpgsql;
560
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.schema_watch_func} IS 'Prostgles internal function used to notify when schema has changed';
561
-
562
- END IF;
563
-
564
- END
565
- $do$;
566
-
567
-
568
- COMMIT;
569
- `;
570
- // const prgl_exists = await this.db.oneOrNone(`
571
- // DROP SCHEMA IF EXISTS prostgles CASCADE;
572
- // SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles'
573
- // `);
574
- // if(!prgl_exists){
575
- // await this.db.any(q);
576
- // }
577
- await this.db.any(q);
578
- if (!this.canContinue())
579
- return;
580
- /* Prepare App id */
581
- if (!this.appID) {
582
- const raw = await this.db.one("INSERT INTO prostgles.apps (check_frequency_ms, watching_schema, application_name) VALUES($1, $2, current_setting('application_name')) RETURNING *; ", [this.appCheckFrequencyMS, Boolean(this.onSchemaChange)]);
583
- this.appID = raw.id;
584
- if (!this.appCheck) {
585
- this.appCheck = setInterval(async () => {
586
- let appQ = "";
587
- try { // drop owned by api
588
- this.appChecking = true;
589
- let trgUpdateLastUsed = "", listeners = this.getActiveListeners();
590
- if (listeners.length) {
591
- trgUpdateLastUsed = `
592
- UPDATE prostgles.app_triggers
593
- SET last_used = CASE WHEN (table_name, condition) IN (
594
- ${listeners.map(l => ` ( ${(0, exports.asValue)(l.table_name)}, ${(0, exports.asValue)(l.condition)} ) `).join(", ")}
595
- ) THEN NOW() ELSE last_used END
596
- WHERE app_id = ${(0, exports.asValue)(this.appID)};
597
- `;
598
- }
599
- appQ = `
600
-
601
- DO $$
602
- BEGIN
603
-
604
- /* ${REALTIME_TRIGGER_CHECK_QUERY} */
605
- /* prostgles schema must exist */
606
- IF
607
- EXISTS (
608
- SELECT 1
609
- FROM information_schema.tables
610
- WHERE table_schema = 'prostgles'
611
- AND table_name = 'apps'
612
- )
613
- THEN
614
-
615
-
616
- /* Concurrency control to avoid deadlock
617
- IF NOT EXISTS (
618
- SELECT 1 FROM prostgles.apps
619
- WHERE last_check < last_check_ended
620
- AND last_check_ended > NOW() - interval '5 minutes'
621
- ) THEN
622
- */
623
- UPDATE prostgles.apps
624
- SET last_check = NOW()
625
- WHERE id = ${(0, exports.asValue)(this.appID)};
626
-
627
-
628
-
629
- /* Delete unused triggers. Might deadlock */
630
- IF EXISTS ( SELECT 1 FROM prostgles.app_triggers)
631
-
632
- /* If this is the latest app then proceed
633
- AND (
634
- SELECT id = ${(0, exports.asValue)(this.appID)}
635
- FROM prostgles.apps
636
- ORDER BY last_check DESC
637
- LIMIT 1
638
- ) = TRUE
639
- */
640
-
641
- THEN
642
-
643
- /* TODO: Fixed deadlocks */
644
- --LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
645
-
646
- /* UPDATE currently used triggers */
647
- ${trgUpdateLastUsed}
648
-
649
- /* DELETE stale triggers for current app. Other triggers will be deleted on app startup */
650
- DELETE FROM prostgles.app_triggers
651
- WHERE app_id = ${(0, exports.asValue)(this.appID)}
652
- AND last_used < NOW() - 4 * ${(0, exports.asValue)(this.appCheckFrequencyMS)} * interval '1 millisecond'; -- 10 seconds at the moment
653
-
654
- END IF;
655
-
656
-
657
-
658
- UPDATE prostgles.apps
659
- SET last_check_ended = NOW()
660
- WHERE id = ${(0, exports.asValue)(this.appID)};
661
-
662
- /*
663
- END IF;
664
- */
665
-
666
-
667
- END IF;
668
-
669
- -- must not commit without a lock
670
- --COMMIT;
671
- END $$;
672
- `;
673
- await this.db.any(appQ);
674
- (0, exports.log)("updated last_check");
675
- }
676
- catch (e) {
677
- /** In some cases a query idles and blocks everything else. Terminate all similar queries */
678
- this.db.any("SELECT state, pg_terminate_backend(pid) from pg_stat_activity WHERE query ilike ${qid} and pid <> pg_backend_pid();", { qid: "%" + REALTIME_TRIGGER_CHECK_QUERY + "%" });
679
- /** If this database was dropped then stop interval */
680
- if (e?.code === "3D000") { // && e.message.includes(this.db.$cn.database)
681
- clearInterval(this.appCheck);
682
- }
683
- console.error("appCheck FAILED: \n", e, appQ);
684
- }
685
- this.appChecking = false;
686
- }, 0.8 * this.appCheckFrequencyMS);
687
- }
688
- }
689
- this.postgresNotifListenManager = new PostgresNotifListenManager_1.PostgresNotifListenManager(this.db, this.notifListener, this.NOTIF_CHANNEL.getFull());
690
- await this.prepareTriggers();
691
- return this;
692
- }
693
- catch (e) {
694
- console.error("PubSubManager init failed: ", e);
695
- }
696
- };
697
- this.DB_OBJ_NAMES = {
698
- trigger_add_remove_func: "prostgles.trigger_add_remove_func",
699
- data_watch_func: "prostgles.prostgles_trigger_function",
700
- schema_watch_func: "prostgles.schema_watch_func",
701
- schema_watch_trigger: "prostgles_schema_watch_trigger_new"
702
- };
703
- this.prepareTriggers = async () => {
704
- // SELECT * FROM pg_catalog.pg_event_trigger WHERE evtname
705
- if (!this.appID)
706
- throw "prepareTriggers failed: this.appID missing";
707
- if (this.dboBuilder.prostgles.opts.watchSchema && !(await (0, Prostgles_1.isSuperUser)(this.db))) {
708
- console.warn("prostgles watchSchema requires superuser db user. Will not watch using event triggers");
709
- }
710
- try {
711
- await this.db.any(`
712
- BEGIN;-- ISOLATION LEVEL SERIALIZABLE;
713
-
714
- /** ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
715
- * Drop stale triggers
716
- * */
717
- DO
718
- $do$
719
- DECLARE trg RECORD;
720
- q TEXT;
721
- ev_trg_needed BOOLEAN := FALSE;
722
- ev_trg_exists BOOLEAN := FALSE;
723
- is_super_user BOOLEAN := FALSE;
724
- BEGIN
725
- --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
726
-
727
- LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
728
- EXECUTE format(
729
- $q$
730
-
731
- CREATE TEMP TABLE %1$I AS --ON COMMIT DROP AS
732
- SELECT * FROM prostgles.app_triggers;
733
-
734
- DELETE FROM prostgles.app_triggers;
735
-
736
- INSERT INTO prostgles.app_triggers
737
- SELECT * FROM %1$I;
738
-
739
- DROP TABLE IF EXISTS %1$I;
740
- $q$,
741
- ${(0, exports.asValue)('triggers_' + this.appID)}
742
- );
743
-
744
- is_super_user := EXISTS (select 1 from pg_user where usename = CURRENT_USER AND usesuper IS TRUE);
745
- /**
746
- * Delete stale app records
747
- * */
748
- DELETE FROM prostgles.apps
749
- WHERE last_check < NOW() - 8 * check_frequency_ms * interval '1 millisecond';
750
-
751
- DELETE FROM prostgles.app_triggers
752
- WHERE app_id NOT IN (SELECT id FROM prostgles.apps);
753
-
754
- /* DROP the old buggy schema watch trigger */
755
- IF EXISTS (
756
- SELECT 1 FROM pg_catalog.pg_event_trigger
757
- WHERE evtname = 'prostgles_schema_watch_trigger'
758
- ) AND is_super_user IS TRUE
759
- THEN
760
- DROP EVENT TRIGGER IF EXISTS prostgles_schema_watch_trigger;
761
- END IF;
762
-
763
- ev_trg_needed := EXISTS (SELECT 1 FROM prostgles.apps WHERE watching_schema IS TRUE);
764
- ev_trg_exists := EXISTS (
765
- SELECT 1 FROM pg_catalog.pg_event_trigger
766
- WHERE evtname = ${(0, exports.asValue)(this.DB_OBJ_NAMES.schema_watch_trigger)}
767
- );
768
-
769
- -- RAISE NOTICE ' ev_trg_needed %, ev_trg_exists %', ev_trg_needed, ev_trg_exists;
770
-
771
- /**
772
- * DROP stale event trigger
773
- * */
774
- IF is_super_user IS TRUE AND ev_trg_needed IS FALSE AND ev_trg_exists IS TRUE THEN
775
-
776
- SELECT format(
777
- $$ DROP EVENT TRIGGER IF EXISTS %I ; $$
778
- , ${(0, exports.asValue)(this.DB_OBJ_NAMES.schema_watch_trigger)}
779
- )
780
- INTO q;
781
-
782
- --RAISE NOTICE ' DROP EVENT TRIGGER %', q;
783
-
784
- EXECUTE q;
785
-
786
- /**
787
- * CREATE event trigger
788
- * */
789
- ELSIF
790
- is_super_user IS TRUE
791
- AND ev_trg_needed IS TRUE
792
- AND ev_trg_exists IS FALSE
793
- THEN
794
-
795
- DROP EVENT TRIGGER IF EXISTS ${this.DB_OBJ_NAMES.schema_watch_trigger};
796
- CREATE EVENT TRIGGER ${this.DB_OBJ_NAMES.schema_watch_trigger} ON ddl_command_end
797
- WHEN TAG IN ('COMMENT', 'CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW', 'CREATE TABLE AS', 'SELECT INTO')
798
- --WHEN TAG IN ('CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE TRIGGER', 'DROP TRIGGER')
799
- EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.schema_watch_func}();
800
-
801
- --RAISE NOTICE ' CREATED EVENT TRIGGER %', q;
802
- END IF;
803
-
804
-
805
- END
806
- $do$;
807
-
808
-
809
- COMMIT;
810
- `).catch(e => {
811
- console.error("prepareTriggers failed: ", e);
812
- throw e;
813
- });
814
- return true;
815
- }
816
- catch (e) {
817
- console.error("prepareTriggers failed: ", e);
818
- throw e;
819
- }
820
- };
821
- /* Relay relevant data to relevant subscriptions */
822
- this.notifListener = async (data) => {
823
- const str = data.payload;
824
- if (!str) {
825
- console.error("Empty notif?");
826
- return;
827
- }
828
- const dataArr = str.split(PubSubManager.DELIMITER), notifType = dataArr[0];
829
- (0, exports.log)(str);
830
- if (notifType === this.NOTIF_TYPE.schema) {
831
- if (this.onSchemaChange) {
832
- const command = dataArr[1], event_type = dataArr[2], query = dataArr[3];
833
- if (query) {
834
- this.onSchemaChange({ command, query });
835
- }
836
- }
837
- return;
838
- }
839
- if (notifType !== this.NOTIF_TYPE.data) {
840
- console.error("Unexpected notif type: ", notifType);
841
- return;
842
- }
843
- const table_name = dataArr[1], op_name = dataArr[2], condition_ids_str = dataArr[3];
844
- // 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)]);
845
- // const conditions: string[] = triggers.map(t => t.condition);
846
- (0, exports.log)("PG Trigger ->", dataArr.join("__"));
847
- if (condition_ids_str && condition_ids_str.startsWith("error") &&
848
- this._triggers && this._triggers[table_name] && this._triggers[table_name].length) {
849
- const pref = "INTERNAL ERROR. Schema might have changed";
850
- console.error(`${pref}: ${condition_ids_str}`);
851
- this._triggers[table_name].map(c => {
852
- const subs = this.getSubs(table_name, c);
853
- subs.map(s => {
854
- this.pushSubData(s, pref + ". Check server logs");
855
- });
856
- });
857
- }
858
- else if (condition_ids_str?.split(",").length &&
859
- condition_ids_str?.split(",").every((c) => Number.isInteger(+c)) &&
860
- this._triggers?.[table_name]?.length) {
861
- const idxs = condition_ids_str.split(",").map(v => +v);
862
- const conditions = this._triggers[table_name].filter((c, i) => idxs.includes(i));
863
- (0, exports.log)("PG Trigger -> ", { table_name, op_name, condition_ids_str, conditions }, this._triggers[table_name]);
864
- conditions.map(condition => {
865
- const subs = this.getSubs(table_name, condition);
866
- const syncs = this.getSyncs(table_name, condition);
867
- syncs.map((s) => {
868
- this.syncData(s, undefined, "trigger");
869
- });
870
- if (!subs) {
871
- // console.error(`sub missing for ${table_name} ${condition}`, this.triggers);
872
- // console.log(this.subs)
873
- return;
874
- }
875
- /* Throttle the subscriptions */
876
- for (var i = 0; i < subs.length; i++) {
877
- var sub = subs[i];
878
- if (this.dbo[sub.table_name] &&
879
- sub.is_ready &&
880
- (sub.socket_id && this.sockets[sub.socket_id]) || sub.func) {
881
- const throttle = sub.throttle || 0;
882
- if (sub.last_throttled <= Date.now() - throttle) {
883
- /* It is assumed the policy was checked before this point */
884
- this.pushSubData(sub);
885
- // sub.last_throttled = Date.now();
886
- }
887
- else if (!sub.is_throttling) {
888
- (0, exports.log)("throttling sub");
889
- sub.is_throttling = setTimeout(() => {
890
- (0, exports.log)("throttling finished. pushSubData...");
891
- sub.is_throttling = null;
892
- this.pushSubData(sub);
893
- }, throttle); // sub.throttle);
894
- }
895
- }
896
- }
897
- });
898
- }
899
- else {
900
- // if(!this._triggers || !this._triggers[table_name] || !this._triggers[table_name].length){
901
- // console.warn(190, "Trigger sub not found. DROPPING TRIGGER", table_name, condition_ids_str, this._triggers);
902
- // this.dropTrigger(table_name);
903
- // } else {
904
- // }
905
- console.warn(190, "Trigger sub issue: ", table_name, condition_ids_str, this._triggers);
906
- }
907
- };
908
- this.getActiveListeners = () => {
909
- let result = [];
910
- const upsert = (t, c) => {
911
- if (!result.find(r => r.table_name === t && r.condition === c)) {
912
- result.push({ table_name: t, condition: c });
913
- }
914
- };
915
- (this.syncs || []).map(s => {
916
- upsert(s.table_name, s.condition);
917
- });
918
- Object.keys(this.subs || {}).map(table_name => {
919
- Object.keys(this.subs[table_name] || {}).map(condition => {
920
- if (this.subs[table_name][condition].subs.length) {
921
- upsert(table_name, condition);
922
- }
923
- });
924
- });
925
- return result;
926
- };
927
- this.checkIfTimescaleBug = async (table_name) => {
928
- const schema = "_timescaledb_catalog", res = await this.db.oneOrNone("SELECT EXISTS( \
929
- SELECT * \
930
- FROM information_schema.tables \
931
- WHERE 1 = 1 \
932
- AND table_schema = ${schema} \
933
- AND table_name = 'hypertable' \
934
- );", { schema });
935
- if (res.exists) {
936
- let isHyperTable = await this.db.any("SELECT * FROM " + (0, prostgles_types_1.asName)(schema) + ".hypertable WHERE table_name = ${table_name};", { table_name, schema });
937
- if (isHyperTable && isHyperTable.length) {
938
- throw "Triggers do not work on timescaledb hypertables due to bug:\nhttps://github.com/timescale/timescaledb/issues/1084";
939
- }
940
- }
941
- return true;
942
- };
943
- /*
944
- A table will only have a trigger with all conditions (for different subs)
945
- conditions = ["user_id = 1"]
946
- fields = ["user_id"]
947
- */
948
- this.getMyTriggerQuery = async () => {
949
- return pgp.as.format(`
950
- SELECT * --, ROW_NUMBER() OVER(PARTITION BY table_name ORDER BY table_name, condition ) - 1 as id
951
- FROM prostgles.v_triggers
952
- WHERE app_id = $1
953
- ORDER BY table_name, condition
954
- `, [this.appID]);
955
- };
956
- this.addTriggerPool = undefined;
957
- const { wsChannelNamePrefix, pgChannelName, onSchemaChange, dboBuilder } = options;
958
- if (!dboBuilder.db || !dboBuilder.dbo) {
959
- throw 'MISSING: db_pg, db';
960
- }
961
- this.onSchemaChange = onSchemaChange;
962
- this.dboBuilder = dboBuilder;
963
- this.sockets = {};
964
- this.subs = {};
965
- this.syncs = [];
966
- this.socketChannelPreffix = wsChannelNamePrefix || "_psqlWS_";
967
- (0, exports.log)("Created PubSubManager");
968
- }
969
- get db() {
970
- return this.dboBuilder.db;
971
- }
972
- get dbo() {
973
- return this.dboBuilder.dbo;
974
- }
975
- isReady() {
976
- if (!this.postgresNotifListenManager)
977
- throw "this.postgresNotifListenManager missing";
978
- return this.postgresNotifListenManager.isListening();
979
- }
980
- getSubs(table_name, condition) {
981
- return this.subs?.[table_name]?.[condition]?.subs;
982
- }
983
- getSyncs(table_name, condition) {
984
- return (this.syncs || [])
985
- .filter((s) => s.table_name === table_name && s.condition === condition);
986
- }
987
- pushSubData(sub, err) {
988
- if (!sub)
989
- throw "pushSubData: invalid sub";
990
- const { table_name, filter, params, table_rules, socket_id, channel_name, func } = sub; //, subOne = false
991
- sub.last_throttled = Date.now();
992
- if (err) {
993
- if (socket_id) {
994
- this.sockets[socket_id].emit(channel_name, { err });
995
- }
996
- return true;
997
- }
998
- return new Promise(async (resolve, reject) => {
999
- /* TODO: Retire subOne -> it's redundant */
1000
- // this.dbo[table_name][subOne? "findOne" : "find"](filter, params, null, table_rules)
1001
- if (!this.dbo?.[table_name]?.find) {
1002
- throw new Error(`1107 this.dbo.${table_name}.find`);
1003
- }
1004
- this.dbo?.[table_name]?.find?.(filter, params, undefined, table_rules)
1005
- .then(data => {
1006
- if (socket_id && this.sockets[socket_id]) {
1007
- (0, exports.log)("Pushed " + data.length + " records to sub");
1008
- this.sockets[socket_id].emit(channel_name, { data }, () => {
1009
- resolve(data);
1010
- });
1011
- /* TO DO: confirm receiving data or server will unsubscribe
1012
- { data }, (cb)=> { console.log(cb) });
1013
- */
1014
- }
1015
- else if (func) {
1016
- func(data);
1017
- resolve(data);
1018
- }
1019
- sub.last_throttled = Date.now();
1020
- }).catch(err => {
1021
- const errObj = { _err_msg: err.toString(), err };
1022
- if (socket_id && this.sockets[socket_id]) {
1023
- this.sockets[socket_id].emit(channel_name, { err: errObj });
1024
- }
1025
- else if (func) {
1026
- func({ err: errObj });
1027
- }
1028
- reject(errObj);
1029
- });
1030
- });
1031
- }
1032
- upsertSocket(socket, channel_name) {
1033
- if (socket && !this.sockets[socket.id]) {
1034
- this.sockets[socket.id] = socket;
1035
- socket.on("disconnect", () => this.onSocketDisconnected(socket));
1036
- }
1037
- }
1038
- async syncData(sync, clientData, source) {
1039
- return await (0, SyncReplication_1.syncData)(this, sync, clientData, source);
1040
- }
1041
- /**
1042
- * Returns a sync channel
1043
- * A sync channel is unique per socket for each filter
1044
- */
1045
- async addSync(syncParams) {
1046
- const { socket = null, table_info = null, table_rules, synced_field = null, allow_delete = false, id_fields = [], filter = {}, params, condition = "", throttle = 0 } = syncParams || {};
1047
- let conditionParsed = parseCondition(condition);
1048
- if (!socket || !table_info)
1049
- throw "socket or table_info missing";
1050
- const { name: table_name } = table_info, channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
1051
- if (!synced_field)
1052
- throw "synced_field missing from table_rules";
1053
- this.upsertSocket(socket, channel_name);
1054
- const upsertSync = () => {
1055
- let newSync = {
1056
- channel_name,
1057
- table_name,
1058
- filter,
1059
- condition: conditionParsed,
1060
- synced_field,
1061
- id_fields,
1062
- allow_delete,
1063
- table_rules,
1064
- throttle: Math.max(throttle || 0, table_rules?.sync?.throttle || 0),
1065
- batch_size: (0, utils_1.get)(table_rules, "sync.batch_size") || exports.DEFAULT_SYNC_BATCH_SIZE,
1066
- last_throttled: 0,
1067
- socket_id: socket.id,
1068
- is_sync: true,
1069
- last_synced: 0,
1070
- lr: undefined,
1071
- table_info,
1072
- is_syncing: false,
1073
- wal: undefined,
1074
- socket,
1075
- params
1076
- };
1077
- /* Only a sync per socket per table per condition allowed */
1078
- this.syncs = this.syncs || [];
1079
- let existing = this.syncs.find(s => s.socket_id === socket.id && s.channel_name === channel_name);
1080
- if (!existing) {
1081
- this.syncs.push(newSync);
1082
- // console.log("Added SYNC");
1083
- socket.removeAllListeners(channel_name + "unsync");
1084
- socket.once(channel_name + "unsync", (_data, cb) => {
1085
- this.onSocketDisconnected(socket, channel_name);
1086
- cb(null, { res: "ok" });
1087
- });
1088
- socket.removeAllListeners(channel_name);
1089
- socket.on(channel_name, (data, cb) => {
1090
- if (!data) {
1091
- cb({ err: "Unexpected request. Need data or onSyncRequest" });
1092
- return;
1093
- }
1094
- /*
1095
- */
1096
- /* Server will:
1097
- 1. Ask for last_synced emit(onSyncRequest)
1098
- 2. Ask for data >= server_synced emit(onPullRequest)
1099
- -> Upsert that data
1100
- 2. Push data >= last_synced emit(data.data)
1101
-
1102
- Client will:
1103
- 1. Send last_synced on(onSyncRequest)
1104
- 2. Send data >= server_synced on(onPullRequest)
1105
- 3. Send data on CRUD emit(data.data | data.deleted)
1106
- 4. Upsert data.data | deleted on(data.data | data.deleted)
1107
- */
1108
- // if(data.data){
1109
- // console.error("THIS SHOUKD NEVER FIRE !! NEW DATA FROM SYNC");
1110
- // this.upsertClientData(newSync, data.data);
1111
- // } else
1112
- if (data.onSyncRequest) {
1113
- // console.log("syncData from socket")
1114
- this.syncData(newSync, data.onSyncRequest, "client");
1115
- // console.log("onSyncRequest ", socket._user)
1116
- }
1117
- else {
1118
- console.error("Unexpected sync request data from client: ", data);
1119
- }
1120
- });
1121
- // socket.emit(channel_name, { onSyncRequest: true }, (response) => {
1122
- // console.log(response)
1123
- // });
1124
- }
1125
- else {
1126
- console.error("UNCLOSED DUPLICATE SYNC FOUND");
1127
- }
1128
- return newSync;
1129
- };
1130
- // const { min_id, max_id, count, max_synced } = params;
1131
- let sync = upsertSync();
1132
- await this.addTrigger({ table_name, condition: conditionParsed });
1133
- return channel_name;
1134
- }
1135
- /* Must return a channel for socket */
1136
- /* The distinct list of channel names must have a corresponding trigger in the database */
1137
- async addSub(subscriptionParams) {
1138
- const { socket, func = null, table_info = null, table_rules, filter = {}, params = {}, condition = "", throttle = 0, //subOne = false,
1139
- relatedTableSubscriptions } = subscriptionParams || {};
1140
- let validated_throttle = subscriptionParams.throttle || 10;
1141
- if ((!socket && !func) || !table_info)
1142
- throw "socket/func or table_info missing";
1143
- const pubThrottle = (0, utils_1.get)(table_rules, ["subscribe", "throttle"]) || 0;
1144
- if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
1145
- validated_throttle = pubThrottle;
1146
- }
1147
- if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
1148
- validated_throttle = throttle;
1149
- }
1150
- const channel_name = `${this.socketChannelPreffix}.${table_info.name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
1151
- this.upsertSocket(socket, channel_name);
1152
- const upsertSub = (newSubData) => {
1153
- const { table_name, condition: _cond, is_ready = false, parentSubParams } = newSubData, condition = parseCondition(_cond), newSub = {
1154
- socket,
1155
- table_name: table_info.name,
1156
- table_info,
1157
- filter,
1158
- params,
1159
- table_rules,
1160
- channel_name,
1161
- parentSubParams,
1162
- func: func ?? undefined,
1163
- socket_id: socket?.id,
1164
- throttle: validated_throttle,
1165
- is_throttling: null,
1166
- last_throttled: 0,
1167
- is_ready,
1168
- };
1169
- this.subs[table_name] = this.subs[table_name] ?? {};
1170
- this.subs[table_name][condition] = this.subs[table_name][condition] ?? { subs: [] };
1171
- this.subs[table_name][condition].subs = this.subs[table_name][condition].subs ?? [];
1172
- // console.log("1034 upsertSub", this.subs)
1173
- const sub_idx = this.subs[table_name][condition].subs.findIndex(s => s.channel_name === channel_name &&
1174
- (socket && s.socket_id === socket.id ||
1175
- func && s.func === func));
1176
- if (sub_idx < 0) {
1177
- this.subs[table_name][condition].subs.push(newSub);
1178
- if (socket) {
1179
- const chnUnsub = channel_name + "unsubscribe";
1180
- socket.removeAllListeners(chnUnsub);
1181
- socket.once(chnUnsub, (_data, cb) => {
1182
- const res = this.onSocketDisconnected(socket, channel_name);
1183
- cb(null, { res });
1184
- });
1185
- }
1186
- }
1187
- else {
1188
- this.subs[table_name][condition].subs[sub_idx] = newSub;
1189
- }
1190
- if (is_ready) {
1191
- this.pushSubData(newSub);
1192
- }
1193
- };
1194
- if (table_info.is_view) {
1195
- if (relatedTableSubscriptions?.length) {
1196
- relatedTableSubscriptions.map(async (relatedTable) => {
1197
- const params = {
1198
- table_name: relatedTable.tableName,
1199
- condition: relatedTable.condition,
1200
- parentSubParams: {
1201
- ...subscriptionParams,
1202
- channel_name
1203
- },
1204
- };
1205
- upsertSub({
1206
- ...params,
1207
- is_ready: false
1208
- });
1209
- await this.addTrigger(params);
1210
- upsertSub({
1211
- ...params,
1212
- is_ready: true
1213
- });
1214
- });
1215
- return channel_name;
1216
- }
1217
- else {
1218
- throw "PubSubManager: view parent_tables missing";
1219
- }
1220
- /* */
1221
- }
1222
- else {
1223
- /* Just a table, add table + condition trigger */
1224
- // console.log(table_info, 202);
1225
- upsertSub({
1226
- table_name: table_info.name,
1227
- condition: parseCondition(condition),
1228
- parentSubParams: undefined,
1229
- is_ready: false
1230
- });
1231
- await this.addTrigger({
1232
- table_name: table_info.name,
1233
- condition: parseCondition(condition),
1234
- });
1235
- upsertSub({
1236
- table_name: table_info.name,
1237
- condition: parseCondition(condition),
1238
- parentSubParams: undefined,
1239
- is_ready: true
1240
- });
1241
- return channel_name;
1242
- }
1243
- }
1244
- removeLocalSub(table_name, condition, func) {
1245
- let cond = parseCondition(condition);
1246
- if ((0, utils_1.get)(this.subs, [table_name, cond, "subs"])) {
1247
- this.subs[table_name][cond].subs.map((sub, i) => {
1248
- if (sub.func && sub.func === func) {
1249
- this.subs[table_name][cond].subs.splice(i, 1);
1250
- }
1251
- });
1252
- }
1253
- else {
1254
- console.error("Could not unsubscribe. Subscription might not have initialised yet");
1255
- }
1256
- }
1257
- onSocketDisconnected(socket, channel_name) {
1258
- // process.on('warning', e => {
1259
- // console.warn(e.stack)
1260
- // });
1261
- // console.log("onSocketDisconnected", channel_name, this.syncs)
1262
- if (this.subs) {
1263
- Object.keys(this.subs).map(table_name => {
1264
- Object.keys(this.subs[table_name]).map(condition => {
1265
- this.subs[table_name][condition].subs.map((sub, i) => {
1266
- /**
1267
- * If a channel name is specified then delete triggers
1268
- */
1269
- if ((socket && sub.socket_id === socket.id) &&
1270
- (!channel_name || sub.channel_name === channel_name)) {
1271
- this.subs[table_name][condition].subs.splice(i, 1);
1272
- if (!this.subs[table_name][condition].subs.length) {
1273
- delete this.subs[table_name][condition];
1274
- if ((0, prostgles_types_1.isEmpty)(this.subs[table_name])) {
1275
- delete this.subs[table_name];
1276
- }
1277
- }
1278
- }
1279
- });
1280
- });
1281
- });
1282
- }
1283
- if (this.syncs) {
1284
- this.syncs = this.syncs.filter(s => {
1285
- const matchesSocket = Boolean(socket && s.socket_id !== socket.id);
1286
- if (channel_name) {
1287
- return matchesSocket || s.channel_name !== channel_name;
1288
- }
1289
- return matchesSocket;
1290
- });
1291
- }
1292
- if (!socket) {
1293
- }
1294
- else if (!channel_name) {
1295
- delete this.sockets[socket.id];
1296
- }
1297
- else {
1298
- socket.removeAllListeners(channel_name);
1299
- socket.removeAllListeners(channel_name + "unsync");
1300
- socket.removeAllListeners(channel_name + "unsubscribe");
1301
- }
1302
- return "ok";
1303
- }
1304
- async addTrigger(params) {
1305
- try {
1306
- let { table_name, condition } = { ...params };
1307
- if (!table_name)
1308
- throw "MISSING table_name";
1309
- if (!this.appID)
1310
- throw "MISSING appID";
1311
- if (!condition || !condition.trim().length) {
1312
- condition = "TRUE";
1313
- }
1314
- // console.log(1623, { app_id, addTrigger: { table_name, condition } });
1315
- await this.checkIfTimescaleBug(table_name);
1316
- const trgVals = {
1317
- tbl: (0, exports.asValue)(table_name),
1318
- cond: (0, exports.asValue)(condition),
1319
- };
1320
- await this.db.any(`
1321
- BEGIN WORK;
1322
- LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
1323
-
1324
- INSERT INTO prostgles.app_triggers (table_name, condition, app_id)
1325
- VALUES (${trgVals.tbl}, ${trgVals.cond}, ${(0, exports.asValue)(this.appID)})
1326
- ON CONFLICT DO NOTHING;
1327
-
1328
- COMMIT WORK;
1329
- `);
1330
- (0, exports.log)("addTrigger.. ", { table_name, condition });
1331
- const triggers = await this.db.any(await this.getMyTriggerQuery());
1332
- this._triggers = {};
1333
- triggers.map(t => {
1334
- this._triggers = this._triggers || {};
1335
- this._triggers[t.table_name] = this._triggers[t.table_name] || [];
1336
- if (!this._triggers[t.table_name].includes(t.condition)) {
1337
- this._triggers[t.table_name].push(t.condition);
1338
- }
1339
- });
1340
- (0, exports.log)("trigger added.. ", { table_name, condition });
1341
- return true;
1342
- // console.log("1612", JSON.stringify(triggers, null, 2))
1343
- // console.log("1613",JSON.stringify(this._triggers, null, 2))
1344
- }
1345
- catch (e) {
1346
- console.trace("Failed adding trigger", e);
1347
- // throw e
1348
- }
1349
- }
1350
- }
1351
- exports.PubSubManager = PubSubManager;
1352
- _a = PubSubManager;
1353
- PubSubManager.DELIMITER = '|$prstgls$|';
1354
- // ,datname
1355
- // ,usename
1356
- // ,client_hostname
1357
- // ,client_port
1358
- // ,backend_start
1359
- // ,query_start
1360
- // ,query
1361
- // ,state
1362
- // console.log(await _db.any(`
1363
- // SELECT pid, application_name, state
1364
- // FROM pg_stat_activity
1365
- // WHERE application_name IS NOT NULL AND application_name != '' -- state = 'active';
1366
- // `))
1367
- PubSubManager.canCreate = async (db) => {
1368
- const canExecute = await (0, DboBuilder_1.canEXECUTE)(db);
1369
- const isSuperUs = await (0, Prostgles_1.isSuperUser)(db);
1370
- return { canExecute, isSuperUs, yes: canExecute && isSuperUs };
1371
- };
1372
- PubSubManager.create = async (options) => {
1373
- const res = new PubSubManager(options);
1374
- return await res.init();
1375
- };
1376
- PubSubManager.SCHEMA_ALTERING_QUERIES = ['CREATE TABLE', 'ALTER TABLE', 'DROP TABLE', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW', 'CREATE TABLE AS', 'SELECT INTO'];
1377
- PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID = "prostgles internal query that should be excluded from ";
1378
- const parseCondition = (condition) => Boolean(condition && condition.trim().length) ? condition : "TRUE";
1379
- function omitKeys(obj, exclude) {
1380
- return pickKeys(obj, (0, prostgles_types_1.getKeys)(obj).filter(k => !exclude.includes(k)));
1381
- }
1382
- exports.omitKeys = omitKeys;
1383
- function pickKeys(obj, include = []) {
1384
- let keys = include;
1385
- if (!keys.length) {
1386
- return {};
1387
- }
1388
- if (obj && keys.length) {
1389
- let res = {};
1390
- keys.forEach(k => {
1391
- res[k] = obj[k];
1392
- });
1393
- return res;
1394
- }
1395
- return obj;
1396
- }
1397
- exports.pickKeys = pickKeys;
1398
- //# sourceMappingURL=PubSubManager.js.map