prostgles-server 4.2.157 → 4.2.159

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 (159) hide show
  1. package/dist/Auth/AuthHandler.js +2 -2
  2. package/dist/Auth/AuthHandler.js.map +1 -1
  3. package/dist/Auth/AuthTypes.d.ts +4 -8
  4. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  5. package/dist/Auth/setAuthProviders.d.ts +1 -1
  6. package/dist/Auth/setAuthProviders.d.ts.map +1 -1
  7. package/dist/Auth/setAuthProviders.js +6 -7
  8. package/dist/Auth/setAuthProviders.js.map +1 -1
  9. package/dist/Auth/setEmailProvider.d.ts +1 -1
  10. package/dist/Auth/setEmailProvider.d.ts.map +1 -1
  11. package/dist/Auth/setEmailProvider.js +22 -2
  12. package/dist/Auth/setEmailProvider.js.map +1 -1
  13. package/dist/Auth/setupAuthRoutes.js +1 -1
  14. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  15. package/dist/Prostgles.d.ts +1 -0
  16. package/dist/Prostgles.d.ts.map +1 -1
  17. package/dist/Prostgles.js +6 -0
  18. package/dist/Prostgles.js.map +1 -1
  19. package/dist/initProstgles.d.ts.map +1 -1
  20. package/dist/initProstgles.js +2 -6
  21. package/dist/initProstgles.js.map +1 -1
  22. package/package.json +1 -1
  23. package/lib/Auth/AuthHandler.ts +0 -436
  24. package/lib/Auth/AuthTypes.ts +0 -285
  25. package/lib/Auth/getSafeReturnURL.ts +0 -35
  26. package/lib/Auth/sendEmail.ts +0 -83
  27. package/lib/Auth/setAuthProviders.ts +0 -129
  28. package/lib/Auth/setEmailProvider.ts +0 -63
  29. package/lib/Auth/setupAuthRoutes.ts +0 -161
  30. package/lib/DBEventsManager.ts +0 -178
  31. package/lib/DBSchemaBuilder.ts +0 -225
  32. package/lib/DboBuilder/DboBuilder.ts +0 -319
  33. package/lib/DboBuilder/DboBuilderTypes.ts +0 -361
  34. package/lib/DboBuilder/QueryBuilder/Functions.ts +0 -1153
  35. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +0 -288
  36. package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +0 -263
  37. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +0 -271
  38. package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +0 -136
  39. package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +0 -22
  40. package/lib/DboBuilder/QueryStreamer.ts +0 -250
  41. package/lib/DboBuilder/TableHandler/DataValidator.ts +0 -428
  42. package/lib/DboBuilder/TableHandler/TableHandler.ts +0 -205
  43. package/lib/DboBuilder/TableHandler/delete.ts +0 -115
  44. package/lib/DboBuilder/TableHandler/insert.ts +0 -183
  45. package/lib/DboBuilder/TableHandler/insertTest.ts +0 -78
  46. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +0 -62
  47. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +0 -134
  48. package/lib/DboBuilder/TableHandler/update.ts +0 -126
  49. package/lib/DboBuilder/TableHandler/updateBatch.ts +0 -49
  50. package/lib/DboBuilder/TableHandler/updateFile.ts +0 -48
  51. package/lib/DboBuilder/TableHandler/upsert.ts +0 -34
  52. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +0 -393
  53. package/lib/DboBuilder/ViewHandler/count.ts +0 -38
  54. package/lib/DboBuilder/ViewHandler/find.ts +0 -153
  55. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +0 -73
  56. package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +0 -74
  57. package/lib/DboBuilder/ViewHandler/getInfo.ts +0 -32
  58. package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +0 -84
  59. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +0 -96
  60. package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +0 -105
  61. package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +0 -208
  62. package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +0 -163
  63. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +0 -90
  64. package/lib/DboBuilder/ViewHandler/size.ts +0 -37
  65. package/lib/DboBuilder/ViewHandler/subscribe.ts +0 -118
  66. package/lib/DboBuilder/ViewHandler/validateViewRules.ts +0 -70
  67. package/lib/DboBuilder/dboBuilderUtils.ts +0 -222
  68. package/lib/DboBuilder/getColumns.ts +0 -114
  69. package/lib/DboBuilder/getCondition.ts +0 -201
  70. package/lib/DboBuilder/getSubscribeRelatedTables.ts +0 -190
  71. package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +0 -426
  72. package/lib/DboBuilder/insertNestedRecords.ts +0 -355
  73. package/lib/DboBuilder/parseUpdateRules.ts +0 -187
  74. package/lib/DboBuilder/prepareShortestJoinPaths.ts +0 -186
  75. package/lib/DboBuilder/runSQL.ts +0 -182
  76. package/lib/DboBuilder/runTransaction.ts +0 -50
  77. package/lib/DboBuilder/sqlErrCodeToMsg.ts +0 -254
  78. package/lib/DboBuilder/uploadFile.ts +0 -69
  79. package/lib/Event_Trigger_Tags.ts +0 -118
  80. package/lib/FileManager/FileManager.ts +0 -358
  81. package/lib/FileManager/getValidatedFileType.ts +0 -69
  82. package/lib/FileManager/initFileManager.ts +0 -187
  83. package/lib/FileManager/upload.ts +0 -62
  84. package/lib/FileManager/uploadStream.ts +0 -79
  85. package/lib/Filtering.ts +0 -463
  86. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +0 -502
  87. package/lib/JSONBValidation/validation.ts +0 -143
  88. package/lib/Logging.ts +0 -127
  89. package/lib/PostgresNotifListenManager.ts +0 -143
  90. package/lib/Prostgles.ts +0 -479
  91. package/lib/ProstglesTypes.ts +0 -196
  92. package/lib/PubSubManager/PubSubManager.ts +0 -609
  93. package/lib/PubSubManager/addSub.ts +0 -138
  94. package/lib/PubSubManager/addSync.ts +0 -141
  95. package/lib/PubSubManager/getCreatePubSubManagerError.ts +0 -72
  96. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +0 -662
  97. package/lib/PubSubManager/initPubSubManager.ts +0 -79
  98. package/lib/PubSubManager/notifListener.ts +0 -173
  99. package/lib/PubSubManager/orphanTriggerCheck.ts +0 -70
  100. package/lib/PubSubManager/pushSubData.ts +0 -55
  101. package/lib/PublishParser/PublishParser.ts +0 -162
  102. package/lib/PublishParser/getFileTableRules.ts +0 -124
  103. package/lib/PublishParser/getSchemaFromPublish.ts +0 -141
  104. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +0 -177
  105. package/lib/PublishParser/publishTypesAndUtils.ts +0 -399
  106. package/lib/RestApi.ts +0 -127
  107. package/lib/SchemaWatch/SchemaWatch.ts +0 -90
  108. package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +0 -3
  109. package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +0 -45
  110. package/lib/SchemaWatch/getWatchSchemaTagList.ts +0 -27
  111. package/lib/SyncReplication.ts +0 -557
  112. package/lib/TableConfig/TableConfig.ts +0 -468
  113. package/lib/TableConfig/getColumnDefinitionQuery.ts +0 -111
  114. package/lib/TableConfig/getConstraintDefinitionQueries.ts +0 -95
  115. package/lib/TableConfig/getFutureTableSchema.ts +0 -64
  116. package/lib/TableConfig/getPGIndexes.ts +0 -53
  117. package/lib/TableConfig/getTableColumnQueries.ts +0 -129
  118. package/lib/TableConfig/initTableConfig.ts +0 -326
  119. package/lib/index.ts +0 -13
  120. package/lib/initProstgles.ts +0 -322
  121. package/lib/onSocketConnected.ts +0 -102
  122. package/lib/runClientRequest.ts +0 -129
  123. package/lib/shortestPath.ts +0 -122
  124. package/lib/typeTests/DBoGenerated.d.ts +0 -320
  125. package/lib/typeTests/dboTypeCheck.ts +0 -81
  126. package/lib/utils.ts +0 -15
  127. package/tests/client/hooks.spec.ts +0 -205
  128. package/tests/client/index.ts +0 -139
  129. package/tests/client/package-lock.json +0 -637
  130. package/tests/client/package.json +0 -26
  131. package/tests/client/renderReactHook.ts +0 -177
  132. package/tests/client/tsconfig.json +0 -15
  133. package/tests/client/useProstgles.spec.ts +0 -120
  134. package/tests/clientFileTests.spec.ts +0 -102
  135. package/tests/clientOnlyQueries.spec.ts +0 -667
  136. package/tests/clientRestApi.spec.ts +0 -82
  137. package/tests/config_test/DBoGenerated.d.ts +0 -407
  138. package/tests/config_test/index.html +0 -109
  139. package/tests/config_test/index.js +0 -86
  140. package/tests/config_test/index.js.map +0 -1
  141. package/tests/config_test/index.ts +0 -91
  142. package/tests/config_test/init.sql +0 -48
  143. package/tests/config_test/package.json +0 -29
  144. package/tests/config_test/tsconfig.json +0 -23
  145. package/tests/config_testDBoGenerated.d.ts +0 -407
  146. package/tests/isomorphicQueries.spec.ts +0 -1493
  147. package/tests/server/DBoGenerated.d.ts +0 -537
  148. package/tests/server/index.html +0 -73
  149. package/tests/server/index.ts +0 -289
  150. package/tests/server/init.sql +0 -224
  151. package/tests/server/package-lock.json +0 -2164
  152. package/tests/server/package.json +0 -25
  153. package/tests/server/publishTypeCheck.ts +0 -136
  154. package/tests/server/server.ts +0 -35
  155. package/tests/server/testPublish.ts +0 -147
  156. package/tests/server/testTableConfig.ts +0 -156
  157. package/tests/server/tsconfig.json +0 -22
  158. package/tests/serverOnlyQueries.spec.ts +0 -32
  159. package/tests/test.sh +0 -20
