prostgles-server 4.2.192 → 4.2.193

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 (143) hide show
  1. package/dist/Auth/AuthHandler.d.ts +8 -13
  2. package/dist/Auth/AuthHandler.d.ts.map +1 -1
  3. package/dist/Auth/AuthHandler.js +34 -89
  4. package/dist/Auth/AuthHandler.js.map +1 -1
  5. package/dist/Auth/AuthTypes.d.ts +16 -6
  6. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  7. package/dist/Auth/authProviders/setOAuthProviders.js +1 -1
  8. package/dist/Auth/authProviders/setOAuthProviders.js.map +1 -1
  9. package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js +1 -1
  10. package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js.map +1 -1
  11. package/dist/Auth/endpoints/getRegisterRequestHandler.js +1 -1
  12. package/dist/Auth/endpoints/getRegisterRequestHandler.js.map +1 -1
  13. package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
  14. package/dist/Auth/setupAuthRoutes.js +16 -10
  15. package/dist/Auth/setupAuthRoutes.js.map +1 -1
  16. package/dist/Auth/utils/getUserFromRequest.d.ts +7 -0
  17. package/dist/Auth/utils/getUserFromRequest.d.ts.map +1 -0
  18. package/dist/Auth/utils/getUserFromRequest.js +66 -0
  19. package/dist/Auth/utils/getUserFromRequest.js.map +1 -0
  20. package/dist/DboBuilder/DboBuilder.d.ts +1 -1
  21. package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
  22. package/dist/DboBuilder/DboBuilder.js +6 -1
  23. package/dist/DboBuilder/DboBuilder.js.map +1 -1
  24. package/dist/DboBuilder/DboBuilderTypes.d.ts +15 -7
  25. package/dist/DboBuilder/DboBuilderTypes.d.ts.map +1 -1
  26. package/dist/DboBuilder/DboBuilderTypes.js.map +1 -1
  27. package/dist/DboBuilder/QueryBuilder/getNewQuery.js +2 -2
  28. package/dist/DboBuilder/QueryBuilder/getNewQuery.js.map +1 -1
  29. package/dist/DboBuilder/QueryStreamer.js +1 -1
  30. package/dist/DboBuilder/QueryStreamer.js.map +1 -1
  31. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  32. package/dist/DboBuilder/TableHandler/TableHandler.js +2 -3
  33. package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
  34. package/dist/DboBuilder/TableHandler/insert.js +2 -2
  35. package/dist/DboBuilder/TableHandler/update.js +1 -1
  36. package/dist/DboBuilder/TableHandler/update.js.map +1 -1
  37. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +0 -4
  38. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  39. package/dist/DboBuilder/ViewHandler/ViewHandler.js +37 -24
  40. package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
  41. package/dist/DboBuilder/ViewHandler/find.js +1 -1
  42. package/dist/DboBuilder/ViewHandler/find.js.map +1 -1
  43. package/dist/DboBuilder/ViewHandler/getExistsCondition.js +4 -4
  44. package/dist/DboBuilder/ViewHandler/getExistsCondition.js.map +1 -1
  45. package/dist/DboBuilder/ViewHandler/subscribe.d.ts.map +1 -1
  46. package/dist/DboBuilder/ViewHandler/subscribe.js +9 -15
  47. package/dist/DboBuilder/ViewHandler/subscribe.js.map +1 -1
  48. package/dist/DboBuilder/dboBuilderUtils.d.ts.map +1 -1
  49. package/dist/DboBuilder/dboBuilderUtils.js +3 -1
  50. package/dist/DboBuilder/dboBuilderUtils.js.map +1 -1
  51. package/dist/DboBuilder/insertNestedRecords.d.ts +4 -3
  52. package/dist/DboBuilder/insertNestedRecords.d.ts.map +1 -1
  53. package/dist/DboBuilder/insertNestedRecords.js +12 -12
  54. package/dist/DboBuilder/insertNestedRecords.js.map +1 -1
  55. package/dist/DboBuilder/runSQL.d.ts +3 -2
  56. package/dist/DboBuilder/runSQL.d.ts.map +1 -1
  57. package/dist/DboBuilder/runSQL.js +12 -15
  58. package/dist/DboBuilder/runSQL.js.map +1 -1
  59. package/dist/FileManager/initFileManager.d.ts.map +1 -1
  60. package/dist/FileManager/initFileManager.js +5 -4
  61. package/dist/FileManager/initFileManager.js.map +1 -1
  62. package/dist/Prostgles.d.ts +3 -2
  63. package/dist/Prostgles.d.ts.map +1 -1
  64. package/dist/Prostgles.js +8 -16
  65. package/dist/Prostgles.js.map +1 -1
  66. package/dist/ProstglesTypes.d.ts +2 -2
  67. package/dist/ProstglesTypes.d.ts.map +1 -1
  68. package/dist/ProstglesTypes.js +1 -6
  69. package/dist/ProstglesTypes.js.map +1 -1
  70. package/dist/PubSubManager/PubSubManager.js +2 -2
  71. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  72. package/dist/PubSubManager/addSync.d.ts.map +1 -1
  73. package/dist/PubSubManager/addSync.js +1 -3
  74. package/dist/PubSubManager/addSync.js.map +1 -1
  75. package/dist/PublishParser/PublishParser.d.ts +11 -18
  76. package/dist/PublishParser/PublishParser.d.ts.map +1 -1
  77. package/dist/PublishParser/PublishParser.js +28 -27
  78. package/dist/PublishParser/PublishParser.js.map +1 -1
  79. package/dist/PublishParser/getFileTableRules.d.ts +2 -3
  80. package/dist/PublishParser/getFileTableRules.d.ts.map +1 -1
  81. package/dist/PublishParser/getFileTableRules.js +18 -20
  82. package/dist/PublishParser/getFileTableRules.js.map +1 -1
  83. package/dist/PublishParser/getSchemaFromPublish.d.ts +2 -9
  84. package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
  85. package/dist/PublishParser/getSchemaFromPublish.js +5 -5
  86. package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
  87. package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts +1 -1
  88. package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts.map +1 -1
  89. package/dist/PublishParser/getTableRulesWithoutFileTable.js +30 -33
  90. package/dist/PublishParser/getTableRulesWithoutFileTable.js.map +1 -1
  91. package/dist/PublishParser/publishTypesAndUtils.d.ts +6 -11
  92. package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
  93. package/dist/PublishParser/publishTypesAndUtils.js.map +1 -1
  94. package/dist/RestApi.d.ts +1 -1
  95. package/dist/RestApi.d.ts.map +1 -1
  96. package/dist/RestApi.js +19 -16
  97. package/dist/RestApi.js.map +1 -1
  98. package/dist/initProstgles.d.ts.map +1 -1
  99. package/dist/initProstgles.js +11 -15
  100. package/dist/initProstgles.js.map +1 -1
  101. package/dist/onSocketConnected.d.ts.map +1 -1
  102. package/dist/onSocketConnected.js +5 -5
  103. package/dist/onSocketConnected.js.map +1 -1
  104. package/dist/runClientRequest.d.ts +14 -29
  105. package/dist/runClientRequest.d.ts.map +1 -1
  106. package/dist/runClientRequest.js +20 -34
  107. package/dist/runClientRequest.js.map +1 -1
  108. package/lib/Auth/AuthHandler.ts +45 -103
  109. package/lib/Auth/AuthTypes.ts +19 -8
  110. package/lib/Auth/authProviders/setOAuthProviders.ts +1 -1
  111. package/lib/Auth/endpoints/getConfirmEmailRequestHandler.ts +1 -1
  112. package/lib/Auth/endpoints/getRegisterRequestHandler.ts +1 -1
  113. package/lib/Auth/setupAuthRoutes.ts +17 -13
  114. package/lib/Auth/utils/getUserFromRequest.ts +71 -0
  115. package/lib/DboBuilder/DboBuilder.ts +7 -3
  116. package/lib/DboBuilder/DboBuilderTypes.ts +19 -17
  117. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +2 -2
  118. package/lib/DboBuilder/QueryStreamer.ts +1 -1
  119. package/lib/DboBuilder/TableHandler/TableHandler.ts +2 -3
  120. package/lib/DboBuilder/TableHandler/insert.ts +2 -2
  121. package/lib/DboBuilder/TableHandler/update.ts +1 -1
  122. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +38 -37
  123. package/lib/DboBuilder/ViewHandler/find.ts +1 -1
  124. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +4 -4
  125. package/lib/DboBuilder/ViewHandler/subscribe.ts +22 -41
  126. package/lib/DboBuilder/dboBuilderUtils.ts +3 -1
  127. package/lib/DboBuilder/insertNestedRecords.ts +18 -16
  128. package/lib/DboBuilder/runSQL.ts +14 -16
  129. package/lib/FileManager/initFileManager.ts +16 -12
  130. package/lib/Prostgles.ts +10 -24
  131. package/lib/ProstglesTypes.ts +9 -31
  132. package/lib/PubSubManager/PubSubManager.ts +3 -3
  133. package/lib/PubSubManager/addSync.ts +1 -3
  134. package/lib/PublishParser/PublishParser.ts +35 -45
  135. package/lib/PublishParser/getFileTableRules.ts +24 -48
  136. package/lib/PublishParser/getSchemaFromPublish.ts +12 -23
  137. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +30 -41
  138. package/lib/PublishParser/publishTypesAndUtils.ts +8 -21
  139. package/lib/RestApi.ts +43 -31
  140. package/lib/initProstgles.ts +51 -64
  141. package/lib/onSocketConnected.ts +12 -9
  142. package/lib/runClientRequest.ts +50 -66
  143. package/package.json +3 -3
