prostgles-server 4.2.265 → 4.2.267

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 (148) hide show
  1. package/dist/DboBuilder/TableHandler/DataValidator.js +5 -5
  2. package/dist/DboBuilder/TableHandler/DataValidator.js.map +1 -1
  3. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.d.ts.map +1 -1
  4. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js +1 -2
  5. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js.map +1 -1
  6. package/dist/DboBuilder/TableHandler/insertTest.js +2 -2
  7. package/dist/DboBuilder/TableHandler/insertTest.js.map +1 -1
  8. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js +1 -2
  10. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler/updateFile.d.ts.map +1 -1
  12. package/dist/DboBuilder/TableHandler/updateFile.js +1 -2
  13. package/dist/DboBuilder/TableHandler/updateFile.js.map +1 -1
  14. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -1
  15. package/dist/DboBuilder/ViewHandler/getValidatedSubscribeOptions.d.ts +4 -0
  16. package/dist/DboBuilder/ViewHandler/getValidatedSubscribeOptions.d.ts.map +1 -0
  17. package/dist/DboBuilder/ViewHandler/getValidatedSubscribeOptions.js +65 -0
  18. package/dist/DboBuilder/ViewHandler/getValidatedSubscribeOptions.js.map +1 -0
  19. package/dist/DboBuilder/ViewHandler/parseComplexFilter.d.ts.map +1 -1
  20. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js +6 -8
  21. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js.map +1 -1
  22. package/dist/DboBuilder/ViewHandler/subscribe.d.ts.map +1 -1
  23. package/dist/DboBuilder/ViewHandler/subscribe.js +5 -5
  24. package/dist/DboBuilder/ViewHandler/subscribe.js.map +1 -1
  25. package/dist/DboBuilder/dboBuilderUtils.d.ts.map +1 -1
  26. package/dist/DboBuilder/dboBuilderUtils.js +1 -2
  27. package/dist/DboBuilder/dboBuilderUtils.js.map +1 -1
  28. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -1
  29. package/dist/DboBuilder/getSubscribeRelatedTables.js +2 -2
  30. package/dist/DboBuilder/getSubscribeRelatedTables.js.map +1 -1
  31. package/dist/JSONBValidation/JSONBValidation.d.ts.map +1 -1
  32. package/dist/JSONBValidation/JSONBValidation.js +105 -13
  33. package/dist/JSONBValidation/JSONBValidation.js.map +1 -1
  34. package/dist/JSONBValidation/JSONBValidation.spec.js +17 -3
  35. package/dist/JSONBValidation/JSONBValidation.spec.js.map +1 -1
  36. package/dist/JSONBValidation/getJSONBSchemaTSTypes.d.ts +1 -0
  37. package/dist/JSONBValidation/getJSONBSchemaTSTypes.d.ts.map +1 -1
  38. package/dist/JSONBValidation/getJSONBSchemaTSTypes.js +98 -97
  39. package/dist/JSONBValidation/getJSONBSchemaTSTypes.js.map +1 -1
  40. package/dist/JSONBValidation/validate_jsonb_schema_sql.js +3 -3
  41. package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -1
  42. package/dist/Logging.d.ts +2 -1
  43. package/dist/Logging.d.ts.map +1 -1
  44. package/dist/PubSubManager/PubSubManager.d.ts +7 -44
  45. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  46. package/dist/PubSubManager/PubSubManager.js +7 -38
  47. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  48. package/dist/PubSubManager/PubSubManagerUtils.d.ts +22 -0
  49. package/dist/PubSubManager/PubSubManagerUtils.d.ts.map +1 -0
  50. package/dist/PubSubManager/PubSubManagerUtils.js +33 -0
  51. package/dist/PubSubManager/PubSubManagerUtils.js.map +1 -0
  52. package/dist/PubSubManager/addSub.d.ts +1 -1
  53. package/dist/PubSubManager/addSub.d.ts.map +1 -1
  54. package/dist/PubSubManager/addSub.js +20 -24
  55. package/dist/PubSubManager/addSub.js.map +1 -1
  56. package/dist/PubSubManager/addSync.d.ts.map +1 -1
  57. package/dist/PubSubManager/addSync.js +3 -3
  58. package/dist/PubSubManager/addSync.js.map +1 -1
  59. package/dist/PubSubManager/addTrigger.d.ts +1 -1
  60. package/dist/PubSubManager/addTrigger.d.ts.map +1 -1
  61. package/dist/PubSubManager/addTrigger.js +9 -9
  62. package/dist/PubSubManager/addTrigger.js.map +1 -1
  63. package/dist/PubSubManager/deleteOrphanedTriggers.d.ts.map +1 -1
  64. package/dist/PubSubManager/deleteOrphanedTriggers.js +2 -2
  65. package/dist/PubSubManager/deleteOrphanedTriggers.js.map +1 -1
  66. package/dist/PubSubManager/getPubSubManagerInitQuery.d.ts.map +1 -1
  67. package/dist/PubSubManager/getPubSubManagerInitQuery.js +26 -23
  68. package/dist/PubSubManager/getPubSubManagerInitQuery.js.map +1 -1
  69. package/dist/PubSubManager/initPubSubManager.d.ts +1 -1
  70. package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
  71. package/dist/PubSubManager/initPubSubManager.js +3 -3
  72. package/dist/PubSubManager/initPubSubManager.js.map +1 -1
  73. package/dist/PubSubManager/initialiseEventTriggers.d.ts +1 -1
  74. package/dist/PubSubManager/initialiseEventTriggers.d.ts.map +1 -1
  75. package/dist/PubSubManager/initialiseEventTriggers.js +2 -2
  76. package/dist/PubSubManager/initialiseEventTriggers.js.map +1 -1
  77. package/dist/PubSubManager/notifListener.d.ts.map +1 -1
  78. package/dist/PubSubManager/notifListener.js +20 -14
  79. package/dist/PubSubManager/notifListener.js.map +1 -1
  80. package/dist/PubSubManager/orphanTriggerCheck.d.ts.map +1 -1
  81. package/dist/PubSubManager/orphanTriggerCheck.js +2 -2
  82. package/dist/PubSubManager/orphanTriggerCheck.js.map +1 -1
  83. package/dist/PubSubManager/pushSubData.d.ts +1 -1
  84. package/dist/PubSubManager/pushSubData.d.ts.map +1 -1
  85. package/dist/PubSubManager/pushSubData.js +3 -3
  86. package/dist/PubSubManager/pushSubData.js.map +1 -1
  87. package/dist/PublishParser/getTableRulesWithoutFileTable.js +2 -2
  88. package/dist/PublishParser/getTableRulesWithoutFileTable.js.map +1 -1
  89. package/dist/SchemaWatch/SchemaWatch.d.ts.map +1 -1
  90. package/dist/SchemaWatch/SchemaWatch.js +4 -5
  91. package/dist/SchemaWatch/SchemaWatch.js.map +1 -1
  92. package/dist/SyncReplication.d.ts +1 -1
  93. package/dist/SyncReplication.d.ts.map +1 -1
  94. package/dist/SyncReplication.js +7 -7
  95. package/dist/SyncReplication.js.map +1 -1
  96. package/dist/TableConfig/applyTableConfig.js +2 -2
  97. package/dist/TableConfig/applyTableConfig.js.map +1 -1
  98. package/dist/TableConfig/getColumnDefinitionQuery.js +7 -7
  99. package/dist/TableConfig/getColumnDefinitionQuery.js.map +1 -1
  100. package/dist/TableConfig/getConstraintDefinitionQueries.js +4 -4
  101. package/dist/TableConfig/getConstraintDefinitionQueries.js.map +1 -1
  102. package/dist/TableConfig/getSchemaDiffQueries.js +2 -2
  103. package/dist/TableConfig/getSchemaDiffQueries.js.map +1 -1
  104. package/dist/TableConfig/initTableConfig.d.ts.map +1 -1
  105. package/dist/TableConfig/initTableConfig.js +5 -5
  106. package/dist/TableConfig/initTableConfig.js.map +1 -1
  107. package/dist/TableConfig/runMigrations.js +2 -2
  108. package/dist/TableConfig/runMigrations.js.map +1 -1
  109. package/dist/TableConfig/tableConfigSchemaUtils.js +3 -3
  110. package/dist/TableConfig/tableConfigSchemaUtils.js.map +1 -1
  111. package/lib/DboBuilder/TableHandler/DataValidator.ts +1 -1
  112. package/lib/DboBuilder/TableHandler/insert/insertNestedRecords.ts +1 -1
  113. package/lib/DboBuilder/TableHandler/insertTest.ts +1 -1
  114. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +1 -2
  115. package/lib/DboBuilder/TableHandler/updateFile.ts +1 -2
  116. package/lib/DboBuilder/ViewHandler/getValidatedSubscribeOptions.ts +72 -0
  117. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +7 -13
  118. package/lib/DboBuilder/ViewHandler/subscribe.ts +9 -6
  119. package/lib/DboBuilder/dboBuilderUtils.ts +1 -1
  120. package/lib/DboBuilder/getSubscribeRelatedTables.ts +2 -1
  121. package/lib/JSONBValidation/JSONBValidation.spec.ts +38 -3
  122. package/lib/JSONBValidation/JSONBValidation.ts +102 -15
  123. package/lib/JSONBValidation/getJSONBSchemaTSTypes.ts +111 -111
  124. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +3 -3
  125. package/lib/Logging.ts +2 -1
  126. package/lib/PubSubManager/PubSubManager.ts +11 -50
  127. package/lib/PubSubManager/PubSubManagerUtils.ts +37 -0
  128. package/lib/PubSubManager/addSub.ts +21 -30
  129. package/lib/PubSubManager/addSync.ts +2 -7
  130. package/lib/PubSubManager/addTrigger.ts +3 -2
  131. package/lib/PubSubManager/deleteOrphanedTriggers.ts +2 -1
  132. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +29 -39
  133. package/lib/PubSubManager/initPubSubManager.ts +2 -1
  134. package/lib/PubSubManager/initialiseEventTriggers.ts +3 -2
  135. package/lib/PubSubManager/notifListener.ts +20 -6
  136. package/lib/PubSubManager/orphanTriggerCheck.ts +2 -1
  137. package/lib/PubSubManager/pushSubData.ts +3 -2
  138. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +1 -1
  139. package/lib/SchemaWatch/SchemaWatch.ts +2 -5
  140. package/lib/SyncReplication.ts +3 -2
  141. package/lib/TableConfig/applyTableConfig.ts +2 -2
  142. package/lib/TableConfig/getColumnDefinitionQuery.ts +1 -1
  143. package/lib/TableConfig/getConstraintDefinitionQueries.ts +1 -1
  144. package/lib/TableConfig/getSchemaDiffQueries.ts +1 -1
  145. package/lib/TableConfig/initTableConfig.ts +7 -4
  146. package/lib/TableConfig/runMigrations.ts +2 -2
  147. package/lib/TableConfig/tableConfigSchemaUtils.ts +2 -2
  148. package/package.json +2 -2
