prostgles-server 3.0.151 → 3.0.153

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 (30) hide show
  1. package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
  2. package/dist/DboBuilder/QueryBuilder/QueryBuilder.js +4 -3
  3. package/dist/DboBuilder/QueryBuilder/QueryBuilder.js.map +1 -1
  4. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts +20 -0
  5. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -0
  6. package/dist/DboBuilder/getSubscribeRelatedTables.js +153 -0
  7. package/dist/DboBuilder/getSubscribeRelatedTables.js.map +1 -0
  8. package/dist/DboBuilder/subscribe.d.ts.map +1 -1
  9. package/dist/DboBuilder/subscribe.js +27 -173
  10. package/dist/DboBuilder/subscribe.js.map +1 -1
  11. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  12. package/dist/PubSubManager/PubSubManager.js +35 -4
  13. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  14. package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
  15. package/lib/DboBuilder/QueryBuilder/QueryBuilder.js +4 -3
  16. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +7 -6
  17. package/lib/DboBuilder/getSubscribeRelatedTables.d.ts +20 -0
  18. package/lib/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -0
  19. package/lib/DboBuilder/getSubscribeRelatedTables.js +152 -0
  20. package/lib/DboBuilder/getSubscribeRelatedTables.ts +183 -0
  21. package/lib/DboBuilder/subscribe.d.ts.map +1 -1
  22. package/lib/DboBuilder/subscribe.js +27 -173
  23. package/lib/DboBuilder/subscribe.ts +38 -200
  24. package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
  25. package/lib/PubSubManager/PubSubManager.js +35 -4
  26. package/lib/PubSubManager/PubSubManager.ts +61 -27
  27. package/package.json +1 -1
  28. package/tests/client/PID.txt +1 -1
  29. package/tests/server/package-lock.json +1 -1
  30. package/tsconfig.json +4 -0
@@ -17,7 +17,7 @@ import { SelectParams, FieldFilter, asName, WAL, isEmpty, AnyObject } from "pros
17
17
 
18
18
  import { ClientExpressData, syncData } from "../SyncReplication";
19
19
  import { TableRule } from "../PublishParser";
20
- import { find } from "prostgles-types/dist/util";
20
+ import { find, getKeys } from "prostgles-types/dist/util";
21
21
  import { DB_OBJ_NAMES } from "./getInitQuery";
22
22
 
23
23
 
@@ -362,10 +362,34 @@ export class PubSubManager {
362
362
  isReady() {
363
363
  if (!this.postgresNotifListenManager) throw "this.postgresNotifListenManager missing";
364
364
  return this.postgresNotifListenManager.isListening();
365
- }
365
+ }
366
366
 
367
367
  getSubs(table_name: string, condition: string): SubscriptionParams[] {
368
- return this.subs?.[table_name]?.[condition]?.subs
368
+ const subs = this.subs?.[table_name]?.[condition]?.subs ?? [];
369
+
370
+ // if(!subs){
371
+ // log("Subs not found:", { table_name, condition }, this.subs)
372
+ // }
373
+
374
+ return subs.flatMap(s => {
375
+ /* Return parentSubs to ensure throttling works */
376
+ if(s.parentSubParams){
377
+ const parentSubs: SubscriptionParams[] = [];
378
+ const parentChannel = s.parentSubParams.channel_name;
379
+ for(const tableName in getKeys(this.subs)){
380
+ for(const condition in getKeys(this.subs[tableName]!)){
381
+ this.subs[tableName]![condition]!.subs.forEach(parentSub => {
382
+ if(!parentSub.parentSubParams && parentSub.channel_name === parentChannel){
383
+ parentSubs.push(parentSub)
384
+ }
385
+ });
386
+ }
387
+ }
388
+ return parentSubs ?? s;
389
+ }
390
+
391
+ return s;
392
+ });
369
393
  }
370
394
 