@@ -1,11 +1,7 @@
1
1
  import { FileColumnConfig } from "prostgles-types";
2
2
  import { Auth, AuthRequestParams, SessionUser } from "./Auth/AuthTypes";
3
3
  import { EventTriggerTagFilter } from "./Event_Trigger_Tags";
4
- import {
5
- CloudClient,
6
- ImageOptions,
7
- LocalConfig,
8
- } from "./FileManager/FileManager";
4
+ import { CloudClient, ImageOptions, LocalConfig } from "./FileManager/FileManager";
9
5
  import { DbConnection, OnReadyCallback } from "./initProstgles";
10
6
  import { EventInfo } from "./Logging";
11
7
  import { ExpressApp, RestApiConfig } from "./RestApi";
@@ -20,11 +16,7 @@ import pg from "pg-promise/typescript/pg-subset";
20
16
  import { AnyObject } from "prostgles-types";
21
17
  import type { Server } from "socket.io";
22
18
  import { DB } from "./Prostgles";
23
- import {
24
- Publish,
25
- PublishMethods,
26
- PublishParams,
27
- } from "./PublishParser/PublishParser";
19
+ import { Awaitable, Publish, PublishMethods, PublishParams } from "./PublishParser/PublishParser";
28
20
 
29
21
  /**
30
22
  * Allows uploading and downloading files.
@@ -102,12 +94,7 @@ export type FileTableConfig = {
102
94
  localConfig?: LocalConfig;
103
95
  };
104
96
 
105
- export const JOIN_TYPES = [
106
- "one-many",
107
- "many-one",
108
- "one-one",
109
- "many-many",
110
- ] as const;
97
+ export const JOIN_TYPES = ["one-many", "many-one", "one-one", "many-many"] as const;
111
98
  export type Join = {
112
99
  tables: [string, string];
113
100
  on: { [key: string]: string }[]; // Allow multi references to table
@@ -115,10 +102,7 @@ export type Join = {
115
102
  };
116
103
  type Joins = Join[] | "inferred";
117
104
 
118
- export type ProstglesInitOptions<
119
- S = void,
120
- SUser extends SessionUser = SessionUser,
121
- > = {
105
+ export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUser> = {
122
106
  /**
123
107
  * Database connection details and options
124
108
  */