@@ -1,79 +0,0 @@
1
- import { isDefined, isObject } from "prostgles-types";
2
- import { PostgresNotifListenManager } from "../PostgresNotifListenManager";
3
- import { getWatchSchemaTagList } from "../SchemaWatch/getWatchSchemaTagList";
4
- import { NOTIF_CHANNEL, PubSubManager, asValue } from "./PubSubManager";
5
- import { getPubSubManagerInitQuery } from "./getPubSubManagerInitQuery";
6
- export const REALTIME_TRIGGER_CHECK_QUERY = "prostgles-server internal query used to manage realtime triggers" as const;
7
-
8
- export const tout = (ms: number) => new Promise(res => setTimeout(res, ms));
9
-
10
- export async function initPubSubManager(this: PubSubManager): Promise<PubSubManager | undefined> {
11
- if (!this.getIsDestroyed()) return undefined;
12
-
13
- const initQuery = await getPubSubManagerInitQuery.bind(this.dboBuilder)();
14
-
15
- /**
16
- * High database activity might cause deadlocks.
17
- * Must retry
18
- */
19
- let didDeadlock = false;
20
- let tries = 3;
21
- let error: any;
22
- while (isDefined(initQuery) && tries > 0) {
23
- try {
24
- /** Try to reduce race condition deadlocks due to multiple clients connecting at the same time */
25
- await tout(Math.random());
26
-
27
- // await this.dboBuilder.runClientTransactionStatement(initQuery);// this.db.tx(t => t.any(initQuery));
28
- await this.db.tx(t => t.any(initQuery));
29
- error = undefined;
30
- tries = 0;
31
- } catch (e: any) {
32
- if(!didDeadlock && isObject(e) && e.code === "40P01"){
33
- didDeadlock = true;
34
- tries = 5;
35
- console.error("Deadlock detected. Retrying...");
36
- }
37
- error = e;
38
- tries --;
39
- }
40
- }
41
- if(error){
42
- throw error;
43
- }
44
-
45
- if (!this.getIsDestroyed()) return;
46
-
47
- /* Prepare App id */
48
- if (!this.appInfoWasInserted) {
49
- this.appInfoWasInserted = true;
50
- const check_frequency_ms = this.appCheckFrequencyMS;
51
- const watching_schema_tag_names = this.dboBuilder.prostgles.schemaWatch?.type.watchType !== "NONE" ? getWatchSchemaTagList(this.dboBuilder.prostgles.opts.watchSchema) : null;
52
- await this.db.one(
53
- "INSERT INTO prostgles.apps (id, check_frequency_ms, watching_schema_tag_names, application_name) \
54
- VALUES($1, $2, $3, current_setting('application_name')) \
55
- RETURNING *; "
56
- , [
57
- this.appId,
58
- check_frequency_ms,
59
- watching_schema_tag_names
60
- ]
61
- );
62
-
63
- const appRecord = await this.db.one("SELECT * FROM prostgles.apps WHERE id = $1", [this.appId]);
64
- if (!appRecord || !appRecord.application_name?.includes(this.appId)) {
65
- throw `initPubSubManager error: App record with application_name containing appId (${this.appId}) not found`;
66
- }
67
-
68
- await this.db.any(`
69
- DELETE FROM prostgles.app_triggers
70
- WHERE app_id = ${asValue(this.appId)}
71
- `);
72
- }
73
-
74
- this.postgresNotifListenManager = new PostgresNotifListenManager(this.db, this.notifListener, NOTIF_CHANNEL.getFull(this.appId));
75
-
76
- await this.initialiseEventTriggers();
77
-
78
- return this;
79
- }
@@ -1,173 +0,0 @@
1
- import { log, NOTIF_TYPE, pickKeys, PubSubManager } from "./PubSubManager";
2
-
3
- /* Relay relevant data to relevant subscriptions */
4
- export async function notifListener(this: PubSubManager, data: { payload: string }) {
5
- const str = data.payload;
6
-
7
- if (!str) {
8
- console.error("Unexpected Empty notif")
9
- return;
10
- }
11
-
12
- const dataArr = str.split(PubSubManager.DELIMITER);
13
- const notifType = dataArr[0];
14
-
15
- log(str);
16
-
17
- if (notifType === NOTIF_TYPE.schema) {
18
-
19
- if (this.dboBuilder.prostgles.schemaWatch?.onSchemaChange) {
20
- const [_, command, _event_type, query] = dataArr;
21
- await this.dboBuilder.prostgles.opts.onLog?.({ type: "debug", command: "schemaChangeNotif", duration: 0, data: { command, query } });
22
-
23
- if (query && command) {
24
- this.dboBuilder.prostgles.schemaWatch.onSchemaChange({ command, query })
25
- }
26
- }
27
-
28
- return;
29
- } else if(notifType === NOTIF_TYPE.data_trigger_change) {
30
-
31
- await this.refreshTriggers();
32
- return;
33
- }
34
-
35
- if (notifType !== NOTIF_TYPE.data) {
36
- console.error("Unexpected notif type: ", notifType);
37
- return;
38
- }
39
-
40
- if (dataArr.length < 3) {
41
- throw "notifListener: dataArr length < 3"
42
- }
43
-
44
- const [_, table_name, op_name, condition_ids_str] = dataArr;
45
- const condition_ids = condition_ids_str?.split(",").map(v => +v);
46
-
47
-
48
- if(!table_name) {
49
- throw "table_name undef";
50
- }
51
-
52
- const tableTriggerConditions = this._triggers?.[table_name]?.map((condition, idx) => ({
53
- idx,
54
- condition,
55
- subs: this.getTriggerSubs(table_name, condition),
56
- syncs: this.getSyncs(table_name, condition),
57
- }));
58
- let state: "error" | "no-triggers" | "ok" | "invalid_condition_ids" = "ok";
59
-
60
- // const triggers = await this.db.any("SELECT * FROM prostgles.triggers WHERE table_name = $1 AND id IN ($2:csv)", [table_name, condition_ids_str.split(",").map(v => +v)]);
61
- // const conditions: string[] = triggers.map(t => t.condition);
62
-
63
-
64
- if(!tableTriggerConditions?.length){
65
- state = "no-triggers";
66
-
67
- /* Trigger error */
68
- } else if (
69
- condition_ids_str?.startsWith("error")
70
- ) {
71
- state = "error";
72
- const pref = "INTERNAL ERROR";
73
- console.error(`${pref}: condition_ids_str: ${condition_ids_str}`)
74
- tableTriggerConditions.map(({ condition }) => {
75
- const subs = this.getTriggerSubs(table_name, condition);
76
- subs.map(s => {
77
- this.pushSubData(s, pref + ". Check server logs. Schema might have changed");
78
- })
79
- });
80
-
81
- /* Trigger ok */
82
- } else if (
83
- condition_ids?.every(id => Number.isInteger(id))
84
- ) {
85
-
86
- state = "ok";
87
- const firedTableConditions = tableTriggerConditions.filter(({ idx }) => condition_ids.includes(idx));
88
- const orphanedTableConditions = condition_ids.filter((condId) => {
89
- const tc = tableTriggerConditions.at(condId);
90
- return !tc || (tc.subs.length === 0 && tc.syncs.length === 0);
91
- });
92
- if(orphanedTableConditions.length){
93
- this.db
94
- .any(`
95
- /* Delete removed subscriptions */
96
- /* ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID} */
97
- DELETE FROM prostgles.app_triggers at
98
- WHERE EXISTS (
99
- SELECT 1
100
- FROM prostgles.v_triggers t
101
- WHERE t.table_name = $1
102
- AND t.c_id IN ($2:csv)
103
- AND t.app_id = $3
104
- AND at.app_id = t.app_id
105
- AND at.table_name = t.table_name
106
- AND at.condition = t.condition
107
- )
108
- `,
109
- [table_name, orphanedTableConditions, this.appId]
110
- )
111
- .then(() => {
112
- this.refreshTriggers();
113
- })
114
- .catch(e => {
115
- console.error("Error deleting orphaned triggers", e);
116
- });
117
- }
118
-
119
- firedTableConditions.map(({ subs, syncs }) => {
120
-
121
- log("notifListener", subs.map(s => s.channel_name), syncs.map(s => s.channel_name))
122
-
123
- syncs.map((s) => {
124
- this.syncData(s, undefined, "trigger");
125
- });
126
-
127
- /* Throttle the subscriptions */
128
- const activeAndReadySubs = subs.filter(sub =>
129
- sub.triggers.some(trg =>
130
- this.dbo[trg.table_name] &&
131
- sub.is_ready &&
132
- (sub.socket_id && this.sockets[sub.socket_id] || sub.localFuncs)
133
- )
134
- );
135
- activeAndReadySubs.forEach(sub => {
136
- const { throttle = 0, throttleOpts } = sub;
137
- if (!throttleOpts?.skipFirst && sub.last_throttled <= Date.now() - throttle) {
138
- sub.last_throttled = Date.now();
139
-
140
- /* It is assumed the policy was checked before this point */
141
- this.pushSubData(sub);
142
- } else if (!sub.is_throttling) {
143
-
144
- log("throttling sub")
145
- sub.is_throttling = setTimeout(() => {
146
- log("throttling finished. pushSubData...")
147
- sub.is_throttling = null;
148
- sub.last_throttled = Date.now();
149
- this.pushSubData(sub);
150
- }, throttle);
151
- }
152
- });
153
-
154
- });
155
-
156
- /* Trigger unknown issue */
157
- } else {
158
- state = "invalid_condition_ids";
159
- }
160
-
161
- await this._log({
162
- type: "sync",
163
- command: "notifListener",
164
- state,
165
- tableName: table_name,
166
- op_name,
167
- condition_ids_str,
168
- tableTriggers: this._triggers?.[table_name],
169
- tableSyncs: JSON.stringify(this.syncs.filter(s => s.table_name === table_name).map(s => pickKeys(s, ["condition", "socket_id"]))),
170
- connectedSocketIds: this.connectedSocketIds,
171
- });
172
-
173
- }
@@ -1,70 +0,0 @@
1
- import { PubSubManager } from './PubSubManager';
2
- import { REALTIME_TRIGGER_CHECK_QUERY } from "./initPubSubManager";
3
-
4
- /**
5
- * Schema and Data watch triggers (DB_OBJ_NAMES.schema_watch_func, DB_OBJ_NAMES.data_watch_func)
6
- * survive and continue to user resources even after the client disconnects.
7
- * We must therefore delete apps that do not have active connections
8
- */
9
-
10
- const queryIdentifier = "prostgles query used to keep track of which prgl backend clients are still connected";
11
- const connectedApplicationNamesQuery = `
12
- SELECT DISTINCT application_name
13
- FROM prostgles.apps
14
- WHERE application_name IN (
15
- SELECT application_name
16
- FROM pg_catalog.pg_stat_activity
17
- )
18
- `;
19
-
20
- export const DELETE_DISCONNECTED_APPS_QUERY = `
21
- DELETE FROM prostgles.apps a
22
- WHERE NOT EXISTS (
23
- SELECT 1
24
- FROM pg_catalog.pg_stat_activity s
25
- WHERE s.application_name = a.application_name
26
- )
27
- `;
28
-
29
- /** It is a function to prevent undefined EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID */
30
- export const getAppCheckQuery = () => `
31
- /*
32
- ${queryIdentifier}
33
- ${REALTIME_TRIGGER_CHECK_QUERY}
34
- ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
35
- */
36
- IF
37
- /* prostgles schema must exist */
38
- EXISTS (
39
- SELECT 1
40
- FROM information_schema.tables
41
- WHERE table_schema = 'prostgles'
42
- AND table_name = 'apps'
43
- )
44
- /* Ensure we don't check in paralel */
45
- AND NOT EXISTS (
46
- SELECT 1
47
- FROM pg_catalog.pg_stat_activity s
48
- WHERE s.query ilike '%${queryIdentifier}%'
49
- AND s.state = 'active'
50
- )
51
- THEN
52
-
53
- IF EXISTS (
54
- ${connectedApplicationNamesQuery}
55
- ) THEN
56
-
57
- /* Remove disconnected apps */
58
- WITH deleted_apps AS (
59
- ${DELETE_DISCONNECTED_APPS_QUERY}
60
- RETURNING a.id
61
- )
62
- DELETE FROM prostgles.app_triggers
63
- WHERE app_id IN (
64
- SELECT id
65
- FROM deleted_apps
66
- );
67
-
68
- END IF;
69
- END IF;
70
- `;
@@ -1,55 +0,0 @@
1
- import { parseLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
2
- import { log, PubSubManager, Subscription } from "./PubSubManager";
3
-
4
- export async function pushSubData(this: PubSubManager, sub: Subscription, err?: any) {
5
- if (!sub) throw "pushSubData: invalid sub";
6
-
7
- const { socket_id, channel_name } = sub;
8
- if(!this.subs.some(s => s.channel_name === channel_name)){
9
- // Might be throttling a sub that was removed
10
- return;
11
- }
12
- const localFuncs = parseLocalFuncs(sub.localFuncs);
13
-
14
- if (err) {
15
- if (socket_id) {
16
- this.sockets[socket_id].emit(channel_name, { err });
17
- }
18
- return true;
19
- }
20
-
21
- return new Promise(async (resolve, reject) => {
22
- /* TODO: Retire subOne -> it's redundant */
23
-
24
- const { data, err } = await this.getSubData(sub);
25
-
26
- if(data){
27
-
28
- if (socket_id && this.sockets[socket_id]) {
29
- log("Pushed " + data.length + " records to sub")
30
- this.sockets[socket_id].emit(channel_name, { data }, () => {
31
- resolve(data);
32
- });
33
- /* TO DO: confirm receiving data or server will unsubscribe
34
- { data }, (cb)=> { console.log(cb) });
35
- */
36
- } else if (localFuncs) {
37
- localFuncs.onData(data);
38
- resolve(data);
39
- }
40
- // sub.last_throttled = Date.now();
41
-
42
- } else {
43
- const errObj = { _err_msg: err.toString(), err };
44
- if (socket_id && this.sockets[socket_id]) {
45
- this.sockets[socket_id].emit(channel_name, { err: errObj });
46
- } else if (localFuncs) {
47
- if(!localFuncs.onError){
48
- console.error("Uncaught subscription error", err);
49
- }
50
- localFuncs.onError?.(errObj);
51
- }
52
- reject(errObj);
53
- }
54
- });
55
- }
@@ -1,162 +0,0 @@
1
- import { Method, isObject } from "prostgles-types";
2
- import { AuthResult, SessionUser } from "../Auth/AuthTypes";
3
- import { LocalParams } from "../DboBuilder/DboBuilder";
4
- import { DB, DBHandlerServer, Prostgles } from "../Prostgles";
5
- import { VoidFunction } from "../SchemaWatch/SchemaWatch";
6
- import { getFileTableRules } from "./getFileTableRules";
7
- import { getSchemaFromPublish } from "./getSchemaFromPublish";
8
- import { getTableRulesWithoutFileTable } from "./getTableRulesWithoutFileTable";
9
- import { DboTable, DboTableCommand, ParsedPublishTable, PublishMethods, PublishObject, PublishParams, RULE_TO_METHODS, TableRule } from "./publishTypesAndUtils";
10
-
11
- export class PublishParser {
12
- publish: any;
13
- publishMethods?: PublishMethods<void, SessionUser> | undefined;
14
- publishRawSQL?: any;
15
- dbo: DBHandlerServer;
16
- db: DB
17
- prostgles: Prostgles;
18
-
19
- constructor(publish: any, publishMethods: PublishMethods<void, SessionUser> | undefined, publishRawSQL: any, dbo: DBHandlerServer, db: DB, prostgles: Prostgles) {
20
- this.publish = publish;
21
- this.publishMethods = publishMethods;
22
- this.publishRawSQL = publishRawSQL;
23
- this.dbo = dbo;
24
- this.db = db;
25
- this.prostgles = prostgles;
26
-
27
- if (!this.dbo || !this.publish) throw "INTERNAL ERROR: dbo and/or publish missing";
28
- }
29
-
30
- async getPublishParams(localParams: LocalParams, clientInfo?: AuthResult): Promise<PublishParams> {
31
- if (!this.dbo) throw "dbo missing"
32
- return {
33
- ...(clientInfo || await this.prostgles.authHandler?.getClientInfo(localParams)),
34
- dbo: this.dbo as any,
35
- db: this.db,
36
- socket: localParams.socket!,
37
- tables: this.prostgles.dboBuilder.tables,
38
- }
39
- }
40
-
41
- async getAllowedMethods(reqInfo: Pick<LocalParams, "httpReq" | "socket">, userData?: AuthResult): Promise<{ [key: string]: Method; }> {
42
- const methods: { [key: string]: Method; } = {};
43
-
44
- const publishParams = await this.getPublishParams(reqInfo, userData);
45
- const _methods = await applyParamsIfFunc(this.publishMethods, publishParams);
46
-
47
- if (_methods && Object.keys(_methods).length) {
48
- Object.entries(_methods).map(([key, method]) => {
49
- const isFuncLike = (maybeFunc: VoidFunction | Promise<void>) => (typeof maybeFunc === "function" || maybeFunc && typeof maybeFunc.then === "function");
50
- //@ts-ignore
51
- if (method && (isFuncLike(method) || isObject(method) && isFuncLike(method.run))) {
52
- //@ts-ignore
53
- methods[key] = _methods[key];
54
- } else {
55
- throw `invalid publishMethods item -> ${key} \n Expecting a function or promise`
56
- }
57
- });
58
- }
59
-
60
- return methods;
61
- }
62
-
63
- /**
64
- * Parses the first level of publish. (If false then nothing if * then all tables and views)
65
- * @param socket
66
- * @param user
67
- */
68
- async getPublish(localParams: LocalParams, clientInfo?: AuthResult): Promise<PublishObject> {
69
- const publishParams = await this.getPublishParams(localParams, clientInfo)
70
- const _publish = await applyParamsIfFunc(this.publish, publishParams);
71
-
72
- if (_publish === "*") {
73
- const publish: PublishObject = {};
74
- this.prostgles.dboBuilder.tablesOrViews?.map(tov => {
75
- publish[tov.name] = "*";
76
- });
77
- return publish;
78
- }
79
-
80
- return _publish;
81
- }
82
- async getValidatedRequestRuleWusr({ tableName, command, localParams }: DboTableCommand): Promise<TableRule> {
83
-
84
- const clientInfo = await this.prostgles.authHandler!.getClientInfo(localParams);
85
- const rules = await this.getValidatedRequestRule({ tableName, command, localParams }, clientInfo);
86
- return rules;
87
- }
88
-
89
- async getValidatedRequestRule({ tableName, command, localParams }: DboTableCommand, clientInfo?: AuthResult): Promise<TableRule> {
90
- if (!this.dbo) throw "INTERNAL ERROR: dbo is missing";
91
-
92
- if (!command || !tableName) throw "command OR tableName are missing";
93
-
94
- const rtm = RULE_TO_METHODS.find(rtms => (rtms.methods as any).includes(command));
95
- if (!rtm) {
96
- throw "Invalid command: " + command;
97
- }
98
-
99
- /* Must be local request -> allow everything */
100
- if (!localParams || (!localParams.socket && !localParams.httpReq)) {
101
- return RULE_TO_METHODS.reduce((a, v) => ({
102
- ...a,
103
- [v.rule]: v.no_limits
104
- }), {})
105
- }
106
-
107
- /* Must be from socket. Must have a publish */
108
- if (!this.publish) throw "publish is missing";
109
-
110
- /* Get any publish errors for socket */
111
- const schm = localParams?.socket?.prostgles?.schema?.[tableName]?.[command];
112
-
113
- if (schm && schm.err) throw schm.err;
114
-
115
- const table_rule = await this.getTableRules({ tableName, localParams }, clientInfo);
116
- if (!table_rule) throw { stack: ["getValidatedRequestRule()"], message: "Invalid or disallowed table: " + tableName };
117
-
118
-
119
- if (command === "upsert") {
120
- if (!table_rule.update || !table_rule.insert) {
121
- throw { stack: ["getValidatedRequestRule()"], message: `Invalid or disallowed command: upsert` };
122
- }
123
- }
124
-
125
- if (rtm && table_rule && table_rule[rtm.rule]) {
126
- return table_rule;
127
- } else throw { stack: ["getValidatedRequestRule()"], message: `Invalid or disallowed command: ${tableName}.${command}` };
128
- }
129
-
130
- async getTableRules(args: DboTable, clientInfo?: AuthResult): Promise<ParsedPublishTable | undefined> {
131
-
132
- if(this.dbo[args.tableName]?.is_media){
133
- const fileTablePublishRules = await this.getTableRulesWithoutFileTable(args, clientInfo)
134
- const { rules } = await getFileTableRules.bind(this)(args.tableName, fileTablePublishRules, args.localParams, clientInfo);
135
- return rules;
136
- }
137
-
138
- return await this.getTableRulesWithoutFileTable(args, clientInfo)
139
- }
140
-
141
- getTableRulesWithoutFileTable = getTableRulesWithoutFileTable.bind(this);
142
-
143
- /* Prepares schema for client. Only allowed views and commands will be present */
144
- getSchemaFromPublish = getSchemaFromPublish.bind(this);
145
-
146
- }
147
-
148
- export * from "./publishTypesAndUtils";
149
-
150
- type FunctionWithArguments = (...args: any) => any
151
- function applyParamsIfFunc<T>(maybeFunc: T, ...params: any): T extends FunctionWithArguments ? ReturnType<T> : T {
152
- if (
153
- (maybeFunc !== null && maybeFunc !== undefined) &&
154
- //@ts-ignore
155
- (typeof maybeFunc === "function" || typeof maybeFunc.then === "function")
156
- ) {
157
- return (maybeFunc as FunctionWithArguments)(...params);
158
- }
159
-
160
- //@ts-ignore
161
- return maybeFunc;
162
- }
@@ -1,124 +0,0 @@
1
- import { AnyObject, FullFilter, isDefined } from "prostgles-types";
2
- import { AuthResult } from "../Auth/AuthTypes";
3
- import { LocalParams } from "../DboBuilder/DboBuilder";
4
- import { parseFieldFilter } from "../DboBuilder/ViewHandler/parseFieldFilter";
5
- import { PublishParser } from "./PublishParser";
6
- import { ParsedPublishTable, UpdateRule } from "./publishTypesAndUtils";
7
-
8
- /**
9
- * Permissions for referencedTables columns are propagated to the file table (even if file table has no permissions)
10
- * File table existing permissions that include the referenced column resulting permissions are left as they are
11
- * Select on a referenced column allows selecting from file table any records that join the referenced table and the select filters
12
- * Insert on a referenced column allows inserting a file (according to any file type/size rules) only if it is a nested from that table
13
- * Update on a referenced column allows updating a file (delete and insert) only if it is a nested update from that table
14
- * Delete on a referenced column table allows deleting any referenced file
15
- */
16
- export async function getFileTableRules (this: PublishParser, fileTableName: string, fileTablePublishRules: ParsedPublishTable | undefined, localParams: LocalParams, clientInfo: AuthResult | undefined) {
17
- const forcedDeleteFilters: FullFilter<AnyObject, void>[] = [];
18
- const forcedSelectFilters: FullFilter<AnyObject, void>[] = [];
19
- const forcedUpdateFilters: FullFilter<AnyObject, void>[] = [];
20
- const allowedNestedInserts: { table: string; column: string }[] = [];
21
- const referencedColumns = this.prostgles.dboBuilder.tablesOrViews?.filter(t => !t.is_view && t.name !== fileTableName).map(t => {
22
- const refCols = t.columns.filter(c => c.references?.some(r => r.ftable === fileTableName));
23
- if(!refCols.length) return undefined;
24
- return {
25
- tableName: t.name,
26
- fileColumns: refCols.map(c => c.name),
27
- allColumns: t.columns.map(c => c.name),
28
- }
29
- }).filter(isDefined)
30
- if(referencedColumns?.length){
31
- for await (const { tableName, fileColumns, allColumns } of referencedColumns){
32
- const table_rules = await this.getTableRules({ localParams, tableName }, clientInfo);
33
- if(table_rules){
34
- fileColumns.map(column => {
35
- const path = [{ table: tableName, on: [{ id: column }] }];
36
- if(table_rules.delete){
37
- forcedDeleteFilters.push({
38
- $existsJoined: {
39
- path,
40
- filter: table_rules.delete.forcedFilter ?? {},
41
- }
42
- })
43
- }
44
- if(table_rules.select){
45
- const parsedFields = parseFieldFilter(table_rules.select.fields, false, allColumns);
46
- /** Must be allowed to view this column */
47
- if(parsedFields.includes(column as any)){
48
- forcedSelectFilters.push({
49
- $existsJoined: {
50
- path,
51
- filter: table_rules.select.forcedFilter ?? {},
52
- }
53
- });
54
- }
55
- }
56
- if(table_rules.insert){
57
- const parsedFields = parseFieldFilter(table_rules.insert.fields, false, allColumns);
58
- /** Must be allowed to view this column */
59
- if(parsedFields.includes(column as any)){
60
- allowedNestedInserts.push({ table: tableName, column });
61
- }
62
- }
63
- if(table_rules.update){
64
- const parsedFields = parseFieldFilter(table_rules.update.fields, false, allColumns);
65
- /** Must be allowed to view this column */
66
- if(parsedFields.includes(column as any)){
67
- forcedUpdateFilters.push({
68
- $existsJoined: {
69
- path,
70
- filter: table_rules.update.forcedFilter ?? {},
71
- }
72
- });
73
- }
74
- }
75
- })
76
- }
77
- }
78
- }
79
-
80
- const fileTableRule: ParsedPublishTable = {
81
- ...fileTablePublishRules,
82
- };
83
-
84
- const getForcedFilter = (rule: Pick<UpdateRule, "forcedFilter"> | undefined, forcedFilters: FullFilter<AnyObject, void>[]) => {
85
- return (rule && !rule.forcedFilter)? {} : {
86
- forcedFilter: {
87
- $or: forcedFilters.concat(rule?.forcedFilter? [rule?.forcedFilter] : []),
88
- }
89
- }
90
- }
91
- if(forcedSelectFilters.length || fileTablePublishRules?.select){
92
- fileTableRule.select = {
93
- fields: "*",
94
- ...fileTablePublishRules?.select,
95
- ...getForcedFilter(fileTablePublishRules?.select, forcedSelectFilters),
96
- }
97
- }
98
- if(forcedDeleteFilters.length || fileTablePublishRules?.delete){
99
- fileTableRule.delete = {
100
- filterFields: "*",
101
- ...fileTablePublishRules?.delete,
102
- ...getForcedFilter(fileTablePublishRules?.delete, forcedDeleteFilters),
103
- }
104
- }
105
- if(forcedUpdateFilters.length || fileTablePublishRules?.update){
106
- fileTableRule.update = {
107
- fields: "*",
108
- ...fileTablePublishRules?.update,
109
- ...getForcedFilter(fileTablePublishRules?.update, forcedUpdateFilters),
110
- }
111
- }
112
-
113
- if(allowedNestedInserts.length || fileTablePublishRules?.insert){
114
- fileTableRule.insert = {
115
- fields: "*",
116
- ...fileTablePublishRules?.insert,
117
- allowedNestedInserts: fileTablePublishRules?.insert? undefined : allowedNestedInserts,
118
- }
119
- }
120
-
121
- /** Add missing implied methods (getColumns, getInfo) */
122
- const rules = await this.getTableRulesWithoutFileTable.bind(this)({ localParams, tableName: fileTableName }, clientInfo, { [fileTableName]: fileTableRule })
123
- return { rules, allowedInserts: allowedNestedInserts };
124
- }