prostgles-server 3.0.154 → 4.0.1

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 (136) hide show
  1. package/dist/AuthHandler.d.ts.map +1 -1
  2. package/dist/AuthHandler.js +6 -3
  3. package/dist/AuthHandler.js.map +1 -1
  4. package/dist/DBEventsManager.d.ts.map +1 -1
  5. package/dist/DBEventsManager.js +5 -4
  6. package/dist/DBEventsManager.js.map +1 -1
  7. package/dist/DBSchemaBuilder.js +14 -14
  8. package/dist/DBSchemaBuilder.js.map +1 -1
  9. package/dist/DboBuilder/QueryBuilder/Functions.js +1 -1
  10. package/dist/DboBuilder/QueryBuilder/Functions.js.map +1 -1
  11. package/dist/DboBuilder/QueryBuilder/makeSelectQuery.js.map +1 -1
  12. package/dist/DboBuilder/TableHandler.js +1 -2
  13. package/dist/DboBuilder/TableHandler.js.map +1 -1
  14. package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
  15. package/dist/DboBuilder/ViewHandler.js +10 -8
  16. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  17. package/dist/DboBuilder/getCondition.js +1 -1
  18. package/dist/DboBuilder/getCondition.js.map +1 -1
  19. package/dist/DboBuilder/insert.js +1 -1
  20. package/dist/DboBuilder/insert.js.map +1 -1
  21. package/dist/DboBuilder/insertDataParse.js +4 -3
  22. package/dist/DboBuilder/insertDataParse.js.map +1 -1
  23. package/dist/DboBuilder/subscribe.d.ts +2 -2
  24. package/dist/DboBuilder/subscribe.d.ts.map +1 -1
  25. package/dist/DboBuilder/subscribe.js +3 -4
  26. package/dist/DboBuilder/subscribe.js.map +1 -1
  27. package/dist/DboBuilder.d.ts.map +1 -1
  28. package/dist/DboBuilder.js +5 -4
  29. package/dist/DboBuilder.js.map +1 -1
  30. package/dist/FileManager.js +1 -1
  31. package/dist/FileManager.js.map +1 -1
  32. package/dist/Filtering.js.map +1 -1
  33. package/dist/JSONBValidation/validation.js.map +1 -1
  34. package/dist/Prostgles.js +1 -1
  35. package/dist/Prostgles.js.map +1 -1
  36. package/dist/PubSubManager/PubSubManager.d.ts +28 -26
  37. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  38. package/dist/PubSubManager/PubSubManager.js +53 -433
  39. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  40. package/dist/PubSubManager/addSub.d.ts +8 -0
  41. package/dist/PubSubManager/addSub.d.ts.map +1 -0
  42. package/dist/PubSubManager/addSub.js +163 -0
  43. package/dist/PubSubManager/addSub.js.map +1 -0
  44. package/dist/PubSubManager/addSync.d.ts +8 -0
  45. package/dist/PubSubManager/addSync.d.ts.map +1 -0
  46. package/dist/PubSubManager/addSync.js +110 -0
  47. package/dist/PubSubManager/addSync.js.map +1 -0
  48. package/dist/PubSubManager/notifListener.d.ts +5 -0
  49. package/dist/PubSubManager/notifListener.d.ts.map +1 -0
  50. package/dist/PubSubManager/notifListener.js +99 -0
  51. package/dist/PubSubManager/notifListener.js.map +1 -0
  52. package/dist/PubSubManager/pushSubData.d.ts +3 -0
  53. package/dist/PubSubManager/pushSubData.d.ts.map +1 -0
  54. package/dist/PubSubManager/pushSubData.js +48 -0
  55. package/dist/PubSubManager/pushSubData.js.map +1 -0
  56. package/dist/PublishParser.d.ts.map +1 -1
  57. package/dist/PublishParser.js +4 -3
  58. package/dist/PublishParser.js.map +1 -1
  59. package/dist/TableConfig/TableConfig.js.map +1 -1
  60. package/dist/TableConfig/getConstraintDefinitionQueries.js.map +1 -1
  61. package/dist/TableConfig/getTableColumnQueries.js.map +1 -1
  62. package/lib/AuthHandler.d.ts.map +1 -1
  63. package/lib/AuthHandler.js +6 -3
  64. package/lib/AuthHandler.ts +12 -7
  65. package/lib/DBEventsManager.d.ts.map +1 -1
  66. package/lib/DBEventsManager.js +5 -4
  67. package/lib/DBEventsManager.ts +10 -9
  68. package/lib/DBSchemaBuilder.js +14 -14
  69. package/lib/DBSchemaBuilder.ts +14 -14
  70. package/lib/DboBuilder/QueryBuilder/Functions.js +1 -1
  71. package/lib/DboBuilder/QueryBuilder/Functions.ts +2 -2
  72. package/lib/DboBuilder/QueryBuilder/makeSelectQuery.ts +1 -1
  73. package/lib/DboBuilder/TableHandler.d.ts +1 -5
  74. package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
  75. package/lib/DboBuilder/TableHandler.js +1 -2
  76. package/lib/DboBuilder/TableHandler.ts +3 -3
  77. package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
  78. package/lib/DboBuilder/ViewHandler.js +10 -8
  79. package/lib/DboBuilder/ViewHandler.ts +22 -20
  80. package/lib/DboBuilder/getCondition.js +1 -1
  81. package/lib/DboBuilder/getCondition.ts +2 -2
  82. package/lib/DboBuilder/insert.js +1 -1
  83. package/lib/DboBuilder/insert.ts +1 -1
  84. package/lib/DboBuilder/insertDataParse.js +4 -3
  85. package/lib/DboBuilder/insertDataParse.ts +15 -13
  86. package/lib/DboBuilder/subscribe.d.ts +2 -2
  87. package/lib/DboBuilder/subscribe.d.ts.map +1 -1
  88. package/lib/DboBuilder/subscribe.js +3 -4
  89. package/lib/DboBuilder/subscribe.ts +8 -9
  90. package/lib/DboBuilder.d.ts.map +1 -1
  91. package/lib/DboBuilder.js +5 -4
  92. package/lib/DboBuilder.ts +15 -21
  93. package/lib/FileManager.js +1 -1
  94. package/lib/FileManager.ts +5 -5
  95. package/lib/Filtering.ts +1 -1
  96. package/lib/JSONBValidation/validation.ts +3 -3
  97. package/lib/Prostgles.js +1 -1
  98. package/lib/Prostgles.ts +3 -3
  99. package/lib/PubSubManager/PubSubManager.d.ts +27 -29
  100. package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
  101. package/lib/PubSubManager/PubSubManager.js +54 -437
  102. package/lib/PubSubManager/PubSubManager.ts +93 -546
  103. package/lib/PubSubManager/addSub.d.ts +8 -0
  104. package/lib/PubSubManager/addSub.d.ts.map +1 -0
  105. package/lib/PubSubManager/addSub.js +162 -0
  106. package/lib/PubSubManager/addSub.ts +186 -0
  107. package/lib/PubSubManager/addSync.d.ts +8 -0
  108. package/lib/PubSubManager/addSync.d.ts.map +1 -0
  109. package/lib/PubSubManager/addSync.js +109 -0
  110. package/lib/PubSubManager/addSync.ts +127 -0
  111. package/lib/PubSubManager/notifListener.d.ts +5 -0
  112. package/lib/PubSubManager/notifListener.d.ts.map +1 -0
  113. package/lib/PubSubManager/notifListener.js +98 -0
  114. package/lib/PubSubManager/notifListener.ts +124 -0
  115. package/lib/PubSubManager/pushSubData.d.ts +3 -0
  116. package/lib/PubSubManager/pushSubData.d.ts.map +1 -0
  117. package/lib/PubSubManager/pushSubData.js +47 -0
  118. package/lib/PubSubManager/pushSubData.ts +47 -0
  119. package/lib/PublishParser.d.ts.map +1 -1
  120. package/lib/PublishParser.js +4 -3
  121. package/lib/PublishParser.ts +4 -3
  122. package/lib/TableConfig/TableConfig.ts +7 -7
  123. package/lib/TableConfig/getConstraintDefinitionQueries.ts +1 -1
  124. package/lib/TableConfig/getTableColumnQueries.ts +2 -2
  125. package/package.json +2 -2
  126. package/tests/client/PID.txt +1 -1
  127. package/tests/client/package-lock.json +15 -15
  128. package/tests/client/package.json +1 -1
  129. package/tests/client/tsconfig.json +2 -1
  130. package/tests/client_only_queries.js +1 -1
  131. package/tests/client_only_queries.ts +1 -1
  132. package/tests/isomorphic_queries.ts +1 -1
  133. package/tests/server/DBoGenerated.d.ts +2 -2
  134. package/tests/server/package-lock.json +3 -3
  135. package/tests/server/tsconfig.json +2 -1
  136. package/tsconfig.json +1 -1