@@ -187,9 +171,7 @@ export type ProstglesInitOptions<
187
171
  /**
188
172
  * If defined and resolves to true then the connected client can run SQL queries
189
173
  */
190
- publishRawSQL?(
191
- params: PublishParams<S, SUser>,
192
- ): (boolean | "*") | Promise<boolean | "*">;
174
+ publishRawSQL?(params: PublishParams<S, SUser>): Awaitable<boolean | "*">;
193
175
 
194
176
  /**
195
177
  * Server-side functions that can be invoked by the client
@@ -236,14 +218,14 @@ export type ProstglesInitOptions<
236
218
  * Use for connection verification. Will disconnect socket on any errors
237
219
  */
238
220
  onSocketConnect?: (
239
- args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket },
221
+ args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }
240
222
  ) => void | Promise<void>;
241
223
 
242
224
  /**
243
225
  * Called when a socket disconnects
244
226
  */
245
227
  onSocketDisconnect?: (
246
- args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket },
228
+ args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }
247
229
  ) => void | Promise<void>;
248
230
 
249
231
  /**
@@ -281,11 +263,7 @@ export type ProstglesInitOptions<
281
263
  * - `OnSchemaChangeCallback` - custom callback to be fired. Nothing else triggered
282
264
  * Useful for development
283
265
  */
