prostgles-server 4.2.266 → 4.2.268

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/Auth/AuthTypes.d.ts +1 -1
  2. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  3. package/dist/Auth/AuthTypes.js.map +1 -1
  4. package/dist/Auth/utils/getSidAndUserFromRequest.js +4 -4
  5. package/dist/Auth/utils/getSidAndUserFromRequest.js.map +1 -1
  6. package/dist/DboBuilder/TableHandler/DataValidator.js +5 -5
  7. package/dist/DboBuilder/TableHandler/DataValidator.js.map +1 -1
  8. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js +1 -2
  10. package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler/insertTest.js +2 -2
  12. package/dist/DboBuilder/TableHandler/insertTest.js.map +1 -1
  13. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.d.ts.map +1 -1
  14. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js +1 -2
  15. package/dist/DboBuilder/TableHandler/onDeleteFromFileTable.js.map +1 -1
  16. package/dist/DboBuilder/TableHandler/updateFile.d.ts.map +1 -1
  17. package/dist/DboBuilder/TableHandler/updateFile.js +1 -2
  18. package/dist/DboBuilder/TableHandler/updateFile.js.map +1 -1
  19. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -1
  20. package/dist/DboBuilder/ViewHandler/parseComplexFilter.d.ts.map +1 -1
  21. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js +6 -8
  22. package/dist/DboBuilder/ViewHandler/parseComplexFilter.js.map +1 -1
  23. package/dist/DboBuilder/dboBuilderUtils.d.ts.map +1 -1
  24. package/dist/DboBuilder/dboBuilderUtils.js +1 -2
  25. package/dist/DboBuilder/dboBuilderUtils.js.map +1 -1
  26. package/dist/DboBuilder/getSubscribeRelatedTables.d.ts.map +1 -1
  27. package/dist/DboBuilder/getSubscribeRelatedTables.js +2 -2
  28. package/dist/DboBuilder/getSubscribeRelatedTables.js.map +1 -1
  29. package/dist/JSONBValidation/getJSONBSchemaTSTypes.js +3 -3
  30. package/dist/JSONBValidation/getJSONBSchemaTSTypes.js.map +1 -1
  31. package/dist/JSONBValidation/validate_jsonb_schema_sql.js +3 -3
  32. package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -1
  33. package/dist/Logging.d.ts +2 -1
  34. package/dist/Logging.d.ts.map +1 -1
  35. package/dist/PubSubManager/PubSubManager.d.ts +0 -21
  36. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  37. package/dist/PubSubManager/PubSubManager.js +6 -37
  38. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  39. package/dist/PubSubManager/PubSubManagerUtils.d.ts +22 -0
  40. package/dist/PubSubManager/PubSubManagerUtils.d.ts.map +1 -0
  41. package/dist/PubSubManager/PubSubManagerUtils.js +33 -0
  42. package/dist/PubSubManager/PubSubManagerUtils.js.map +1 -0
  43. package/dist/PubSubManager/addSub.d.ts.map +1 -1
  44. package/dist/PubSubManager/addSub.js +3 -3
  45. package/dist/PubSubManager/addSub.js.map +1 -1
  46. package/dist/PubSubManager/addSync.d.ts.map +1 -1
  47. package/dist/PubSubManager/addSync.js +3 -3
  48. package/dist/PubSubManager/addSync.js.map +1 -1
  49. package/dist/PubSubManager/addTrigger.d.ts +1 -1
  50. package/dist/PubSubManager/addTrigger.d.ts.map +1 -1
  51. package/dist/PubSubManager/addTrigger.js +9 -9
  52. package/dist/PubSubManager/addTrigger.js.map +1 -1
  53. package/dist/PubSubManager/deleteOrphanedTriggers.d.ts.map +1 -1
  54. package/dist/PubSubManager/deleteOrphanedTriggers.js +2 -2
  55. package/dist/PubSubManager/deleteOrphanedTriggers.js.map +1 -1
  56. package/dist/PubSubManager/getPubSubManagerInitQuery.d.ts.map +1 -1
  57. package/dist/PubSubManager/getPubSubManagerInitQuery.js +26 -23
  58. package/dist/PubSubManager/getPubSubManagerInitQuery.js.map +1 -1
  59. package/dist/PubSubManager/initPubSubManager.d.ts +1 -1
  60. package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
  61. package/dist/PubSubManager/initPubSubManager.js +3 -3
  62. package/dist/PubSubManager/initPubSubManager.js.map +1 -1
  63. package/dist/PubSubManager/initialiseEventTriggers.d.ts +1 -1
  64. package/dist/PubSubManager/initialiseEventTriggers.d.ts.map +1 -1
  65. package/dist/PubSubManager/initialiseEventTriggers.js +2 -2
  66. package/dist/PubSubManager/initialiseEventTriggers.js.map +1 -1
  67. package/dist/PubSubManager/notifListener.d.ts.map +1 -1
  68. package/dist/PubSubManager/notifListener.js +11 -10
  69. package/dist/PubSubManager/notifListener.js.map +1 -1
  70. package/dist/PubSubManager/orphanTriggerCheck.d.ts.map +1 -1
  71. package/dist/PubSubManager/orphanTriggerCheck.js +2 -2
  72. package/dist/PubSubManager/orphanTriggerCheck.js.map +1 -1
  73. package/dist/PubSubManager/pushSubData.d.ts +1 -1
  74. package/dist/PubSubManager/pushSubData.d.ts.map +1 -1
  75. package/dist/PubSubManager/pushSubData.js +2 -2
  76. package/dist/PubSubManager/pushSubData.js.map +1 -1
  77. package/dist/PublishParser/getTableRulesWithoutFileTable.js +2 -2
  78. package/dist/PublishParser/getTableRulesWithoutFileTable.js.map +1 -1
  79. package/dist/SchemaWatch/SchemaWatch.d.ts.map +1 -1
  80. package/dist/SchemaWatch/SchemaWatch.js +4 -5
  81. package/dist/SchemaWatch/SchemaWatch.js.map +1 -1
  82. package/dist/SyncReplication.d.ts +1 -1
  83. package/dist/SyncReplication.d.ts.map +1 -1
  84. package/dist/SyncReplication.js +7 -7
  85. package/dist/SyncReplication.js.map +1 -1
  86. package/dist/TableConfig/applyTableConfig.js +2 -2
  87. package/dist/TableConfig/applyTableConfig.js.map +1 -1
  88. package/dist/TableConfig/getColumnDefinitionQuery.js +7 -7
  89. package/dist/TableConfig/getColumnDefinitionQuery.js.map +1 -1
  90. package/dist/TableConfig/getConstraintDefinitionQueries.js +4 -4
  91. package/dist/TableConfig/getConstraintDefinitionQueries.js.map +1 -1
  92. package/dist/TableConfig/getSchemaDiffQueries.js +2 -2
  93. package/dist/TableConfig/getSchemaDiffQueries.js.map +1 -1
  94. package/dist/TableConfig/initTableConfig.d.ts.map +1 -1
  95. package/dist/TableConfig/initTableConfig.js +5 -5
  96. package/dist/TableConfig/initTableConfig.js.map +1 -1
  97. package/dist/TableConfig/runMigrations.js +2 -2
  98. package/dist/TableConfig/runMigrations.js.map +1 -1
  99. package/dist/TableConfig/tableConfigSchemaUtils.js +3 -3
  100. package/dist/TableConfig/tableConfigSchemaUtils.js.map +1 -1
  101. package/lib/Auth/AuthTypes.ts +2 -6
  102. package/lib/Auth/utils/getSidAndUserFromRequest.ts +4 -4
  103. package/lib/DboBuilder/TableHandler/DataValidator.ts +1 -1
  104. package/lib/DboBuilder/TableHandler/insert/insertNestedRecords.ts +1 -1
  105. package/lib/DboBuilder/TableHandler/insertTest.ts +1 -1
  106. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +1 -2
  107. package/lib/DboBuilder/TableHandler/updateFile.ts +1 -2
  108. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +7 -13
  109. package/lib/DboBuilder/dboBuilderUtils.ts +1 -1
  110. package/lib/DboBuilder/getSubscribeRelatedTables.ts +2 -1
  111. package/lib/JSONBValidation/getJSONBSchemaTSTypes.ts +1 -1
  112. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +3 -3
  113. package/lib/Logging.ts +2 -1
  114. package/lib/PubSubManager/PubSubManager.ts +2 -42
  115. package/lib/PubSubManager/PubSubManagerUtils.ts +37 -0
  116. package/lib/PubSubManager/addSub.ts +2 -7
  117. package/lib/PubSubManager/addSync.ts +2 -7
  118. package/lib/PubSubManager/addTrigger.ts +3 -2
  119. package/lib/PubSubManager/deleteOrphanedTriggers.ts +2 -1
  120. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +29 -39
  121. package/lib/PubSubManager/initPubSubManager.ts +2 -1
  122. package/lib/PubSubManager/initialiseEventTriggers.ts +3 -2
  123. package/lib/PubSubManager/notifListener.ts +4 -2
  124. package/lib/PubSubManager/orphanTriggerCheck.ts +2 -1
  125. package/lib/PubSubManager/pushSubData.ts +2 -1
  126. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +1 -1
  127. package/lib/SchemaWatch/SchemaWatch.ts +2 -5
  128. package/lib/SyncReplication.ts +3 -2
  129. package/lib/TableConfig/applyTableConfig.ts +2 -2
  130. package/lib/TableConfig/getColumnDefinitionQuery.ts +1 -1
  131. package/lib/TableConfig/getConstraintDefinitionQueries.ts +1 -1
  132. package/lib/TableConfig/getSchemaDiffQueries.ts +1 -1
  133. package/lib/TableConfig/initTableConfig.ts +7 -4
  134. package/lib/TableConfig/runMigrations.ts +2 -2
  135. package/lib/TableConfig/tableConfigSchemaUtils.ts +2 -2
  136. package/package.json +1 -1