371
395
  getSyncs(table_name: string, condition: string) {
@@ -388,7 +412,7 @@ export class PubSubManager {
388
412
 
389
413
  if (notifType === this.NOTIF_TYPE.schema) {
390
414
  if (this.onSchemaChange) {
391
- const command = dataArr[1],
415
+ const command = dataArr[1]!,
392
416
  event_type = dataArr[2],
393
417
  query = dataArr[3];
394
418
 
@@ -404,27 +428,37 @@ export class PubSubManager {
404
428
  console.error("Unexpected notif type: ", notifType);
405
429
  return;
406
430
  }
407
-
408
- const table_name = dataArr[1],
409
- op_name = dataArr[2],
410
- condition_ids_str = dataArr[3];
431
+ if(dataArr.length < 3){
432
+ throw "dataArr length < 3"
433
+ }
434
+ const table_name = dataArr[1]!,
435
+ op_name = dataArr[2]!,
436
+ condition_ids_str = dataArr[3]!;
411
437
 
412
438
  // 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)]);
413
439
  // const conditions: string[] = triggers.map(t => t.condition);
414
440
 
441
+ /**
442
+ * Trigger error
443
+ */
415
444
  log("PG Trigger ->", dataArr.join("__"))
416
445
  if (
417
446
  condition_ids_str && condition_ids_str.startsWith("error") &&
418
- this._triggers && this._triggers[table_name] && this._triggers[table_name].length
447
+ this._triggers &&
448
+ this._triggers[table_name]?.length
419
449
  ) {
420
450
  const pref = "INTERNAL ERROR";
421
451
  console.error(`${pref}: condition_ids_str: ${condition_ids_str}`)
422
- this._triggers[table_name].map(c => {
452
+ this._triggers[table_name]!.map(c => {
423
453
  const subs = this.getSubs(table_name, c);
424
454
  subs.map(s => {
425
455
  this.pushSubData(s, pref + ". Check server logs. Schema might have changed");
426
456
  })
427
457
  });
458
+
459
+ /**
460
+ * Trigger ok
461
+ */
428
462
  } else if (
429
463
  condition_ids_str?.split(",").length &&
430
464
  condition_ids_str?.split(",").every((c: string) => Number.isInteger(+c)) &&
@@ -433,7 +467,7 @@ export class PubSubManager {
433
467
 
434
468
 
435
469
  const idxs = condition_ids_str.split(",").map(v => +v);
436
- const conditions = this._triggers[table_name].filter((c, i) => idxs.includes(i))
470
+ const conditions = this._triggers[table_name]!.filter((c, i) => idxs.includes(i))
437
471
 
438
472
  log("PG Trigger -> ", { table_name, op_name, condition_ids_str, conditions }, this._triggers[table_name]);
439
473
 
@@ -456,7 +490,7 @@ export class PubSubManager {
456
490
 
457
491
  /* Throttle the subscriptions */
458
492
  for (let i = 0; i < subs.length; i++) {
459
- const sub = subs[i];
493
+ const sub = subs[i]!;
460
494
  if (
461
495
  this.dbo[sub.table_name] &&
462
496
  sub.is_ready &&
@@ -717,11 +751,11 @@ export class PubSubManager {
717
751
  };
718
752
 
719
753
  this.subs[table_name] = this.subs[table_name] ?? {};
720
- this.subs[table_name][condition] = this.subs[table_name][condition] ?? { subs: [] };
721
- this.subs[table_name][condition].subs = this.subs[table_name][condition].subs ?? [];
754
+ this.subs[table_name]![condition] = this.subs[table_name]![condition] ?? { subs: [] };
755
+ this.subs[table_name]![condition]!.subs = this.subs[table_name]![condition]!.subs ?? [];
722
756
 
723
757
  // console.log("1034 upsertSub", this.subs)
724
- const sub_idx = this.subs[table_name][condition].subs.findIndex(s =>
758
+ const sub_idx = this.subs[table_name]![condition]!.subs.findIndex(s =>
725
759
  s.channel_name === channel_name &&
726
760
  (
727
761
  socket && s.socket_id === socket.id ||
@@ -729,7 +763,7 @@ export class PubSubManager {
729
763
  )
730
764
  );
731
765
  if (sub_idx < 0) {
732
- this.subs[table_name][condition].subs.push(newSub);
766
+ this.subs[table_name]![condition]!.subs.push(newSub);
733
767
  if (socket) {
734
768
  const chnUnsub = channel_name + "unsubscribe";
735
769
  socket.removeAllListeners(chnUnsub);
@@ -739,7 +773,7 @@ export class PubSubManager {
739
773
  });
740
774
  }
741
775
  } else {
742
- this.subs[table_name][condition].subs[sub_idx] = newSub;
776
+ this.subs[table_name]![condition]!.subs[sub_idx] = newSub;
743
777
  }
744
778
 
745
779
  if (isReadyOverride ?? is_ready) {
@@ -812,11 +846,11 @@ export class PubSubManager {
812
846
  removeLocalSub(table_name: string, condition: string, func: (items: object[]) => any) {
813
847
  const cond = parseCondition(condition);
814
848
  if (get(this.subs, [table_name, cond, "subs"])) {
815
- this.subs[table_name][cond].subs.map((sub, i) => {
849
+ this.subs[table_name]![cond]!.subs.map((sub, i) => {
816
850
  if (
817
851
  sub.func && sub.func === func
818
852
  ) {
819
- this.subs[table_name][cond].subs.splice(i, 1);
853
+ this.subs[table_name]![cond]!.subs.splice(i, 1);
820
854
  }
821
855
  });
822
856
  } else {
@@ -836,7 +870,7 @@ export class PubSubManager {
836
870
  });
837
871
  Object.keys(this.subs || {}).map(table_name => {
838
872
  Object.keys(this.subs[table_name] || {}).map(condition => {
839
- if (this.subs[table_name][condition].subs.length) {
873
+ if (this.subs[table_name]![condition]!.subs.length) {
840
874
  upsert(table_name, condition);
841
875
  }
842
876
  });
@@ -852,8 +886,8 @@ export class PubSubManager {
852
886
  // console.log("onSocketDisconnected", channel_name, this.syncs)
853
887
  if (this.subs) {
854
888
  Object.keys(this.subs).map(table_name => {
855
- Object.keys(this.subs[table_name]).map(condition => {
856
- this.subs[table_name][condition].subs.map((sub, i) => {
889
+ Object.keys(this.subs[table_name]!).map(condition => {
890
+ this.subs[table_name]![condition]!.subs.map((sub, i) => {
857
891
 
858
892
  /**
859
893
  * If a channel name is specified then delete triggers
@@ -862,9 +896,9 @@ export class PubSubManager {
862
896
  (socket && sub.socket_id === socket.id) &&
863
897
  (!channel_name || sub.channel_name === channel_name)
864
898
  ) {
865
- this.subs[table_name][condition].subs.splice(i, 1);
866
- if (!this.subs[table_name][condition].subs.length) {
867
- delete this.subs[table_name][condition];
899
+ this.subs[table_name]![condition]!.subs.splice(i, 1);
900
+ if (!this.subs[table_name]![condition]!.subs.length) {
901
+ delete this.subs[table_name]![condition];
868
902
 
869
903
  if (isEmpty(this.subs[table_name])) {
870
904
  delete this.subs[table_name];
@@ -983,8 +1017,8 @@ export class PubSubManager {
983
1017
  triggers.map(t => {
984
1018
  this._triggers = this._triggers || {};
985
1019
  this._triggers[t.table_name] = this._triggers[t.table_name] || [];
986
- if (!this._triggers[t.table_name].includes(t.condition)) {
987
- this._triggers[t.table_name].push(t.condition)
1020
+ if (!this._triggers[t.table_name]?.includes(t.condition)) {
1021
+ this._triggers[t.table_name]?.push(t.condition)
988
1022
  }
989
1023
  });
990
1024
  log("trigger added.. ", { table_name, condition });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "3.0.151",
3
+ "version": "3.0.153",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1 +1 @@
1
- 40773
1
+ 39332
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "3.0.150",
24
+ "version": "3.0.152",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.272.0",
package/tsconfig.json CHANGED
@@ -15,6 +15,10 @@
15
15
  "moduleResolution": "node",
16
16
  "allowSyntheticDefaultImports": true,
17
17
  "resolveJsonModule": true,
18
+
19
+ /** TODO */
20
+ "noUncheckedIndexedAccess": false,
21
+
18
22
  // "outFile": "bundle.js",
19
23
  // "moduleResolution": "node"
20
24
  "keyofStringsOnly": true,