284
- watchSchema?:
285
- | boolean
286
- | EventTriggerTagFilter
287
- | "hotReloadMode"
288
- | OnSchemaChangeCallback;
266
+ watchSchema?: boolean | EventTriggerTagFilter | "hotReloadMode" | OnSchemaChangeCallback;
289
267
 
290
268
  /**
291
269
  * Called when a notice is received from the database
@@ -357,6 +335,6 @@ type OnMigrate = (args: {
357
335
  getConstraints: (
358
336
  table: string,
359
337
  column?: string,
360
- types?: ColConstraint["type"][],
338
+ types?: ColConstraint["type"][]
361
339
  ) => Promise<ColConstraint[]>;
362
340
  }) => void;
@@ -295,7 +295,7 @@ export class PubSubManager {
295
295
 
296
296
  await this.db
297
297
  .tx((t) => t.any(query))
298
- .catch((e) => {
298
+ .catch((e: any) => {
299
299
  console.error("prepareTriggers failed: ", e);
300
300
  throw e;
301
301
  });
@@ -545,10 +545,10 @@ export class PubSubManager {
545
545
  socketId: socket?.id,
546
546
  state: !addedTrigger.tbl ? "fail" : "ok",
547
547
  error: addedTrigger.error,
548
- sid: this.dboBuilder.prostgles.authHandler?.getSIDNoError({ socket }),
548
+ sid: socket && this.dboBuilder.prostgles.authHandler?.getSIDNoError({ socket }),
549
549
  tableName: addedTrigger.tbl ?? params.table_name,
550
550
  connectedSocketIds: this.dboBuilder.prostgles.connectedSockets.map((s) => s.id),
551
- localParams: { socket },
551
+ localParams: socket && { clientReq: { socket } },
552
552
  });
553
553
 
554
554
  if (addedTrigger.error) throw addedTrigger.error;
@@ -15,9 +15,7 @@ export async function addSync(
15
15
  this: PubSubManager,
16
16
  syncParams: AddSyncParams
17
17
  ): Promise<{ channelName: string }> {
18
- const sid = this.dboBuilder.prostgles.authHandler?.getSIDNoError({
19
- socket: syncParams.socket,
20
- });
18
+ const sid = this.dboBuilder.prostgles.authHandler?.getSIDNoError({ socket: syncParams.socket });
21
19
  const res = await tryCatchV2(async () => {
22
20
  const {
23
21
  socket = null,
@@ -1,7 +1,8 @@
1
1
  import { Method, getObjectEntries, isObject } from "prostgles-types";
2
- import { AuthResult, SessionUser } from "../Auth/AuthTypes";
3
- import { LocalParams } from "../DboBuilder/DboBuilder";
2
+ import { AuthClientRequest, AuthResult, SessionUser } from "../Auth/AuthTypes";
3
+ import { PublishFullyTyped } from "../DBSchemaBuilder";
4
4
  import { DB, DBHandlerServer, Prostgles } from "../Prostgles";
5
+ import { ProstglesInitOptions } from "../ProstglesTypes";
5
6
  import { VoidFunction } from "../SchemaWatch/SchemaWatch";
6
7
  import { getFileTableRules } from "./getFileTableRules";
7
8
  import { getSchemaFromPublish } from "./getSchemaFromPublish";
@@ -16,8 +17,6 @@ import {
16
17
  RULE_TO_METHODS,
17
18
  TableRule,
18
19
  } from "./publishTypesAndUtils";
19
- import { ProstglesInitOptions } from "../ProstglesTypes";
20
- import { PublishFullyTyped } from "../DBSchemaBuilder";
21
20
 
22
21
  export class PublishParser {
23
22
  publish: ProstglesInitOptions["publish"];
@@ -27,44 +26,37 @@ export class PublishParser {
27
26
  db: DB;
28
27
  prostgles: Prostgles;
29
28
 
30
- constructor(
31
- publish: any,
32
- publishMethods: PublishMethods<void, SessionUser> | undefined,
33
- publishRawSQL: any,
34
- dbo: DBHandlerServer,
35
- db: DB,
36
- prostgles: Prostgles
37
- ) {
38
- this.publish = publish;
39
- this.publishMethods = publishMethods;
40
- this.publishRawSQL = publishRawSQL;
29
+ constructor(prostgles: Prostgles) {
30
+ this.prostgles = prostgles;
31
+ this.publish = prostgles.opts.publish;
32
+ this.publishMethods = prostgles.opts.publishMethods;
33
+ this.publishRawSQL = prostgles.opts.publishRawSQL;
34
+ const { dbo, db } = prostgles;
35
+ if (!dbo || !db) throw "INTERNAL ERROR: dbo and/or db missing";
41
36
  this.dbo = dbo;
42
37
  this.db = db;
43
- this.prostgles = prostgles;
44
-
45
- if (!this.publish) throw "INTERNAL ERROR: dbo and/or publish missing";
46
38
  }
47
39
 
48
40
  async getPublishParams(
49
- localParams: LocalParams,
41
+ clientReq: AuthClientRequest,
50
42
  clientInfo?: AuthResult
51
43
  ): Promise<PublishParams> {
52
44
  return {
53
- ...(clientInfo || (await this.prostgles.authHandler?.getClientInfo(localParams))),
45
+ ...(clientInfo || (await this.prostgles.authHandler?.getUserFromRequest(clientReq))),
54
46
  dbo: this.dbo as any,
55
47
  db: this.db,
56
- socket: localParams.socket!,
48
+ clientReq,
57
49
  tables: this.prostgles.dboBuilder.tables,
58
50
  };
59
51
  }
60
52
 
61
53
  async getAllowedMethods(
62
- reqInfo: Pick<LocalParams, "httpReq" | "socket">,
63
- userData?: AuthResult
54
+ clientReq: AuthClientRequest,
55
+ userData: AuthResult | undefined
64
56
  ): Promise<{ [key: string]: Method }> {
65
57
  const methods: { [key: string]: Method } = {};
66
58
 
67
- const publishParams = await this.getPublishParams(reqInfo, userData);
59
+ const publishParams = await this.getPublishParams(clientReq, userData);
68
60
  const _methods = await applyParamsIfFunc(this.publishMethods, publishParams);
69
61
 
70
62
  if (_methods && Object.keys(_methods).length) {
@@ -90,10 +82,10 @@ export class PublishParser {
90
82
  * Parses the first level of publish. (If false then nothing if * then all tables and views)
91
83
  */