@@ -1,5 +1,5 @@
1
1
  import { DATA_TYPES } from "prostgles-types";
2
- import { PubSubManager } from "../PubSubManager/PubSubManager";
2
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "../PubSubManager/PubSubManagerUtils";
3
3
 
4
4
  const raiseException = (err: string) => `
5
5
  IF (context->'silent')::BOOLEAN = TRUE THEN
@@ -15,7 +15,7 @@ export const JSONB_DATA_TYPES = [...DATA_TYPES, "Lookup", "Lookup[]"] as const;
15
15
  export const validate_jsonb_schema_sql = `
16
16
 
17
17
  /*
18
- * ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
18
+ * ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
19
19
  */
20
20
 
21
21
  CREATE OR REPLACE FUNCTION ${VALIDATE_SCHEMA_FUNCNAME}(
@@ -402,7 +402,7 @@ BEGIN
402
402
  END;
403
403
  $f$ LANGUAGE 'plpgsql' IMMUTABLE;
404
404
 
405
- COMMENT ON FUNCTION ${VALIDATE_SCHEMA_FUNCNAME} /* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
405
+ COMMENT ON FUNCTION ${VALIDATE_SCHEMA_FUNCNAME} /* ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
406
406
  IS $$prostgles-server internal function used in column CHECK conditions to validate jsonb data against a column schema specified in tableConfig.
407
407
  Example usage:
408
408
  validate_jsonb_schema(
package/lib/Logging.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { AnyObject, ClientSchema, TableHandler } from "prostgles-types";
2
2
  import { LocalParams } from "./DboBuilder/DboBuilder";
3
- import { NotifTypeName, PubSubManagerTriggers } from "./PubSubManager/PubSubManager";
3
+ import { PubSubManagerTriggers } from "./PubSubManager/PubSubManager";
4
+ import type { NotifTypeName } from "./PubSubManager/PubSubManagerUtils";
4
5
 
5
6
  type ClientInfo = {
6
7
  socketId: string | undefined;
@@ -19,19 +19,16 @@ import { initialiseEventTriggers } from "./initialiseEventTriggers";
19
19
  import { initPubSubManager } from "./initPubSubManager";
20
20
  import { refreshTriggers } from "./refreshTriggers";
21
21
 
22
- import * as pgPromise from "pg-promise";
23
- import pg from "pg-promise/typescript/pg-subset";
24
-
25
22
  import {
26
23
  AnyObject,
27
24
  CHANNELS,
28
25
  FieldFilter,
29
26
  SelectParams,
30
- SubscribeParams,
31
27
  WAL,
28
+ type SubscribeOptions,
32
29
  } from "prostgles-types";
33
30
 
34
- import { find, pickKeys } from "prostgles-types/dist/util";
31
+ import { find, pickKeys } from "prostgles-types";
35
32
  import { LocalFuncs, getOnDataFunc, matchesLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
36
33
  import { EventTypes } from "../Logging";
37
34
  import { ParsedTableRule } from "../PublishParser/PublishParser";
@@ -39,18 +36,7 @@ import { syncData } from "../SyncReplication";
39
36
  import { addSub } from "./addSub";
40
37
  import { notifListener } from "./notifListener";
41
38
  import { pushSubData } from "./pushSubData";
42
-
43
- type PGP = pgPromise.IMain<{}, pg.IClient>;
44
- export const pgp: PGP = pgPromise({});
45
- export const asValue = (v: any) => pgp.as.format("$1", [v]);
46
- export const DEFAULT_SYNC_BATCH_SIZE = 50;
47
-
48
- export const log = (...args: any[]) => {
49
- if (process.env.TEST_TYPE) {
50
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
51
- console.log(...args);
52
- }
53
- };
39
+ import { log } from "./PubSubManagerUtils";
54
40
 
55
41
  export type BasicCallback = (err?: any, res?: any) => void;
56
42
 
@@ -109,7 +95,7 @@ export type ViewSubscriptionOptions = (
109
95
  }[];
110
96
  };
111
97
 
112
- export type SubscriptionParams = Pick<SubscribeParams, "throttle" | "throttleOpts"> & {
98
+ export type SubscriptionParams = {
113
99
  socket_id?: string;
114
100
  channel_name: string;
115
101
 
@@ -124,22 +110,23 @@ export type SubscriptionParams = Pick<SubscribeParams, "throttle" | "throttleOpt
124
110
  /* Used as input */
125
111
  table_rules?: ParsedTableRule;
126
112
  filter: object;
127
- params: SelectParams;
113
+ selectParams: SelectParams;
114
+ subscribeOptions: SubscribeOptions;
128
115
 
129
116
  localFuncs?: LocalFuncs;
130
117
  socket: PRGLIOSocket | undefined;
131
118
 
132
- last_throttled: number;
119
+ lastPushed: number;
133
120
  is_throttling?: any;
134
121
  is_ready?: boolean;
135
122
  };
