prostgles-server 3.0.153 → 3.0.155

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 (42) hide show
  1. package/dist/PubSubManager/PubSubManager.d.ts +21 -26
  2. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  3. package/dist/PubSubManager/PubSubManager.js +43 -393
  4. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  5. package/dist/PubSubManager/addSub.d.ts +7 -0
  6. package/dist/PubSubManager/addSub.d.ts.map +1 -0
  7. package/dist/PubSubManager/addSub.js +153 -0
  8. package/dist/PubSubManager/addSub.js.map +1 -0
  9. package/dist/PubSubManager/addSync.d.ts +8 -0
  10. package/dist/PubSubManager/addSync.d.ts.map +1 -0
  11. package/dist/PubSubManager/addSync.js +110 -0
  12. package/dist/PubSubManager/addSync.js.map +1 -0
  13. package/dist/PubSubManager/notifListener.d.ts +5 -0
  14. package/dist/PubSubManager/notifListener.d.ts.map +1 -0
  15. package/dist/PubSubManager/notifListener.js +97 -0
  16. package/dist/PubSubManager/notifListener.js.map +1 -0
  17. package/lib/DboBuilder/TableHandler.d.ts +1 -5
  18. package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
  19. package/lib/PubSubManager/PubSubManager.d.ts +20 -29
  20. package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
  21. package/lib/PubSubManager/PubSubManager.js +44 -397
  22. package/lib/PubSubManager/PubSubManager.ts +82 -508
  23. package/lib/PubSubManager/addSub.d.ts +7 -0
  24. package/lib/PubSubManager/addSub.d.ts.map +1 -0
  25. package/lib/PubSubManager/addSub.js +152 -0
  26. package/lib/PubSubManager/addSub.ts +178 -0
  27. package/lib/PubSubManager/addSync.d.ts +8 -0
  28. package/lib/PubSubManager/addSync.d.ts.map +1 -0
  29. package/lib/PubSubManager/addSync.js +109 -0
  30. package/lib/PubSubManager/addSync.ts +127 -0
  31. package/lib/PubSubManager/notifListener.d.ts +5 -0
  32. package/lib/PubSubManager/notifListener.d.ts.map +1 -0
  33. package/lib/PubSubManager/notifListener.js +96 -0
  34. package/lib/PubSubManager/notifListener.ts +122 -0
  35. package/package.json +2 -2
  36. package/tests/client/PID.txt +1 -1
  37. package/tests/client/tsconfig.json +2 -1
  38. package/tests/client_only_queries.js +1 -1
  39. package/tests/client_only_queries.ts +1 -1
  40. package/tests/isomorphic_queries.ts +1 -1
  41. package/tests/server/package-lock.json +1 -1
  42. package/tests/server/tsconfig.json +2 -1
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.notifListener = void 0;
4
+ const PubSubManager_1 = require("./PubSubManager");
5
+ /* Relay relevant data to relevant subscriptions */
6
+ async function notifListener(data) {
7
+ const str = data.payload;
8
+ if (!str) {
9
+ console.error("Unexpected Empty notif");
10
+ return;
11
+ }
12
+ const dataArr = str.split(PubSubManager_1.PubSubManager.DELIMITER);
13
+ const notifType = dataArr[0];
14
+ (0, PubSubManager_1.log)(str);
15
+ if (notifType === this.NOTIF_TYPE.schema) {
16
+ if (this.onSchemaChange) {
17
+ const [_, command, _event_type, query] = dataArr;
18
+ if (query) {
19
+ this.onSchemaChange({ command, query });
20
+ }
21
+ }
22
+ return;
23
+ }
24
+ if (notifType !== this.NOTIF_TYPE.data) {
25
+ console.error("Unexpected notif type: ", notifType);
26
+ return;
27
+ }
28
+ if (dataArr.length < 3) {
29
+ throw "notifListener: dataArr length < 3";
30
+ }
31
+ const [_, table_name, op_name, condition_ids_str] = dataArr;
32
+ // 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)]);
33
+ // const conditions: string[] = triggers.map(t => t.condition);
34
+ (0, PubSubManager_1.log)("notifListener", dataArr.join("__"));
35
+ /* Trigger error */
36
+ if (condition_ids_str?.startsWith("error") &&
37
+ this._triggers?.[table_name]?.length) {
38
+ const pref = "INTERNAL ERROR";
39
+ console.error(`${pref}: condition_ids_str: ${condition_ids_str}`);
40
+ this._triggers[table_name].map(c => {
41
+ const subs = this.getSubs(table_name, c);
42
+ subs.map(s => {
43
+ this.pushSubData(s, pref + ". Check server logs. Schema might have changed");
44
+ });
45
+ });
46
+ /* Trigger ok */
47
+ }
48
+ else if (condition_ids_str?.split(",").length &&
49
+ condition_ids_str?.split(",").every((c) => Number.isInteger(+c)) &&
50
+ this._triggers?.[table_name]?.length) {
51
+ const idxs = condition_ids_str.split(",").map(v => +v);
52
+ const conditions = this._triggers[table_name].filter((c, i) => idxs.includes(i));
53
+ (0, PubSubManager_1.log)("notifListener", { table_name, op_name, condition_ids_str, conditions }, this._triggers[table_name]);
54
+ conditions.map(condition => {
55
+ const subs = this.getSubs(table_name, condition);
56
+ const syncs = this.getSyncs(table_name, condition);
57
+ (0, PubSubManager_1.log)("notifListener", { table_name, condition, subs, syncs });
58
+ syncs.map((s) => {
59
+ this.syncData(s, undefined, "trigger");
60
+ });
61
+ /* Throttle the subscriptions */
62
+ subs.forEach(sub => {
63
+ sub.triggers.forEach(trg => {
64
+ if (this.dbo[trg.table_name] &&
65
+ sub.is_ready &&
66
+ (sub.socket_id && this.sockets[sub.socket_id]) || sub.func) {
67
+ const throttle = sub.throttle || 0;
68
+ if (sub.last_throttled <= Date.now() - throttle) {
69
+ /* It is assumed the policy was checked before this point */
70
+ this.pushSubData(sub);
71
+ // sub.last_throttled = Date.now();
72
+ }
73
+ else if (!sub.is_throttling) {
74
+ (0, PubSubManager_1.log)("throttling sub");
75
+ sub.is_throttling = setTimeout(() => {
76
+ (0, PubSubManager_1.log)("throttling finished. pushSubData...");
77
+ sub.is_throttling = null;
78
+ this.pushSubData(sub);
79
+ }, throttle); // sub.throttle);
80
+ }
81
+ }
82
+ });
83
+ });
84
+ });
85
+ /* Trigger unknown issue */
86
+ }
87
+ else {
88
+ // if(!this._triggers || !this._triggers[table_name] || !this._triggers[table_name].length){
89
+ // console.warn(190, "Trigger sub not found. DROPPING TRIGGER", table_name, condition_ids_str, this._triggers);
90
+ // this.dropTrigger(table_name);
91
+ // } else {
92
+ // }
93
+ console.warn(190, "Trigger sub issue: ", table_name, condition_ids_str, this._triggers);
94
+ }
95
+ }
96
+ exports.notifListener = notifListener;
@@ -0,0 +1,122 @@
1
+ import { log, PubSubManager } from "./PubSubManager";
2
+
3
+ /* Relay relevant data to relevant subscriptions */
4
+ export async function notifListener(this: PubSubManager, data: { payload: string }) {
5
+ const str = data.payload;
6
+
7
+ if (!str) {
8
+ console.error("Unexpected Empty notif")
9
+ return;
10
+ }
11
+
12
+ const dataArr = str.split(PubSubManager.DELIMITER);
13
+ const notifType = dataArr[0];
14
+
15
+ log(str);
16
+
17
+ if (notifType === this.NOTIF_TYPE.schema) {
18
+ if (this.onSchemaChange) {
19
+ const [_, command, _event_type, query] = dataArr;
20
+
21
+ if (query) {
22
+ this.onSchemaChange({ command, query })
23
+ }
24
+ }
25
+
26
+ return;
27
+ }
28
+
29
+ if (notifType !== this.NOTIF_TYPE.data) {
30
+ console.error("Unexpected notif type: ", notifType);
31
+ return;
32
+ }
33
+
34
+ if (dataArr.length < 3) {
35
+ throw "notifListener: dataArr length < 3"
36
+ }
37
+
38
+ const [_, table_name, op_name, condition_ids_str] = dataArr;
39
+
40
+ // 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)]);
41
+ // const conditions: string[] = triggers.map(t => t.condition);
42
+ log("notifListener", dataArr.join("__"))
43
+
44
+ /* Trigger error */
45
+ if (
46
+ condition_ids_str?.startsWith("error") &&
47
+ this._triggers?.[table_name]?.length
48
+ ) {
49
+ const pref = "INTERNAL ERROR";
50
+ console.error(`${pref}: condition_ids_str: ${condition_ids_str}`)
51
+ this._triggers[table_name]!.map(c => {
52
+ const subs = this.getSubs(table_name, c);
53
+ subs.map(s => {
54
+ this.pushSubData(s, pref + ". Check server logs. Schema might have changed");
55
+ })
56
+ });
57
+
58
+ /* Trigger ok */
59
+ } else if (
60
+ condition_ids_str?.split(",").length &&
61
+ condition_ids_str?.split(",").every((c: string) => Number.isInteger(+c)) &&
62
+ this._triggers?.[table_name]?.length
63
+ ) {
64
+
65
+
66
+ const idxs = condition_ids_str.split(",").map(v => +v);
67
+ const conditions = this._triggers[table_name]!.filter((c, i) => idxs.includes(i))
68
+
69
+ log("notifListener", { table_name, op_name, condition_ids_str, conditions }, this._triggers[table_name]);
70
+
71
+ conditions.map(condition => {
72
+
73
+ const subs = this.getSubs(table_name, condition);
74
+ const syncs = this.getSyncs(table_name, condition);
75
+
76
+ log("notifListener", { table_name, condition, subs, syncs })
77
+
78
+ syncs.map((s) => {
79
+ this.syncData(s, undefined, "trigger");
80
+ });
81
+
82
+ /* Throttle the subscriptions */
83
+ subs.forEach(sub => {
84
+ sub.triggers.forEach(trg => {
85
+ if (
86
+ this.dbo[trg.table_name] &&
87
+ sub.is_ready &&
88
+ (sub.socket_id && this.sockets[sub.socket_id]) || sub.func
89
+ ) {
90
+ const throttle = sub.throttle || 0;
91
+ if (sub.last_throttled <= Date.now() - throttle) {
92
+
93
+ /* It is assumed the policy was checked before this point */
94
+ this.pushSubData(sub);
95
+ // sub.last_throttled = Date.now();
96
+ } else if (!sub.is_throttling) {
97
+
98
+
99
+ log("throttling sub")
100
+ sub.is_throttling = setTimeout(() => {
101
+ log("throttling finished. pushSubData...")
102
+ sub.is_throttling = null;
103
+ this.pushSubData(sub);
104
+ }, throttle);// sub.throttle);
105
+ }
106
+ }
107
+ });
108
+ });
109
+
110
+ });
111
+
112
+ /* Trigger unknown issue */
113
+ } else {
114
+
115
+ // if(!this._triggers || !this._triggers[table_name] || !this._triggers[table_name].length){
116
+ // console.warn(190, "Trigger sub not found. DROPPING TRIGGER", table_name, condition_ids_str, this._triggers);
117
+ // this.dropTrigger(table_name);
118
+ // } else {
119
+ // }
120
+ console.warn(190, "Trigger sub issue: ", table_name, condition_ids_str, this._triggers);
121
+ }
122
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-server",
3
- "version": "3.0.153",
3
+ "version": "3.0.155",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,7 +12,7 @@
12
12
  "start": "tsc --watch",
13
13
  "build": "tsc",
14
14
  "lint": "eslint . --ext .ts --quiet --fix",
15
- "test": "npm run lint && rm -rf ./node_modules/* && npm i && tsc && cd tests/ && ./test.sh"
15
+ "test": "rm -rf ./node_modules/* && npm i && npm run lint && tsc && cd tests/ && ./test.sh"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
@@ -1 +1 @@
1
- 39332
1
+ 51927
@@ -10,7 +10,8 @@
10
10
  // "rootDir": ".",
11
11
  // "declaration": true,
12
12
  // "declarationMap": true,
13
- "keyofStringsOnly": true
13
+ "keyofStringsOnly": true,
14
+ "skipLibCheck": false
14
15
  },