92
84
  async getPublish(
93
- localParams: LocalParams,
94
- clientInfo?: AuthResult
85
+ clientReq: AuthClientRequest,
86
+ clientInfo: AuthResult
95
87
  ): Promise<PublishFullyTyped | undefined> {
96
- const publishParams = await this.getPublishParams(localParams, clientInfo);
88
+ const publishParams = await this.getPublishParams(clientReq, clientInfo);
97
89
  const _publish = await applyParamsIfFunc(this.publish, publishParams);
98
90
 
99
91
  if (_publish === "*") {
@@ -106,32 +98,30 @@ export class PublishParser {
106
98
 
107
99
  return _publish || undefined;
108
100
  }
101
+
109
102
  async getValidatedRequestRuleWusr({
110
103
  tableName,
111
104
  command,
112
- localParams,
105
+ clientReq,
113
106
  }: DboTableCommand): Promise<TableRule> {
114
- const clientInfo = await this.prostgles.authHandler!.getClientInfo(localParams);
115
- const rules = await this.getValidatedRequestRule(
116
- { tableName, command, localParams },
117
- clientInfo
118
- );
107
+ const clientInfo = await this.prostgles.authHandler?.getUserFromRequest(clientReq);
108
+ const rules = await this.getValidatedRequestRule({ tableName, command, clientReq }, clientInfo);
119
109
  return rules;
120
110
  }
121
111
 
122
112
  async getValidatedRequestRule(
123
- { tableName, command, localParams }: DboTableCommand,
124
- clientInfo?: AuthResult
113
+ { tableName, command, clientReq }: DboTableCommand,
114
+ clientInfo: AuthResult | undefined
125
115
  ): Promise<TableRule> {
126
116
  if (!command || !tableName) throw "command OR tableName are missing";
127
117
 
128
- const rtm = RULE_TO_METHODS.find((rtms) => (rtms.methods as any).includes(command));
118
+ const rtm = RULE_TO_METHODS.find((rtms) => rtms.methods.some((v) => v === command));
129
119
  if (!rtm) {
130
120
  throw "Invalid command: " + command;
131
121
  }
132
122
 
133
123
  /* Must be local request -> allow everything */
134
- if (!localParams.socket && !localParams.httpReq) {
124
+ if (!clientReq) {
135
125
  return RULE_TO_METHODS.reduce(
136
126
  (a, v) => ({
137
127
  ...a,
@@ -145,19 +135,19 @@ export class PublishParser {
145
135
  if (!this.publish) throw "publish is missing";
146
136
 
147
137
  /* Get any publish errors for socket */
148
- const errorInfo = localParams.socket?.prostgles?.tableSchemaErrors[tableName]?.[command];
138
+ const errorInfo = clientReq.socket?.prostgles?.tableSchemaErrors[tableName]?.[command];
149
139
 
150
140
  if (errorInfo) throw errorInfo.error;
151
141
 
152
- const table_rule = await this.getTableRules({ tableName, localParams }, clientInfo);
153
- if (!table_rule)
142
+ const tableRule = await this.getTableRules({ tableName, clientReq }, clientInfo);
143
+ if (!tableRule)
154
144
  throw {
155
145
  stack: ["getValidatedRequestRule()"],
156
146
  message: "Invalid or disallowed table: " + tableName,
157
147
  };
158
148
 
159
149
  if (command === "upsert") {
160
- if (!table_rule.update || !table_rule.insert) {
150
+ if (!tableRule.update || !tableRule.insert) {
161
151
  throw {
162
152
  stack: ["getValidatedRequestRule()"],
163
153
  message: `Invalid or disallowed command: upsert`,
@@ -165,8 +155,8 @@ export class PublishParser {
165
155
  }
166
156
  }
167
157
 
168
- if (table_rule[rtm.rule]) {
169
- return table_rule;
158
+ if (tableRule[rtm.rule]) {
159
+ return tableRule;
170
160
  } else
171
161
  throw {
172
162
  stack: ["getValidatedRequestRule()"],
@@ -176,14 +166,14 @@ export class PublishParser {
176
166
 
177
167
  async getTableRules(
178
168
  args: DboTable,
179
- clientInfo?: AuthResult
169
+ clientInfo: AuthResult | undefined
180
170
  ): Promise<ParsedPublishTable | undefined> {
181
171
  if (this.dbo[args.tableName]?.is_media) {
182
172
  const fileTablePublishRules = await this.getTableRulesWithoutFileTable(args, clientInfo);
183
173
  const { rules } = await getFileTableRules.bind(this)(
184
174
  args.tableName,
185
175
  fileTablePublishRules,
186
- args.localParams,
176
+ args.clientReq,
187
177
  clientInfo
188
178
  );
189
179
  return rules;
@@ -1,6 +1,5 @@
1
1
  import { AnyObject, FullFilter, isDefined } from "prostgles-types";
2
- import { AuthResult } from "../Auth/AuthTypes";
3
- import { LocalParams } from "../DboBuilder/DboBuilder";
2
+ import { AuthClientRequest, AuthResult } from "../Auth/AuthTypes";
4
3
  import { parseFieldFilter } from "../DboBuilder/ViewHandler/parseFieldFilter";
5
4
  import { PublishParser } from "./PublishParser";
6
5
  import { ParsedPublishTable, UpdateRule } from "./publishTypesAndUtils";
@@ -17,8 +16,8 @@ export async function getFileTableRules(
17
16
  this: PublishParser,
18
17
  fileTableName: string,
19
18
  fileTablePublishRules: ParsedPublishTable | undefined,
20
- localParams: LocalParams,
21
- clientInfo: AuthResult | undefined,
19
+ clientReq: AuthClientRequest | undefined,
20
+ clientInfo: AuthResult | undefined
22
21
  ) {
23
22
  const forcedDeleteFilters: FullFilter<AnyObject, void>[] = [];
24
23
  const forcedSelectFilters: FullFilter<AnyObject, void>[] = [];
@@ -28,7 +27,7 @@ export async function getFileTableRules(
28
27
  ?.filter((t) => !t.is_view && t.name !== fileTableName)
29
28
  .map((t) => {
30
29
  const refCols = t.columns.filter((c) =>
31
- c.references?.some((r) => r.ftable === fileTableName),
30
+ c.references?.some((r) => r.ftable === fileTableName)
32
31
  );
33
32
  if (!refCols.length) return undefined;
34
33
  return {
@@ -39,65 +38,46 @@ export async function getFileTableRules(
39
38
  })
40
39
  .filter(isDefined);
41
40
  if (referencedColumns?.length) {
42
- for await (const {
43
- tableName,
44
- fileColumns,
45
- allColumns,
46
- } of referencedColumns) {
47
- const table_rules = await this.getTableRules(
48
- { localParams, tableName },
49
- clientInfo,
50
- );
51
- if (table_rules) {
41
+ for await (const { tableName, fileColumns, allColumns } of referencedColumns) {
42
+ const tableRules = await this.getTableRules({ clientReq, tableName }, clientInfo);
43
+ if (tableRules) {
52
44
  fileColumns.map((column) => {
53
45
  const path = [{ table: tableName, on: [{ id: column }] }];
54
- if (table_rules.delete) {
46
+ if (tableRules.delete) {
55
47
  forcedDeleteFilters.push({
56
48
  $existsJoined: {
57
49
  path,
58
- filter: table_rules.delete.forcedFilter ?? {},
50
+ filter: tableRules.delete.forcedFilter ?? {},
59
51
  },
60
52
  });
61
53
  }
62
- if (table_rules.select) {
63
- const parsedFields = parseFieldFilter(
64
- table_rules.select.fields,
65
- false,
66
- allColumns,
67
- );
54
+ if (tableRules.select) {
55
+ const parsedFields = parseFieldFilter(tableRules.select.fields, false, allColumns);
68
56
  /** Must be allowed to view this column */
69
57
  if (parsedFields.includes(column as any)) {
70
58
  forcedSelectFilters.push({
71
59
  $existsJoined: {
72
60
  path,
73
- filter: table_rules.select.forcedFilter ?? {},
61
+ filter: tableRules.select.forcedFilter ?? {},
74
62
  },
75
63
  });
76
64
  }
77
65
  }
78
- if (table_rules.insert) {
79
- const parsedFields = parseFieldFilter(
80
- table_rules.insert.fields,
81
- false,
82
- allColumns,
83
- );
66
+ if (tableRules.insert) {
67
+ const parsedFields = parseFieldFilter(tableRules.insert.fields, false, allColumns);
84
68
  /** Must be allowed to view this column */
85
69
  if (parsedFields.includes(column as any)) {
86
70
  allowedNestedInserts.push({ table: tableName, column });
87
71
  }
88
72
  }
89
- if (table_rules.update) {
90
- const parsedFields = parseFieldFilter(
91
- table_rules.update.fields,
92
- false,
93
- allColumns,
94
- );
73
+ if (tableRules.update) {
74
+ const parsedFields = parseFieldFilter(tableRules.update.fields, false, allColumns);
95
75
  /** Must be allowed to view this column */
96
76
  if (parsedFields.includes(column as any)) {
97
77
  forcedUpdateFilters.push({
98
78
  $existsJoined: {
99
79
  path,
100
- filter: table_rules.update.forcedFilter ?? {},
80
+ filter: tableRules.update.forcedFilter ?? {},
101
81
  },
102
82
  });
103
83
  }
@@ -113,15 +93,13 @@ export async function getFileTableRules(
113
93
 
114
94
  const getForcedFilter = (
115
95
  rule: Pick<UpdateRule, "forcedFilter"> | undefined,
116
- forcedFilters: FullFilter<AnyObject, void>[],
96
+ forcedFilters: FullFilter<AnyObject, void>[]
117
97
  ) => {
118
- return rule && !rule.forcedFilter
119
- ? {}
98
+ return rule && !rule.forcedFilter ?
99
+ {}
120
100
  : {
121
101
  forcedFilter: {
122
- $or: forcedFilters.concat(
123
- rule?.forcedFilter ? [rule.forcedFilter] : [],
124
- ),
102
+ $or: forcedFilters.concat(rule?.forcedFilter ? [rule.forcedFilter] : []),
125
103
  },
126
104
  };
127
105
  };
@@ -151,17 +129,15 @@ export async function getFileTableRules(
151
129
  fileTableRule.insert = {
152
130
  fields: "*",
153
131
  ...fileTablePublishRules?.insert,
154
- allowedNestedInserts: fileTablePublishRules?.insert
155
- ? undefined
156
- : allowedNestedInserts,
132
+ allowedNestedInserts: fileTablePublishRules?.insert ? undefined : allowedNestedInserts,
157
133
  };
158
134
  }
159
135
 
160
136
  /** Add missing implied methods (getColumns, getInfo) */
161
137
  const rules = await this.getTableRulesWithoutFileTable.bind(this)(
162
- { localParams, tableName: fileTableName },
138
+ { clientReq, tableName: fileTableName },
163
139
  clientInfo,
164
- { [fileTableName]: fileTableRule },
140
+ { [fileTableName]: fileTableRule }
165
141
  );
166
142
  return { rules, allowedInserts: allowedNestedInserts };
167
143
  }
@@ -1,27 +1,18 @@
1
1
  import {
2
2
  DBSchemaTable,
3
+ getKeys,
3
4
  MethodKey,
5
+ pickKeys,
4
6
  TableInfo,
5
7
  TableSchemaErrors,
6
8
  TableSchemaForClient,
7
- getKeys,
8
- pickKeys,
9
9
  } from "prostgles-types";
10
- import { AuthResult, ExpressReq } from "../Auth/AuthTypes";
11
- import { getErrorAsObject, PRGLIOSocket } from "../DboBuilder/DboBuilder";
12
- import { PublishObject, PublishParser } from "./PublishParser";
10
+ import { AuthClientRequest, AuthResult } from "../Auth/AuthTypes";
11
+ import { getErrorAsObject } from "../DboBuilder/DboBuilder";
13
12
  import { TABLE_METHODS } from "../Prostgles";
13
+ import { PublishObject, PublishParser } from "./PublishParser";
14
14
 
15
- type Args = (
16
- | {
17
- socket: PRGLIOSocket;
18
- httpReq?: undefined;
19
- }
20
- | {
21
- httpReq: ExpressReq;
22
- socket?: undefined;
23
- }
24
- ) & {
15
+ type Args = AuthClientRequest & {
25
16
  userData: AuthResult | undefined;
26
17
  };
27
18
 
@@ -39,7 +30,8 @@ export async function getSchemaFromPublish(
39
30
 
40
31
  try {
41
32
  /* Publish tables and views based on socket */
42
- const clientInfo = userData ?? (await this.prostgles.authHandler?.getClientInfo(clientReq));
33
+ const clientInfo =
34
+ userData ?? (await this.prostgles.authHandler?.getUserFromRequest(clientReq));
43
35
 
44
36
  let _publish: PublishObject | undefined;
45
37
  try {
@@ -81,10 +73,7 @@ export async function getSchemaFromPublish(
81
73
  throw errMsg;
82
74
  }
83
75
 
84
- const table_rules = await this.getTableRules(
85
- { localParams: clientReq, tableName },
86
- clientInfo
87
- );
76
+ const table_rules = await this.getTableRules({ clientReq, tableName }, clientInfo);
88
77
 
89
78
  if (table_rules && Object.keys(table_rules).length) {
90
79
  schema[tableName] = {};
@@ -117,13 +106,13 @@ export async function getSchemaFromPublish(
117
106
  : {};
118
107
 
119
108
  /* Test for issues with the common table CRUD methods () */
120
- if (TABLE_METHODS.includes(method as any)) {
109
+ if (TABLE_METHODS.some((tm) => tm === method)) {
121
110
  try {
122
111
  const valid_table_command_rules = await this.getValidatedRequestRule(
123
112
  {
124
113
  tableName,
125
114
  command: method,
126
- localParams: clientReq,
115
+ clientReq,
127
116
  },
128
117
  clientInfo
129
118
  );
@@ -156,7 +145,7 @@ export async function getSchemaFromPublish(
156
145
 
157
146
  if (method === "getInfo" || method === "getColumns") {
158
147
  const tableRules = await this.getValidatedRequestRule(
159
- { tableName, command: method, localParams: clientReq },
148
+ { tableName, command: method, clientReq },
160
149
  clientInfo
161
150
  );
162
151
  const res = await (this.dbo[tableName] as any)[method](