@@ -1,6 +1,5 @@
1
- import { AnyObject, asName } from "prostgles-types";
1
+ import { AnyObject, asName, pickKeys } from "prostgles-types";
2
2
  import { LocalParams, type Media } from "../DboBuilder";
3
- import { pickKeys } from "../../PubSubManager/PubSubManager";
4
3
  import { TableHandler } from "./TableHandler";
5
4
 
6
5
  type OnDeleteFromFileTableArgs = {
@@ -1,6 +1,5 @@
1
- import { AnyObject, getKeys, isObject } from "prostgles-types";
1
+ import { AnyObject, getKeys, isObject, omitKeys } from "prostgles-types";
2
2
  import { ParsedTableRule, ValidateRowBasic } from "../../PublishParser/PublishParser";
3
- import { omitKeys } from "../../PubSubManager/PubSubManager";
4
3
  import { LocalParams, Media } from "../DboBuilder";
5
4
  import { isFile, uploadFile } from "../uploadFile";
6
5
  import { TableHandler } from "./TableHandler";
@@ -7,7 +7,7 @@ import {
7
7
  import { FUNCTIONS, parseFunction } from "../QueryBuilder/Functions";
8
8
  import { asNameAlias, parseFunctionObject } from "../QueryBuilder/QueryBuilder";
9
9
  import { TableSchemaColumn } from "../DboBuilderTypes";
10
- import { asValue } from "../../PubSubManager/PubSubManager";
10
+ import { asValue } from "../../PubSubManager/PubSubManagerUtils";
11
11
 
12
12
  const allowedComparators = FILTER_OPERANDS; //[">", "<", "=", "<=", ">=", "<>", "!="]
13
13
  type Args = {
@@ -73,32 +73,26 @@ export const parseComplexFilter = ({
73
73
  const leftVal = getFuncQuery(leftFilter);
74
74
  let result = leftVal;
75
75
  if (comparator) {
76
- if (
77
- typeof comparator !== "string" ||
78
- !allowedComparators.includes(comparator as any)
79
- ) {
76
+ if (typeof comparator !== "string" || !allowedComparators.includes(comparator as any)) {
80
77
  throw `Invalid $filter. comparator ${JSON.stringify(comparator)} is not valid. Expecting one of: ${allowedComparators}`;
81
78
  }
82
79
  if (!rightFilterOrValue) {
83
80
  throw "Invalid $filter. Expecting a value or function after the comparator";
84
81
  }
85
- const maybeValidComparator =
86
- comparator as keyof typeof FILTER_OPERAND_TO_SQL_OPERAND;
82
+ const maybeValidComparator = comparator as keyof typeof FILTER_OPERAND_TO_SQL_OPERAND;
87
83
  const sqlOperand = FILTER_OPERAND_TO_SQL_OPERAND[maybeValidComparator];
88
84
  if (!sqlOperand) {
89
85
  throw `Invalid $filter. comparator ${comparator} is not valid. Expecting one of: ${allowedComparators}`;
90
86
  }
91
87
 
92
- let rightVal = isObject(rightFilterOrValue)
93
- ? getFuncQuery(rightFilterOrValue)
88
+ let rightVal =
89
+ isObject(rightFilterOrValue) ?
90
+ getFuncQuery(rightFilterOrValue)
94
91
  : parseFilterRightValue(rightFilterOrValue, {
95
92
  selectItem: undefined,
96
93
  expect: ["$in", "$nin"].includes(comparator) ? "csv" : undefined,
97
94
  });
98
- if (
99
- maybeValidComparator === "$between" ||
100
- maybeValidComparator === "$notBetween"
101
- ) {
95
+ if (maybeValidComparator === "$between" || maybeValidComparator === "$notBetween") {
102
96
  if (!Array.isArray(rightVal) || rightVal.length !== 2) {
103
97
  throw "Between filter expects an array of two values";
104
98
  }
@@ -11,9 +11,9 @@ import {
11
11
  getKeys,
12
12
  isObject,
13
13
  omitKeys,
14
+ pickKeys,
14
15
  } from "prostgles-types";
15
16
  import { DB } from "../Prostgles";
16
- import { pickKeys } from "../PubSubManager/PubSubManager";
17
17
  import { LocalParams, SortItem, pgp } from "./DboBuilderTypes";
18
18
  import { asNameAlias } from "./QueryBuilder/QueryBuilder";
19
19
  import { ViewHandler } from "./ViewHandler/ViewHandler";
@@ -6,10 +6,11 @@ import {
6
6
  SubscribeParams,
7
7
  } from "prostgles-types";
8
8
  import { ParsedTableRule } from "../PublishParser/PublishParser";
9
- import { log, ViewSubscriptionOptions } from "../PubSubManager/PubSubManager";
9
+ import { ViewSubscriptionOptions } from "../PubSubManager/PubSubManager";
10
10
  import { Filter, getSerializedClientErrorFromPGError, LocalParams } from "./DboBuilder";
11
11
  import { NewQuery } from "./QueryBuilder/QueryBuilder";
12
12
  import { ViewHandler } from "./ViewHandler/ViewHandler";
13
+ import { log } from "../PubSubManager/PubSubManagerUtils";
13
14
 
14
15
  type Args = {
15
16
  selectParams: Omit<SubscribeParams, "throttle">;
@@ -1,6 +1,6 @@
1
1
  import { getKeys, isObject, type JSONB, type TableSchema } from "prostgles-types";
2
2
  import { postgresToTsType } from "../DboBuilder/DboBuilder";
3
- import { asValue } from "../PubSubManager/PubSubManager";
3
+ import { asValue } from "../PubSubManager/PubSubManagerUtils";
4
4
  import { getFieldTypeObj } from "./JSONBValidation";
5
5
 
6
6
  type ColOpts = { nullable?: boolean };
@@ -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,20 +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,
32
28
  type SubscribeOptions,
33
29
  } from "prostgles-types";
34
30
 
35
- import { find, pickKeys } from "prostgles-types/dist/util";
31
+ import { find, pickKeys } from "prostgles-types";
36
32
  import { LocalFuncs, getOnDataFunc, matchesLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
37
33
  import { EventTypes } from "../Logging";
38
34
  import { ParsedTableRule } from "../PublishParser/PublishParser";
@@ -40,18 +36,7 @@ import { syncData } from "../SyncReplication";
40
36
  import { addSub } from "./addSub";
41
37
  import { notifListener } from "./notifListener";
42
38
  import { pushSubData } from "./pushSubData";
43
-
44
- type PGP = pgPromise.IMain<{}, pg.IClient>;
45
- export const pgp: PGP = pgPromise({});
46
- export const asValue = (v: any) => pgp.as.format("$1", [v]);
47
- export const DEFAULT_SYNC_BATCH_SIZE = 50;
48
-
49
- export const log = (...args: any[]) => {
50
- if (process.env.TEST_TYPE) {
51
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
52
- console.log(...args);
53
- }
54
- };
39
+ import { log } from "./PubSubManagerUtils";
55
40
 
56
41
  export type BasicCallback = (err?: any, res?: any) => void;
57
42
 
@@ -164,11 +149,6 @@ export type PubSubManagerTriggers = Record<string, { condition: string; hash: st
164
149
  * Used to facilitate table subscribe and sync
165
150
  */
166
151
  export class PubSubManager {
167
- static DELIMITER = "|$prstgls$|" as const;
168
-
169
- static EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID =
170
- "prostgles internal query that should be excluded from schema watch " as const;
171
-
172
152
  public static create = async (dboBuilder: DboBuilder) => {
173
153
  const instance = new PubSubManager(dboBuilder);
174
154
  const result = await initPubSubManager.bind(instance)();
@@ -403,21 +383,6 @@ export class PubSubManager {
403
383
  addTrigger = addTrigger.bind(this);
404
384
  }
405
385
 
406
- export const NOTIF_TYPE = {
407
- data: "data_has_changed",
408
- data_trigger_change: "data_watch_triggers_have_changed",
409
- schema: "schema_has_changed",
410
- } as const;
411
-
412
- export type NotifTypeName = (typeof NOTIF_TYPE)[keyof typeof NOTIF_TYPE];
413
- export const NOTIF_CHANNEL = {
414
- preffix: "prostgles_" as const,
415
- getFull: (appID: string | undefined) => {
416
- if (!appID) throw "No appID";
417
- return NOTIF_CHANNEL.preffix + appID;
418
- },
419
- };
420
-
421
386
  function debounce<Params extends any[]>(
422
387
  func: (...args: Params) => any,
423
388
  timeout: number
@@ -430,8 +395,3 @@ function debounce<Params extends any[]>(
430
395
  }, timeout);
431
396
  };
432
397
  }
433
-
434
- export const parseCondition = (condition: string): string =>
435
- condition && condition.trim().length ? condition : "TRUE";
436
-
437
- 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,13 +1,8 @@
1
1
  import { SubscriptionChannels } from "prostgles-types";
2
2
  import { VoidFunction } from "../SchemaWatch/SchemaWatch";
3
3
  import { tout } from "./initPubSubManager";
4
- import {
5
- BasicCallback,
6
- parseCondition,
7
- PubSubManager,
8
- Subscription,
9
- SubscriptionParams,
10
- } from "./PubSubManager";
4
+ import { BasicCallback, PubSubManager, Subscription, SubscriptionParams } from "./PubSubManager";
5
+ import { parseCondition } from "./PubSubManagerUtils";
11
6
 
12
7
  type AddSubscriptionParams = SubscriptionParams & {
13
8
  condition: string;
@@ -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,5 +1,7 @@
1
+ import { pickKeys } from "prostgles-types";
1
2
  import { parseFieldFilter } from "../DboBuilder/ViewHandler/parseFieldFilter";
2
- import { log, NOTIF_TYPE, NotifTypeName, pickKeys, PubSubManager } from "./PubSubManager";
3
+ import { PubSubManager } from "./PubSubManager";
4
+ import { DELIMITER, log, NOTIF_TYPE, type NotifTypeName } from "./PubSubManagerUtils";
3
5
 
4
6
  /* Relay relevant data to relevant subscriptions */
5
7
  export async function notifListener(this: PubSubManager, data: { payload: string }) {
@@ -10,7 +12,7 @@ export async function notifListener(this: PubSubManager, data: { payload: string
10
12
  return;
11
13
  }
12
14
 
13
- const dataArr = str.split(PubSubManager.DELIMITER);
15
+ const dataArr = str.split(DELIMITER);
14
16
  const notifType = dataArr[0] as NotifTypeName;
15
17
 
16
18
  log(str);
@@ -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;
@@ -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;
@@ -1,6 +1,6 @@
1
1
  import type { ProstglesError } from "prostgles-types";
2
2
  import { type Prostgles } from "../Prostgles";
3
- import { PubSubManager } from "../PubSubManager/PubSubManager";
3
+ import { EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID } from "../PubSubManager/PubSubManagerUtils";
4
4
  import { getSchemaDiffQueries } from "./getSchemaDiffQueries";
5
5
  import { getSchemaForTableConfig } from "./getSchemaForTableConfig";
6
6
  import { getTableConfigSchemaQueries } from "./getTableConfigSchemaQueries";
@@ -30,7 +30,7 @@ import { getSchemaUtils } from "./tableConfigSchemaUtils";
30
30
  */
31
31
 
32
32
  export const applyTableConfig = async (prostgles: Prostgles, commit = false) => {
33
- const NO_RELOAD = `/* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */ \n\n`;
33
+ const NO_RELOAD = `/* ${EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */ \n\n`;
34
34
  const utils = await getSchemaUtils(prostgles);
35
35
  const { db } = utils;
36
36
  const { fileTable, tableConfig, tableConfigMigrations, onLog, sqlFilePath } = prostgles.opts;
@@ -1,6 +1,6 @@
1
1
  import { asName, pickKeys } from "prostgles-types";
2
2
  import { DB } from "../Prostgles";
3
- import { asValue } from "../PubSubManager/PubSubManager";
3
+ import { asValue } from "../PubSubManager/PubSubManagerUtils";
4
4
  import { VALIDATE_SCHEMA_FUNCNAME } from "../JSONBValidation/validate_jsonb_schema_sql";
5
5
  import { BaseColumnTypes, ColumnConfig } from "./TableConfig";
6
6
  import pgPromise from "pg-promise";
@@ -1,7 +1,7 @@
1
1
  import pgPromise from "pg-promise";
2
2
  import { asName } from "prostgles-types";
3
3
  import { DB } from "../Prostgles";
4
- import { asValue } from "../PubSubManager/PubSubManager";
4
+ import { asValue } from "../PubSubManager/PubSubManagerUtils";
5
5
  import { TableConfig } from "./TableConfig";
6
6
 
7
7
  type Args = {
@@ -1,5 +1,5 @@
1
1
  import { isDefined, isEqual } from "prostgles-types";
2
- import { asValue } from "../PubSubManager/PubSubManager";
2
+ import { asValue } from "../PubSubManager/PubSubManagerUtils";
3
3
  import { type SchemaInfo } from "./getSchemaForTableConfig";
4
4
 
5
5
  /**