@@ -0,0 +1,8 @@
1
+ import { SubscriptionChannels } from "prostgles-types";
2
+ import { PubSubManager, SubscriptionParams } from "./PubSubManager";
3
+ type AddSubscriptionParams = SubscriptionParams & {
4
+ condition: string;
5
+ };
6
+ export declare function addSub(this: PubSubManager, subscriptionParams: Omit<AddSubscriptionParams, "channel_name" | "parentSubParams">): Promise<SubscriptionChannels>;
7
+ export {};
8
+ //# sourceMappingURL=addSub.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addSub.d.ts","sourceRoot":"","sources":["addSub.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAiC,aAAa,EAAgB,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAEjH,KAAK,qBAAqB,GAAG,kBAAkB,GAAG;IAChD,SAAS,EAAE,MAAM,CAAC;CACnB,CAAA;AAID,wBAAsB,MAAM,CAAC,IAAI,EAAE,aAAa,EAAE,kBAAkB,EAAE,IAAI,CAAC,qBAAqB,EAAE,cAAc,GAAG,iBAAiB,CAAC,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAiHpK"}
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addSub = void 0;
4
+ const PubSubManager_1 = require("./PubSubManager");
5
+ /* Must return a channel for socket */
6
+ /* The distinct list of {table_name, condition} must have a corresponding trigger in the database */
7
+ async function addSub(subscriptionParams) {
8
+ const { socket, func, table_rules, filter = {}, params = {}, condition = "", throttle = 0, //subOne = false,
9
+ viewOptions, table_info, } = subscriptionParams || {};
10
+ const table_name = table_info.name;
11
+ if (!socket && !func) {
12
+ throw "socket AND func missing";
13
+ }
14
+ if (socket && func) {
15
+ throw "addSub: cannot have socket AND func";
16
+ }
17
+ let validated_throttle = subscriptionParams.throttle || 10;
18
+ const pubThrottle = table_rules?.subscribe?.throttle || 0;
19
+ if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
20
+ validated_throttle = pubThrottle;
21
+ }
22
+ if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
23
+ validated_throttle = throttle;
24
+ }
25
+ const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
26
+ const result = {
27
+ channelName: channel_name,
28
+ channelNameReady: channel_name + ".ready",
29
+ channelNameUnsubscribe: channel_name + ".unsubscribe"
30
+ };
31
+ this.upsertSocket(socket);
32
+ // const upsertSub = _upsertSub.bind(this);
33
+ const newSub = {
34
+ channel_name,
35
+ filter,
36
+ func,
37
+ params,
38
+ last_throttled: 0,
39
+ socket,
40
+ table_info,
41
+ is_ready: true,
42
+ is_throttling: false,
43
+ socket_id: socket?.id,
44
+ table_rules,
45
+ throttle: validated_throttle,
46
+ triggers: [
47
+ {
48
+ table_name: table_name,
49
+ condition: (0, PubSubManager_1.parseCondition)(condition),
50
+ is_related: false,
51
+ }
52
+ ]
53
+ };
54
+ if (viewOptions) {
55
+ for await (const relatedTable of viewOptions.relatedTables) {
56
+ const relatedSub = {
57
+ table_name: relatedTable.tableName,
58
+ condition: (0, PubSubManager_1.parseCondition)(relatedTable.condition),
59
+ is_related: true,
60
+ };
61
+ newSub.triggers.push(relatedSub);
62
+ await this.addTrigger(relatedSub, viewOptions);
63
+ }
64
+ }
65
+ if (func) {
66
+ this.pushSubData(newSub);
67
+ }
68
+ else if (socket) {
69
+ const removeListeners = () => {
70
+ socket.removeAllListeners(channel_name);
71
+ socket.removeAllListeners(result.channelNameReady);
72
+ socket.removeAllListeners(result.channelNameUnsubscribe);
73
+ };
74
+ removeListeners();
75
+ socket.once(result.channelNameReady, () => {
76
+ this.pushSubData(newSub);
77
+ });
78
+ socket.once(result.channelNameUnsubscribe, (_data, cb) => {
79
+ const res = "ok"; // this.onSocketDisconnected({ socket, subChannel: channel_name });
80
+ this.subs = this.subs.filter(s => {
81
+ const isMatch = s.socket && s.socket.id === socket.id && s.channel_name === channel_name;
82
+ return !isMatch;
83
+ });
84
+ removeListeners();
85
+ cb(null, { res });
86
+ });
87
+ }
88
+ this.subs.push(newSub);
89
+ /** A view does not have triggers. Only related triggers */
90
+ if (table_info.is_view) {
91
+ if (!viewOptions?.relatedTables.length) {
92
+ throw "PubSubManager: view parent_tables missing";
93
+ }
94
+ }
95
+ else {
96
+ await this.addTrigger({
97
+ table_name: table_info.name,
98
+ condition: (0, PubSubManager_1.parseCondition)(condition),
99
+ });
100
+ }
101
+ return result;
102
+ }
103
+ exports.addSub = addSub;
104
+ // const _upsertSub = async function(
105
+ // this: PubSubManager,
106
+ // newSubData: {
107
+ // table_name: string;
108
+ // condition: string;
109
+ // is_ready: boolean;
110
+ // channel_name: string;
111
+ // viewOptions: SubscriptionParams["viewOptions"];
112
+ // },
113
+ // consumer: Pick<SubscriptionParams, "channel_name" | "socket" | "func">,
114
+ // isReadyOverride: boolean | undefined
115
+ // ){
116
+ // const { table_name, condition: _cond, is_ready = false, viewOptions } = newSubData;
117
+ // const { channel_name, func, socket } = consumer;
118
+ // const condition = parseCondition(_cond);
119
+ // const newSub: SubscriptionParams = {
120
+ // socket,
121
+ // table_name,
122
+ // filter,
123
+ // params,
124
+ // table_rules,
125
+ // channel_name,
126
+ // parentSubParams,
127
+ // func: func ?? undefined,
128
+ // socket_id: socket?.id,
129
+ // throttle: validated_throttle,
130
+ // is_throttling: null,
131
+ // last_throttled: 0,
132
+ // is_ready,
133
+ // };
134
+ // this.subs[table_name] = this.subs[table_name] ?? {};
135
+ // this.subs[table_name]![condition] = this.subs[table_name]![condition] ?? { subs: [] };
136
+ // this.subs[table_name]![condition]!.subs = this.subs[table_name]![condition]!.subs ?? [];
137
+ // // console.log("1034 upsertSub", this.subs)
138
+ // const sub_idx = this.subs[table_name]![condition]!.subs.findIndex(s =>
139
+ // s.channel_name === channel_name &&
140
+ // (
141
+ // socket && s.socket_id === socket.id ||
142
+ // func && s.func === func
143
+ // ) &&
144
+ // JSON.stringify(s.viewOptions) === JSON.stringify((subscriptionParams.viewOptions)
145
+ // ));
146
+ // if (sub_idx < 0) {
147
+ // this.subs[table_name]![condition]!.subs.push(newSub);
148
+ // if (socket) {
149
+ // const chnUnsub = channel_name + "unsubscribe";
150
+ // socket.removeAllListeners(chnUnsub);
151
+ // socket.once(chnUnsub, (_data: any, cb: BasicCallback) => {
152
+ // const res = this.onSocketDisconnected({ socket, subChannel: channel_name });
153
+ // cb(null, { res });
154
+ // });
155
+ // }
156
+ // } else {
157
+ // this.subs[table_name]![condition]!.subs[sub_idx] = newSub;
158
+ // }
159
+ // if (isReadyOverride ?? is_ready) {
160
+ // this.pushSubData(newSub);
161
+ // }
162
+ // };
@@ -0,0 +1,186 @@
1
+ import { SubscriptionChannels } from "prostgles-types";
2
+ import { BasicCallback, parseCondition, PubSubManager, Subscription, SubscriptionParams } from "./PubSubManager";
3
+
4
+ type AddSubscriptionParams = SubscriptionParams & {
5
+ condition: string;
6
+ }
7
+
8
+ /* Must return a channel for socket */
9
+ /* The distinct list of {table_name, condition} must have a corresponding trigger in the database */
10
+ export async function addSub(this: PubSubManager, subscriptionParams: Omit<AddSubscriptionParams, "channel_name" | "parentSubParams">): Promise<SubscriptionChannels> {
11
+ const {
12
+ socket, func, table_rules, filter = {},
13
+ params = {}, condition = "", throttle = 0, //subOne = false,
14
+ viewOptions, table_info,
15
+ } = subscriptionParams || {};
16
+ const table_name = table_info.name;
17
+
18
+ if (!socket && !func) {
19
+ throw "socket AND func missing";
20
+ }
21
+ if (socket && func) {
22
+ throw "addSub: cannot have socket AND func";
23
+ }
24
+
25
+ let validated_throttle = subscriptionParams.throttle || 10;
26
+ const pubThrottle = table_rules?.subscribe?.throttle || 0;
27
+ if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
28
+ validated_throttle = pubThrottle;
29
+ }
30
+ if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
31
+ validated_throttle = throttle;
32
+ }
33
+
34
+ const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
35
+ const result: SubscriptionChannels = {
36
+ channelName: channel_name,
37
+ channelNameReady: channel_name + ".ready",
38
+ channelNameUnsubscribe: channel_name + ".unsubscribe"
39
+ }
40
+
41
+ this.upsertSocket(socket);
42
+
43
+ // const upsertSub = _upsertSub.bind(this);
44
+
45
+ const newSub: Subscription = {
46
+ channel_name,
47
+ filter,
48
+ func,
49
+ params,
50
+ last_throttled: 0,
51
+ socket,
52
+ table_info,
53
+ is_ready: true,
54
+ is_throttling: false,
55
+ socket_id: socket?.id,
56
+ table_rules,
57
+ throttle: validated_throttle,
58
+ triggers: [
59
+ {
60
+ table_name: table_name,
61
+ condition: parseCondition(condition),
62
+ is_related: false,
63
+ }
64
+ ]
65
+ }
66
+
67
+ if(viewOptions){
68
+ for await(const relatedTable of viewOptions.relatedTables){
69
+ const relatedSub = {
70
+ table_name: relatedTable.tableName,
71
+ condition: parseCondition(relatedTable.condition),
72
+ is_related: true,
73
+ } as const;
74
+
75
+ newSub.triggers.push(relatedSub)
76
+
77
+ await this.addTrigger(relatedSub, viewOptions);
78
+ }
79
+
80
+ }
81
+
82
+ if(func){
83
+ this.pushSubData(newSub);
84
+
85
+ } else if (socket) {
86
+ const removeListeners = () => {
87
+ socket.removeAllListeners(channel_name);
88
+ socket.removeAllListeners(result.channelNameReady);
89
+ socket.removeAllListeners(result.channelNameUnsubscribe);
90
+ }
91
+ removeListeners();
92
+
93
+ socket.once(result.channelNameReady, () => {
94
+ this.pushSubData(newSub);
95
+ });
96
+ socket.once(result.channelNameUnsubscribe, (_data: any, cb: BasicCallback) => {
97
+ const res = "ok";// this.onSocketDisconnected({ socket, subChannel: channel_name });
98
+ this.subs = this.subs.filter(s => {
99
+ const isMatch = s.socket && s.socket.id === socket.id && s.channel_name === channel_name;
100
+ return !isMatch;
101
+ });
102
+ removeListeners();
103
+ cb(null, { res });
104
+ });
105
+ }
106
+
107
+ this.subs.push(newSub);
108
+
109
+ /** A view does not have triggers. Only related triggers */
110
+ if (table_info.is_view) {
111
+ if (!viewOptions?.relatedTables.length) {
112
+ throw "PubSubManager: view parent_tables missing";
113
+ }
114
+
115
+ } else {
116
+ await this.addTrigger({
117
+ table_name: table_info.name,
118
+ condition: parseCondition(condition),
119
+ });
120
+ }
121
+
122
+ return result;
123
+ }
124
+
125
+ // const _upsertSub = async function(
126
+ // this: PubSubManager,
127
+ // newSubData: {
128
+ // table_name: string;
129
+ // condition: string;
130
+ // is_ready: boolean;
131
+ // channel_name: string;
132
+ // viewOptions: SubscriptionParams["viewOptions"];
133
+ // },
134
+ // consumer: Pick<SubscriptionParams, "channel_name" | "socket" | "func">,
135
+ // isReadyOverride: boolean | undefined
136
+ // ){
137
+ // const { table_name, condition: _cond, is_ready = false, viewOptions } = newSubData;
138
+ // const { channel_name, func, socket } = consumer;
139
+ // const condition = parseCondition(_cond);
140
+ // const newSub: SubscriptionParams = {
141
+ // socket,
142
+ // table_name,
143
+ // filter,
144
+ // params,
145
+ // table_rules,
146
+ // channel_name,
147
+ // parentSubParams,
148
+ // func: func ?? undefined,
149
+ // socket_id: socket?.id,
150
+ // throttle: validated_throttle,
151
+ // is_throttling: null,
152
+ // last_throttled: 0,
153
+ // is_ready,
154
+ // };
155
+
156
+ // this.subs[table_name] = this.subs[table_name] ?? {};
157
+ // this.subs[table_name]![condition] = this.subs[table_name]![condition] ?? { subs: [] };
158
+ // this.subs[table_name]![condition]!.subs = this.subs[table_name]![condition]!.subs ?? [];
159
+
160
+ // // console.log("1034 upsertSub", this.subs)
161
+ // const sub_idx = this.subs[table_name]![condition]!.subs.findIndex(s =>
162
+ // s.channel_name === channel_name &&
163
+ // (
164
+ // socket && s.socket_id === socket.id ||
165
+ // func && s.func === func
166
+ // ) &&
167
+ // JSON.stringify(s.viewOptions) === JSON.stringify((subscriptionParams.viewOptions)
168
+ // ));
169
+ // if (sub_idx < 0) {
170
+ // this.subs[table_name]![condition]!.subs.push(newSub);
171
+ // if (socket) {
172
+ // const chnUnsub = channel_name + "unsubscribe";
173
+ // socket.removeAllListeners(chnUnsub);
174
+ // socket.once(chnUnsub, (_data: any, cb: BasicCallback) => {
175
+ // const res = this.onSocketDisconnected({ socket, subChannel: channel_name });
176
+ // cb(null, { res });
177
+ // });
178
+ // }
179
+ // } else {
180
+ // this.subs[table_name]![condition]!.subs[sub_idx] = newSub;
181
+ // }
182
+
183
+ // if (isReadyOverride ?? is_ready) {
184
+ // this.pushSubData(newSub);
185
+ // }
186
+ // };
@@ -0,0 +1,8 @@
1
+ import { PubSubManager } from "./PubSubManager";
2
+ import { AddSyncParams } from "./PubSubManager";
3
+ /**
4
+ * Returns a sync channel
5
+ * A sync channel is unique per socket for each filter
6
+ */
7
+ export declare function addSync(this: PubSubManager, syncParams: AddSyncParams): Promise<string>;
8
+ //# sourceMappingURL=addSync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addSync.d.ts","sourceRoot":"","sources":["addSync.ts"],"names":[],"mappings":"AACA,OAAO,EAAiC,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,aAAa,EAA2B,MAAM,iBAAiB,CAAC;AAEzE;;;GAGG;AACH,wBAAsB,OAAO,CAAC,IAAI,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,mBAsH3E"}
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addSync = void 0;
4
+ const util_1 = require("prostgles-types/dist/util");
5
+ const PubSubManager_1 = require("./PubSubManager");
6
+ const PubSubManager_2 = require("./PubSubManager");
7
+ /**
8
+ * Returns a sync channel
9
+ * A sync channel is unique per socket for each filter
10
+ */
11
+ async function addSync(syncParams) {
12
+ const { socket = null, table_info = null, table_rules, synced_field = null, allow_delete = false, id_fields = [], filter = {}, params, condition = "", throttle = 0 } = syncParams || {};
13
+ const conditionParsed = (0, PubSubManager_1.parseCondition)(condition);
14
+ if (!socket || !table_info)
15
+ throw "socket or table_info missing";
16
+ const { name: table_name } = table_info;
17
+ const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
18
+ if (!synced_field)
19
+ throw "synced_field missing from table_rules";
20
+ this.upsertSocket(socket);
21
+ const upsertSync = () => {
22
+ const newSync = {
23
+ channel_name,
24
+ table_name,
25
+ filter,
26
+ condition: conditionParsed,
27
+ synced_field,
28
+ id_fields,
29
+ allow_delete,
30
+ table_rules,
31
+ throttle: Math.max(throttle || 0, table_rules?.sync?.throttle || 0),
32
+ batch_size: table_rules?.sync?.batch_size || PubSubManager_2.DEFAULT_SYNC_BATCH_SIZE,
33
+ last_throttled: 0,
34
+ socket_id: socket.id,
35
+ is_sync: true,
36
+ last_synced: 0,
37
+ lr: undefined,
38
+ table_info,
39
+ is_syncing: false,
40
+ wal: undefined,
41
+ socket,
42
+ params
43
+ };
44
+ /* Only a sync per socket per table per condition allowed */
45
+ this.syncs = this.syncs || [];
46
+ const existing = (0, util_1.find)(this.syncs, { socket_id: socket.id, channel_name });
47
+ if (!existing) {
48
+ this.syncs.push(newSync);
49
+ // console.log("Added SYNC");
50
+ const unsyncChn = channel_name + "unsync";
51
+ socket.removeAllListeners(unsyncChn);
52
+ socket.once(unsyncChn, (_data, cb) => {
53
+ // this.onSocketDisconnected({ socket, syncChannel: channel_name });
54
+ socket.removeAllListeners(channel_name);
55
+ socket.removeAllListeners(unsyncChn);
56
+ this.syncs = this.syncs.filter(s => {
57
+ const isMatch = s.socket_id && s.socket_id === socket.id && s.channel_name === channel_name;
58
+ return !isMatch;
59
+ });
60
+ cb(null, { res: "ok" });
61
+ });
62
+ socket.removeAllListeners(channel_name);
63
+ socket.on(channel_name, (data, cb) => {
64
+ if (!data) {
65
+ cb({ err: "Unexpected request. Need data or onSyncRequest" });
66
+ return;
67
+ }
68
+ /*
69
+ */
70
+ /* Server will:
71
+ 1. Ask for last_synced emit(onSyncRequest)
72
+ 2. Ask for data >= server_synced emit(onPullRequest)
73
+ -> Upsert that data
74
+ 2. Push data >= last_synced emit(data.data)
75
+
76
+ Client will:
77
+ 1. Send last_synced on(onSyncRequest)
78
+ 2. Send data >= server_synced on(onPullRequest)
79
+ 3. Send data on CRUD emit(data.data | data.deleted)
80
+ 4. Upsert data.data | deleted on(data.data | data.deleted)
81
+ */
82
+ // if(data.data){
83
+ // console.error("THIS SHOUKD NEVER FIRE !! NEW DATA FROM SYNC");
84
+ // this.upsertClientData(newSync, data.data);
85
+ // } else
86
+ if (data.onSyncRequest) {
87
+ // console.log("syncData from socket")
88
+ this.syncData(newSync, data.onSyncRequest, "client");
89
+ // console.log("onSyncRequest ", socket._user)
90
+ }
91
+ else {
92
+ console.error("Unexpected sync request data from client: ", data);
93
+ }
94
+ });
95
+ // socket.emit(channel_name, { onSyncRequest: true }, (response) => {
96
+ // console.log(response)
97
+ // });
98
+ }
99
+ else {
100
+ console.warn("UNCLOSED DUPLICATE SYNC FOUND", existing.channel_name);
101
+ }
102
+ return newSync;
103
+ };
104
+ // const { min_id, max_id, count, max_synced } = params;
105
+ const _sync = upsertSync();
106
+ await this.addTrigger({ table_name, condition: conditionParsed });
107
+ return channel_name;
108
+ }
109
+ exports.addSync = addSync;
@@ -0,0 +1,127 @@
1
+ import { find } from "prostgles-types/dist/util";
2
+ import { BasicCallback, parseCondition, PubSubManager } from "./PubSubManager";
3
+ import { AddSyncParams, DEFAULT_SYNC_BATCH_SIZE } from "./PubSubManager";
4
+
5
+ /**
6
+ * Returns a sync channel
7
+ * A sync channel is unique per socket for each filter
8
+ */
9
+ export async function addSync(this: PubSubManager, syncParams: AddSyncParams) {
10
+ const {
11
+ socket = null, table_info = null, table_rules, synced_field = null,
12
+ allow_delete = false, id_fields = [], filter = {},
13
+ params, condition = "", throttle = 0
14
+ } = syncParams || {};
15
+
16
+ const conditionParsed = parseCondition(condition);
17
+ if (!socket || !table_info) throw "socket or table_info missing";
18
+
19
+
20
+ const { name: table_name } = table_info;
21
+ const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.sync`;
22
+
23
+ if (!synced_field) throw "synced_field missing from table_rules";
24
+
25
+ this.upsertSocket(socket);
26
+
27
+ const upsertSync = () => {
28
+ const newSync = {
29
+ channel_name,
30
+ table_name,
31
+ filter,
32
+ condition: conditionParsed,
33
+ synced_field,
34
+ id_fields,
35
+ allow_delete,
36
+ table_rules,
37
+ throttle: Math.max(throttle || 0, table_rules?.sync?.throttle || 0),
38
+ batch_size: table_rules?.sync?.batch_size || DEFAULT_SYNC_BATCH_SIZE,
39
+ last_throttled: 0,
40
+ socket_id: socket.id,
41
+ is_sync: true,
42
+ last_synced: 0,
43
+ lr: undefined,
44
+ table_info,
45
+ is_syncing: false,
46
+ wal: undefined,
47
+ socket,
48
+ params
49
+ };
50
+
51
+ /* Only a sync per socket per table per condition allowed */
52
+ this.syncs = this.syncs || [];
53
+ const existing = find(this.syncs, { socket_id: socket.id, channel_name });
54
+ if (!existing) {
55
+ this.syncs.push(newSync);
56
+ // console.log("Added SYNC");
57
+
58
+ const unsyncChn = channel_name + "unsync";
59
+ socket.removeAllListeners(unsyncChn);
60
+ socket.once(unsyncChn, (_data: any, cb: BasicCallback) => {
61
+ // this.onSocketDisconnected({ socket, syncChannel: channel_name });
62
+ socket.removeAllListeners(channel_name);
63
+ socket.removeAllListeners(unsyncChn);
64
+ this.syncs = this.syncs.filter(s => {
65
+ const isMatch = s.socket_id && s.socket_id === socket.id && s.channel_name === channel_name;
66
+ return !isMatch;
67
+ });
68
+ cb(null, { res: "ok" })
69
+ });
70
+
71
+ socket.removeAllListeners(channel_name);
72
+ socket.on(channel_name, (data: any, cb: BasicCallback) => {
73
+
74
+ if (!data) {
75
+ cb({ err: "Unexpected request. Need data or onSyncRequest" });
76
+ return;
77
+ }
78
+
79
+ /*
80
+ */
81
+
82
+ /* Server will:
83
+ 1. Ask for last_synced emit(onSyncRequest)
84
+ 2. Ask for data >= server_synced emit(onPullRequest)
85
+ -> Upsert that data
86
+ 2. Push data >= last_synced emit(data.data)
87
+
88
+ Client will:
89
+ 1. Send last_synced on(onSyncRequest)
90
+ 2. Send data >= server_synced on(onPullRequest)
91
+ 3. Send data on CRUD emit(data.data | data.deleted)
92
+ 4. Upsert data.data | deleted on(data.data | data.deleted)
93
+ */
94
+
95
+ // if(data.data){
96
+ // console.error("THIS SHOUKD NEVER FIRE !! NEW DATA FROM SYNC");
97
+ // this.upsertClientData(newSync, data.data);
98
+ // } else
99
+ if (data.onSyncRequest) {
100
+ // console.log("syncData from socket")
101
+ this.syncData(newSync, data.onSyncRequest, "client");
102
+
103
+ // console.log("onSyncRequest ", socket._user)
104
+ } else {
105
+ console.error("Unexpected sync request data from client: ", data)
106
+ }
107
+ });
108
+
109
+ // socket.emit(channel_name, { onSyncRequest: true }, (response) => {
110
+ // console.log(response)
111
+ // });
112
+ } else {
113
+ console.warn("UNCLOSED DUPLICATE SYNC FOUND", existing.channel_name);
114
+ }
115
+
116
+ return newSync;
117
+ };
118
+
119
+
120
+ // const { min_id, max_id, count, max_synced } = params;
121
+
122
+ const _sync = upsertSync();
123
+
124
+ await this.addTrigger({ table_name, condition: conditionParsed });
125
+
126
+ return channel_name;
127
+ }
@@ -0,0 +1,5 @@
1
+ import { PubSubManager } from "./PubSubManager";
2
+ export declare function notifListener(this: PubSubManager, data: {
3
+ payload: string;
4
+ }): Promise<void>;
5
+ //# sourceMappingURL=notifListener.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notifListener.d.ts","sourceRoot":"","sources":["notifListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,wBAAsB,aAAa,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,iBAwHjF"}