15
16
  "exclude": [
16
17
  "dist"
@@ -123,7 +123,7 @@ async function client_only(db, auth, log, methods, tableSchema) {
123
123
  /** 2 second delay to account for client-server clock drift */
124
124
  setTimeout(async () => {
125
125
  // db.planes.findOne({}, { select: { last_updated: "$max"}}).then(log);
126
- sP.unsubscribe();
126
+ await sP.unsubscribe();
127
127
  log(Date.now() + ": sub: db.planes.update({}, { x: 20, last_updated });");
128
128
  const dLastUpdated = Math.max(...p10.map(v => +v.last_updated));
129
129
  const last_updated = Date.now();
@@ -146,7 +146,7 @@ export default async function client_only(db: Required<DBHandlerClient>, auth: A
146
146
 
147
147
  // db.planes.findOne({}, { select: { last_updated: "$max"}}).then(log);
148
148
 
149
- sP.unsubscribe();
149
+ await sP.unsubscribe();
150
150
  log(Date.now() + ": sub: db.planes.update({}, { x: 20, last_updated });");
151
151
  const dLastUpdated = Math.max(...p10.map(v => +v.last_updated))
152
152
  const last_updated = Date.now();
@@ -371,7 +371,7 @@ export default async function isomorphic(db: Required<DBHandlerServer> | Require
371
371
  await db.various.update!({ id: 99 }, { name: "zz3zz2" });
372
372
  await db.various.update!({ id: 99 }, { name: "zz3zz3" });
373
373
  }, { timeout: 4000 });
374
-
374
+
375
375
  await tryRunP("subscribeOne with throttle", async (resolve, reject) => {
376
376
  await db.various.insert!({ id: 99 });
377
377
  const start = Date.now(); // name: "zz3zz"
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "../..": {
23
23
  "name": "prostgles-server",
24
- "version": "3.0.152",
24
+ "version": "3.0.154",
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
27
  "@aws-sdk/client-s3": "^3.272.0",
@@ -11,7 +11,8 @@
11
11
  "moduleResolution": "node",
12
12
  "declaration": true,
13
13
  "declarationMap": true,
14
- "keyofStringsOnly": true
14
+ "keyofStringsOnly": true,
15
+ "skipLibCheck": false
15
16
  },
16
17
  "exclude": [
17
18
  "dist",