136
123
 
137
124
  export type Subscription = Pick<
138
125
  SubscriptionParams,
139
- | "throttle"
126
+ | "selectParams"
127
+ | "subscribeOptions"
140
128
  | "is_throttling"
141
- | "last_throttled"
142
- | "throttleOpts"
129
+ | "lastPushed"
143
130
  | "channel_name"
144
131
  | "is_ready"
145
132
  | "localFuncs"
@@ -147,7 +134,6 @@ export type Subscription = Pick<
147
134
  | "socket_id"
148
135
  | "table_info"
149
136
  | "filter"
150
- | "params"
151
137
  | "table_rules"
152
138
  > & {
153
139
  triggers: {
@@ -163,11 +149,6 @@ export type PubSubManagerTriggers = Record<string, { condition: string; hash: st
163
149
  * Used to facilitate table subscribe and sync
164
150
  */
165
151
  export class PubSubManager {
166
- static DELIMITER = "|$prstgls$|" as const;
167
-
168
- static EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID =
169
- "prostgles internal query that should be excluded from schema watch " as const;
170
-
171
152
  public static create = async (dboBuilder: DboBuilder) => {
172
153
  const instance = new PubSubManager(dboBuilder);
173
154
  const result = await initPubSubManager.bind(instance)();
@@ -298,7 +279,7 @@ export class PubSubManager {
298
279
  getSubData = async (
299
280
  sub: Subscription
300
281
  ): Promise<{ data: any[]; err?: undefined } | { data?: undefined; err: any }> => {
301
- const { table_info, filter, params, table_rules } = sub; //, subOne = false
282
+ const { table_info, filter, selectParams: params, table_rules } = sub; //, subOne = false
302
283
  const { name: table_name } = table_info;
303
284
 
304
285
  if (!this.dbo[table_name]?.find) {
@@ -402,21 +383,6 @@ export class PubSubManager {
402
383
  addTrigger = addTrigger.bind(this);
403
384
  }
404
385
 
405
- export const NOTIF_TYPE = {
406
- data: "data_has_changed",
407
- data_trigger_change: "data_watch_triggers_have_changed",
408
- schema: "schema_has_changed",
409
- } as const;
410
-
411
- export type NotifTypeName = (typeof NOTIF_TYPE)[keyof typeof NOTIF_TYPE];
412
- export const NOTIF_CHANNEL = {
413
- preffix: "prostgles_" as const,
414
- getFull: (appID: string | undefined) => {
415
- if (!appID) throw "No appID";
416
- return NOTIF_CHANNEL.preffix + appID;
417
- },
418
- };
419
-
420
386
  function debounce<Params extends any[]>(
421
387
  func: (...args: Params) => any,
422
388
  timeout: number
@@ -429,8 +395,3 @@ function debounce<Params extends any[]>(
429
395
  }, timeout);
430
396
  };
431
397
  }
432
-
433
- export const parseCondition = (condition: string): string =>
434
- condition && condition.trim().length ? condition : "TRUE";
435
-
436
- export { omitKeys, pickKeys } from "prostgles-types";
@@ -0,0 +1,37 @@
1
+ import * as pgPromise from "pg-promise";
2
+ import pg from "pg-promise/typescript/pg-subset";
3
+
4
+ export const NOTIF_TYPE = {
5
+ data: "data_has_changed",
6
+ data_trigger_change: "data_watch_triggers_have_changed",
7
+ schema: "schema_has_changed",
8
+ } as const;
9
+
10
+ export type NotifTypeName = (typeof NOTIF_TYPE)[keyof typeof NOTIF_TYPE];
11
+ export const NOTIF_CHANNEL = {
12
+ preffix: "prostgles_" as const,
13
+ getFull: (appID: string | undefined) => {
14
+ if (!appID) throw "No appID";
15
+ return NOTIF_CHANNEL.preffix + appID;
16
+ },
17
+ };
18
+
19
+ export const parseCondition = (condition: string): string =>
20
+ condition && condition.trim().length ? condition : "TRUE";
21
+
22
+ export const DELIMITER = "|$prstgls$|";
23
+
24
+ export const EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID =
25
+ "prostgles internal query that should be excluded from schema watch ";
26
+
27
+ type PGP = pgPromise.IMain<{}, pg.IClient>;
28
+ export const pgp: PGP = pgPromise({});
29
+ export const asValue = (v: any) => pgp.as.format("$1", [v]);
30
+ export const DEFAULT_SYNC_BATCH_SIZE = 50;
31
+
32
+ export const log = (...args: any[]) => {
33
+ if (process.env.TEST_TYPE) {
34
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
35
+ console.log(...args);
36
+ }
37
+ };
@@ -1,12 +1,8 @@
1
1
  import { SubscriptionChannels } from "prostgles-types";
2
- import {
3
- BasicCallback,
4
- parseCondition,
5
- PubSubManager,
6
- Subscription,
7
- SubscriptionParams,
8
- } from "./PubSubManager";
9
2
  import { VoidFunction } from "../SchemaWatch/SchemaWatch";
3
+ import { tout } from "./initPubSubManager";
4
+ import { BasicCallback, PubSubManager, Subscription, SubscriptionParams } from "./PubSubManager";
5
+ import { parseCondition } from "./PubSubManagerUtils";
10
6
 
11
7
  type AddSubscriptionParams = SubscriptionParams & {
12
8
  condition: string;
@@ -27,12 +23,11 @@ export async function addSub(
27
23
  localFuncs,
28
24
  table_rules,
29
25
  filter = {},
30
- params = {},
26
+ selectParams = {},
31
27
  condition = "",
32
- throttle = 0, //subOne = false,
33
28
  viewOptions,
34
29
  table_info,
35
- throttleOpts,
30
+ subscribeOptions,
36
31
  } = subscriptionParams;
37
32
  const table_name = table_info.name;
38
33
 
@@ -43,16 +38,7 @@ export async function addSub(
43
38
  throw "addSub: cannot have socket AND func";
44
39
  }
45
40
 
46
- let validated_throttle = subscriptionParams.throttle || 10;
47
- const pubThrottle = table_rules?.subscribe?.throttle || 0;
48
- if (pubThrottle && Number.isInteger(pubThrottle) && pubThrottle > 0) {
49
- validated_throttle = pubThrottle;
50
- }
51
- if (throttle && Number.isInteger(throttle) && throttle >= pubThrottle) {
52
- validated_throttle = throttle;
53
- }
54
-
55
- const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.${JSON.stringify(params)}.${"m"}.sub`;
41
+ const channel_name = `${this.socketChannelPreffix}.${table_name}.${JSON.stringify(filter)}.${JSON.stringify(selectParams)}.${"m"}.sub`;
56
42
  const mainTrigger = {
57
43
  table_name: table_name,
58
44
  condition: parseCondition(condition),
@@ -63,16 +49,16 @@ export async function addSub(
63
49
  channel_name,
64
50
  filter,
65
51
  localFuncs,
66
- params,
67
- last_throttled: 0,
52
+ selectParams: selectParams,
53
+ lastPushed: 0,
68
54
  socket,
69
- throttleOpts,
55
+ subscribeOptions,
70
56
  table_info,
71
57
  is_ready: true,
72
58
  is_throttling: false,
73
59
  socket_id: socket?.id,
74
60
  table_rules,
75
- throttle: validated_throttle,
61
+
76
62
  triggers: [mainTrigger],
77
63
  };
78
64
 
@@ -105,14 +91,21 @@ export async function addSub(
105
91
  }
106
92
  }
107
93
 
94
+ const { skipFirst, throttleOpts, throttle } = subscribeOptions;
95
+ const sendFirstData = async () => {
96
+ if (skipFirst) return;
97
+ if (throttleOpts?.skipFirst && throttle) {
98
+ await tout(throttle);
99
+ }
100
+ void this.pushSubData(newSub);
101
+ };
102
+
108
103
  if (localFuncs) {
109
104
  /**
110
105
  * Must ensure sub will start sending data after all triggers are set up.
111
106
  * Socket clients are not affected as they need to confirm they are ready to receive data
112
107
  */
113
- result.sendFirstData = () => {
114
- void this.pushSubData(newSub);
115
- };
108
+ result.sendFirstData = sendFirstData;
116
109
  } else if (socket) {
117
110
  const removeListeners = () => {
118
111
  socket.removeAllListeners(channel_name);
@@ -121,9 +114,7 @@ export async function addSub(
121
114
  };
122
115
  removeListeners();
123
116
 
124
- socket.once(result.channelNameReady, () => {
125
- void this.pushSubData(newSub);
126
- });
117
+ socket.once(result.channelNameReady, sendFirstData);
127
118
  socket.once(result.channelNameUnsubscribe, (_data: any, cb: BasicCallback) => {
128
119
  const res = "ok";
129
120
  this.subs = this.subs.filter((s) => {
@@ -1,11 +1,6 @@
1
1
  import { find, tryCatchV2 } from "prostgles-types";
2
- import {
3
- AddSyncParams,
4
- BasicCallback,
5
- DEFAULT_SYNC_BATCH_SIZE,
6
- PubSubManager,
7
- parseCondition,
8
- } from "./PubSubManager";
2
+ import { AddSyncParams, BasicCallback, PubSubManager } from "./PubSubManager";
3
+ import { DEFAULT_SYNC_BATCH_SIZE, parseCondition } from "./PubSubManagerUtils";
9
4
 
10
5
  /**
11
6
  * Returns a sync channel
@@ -1,7 +1,8 @@
1
1
  import { tryCatchV2 } from "prostgles-types";
2
- import { asValue, PubSubManager, ViewSubscriptionOptions } from "./PubSubManager";
2
+ import { type PubSubManager, ViewSubscriptionOptions } from "./PubSubManager";
3
3
  import * as crypto from "crypto";
4
4
  import { PRGLIOSocket } from "../DboBuilder/DboBuilderTypes";
5
+ import { asValue, EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "./PubSubManagerUtils";
5
6
 
6
7
  export async function addTrigger(
7
8
  this: PubSubManager,
@@ -31,7 +32,7 @@ export async function addTrigger(
31
32
  await this.db.tx((t) =>
32
33
  t.any(`
33
34
  BEGIN WORK;
34
- /* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
35
+ /* ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
35
36
  /* why is this lock level needed? */
36
37
  --LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
37
38
 
@@ -1,4 +1,5 @@
1
1
  import { PubSubManager } from "./PubSubManager";
2
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "./PubSubManagerUtils";
2
3
 
3
4
  export function deleteOrphanedTriggers(this: PubSubManager, tableName: string) {
4
5
  const activeConditions = (this.getTriggerInfo(tableName) ?? []).filter(
@@ -10,7 +11,7 @@ export function deleteOrphanedTriggers(this: PubSubManager, tableName: string) {
10
11
  .any(
11
12
  `
12
13
  /* Delete removed subscriptions */
13
- /* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
14
+ /* ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
14
15
  DELETE FROM prostgles.app_triggers at
15
16
  WHERE EXISTS (
16
17
  SELECT 1
@@ -1,14 +1,15 @@
1
- import { tryCatch } from "prostgles-types";
1
+ import { tryCatchV2 } from "prostgles-types";
2
+ import { DboBuilder } from "../DboBuilder/DboBuilder";
2
3
  import { pgp } from "../DboBuilder/DboBuilderTypes";
3
4
  import {
4
5
  asValue,
6
+ DELIMITER,
7
+ EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID,
5
8
  NOTIF_CHANNEL,
6
9
  NOTIF_TYPE,
7
- PubSubManager,
8
- } from "./PubSubManager";
9
- const { version } = require("../../package.json");
10
+ } from "./PubSubManagerUtils";
10
11
  import { getAppCheckQuery } from "./orphanTriggerCheck";
11
- import { DboBuilder } from "../DboBuilder/DboBuilder";
12
+ const { version } = require("../../package.json");
12
13
 
13
14
  export const DB_OBJ_NAMES = {
14
15
  trigger_add_remove_func: "prostgles.trigger_add_remove_func",
@@ -35,8 +36,9 @@ const PROSTGLES_SCHEMA_VERSION_OK_QUERY = `
35
36
 
36
37
  const getInitQuery = (debugMode: boolean | undefined, pgVersion: number) => {
37
38
  const canReplaceTriggers = pgVersion >= 140006;
38
- const createTriggerQuery = canReplaceTriggers
39
- ? `CREATE OR REPLACE TRIGGER %1$I`
39
+ const createTriggerQuery =
40
+ canReplaceTriggers ?
41
+ `CREATE OR REPLACE TRIGGER %1$I`
40
42
  : `
41
43
  DROP TRIGGER IF EXISTS %1$I ON %2$s;
42
44
  CREATE TRIGGER %1$I
@@ -48,7 +50,7 @@ BEGIN; -- TRANSACTION ISOLATION LEVEL SERIALIZABLE;
48
50
  --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
49
51
 
50
52
  /*
51
- * ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
53
+ * ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
52
54
  */
53
55
 
54
56
  DO
@@ -328,7 +330,7 @@ BEGIN
328
330
  PERFORM pg_notify(
329
331
  ${asValue(NOTIF_CHANNEL.preffix)} || v_trigger.app_id ,
330
332
  LEFT(concat_ws(
331
- ${asValue(PubSubManager.DELIMITER)},
333
+ ${asValue(DELIMITER)},
332
334
 
333
335
  ${asValue(NOTIF_TYPE.data)},
334
336
  COALESCE(escaped_table, 'MISSING'),
@@ -507,7 +509,7 @@ BEGIN
507
509
  $q$
508
510
  DO $e$
509
511
  BEGIN
510
- /* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
512
+ /* ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
511
513
  %s
512
514
 
513
515
  END $e$;
@@ -530,7 +532,7 @@ BEGIN
530
532
  PERFORM pg_notify(
531
533
  ${asValue(NOTIF_CHANNEL.preffix)} || app.id,
532
534
  LEFT(concat_ws(
533
- ${asValue(PubSubManager.DELIMITER)},
535
+ ${asValue(DELIMITER)},
534
536
  ${asValue(NOTIF_TYPE.data_trigger_change)},
535
537
  json_build_object(
536
538
  'TG_OP', TG_OP,
@@ -603,12 +605,12 @@ BEGIN
603
605
  SELECT *
604
606
  FROM prostgles.apps
605
607
  WHERE tg_tag = ANY(watching_schema_tag_names)
606
- AND curr_query NOT ILIKE '%${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}%'
608
+ AND curr_query NOT ILIKE '%${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}%'
607
609
  LOOP
608
610
  PERFORM pg_notify(
609
611
  ${asValue(NOTIF_CHANNEL.preffix)} || app.id,
610
612
  LEFT(concat_ws(
611
- ${asValue(PubSubManager.DELIMITER)},
613
+ ${asValue(DELIMITER)},
612
614
  ${asValue(NOTIF_TYPE.schema)},
613
615
  tg_tag ,
614
616
  TG_event,
@@ -639,40 +641,28 @@ COMMIT;
639
641
  * undefined returned if the database contains the apropriate prostgles schema
640
642
  */
641
643
  export const getPubSubManagerInitQuery = async function (
642
- this: DboBuilder,
644
+ this: DboBuilder
643
645
  ): Promise<string | undefined> {
644
- const versionNum = await this.db.one(
645
- "SELECT current_setting('server_version_num')::int as val",
646
- );
647
- const initQuery = getInitQuery(
648
- this.prostgles.opts.DEBUG_MODE,
649
- versionNum.val,
650
- );
651
- const { schema_md5 = "none" } = await this.db.oneOrNone(
652
- "SELECT md5($1) as schema_md5",
653
- [initQuery.trim()],
654
- );
646
+ const versionNum = await this.db.one("SELECT current_setting('server_version_num')::int as val");
647
+ const initQuery = getInitQuery(this.prostgles.opts.DEBUG_MODE, versionNum.val);
648
+ const { schema_md5 = "none" } = await this.db.oneOrNone("SELECT md5($1) as schema_md5", [
649
+ initQuery.trim(),
650
+ ]);
655
651
  const query = pgp.as.format(initQuery, { schema_md5, version });
656
652
  const existingSchema = await this.db.any(PROSTGLES_SCHEMA_EXISTS_QUERY);
657
653
  if (!existingSchema.length) {
658
- console.log(
659
- "getPubSubManagerInitQuery: No prostgles.versions table found. Creating...",
660
- );
654
+ console.log("getPubSubManagerInitQuery: No prostgles.versions table found. Creating...");
661
655
  return query;
662
656
  }
663
- const { existingSchemaVersions } = await tryCatch(async () => {
664
- const existingSchemaVersions = await this.db.any(
665
- PROSTGLES_SCHEMA_VERSION_OK_QUERY,
666
- { schema_md5, version },
667
- );
668
- return {
669
- existingSchemaVersions,
670
- };
657
+ const { data: existingSchemaVersions } = await tryCatchV2(async () => {
658
+ const existingSchemaVersions = await this.db.any(PROSTGLES_SCHEMA_VERSION_OK_QUERY, {
659
+ schema_md5,
660
+ version,
661
+ });
662
+ return existingSchemaVersions;
671
663
  });
672
664
  if (!existingSchemaVersions?.length) {
673
- console.log(
674
- "getPubSubManagerInitQuery: Outdated prostgles schema. Re-creating...",
675
- );
665
+ console.log("getPubSubManagerInitQuery: Outdated prostgles schema. Re-creating...");
676
666
  return query;
677
667
  }
678
668
 
@@ -1,8 +1,9 @@
1
1
  import { isDefined, isObject } from "prostgles-types";
2
2
  import { PostgresNotifListenManager } from "../PostgresNotifListenManager";
3
3
  import { getWatchSchemaTagList } from "../SchemaWatch/getWatchSchemaTagList";
4
- import { NOTIF_CHANNEL, PubSubManager, asValue } from "./PubSubManager";
4
+ import { type PubSubManager } from "./PubSubManager";
5
5
  import { getPubSubManagerInitQuery } from "./getPubSubManagerInitQuery";
6
+ import { asValue, NOTIF_CHANNEL } from "./PubSubManagerUtils";
6
7
  export const REALTIME_TRIGGER_CHECK_QUERY =
7
8
  "prostgles-server internal query used to manage realtime triggers";
8
9
 
@@ -1,9 +1,10 @@
1
1
  import * as pgp from "pg-promise";
2
- import { PubSubManager } from "./PubSubManager";
2
+ import { type PubSubManager } from "./PubSubManager";
3
3
  import { getIsSuperUser } from "../Prostgles";
4
4
  import { EVENT_TRIGGER_TAGS } from "../Event_Trigger_Tags";
5
5
  import { DELETE_DISCONNECTED_APPS_QUERY } from "./orphanTriggerCheck";
6
6
  import { DB_OBJ_NAMES } from "./getPubSubManagerInitQuery";
7
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "./PubSubManagerUtils";
7
8
 
8
9
  const asValue = (v: any) => pgp.as.format("$1", [v]);
9
10
 
@@ -27,7 +28,7 @@ export async function initialiseEventTriggers(this: PubSubManager) {
27
28
  BEGIN;-- ISOLATION LEVEL SERIALIZABLE;
28
29
 
29
30
  /**
30
- * ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
31
+ * ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
31
32
  * Drop stale triggers
32
33
  * */
33
34
  DO
@@ -1,4 +1,7 @@
1
- import { log, NOTIF_TYPE, NotifTypeName, pickKeys, PubSubManager } from "./PubSubManager";
1
+ import { pickKeys } from "prostgles-types";
2
+ import { parseFieldFilter } from "../DboBuilder/ViewHandler/parseFieldFilter";
3
+ import { PubSubManager } from "./PubSubManager";
4
+ import { DELIMITER, log, NOTIF_TYPE, type NotifTypeName } from "./PubSubManagerUtils";
2
5
 
3
6
  /* Relay relevant data to relevant subscriptions */
4
7
  export async function notifListener(this: PubSubManager, data: { payload: string }) {
@@ -9,7 +12,7 @@ export async function notifListener(this: PubSubManager, data: { payload: string
9
12
  return;
10
13
  }
11
14
 
12
- const dataArr = str.split(PubSubManager.DELIMITER);
15
+ const dataArr = str.split(DELIMITER);
13
16
  const notifType = dataArr[0] as NotifTypeName;
14
17
 
15
18
  log(str);
@@ -128,10 +131,22 @@ export async function notifListener(this: PubSubManager, data: { payload: string
128
131
  )
129
132
  );
130
133
  activeAndReadySubs.forEach((sub) => {
131
- const { throttle = 0, throttleOpts } = sub;
132
- if (!throttleOpts?.skipFirst && sub.last_throttled <= Date.now() - throttle) {
133
- sub.last_throttled = Date.now();
134
+ const { throttle = 0, throttleOpts, actions } = sub.subscribeOptions;
135
+
136
+ const commandLowerCase = (op_name?.toLowerCase() || "insert") as keyof NonNullable<
137
+ typeof actions
138
+ >;
139
+
140
+ const actionIsIgnored =
141
+ actions &&
142
+ !parseFieldFilter(actions, false, ["insert", "update", "delete"]).includes(
143
+ commandLowerCase as any
144
+ );
145
+ if (actionIsIgnored) {
146
+ return;
147
+ }
134
148
 
149
+ if (!throttleOpts?.skipFirst && sub.lastPushed <= Date.now() - throttle) {
135
150
  /* It is assumed the policy was checked before this point */
136
151
  void this.pushSubData(sub);
137
152
  } else if (!sub.is_throttling) {
@@ -139,7 +154,6 @@ export async function notifListener(this: PubSubManager, data: { payload: string
139
154
  sub.is_throttling = setTimeout(() => {
140
155
  log("throttling finished. pushSubData...");
141
156
  sub.is_throttling = null;
142
- sub.last_throttled = Date.now();
143
157
  void this.pushSubData(sub);
144
158
  }, throttle);
145
159
  }
@@ -1,4 +1,5 @@
1
1
  import { PubSubManager } from "./PubSubManager";
2
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "./PubSubManagerUtils";
2
3
  import { REALTIME_TRIGGER_CHECK_QUERY } from "./initPubSubManager";
3
4
 
4
5
  /**
@@ -32,7 +33,7 @@ export const getAppCheckQuery = () => `
32
33
  /*
33
34
  ${queryIdentifier}
34
35
  ${REALTIME_TRIGGER_CHECK_QUERY}
35
- ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
36
+ ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
36
37
  */
37
38
  IF
38
39
  /* prostgles schema must exist */
@@ -1,6 +1,7 @@
1
1
  import { parseLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
2
2
  import { EventTypes } from "../Logging";
3
- import { log, PubSubManager, Subscription } from "./PubSubManager";
3
+ import { type PubSubManager, Subscription } from "./PubSubManager";
4
+ import { log } from "./PubSubManagerUtils";
4
5
 
5
6
  export async function pushSubData(this: PubSubManager, sub: Subscription, err?: any) {
6
7
  const { socket_id, channel_name } = sub;
@@ -35,6 +36,7 @@ export async function pushSubData(this: PubSubManager, sub: Subscription, err?:
35
36
  return true;
36
37
  }
37
38
 
39
+ sub.lastPushed = Date.now();
38
40
  return new Promise(async (resolve, reject) => {
39
41
  /* TODO: Retire subOne -> it's redundant */
40
42
 
@@ -57,7 +59,6 @@ export async function pushSubData(this: PubSubManager, sub: Subscription, err?:
57
59
  } else {
58
60
  onLog("no client to push data to");
59
61
  }
60
- // sub.last_throttled = Date.now();
61
62
  } else {
62
63
  onLog("fetch data error");
63
64
  const errObj = { _err_msg: err.toString(), err };
@@ -2,7 +2,7 @@ import { getKeys, isObject } from "prostgles-types";
2
2
  import { AuthResultWithSID } from "../Auth/AuthTypes";
3
3
  import { TableHandler } from "../DboBuilder/TableHandler/TableHandler";
4
4
  import { ViewHandler } from "../DboBuilder/ViewHandler/ViewHandler";
5
- import { DEFAULT_SYNC_BATCH_SIZE } from "../PubSubManager/PubSubManager";
5
+ import { DEFAULT_SYNC_BATCH_SIZE } from "../PubSubManager/PubSubManagerUtils";
6
6
  import { PublishParser } from "./PublishParser";
7
7
  import {
8
8
  DboTable,
@@ -1,7 +1,7 @@
1
1
  import type { DboBuilder } from "../DboBuilder/DboBuilder";
2
2
  import { EVENT_TRIGGER_TAGS } from "../Event_Trigger_Tags";
3
3
  import type { OnReadyCallbackBasic } from "../initProstgles";
4
- import { PubSubManager, log } from "../PubSubManager/PubSubManager";
4
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID, log } from "../PubSubManager/PubSubManagerUtils";
5
5
  import {
6
6
  ValidatedWatchSchemaType,
7
7
  getValidatedWatchSchemaType,
@@ -47,10 +47,7 @@ export class SchemaWatch {
47
47
  * Fallback for watchSchema in case of not a superuser (cannot add db event listener)
48
48
  */
49
49
  onSchemaChangeFallback: OnSchemaChangeCallback | undefined = ({ command, query }) => {
50
- if (
51
- typeof query === "string" &&
52
- query.includes(PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID)
53
- ) {
50
+ if (typeof query === "string" && query.includes(EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID)) {
54
51
  log("Schema change event excluded from triggers due to EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID");
55
52
  return;
56
53
  }
@@ -1,6 +1,7 @@
1
- import { PubSubManager, SyncParams, pickKeys, omitKeys, log } from "./PubSubManager/PubSubManager";
2
- import { OrderBy, WAL, AnyObject, SyncBatchParams } from "prostgles-types";
1
+ import { AnyObject, OrderBy, SyncBatchParams, WAL, omitKeys, pickKeys } from "prostgles-types";
3
2
  import { TableHandler } from "./DboBuilder/TableHandler/TableHandler";
3
+ import { PubSubManager, SyncParams } from "./PubSubManager/PubSubManager";
4
+ import { log } from "./PubSubManager/PubSubManagerUtils";
4
5
 
5
6
  export type ClientSyncInfo = Partial<{
6
7
  c_fr: AnyObject;