prostgles-server 2.0.178 → 2.0.179

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 (98) hide show
  1. package/dist/AuthHandler.d.ts +4 -4
  2. package/dist/AuthHandler.d.ts.map +1 -1
  3. package/dist/DBSchemaBuilder.d.ts +6 -6
  4. package/dist/DBSchemaBuilder.d.ts.map +1 -1
  5. package/dist/DBSchemaBuilder.js +25 -8
  6. package/dist/DBSchemaBuilder.js.map +1 -1
  7. package/dist/DboBuilder.d.ts +20 -21
  8. package/dist/DboBuilder.d.ts.map +1 -1
  9. package/dist/DboBuilder.js +1 -1
  10. package/dist/DboBuilder.js.map +1 -1
  11. package/dist/Prostgles.d.ts +8 -10
  12. package/dist/Prostgles.d.ts.map +1 -1
  13. package/dist/Prostgles.js.map +1 -1
  14. package/dist/PublishParser.d.ts +37 -37
  15. package/dist/PublishParser.d.ts.map +1 -1
  16. package/dist/PublishParser.js.map +1 -1
  17. package/dist/index.d.ts +2 -3
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js.map +1 -1
  20. package/lib/AuthHandler.d.ts +148 -0
  21. package/lib/AuthHandler.d.ts.map +1 -0
  22. package/lib/AuthHandler.js +411 -0
  23. package/lib/AuthHandler.ts +3 -3
  24. package/lib/DBEventsManager.d.ts +38 -0
  25. package/lib/DBEventsManager.d.ts.map +1 -0
  26. package/lib/DBEventsManager.js +136 -0
  27. package/lib/DBSchemaBuilder.d.ts +11 -0
  28. package/lib/DBSchemaBuilder.d.ts.map +1 -0
  29. package/lib/DBSchemaBuilder.js +102 -0
  30. package/lib/DBSchemaBuilder.ts +62 -27
  31. package/lib/DboBuilder.d.ts +428 -0
  32. package/lib/DboBuilder.d.ts.map +1 -0
  33. package/lib/DboBuilder.js +3078 -0
  34. package/lib/DboBuilder.ts +25 -25
  35. package/lib/FileManager.d.ts +168 -0
  36. package/lib/FileManager.d.ts.map +1 -0
  37. package/lib/FileManager.js +474 -0
  38. package/lib/Filtering.d.ts +15 -0
  39. package/lib/Filtering.d.ts.map +1 -0
  40. package/lib/Filtering.js +299 -0
  41. package/lib/PostgresNotifListenManager.d.ts +27 -0
  42. package/lib/PostgresNotifListenManager.d.ts.map +1 -0
  43. package/lib/PostgresNotifListenManager.js +122 -0
  44. package/lib/Prostgles.d.ts +193 -0
  45. package/lib/Prostgles.d.ts.map +1 -0
  46. package/lib/Prostgles.js +579 -0
  47. package/lib/Prostgles.ts +6 -6
  48. package/lib/PubSubManager.d.ts +157 -0
  49. package/lib/PubSubManager.d.ts.map +1 -0
  50. package/lib/PubSubManager.js +1400 -0
  51. package/lib/PublishParser.d.ts +262 -0
  52. package/lib/PublishParser.d.ts.map +1 -0
  53. package/lib/PublishParser.js +390 -0
  54. package/lib/PublishParser.ts +39 -38
  55. package/lib/QueryBuilder.d.ts +124 -0
  56. package/lib/QueryBuilder.d.ts.map +1 -0
  57. package/lib/QueryBuilder.js +1349 -0
  58. package/lib/SyncReplication.d.ts +34 -0
  59. package/lib/SyncReplication.d.ts.map +1 -0
  60. package/lib/SyncReplication.js +411 -0
  61. package/lib/TableConfig.d.ts +175 -0
  62. package/lib/TableConfig.d.ts.map +1 -0
  63. package/lib/TableConfig.js +231 -0
  64. package/lib/index.d.ts +10 -0
  65. package/lib/index.d.ts.map +1 -0
  66. package/lib/index.js +45 -0
  67. package/lib/index.ts +3 -4
  68. package/lib/shortestPath.d.ts +10 -0
  69. package/lib/shortestPath.d.ts.map +1 -0
  70. package/lib/shortestPath.js +111 -0
  71. package/lib/utils.d.ts +2 -0
  72. package/lib/utils.d.ts.map +1 -0
  73. package/lib/utils.js +5 -0
  74. package/package.json +3 -3
  75. package/tests/client/PID.txt +1 -1
  76. package/tests/client/index.d.ts +1 -1
  77. package/tests/client/index.d.ts.map +1 -1
  78. package/tests/client_only_queries.d.ts +4 -0
  79. package/tests/client_only_queries.d.ts.map +1 -0
  80. package/tests/isomorphic_queries.d.ts +6 -0
  81. package/tests/isomorphic_queries.d.ts.map +1 -0
  82. package/tests/server/DBoGenerated.d.ts +97 -193
  83. package/tests/server/dboTypeCheck.d.ts +2 -0
  84. package/tests/server/dboTypeCheck.d.ts.map +1 -0
  85. package/tests/server/dboTypeCheck.js +14 -0
  86. package/tests/server/dboTypeCheck.ts +17 -0
  87. package/tests/server/index.d.ts +2 -0
  88. package/tests/server/index.d.ts.map +1 -0
  89. package/tests/server/index.js +11 -11
  90. package/tests/server/index.ts +23 -16
  91. package/tests/server/package-lock.json +5 -5
  92. package/tests/server/publishTypeCheck.d.ts +2 -0
  93. package/tests/server/publishTypeCheck.d.ts.map +1 -0
  94. package/tests/server/publishTypeCheck.js +120 -0
  95. package/tests/server/publishTypeCheck.ts +129 -0
  96. package/tests/server/tsconfig.json +4 -5
  97. package/tests/server_only_queries.d.ts +2 -0
  98. package/tests/server_only_queries.d.ts.map +1 -0
@@ -0,0 +1,262 @@
1
+ import { AnyObject, TableSchemaForClient, DBSchemaTable, FullFilter } from "prostgles-types";
2
+ import { ClientInfo } from "./AuthHandler";
3
+ import { CommonTableRules, LocalParams, PRGLIOSocket } from "./DboBuilder";
4
+ import { Prostgles, DBHandlerServer, DB } from "./Prostgles";
5
+ import type { DBOFullyTyped, PublishFullyTyped } from "./DBSchemaBuilder";
6
+ export declare type Method = (...args: any) => (any | Promise<any>);
7
+ export declare type PublishMethods<S = void> = (params: PublishParams<S>) => {
8
+ [key: string]: Method;
9
+ } | Promise<{
10
+ [key: string]: Method;
11
+ }>;
12
+ export declare type Awaitable<T> = T | Promise<T>;
13
+ declare type Request = {
14
+ socket?: any;
15
+ httpReq?: any;
16
+ };
17
+ declare type DboTable = Request & {
18
+ tableName: string;
19
+ localParams: LocalParams;
20
+ };
21
+ declare type DboTableCommand = Request & DboTable & {
22
+ command: string;
23
+ localParams: LocalParams;
24
+ };
25
+ import { FieldFilter, SelectParams } from "prostgles-types";
26
+ export declare type InsertRequestData = {
27
+ data: object | object[];
28
+ returning: FieldFilter;
29
+ };
30
+ export declare type SelectRequestData = {
31
+ filter: object;
32
+ params: SelectParams;
33
+ };
34
+ export declare type DeleteRequestData = {
35
+ filter: object;
36
+ returning: FieldFilter;
37
+ };
38
+ export declare type UpdateRequestDataOne<R> = {
39
+ filter: FullFilter<R>;
40
+ data: Partial<R>;
41
+ returning: FieldFilter<R>;
42
+ };
43
+ export declare type UpdateReq<R> = {
44
+ filter: FullFilter<R>;
45
+ data: Partial<R>;
46
+ };
47
+ export declare type UpdateRequestDataBatch<R> = {
48
+ data: UpdateReq<R>[];
49
+ };
50
+ export declare type UpdateRequestData<R extends AnyObject = AnyObject> = UpdateRequestDataOne<R> | UpdateRequestDataBatch<R>;
51
+ export declare type ValidateRow<R extends AnyObject = AnyObject> = (row: R) => R | Promise<R>;
52
+ export declare type ValidateUpdateRow<R extends AnyObject = AnyObject> = (args: {
53
+ update: Partial<R>;
54
+ filter: FullFilter<R>;
55
+ }) => R | Promise<R>;
56
+ export declare type SelectRule<Cols extends AnyObject = AnyObject> = {
57
+ /**
58
+ * Fields allowed to be selected. Tip: Use false to exclude field
59
+ */
60
+ fields: FieldFilter<Cols>;
61
+ /**
62
+ * The maximum number of rows a user can get in a select query. null by default. Unless a null or higher limit is specified 100 rows will be returned by the default
63
+ */
64
+ maxLimit?: number | null;
65
+ /**
66
+ * Filter added to every query (e.g. user_id) to restrict access
67
+ */
68
+ forcedFilter?: FullFilter<Cols>;
69
+ /**
70
+ * Fields user can filter by
71
+ * */
72
+ filterFields?: FieldFilter<Cols>;
73
+ /**
74
+ * Validation logic to check/update data for each request
75
+ */
76
+ validate?(args: SelectRequestData): SelectRequestData | Promise<SelectRequestData>;
77
+ };
78
+ export declare type InsertRule<Cols extends AnyObject = AnyObject> = {
79
+ /**
80
+ * Fields allowed to be inserted. Tip: Use false to exclude field
81
+ */
82
+ fields: SelectRule<Cols>["fields"];
83
+ /**
84
+ * Data to include/overwrite on each insert
85
+ */
86
+ forcedData?: Partial<Cols>;
87
+ /**
88
+ * Fields user can view after inserting
89
+ */
90
+ returningFields?: SelectRule<Cols>["fields"];
91
+ /**
92
+ * Validation logic to check/update data for each request. Happens before publish rule checks (for fields, forcedData/forcedFilter)
93
+ */
94
+ preValidate?: ValidateRow<Cols>;
95
+ /**
96
+ * Validation logic to check/update data for each request. Happens after publish rule checks (for fields, forcedData/forcedFilter)
97
+ */
98
+ validate?: InsertRule<Cols>["preValidate"];
99
+ };
100
+ export declare type UpdateRule<Cols extends AnyObject = AnyObject> = {
101
+ /**
102
+ * Fields allowed to be updated. Tip: Use false/0 to exclude field
103
+ */
104
+ fields: SelectRule<Cols>["fields"];
105
+ /**
106
+ * Row level FGAC
107
+ * Used when the editable fields change based on the updated row
108
+ * If specified then the fields from the first matching filter table.count({ ...filter, ...updateFilter }) > 0 will be used
109
+ * If none matching then the "fields" will be used
110
+ * Specify in decreasing order of specificity otherwise a more general filter will match first
111
+ */
112
+ dynamicFields?: {
113
+ filter: SelectRule<Cols>["forcedFilter"];
114
+ fields: SelectRule<Cols>["fields"];
115
+ }[];
116
+ /**
117
+ * Filter added to every query (e.g. user_id) to restrict access
118
+ * This filter cannot be updated
119
+ */
120
+ forcedFilter?: SelectRule<Cols>["forcedFilter"];
121
+ /**
122
+ * Data to include/overwrite on each updatDBe
123
+ */
124
+ forcedData?: InsertRule<Cols>["forcedData"];
125
+ /**
126
+ * Fields user can use to find the updates
127
+ */
128
+ filterFields?: SelectRule<Cols>["fields"];
129
+ /**
130
+ * Fields user can view after updating
131
+ */
132
+ returningFields?: SelectRule<Cols>["fields"];
133
+ /**
134
+ * Validation logic to check/update data for each request
135
+ */
136
+ validate?: ValidateUpdateRow<Cols>;
137
+ };
138
+ export declare type DeleteRule<Cols extends AnyObject = AnyObject> = {
139
+ /**
140
+ * Filter added to every query (e.g. user_id) to restrict access
141
+ */
142
+ forcedFilter?: SelectRule<Cols>["forcedFilter"];
143
+ /**
144
+ * Fields user can filter by
145
+ */
146
+ filterFields?: SelectRule<Cols>["filterFields"];
147
+ /**
148
+ * Fields user can view after deleting
149
+ */
150
+ returningFields?: SelectRule<Cols>["filterFields"];
151
+ /**
152
+ * Validation logic to check/update data for each request
153
+ */
154
+ validate?(...args: any[]): UpdateRequestData<Cols>;
155
+ };
156
+ export declare type SyncRule<Cols extends AnyObject = AnyObject> = {
157
+ /**
158
+ * Primary keys used in updating data
159
+ */
160
+ id_fields: (keyof Cols)[];
161
+ /**
162
+ * Numerical incrementing fieldname (last updated timestamp) used to sync items
163
+ */
164
+ synced_field: keyof Cols;
165
+ /**
166
+ * EXPERIMENTAL. Disabled by default. If true then server will attempt to delete any records missing from client.
167
+ */
168
+ allow_delete?: boolean;
169
+ /**
170
+ * Throttle replication transmission in milliseconds. Defaults to 100
171
+ */
172
+ throttle?: number;
173
+ /**
174
+ * Number of rows to send per trip. Defaults to 50
175
+ */
176
+ batch_size?: number;
177
+ };
178
+ export declare type SubscribeRule = {
179
+ throttle?: number;
180
+ };
181
+ export declare type ViewRule<S = AnyObject> = CommonTableRules & {
182
+ /**
183
+ * What can be read from the table
184
+ */
185
+ select?: SelectRule<S>;
186
+ };
187
+ export declare type TableRule<S = AnyObject> = ViewRule<S> & {
188
+ insert?: InsertRule<S>;
189
+ update?: UpdateRule<S>;
190
+ delete?: DeleteRule<S>;
191
+ sync?: SyncRule<S>;
192
+ subscribe?: SubscribeRule;
193
+ };
194
+ export declare type PublishViewRule<S extends AnyObject = AnyObject> = {
195
+ select?: SelectRule<S> | PublishAllOrNothing;
196
+ getColumns?: PublishAllOrNothing;
197
+ getInfo?: PublishAllOrNothing;
198
+ };
199
+ export declare type PublishTableRule<S extends AnyObject = AnyObject> = PublishViewRule<S> & {
200
+ insert?: InsertRule<S> | PublishAllOrNothing;
201
+ update?: UpdateRule<S> | PublishAllOrNothing;
202
+ delete?: DeleteRule<S> | PublishAllOrNothing;
203
+ sync?: SyncRule<S>;
204
+ subscribe?: SubscribeRule | PublishAllOrNothing;
205
+ };
206
+ export declare type ParsedPublishTable = {
207
+ select?: SelectRule;
208
+ getColumns?: true;
209
+ getInfo?: true;
210
+ insert?: InsertRule;
211
+ update?: UpdateRule;
212
+ delete?: DeleteRule;
213
+ sync?: SyncRule;
214
+ subscribe?: SubscribeRule;
215
+ subscribeOne?: SubscribeRule;
216
+ };
217
+ export declare type PublishParams<S = void> = {
218
+ sid?: string;
219
+ dbo: DBOFullyTyped<S>;
220
+ db?: DB;
221
+ user?: AnyObject;
222
+ socket: PRGLIOSocket;
223
+ };
224
+ export declare type RequestParams = {
225
+ dbo?: DBHandlerServer;
226
+ socket?: any;
227
+ };
228
+ export declare type PublishAllOrNothing = true | "*" | false | null;
229
+ declare type PublishObject = {
230
+ [table_name: string]: (PublishTableRule | PublishViewRule | PublishAllOrNothing);
231
+ };
232
+ export declare type ParsedPublishTables = {
233
+ [table_name: string]: ParsedPublishTable;
234
+ };
235
+ export declare type PublishedResult<Schema = void> = PublishAllOrNothing | PublishFullyTyped<Schema>;
236
+ export declare type Publish<Schema = void> = PublishedResult<Schema> | ((params: PublishParams<Schema>) => Awaitable<PublishedResult<Schema>>);
237
+ export declare class PublishParser {
238
+ publish: any;
239
+ publishMethods?: any;
240
+ publishRawSQL?: any;
241
+ dbo: DBHandlerServer;
242
+ db: DB;
243
+ prostgles: Prostgles;
244
+ constructor(publish: any, publishMethods: any, publishRawSQL: any, dbo: DBHandlerServer, db: DB, prostgles: Prostgles);
245
+ getPublishParams(localParams: LocalParams, clientInfo?: ClientInfo): Promise<PublishParams<any>>;
246
+ getMethods(socket: any): Promise<{}>;
247
+ /**
248
+ * Parses the first level of publish. (If false then nothing if * then all tables and views)
249
+ * @param socket
250
+ * @param user
251
+ */
252
+ getPublish(localParams: LocalParams, clientInfo?: ClientInfo): Promise<PublishObject>;
253
+ getValidatedRequestRuleWusr({ tableName, command, localParams }: DboTableCommand): Promise<TableRule>;
254
+ getValidatedRequestRule({ tableName, command, localParams }: DboTableCommand, clientInfo?: ClientInfo): Promise<TableRule>;
255
+ getTableRules({ tableName, localParams }: DboTable, clientInfo?: ClientInfo): Promise<ParsedPublishTable | undefined>;
256
+ getSchemaFromPublish(socket: any): Promise<{
257
+ schema: TableSchemaForClient;
258
+ tables: DBSchemaTable[];
259
+ }>;
260
+ }
261
+ export {};
262
+ //# sourceMappingURL=PublishParser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PublishParser.d.ts","sourceRoot":"","sources":["PublishParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,SAAS,EAAO,oBAAoB,EAAE,aAAa,EAAwB,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC/I,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAyB,WAAW,EAAE,YAAY,EAA6B,MAAM,cAAc,CAAC;AAC7H,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,EAAE,EAAiB,MAAM,aAAa,CAAC;AAC5E,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC1E,oBAAY,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,CAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAE,CAAC;AAC9D,oBAAY,cAAc,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,KAAK;IAAE,CAAC,GAAG,EAAC,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC;IAAE,CAAC,GAAG,EAAC,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAAC;AAElI,oBAAY,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE1C,aAAK,OAAO,GAAG;IACb,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,OAAO,CAAC,EAAE,GAAG,CAAC;CACf,CAAA;AAED,aAAK,QAAQ,GAAG,OAAO,GAAG;IACxB,SAAS,EAAE,MAAM,CAAC;IA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AAG,OAAO,CAAC,kBAAkB,GAAG,SAAS,CAAC;IAqJrH,oBAAoB,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC;QAAC,MAAM,EAAE,oBAAoB,CAAC;QAAC,MAAM,EAAE,aAAa,EAAE,CAAA;KAAE,CAAC;CAoG3G"}
@@ -0,0 +1,390 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PublishParser = void 0;
4
+ const prostgles_types_1 = require("prostgles-types");
5
+ const DboBuilder_1 = require("./DboBuilder");
6
+ const Prostgles_1 = require("./Prostgles");
7
+ const RULE_TO_METHODS = [
8
+ {
9
+ rule: "getColumns",
10
+ sqlRule: "select",
11
+ methods: prostgles_types_1.RULE_METHODS.getColumns,
12
+ no_limits: true,
13
+ allowed_params: [],
14
+ table_only: false,
15
+ hint: ` expecting false | true | undefined`
16
+ },
17
+ {
18
+ rule: "getInfo",
19
+ sqlRule: "select",
20
+ methods: prostgles_types_1.RULE_METHODS.getInfo,
21
+ no_limits: true,
22
+ allowed_params: [],
23
+ table_only: false,
24
+ hint: ` expecting false | true | undefined`
25
+ },
26
+ {
27
+ rule: "insert",
28
+ sqlRule: "insert",
29
+ methods: prostgles_types_1.RULE_METHODS.insert,
30
+ no_limits: { fields: "*" },
31
+ table_only: true,
32
+ allowed_params: ["fields", "forcedData", "returningFields", "validate", "preValidate"],
33
+ hint: ` expecting "*" | true | { fields: string | string[] | {} }`
34
+ },
35
+ {
36
+ rule: "update",
37
+ sqlRule: "update",
38
+ methods: prostgles_types_1.RULE_METHODS.update,
39
+ no_limits: { fields: "*", filterFields: "*", returningFields: "*" },
40
+ table_only: true,
41
+ allowed_params: ["fields", "filterFields", "forcedFilter", "forcedData", "returningFields", "validate", "dynamicFields"],
42
+ hint: ` expecting "*" | true | { fields: string | string[] | {} }`
43
+ },
44
+ {
45
+ rule: "select",
46
+ sqlRule: "select",
47
+ methods: prostgles_types_1.RULE_METHODS.select,
48
+ no_limits: { fields: "*", filterFields: "*" },
49
+ table_only: false,
50
+ allowed_params: ["fields", "filterFields", "forcedFilter", "validate", "maxLimit"],
51
+ hint: ` expecting "*" | true | { fields: ( string | string[] | {} ) }`
52
+ },
53
+ {
54
+ rule: "delete",
55
+ sqlRule: "delete",
56
+ methods: prostgles_types_1.RULE_METHODS.delete,
57
+ no_limits: { filterFields: "*" },
58
+ table_only: true,
59
+ allowed_params: ["filterFields", "forcedFilter", "returningFields", "validate"],
60
+ hint: ` expecting "*" | true | { filterFields: ( string | string[] | {} ) } \n Will use "select", "update", "delete" and "insert" rules`
61
+ },
62
+ {
63
+ rule: "sync",
64
+ sqlRule: "select",
65
+ methods: prostgles_types_1.RULE_METHODS.sync,
66
+ no_limits: null,
67
+ table_only: true,
68
+ allowed_params: ["id_fields", "synced_field", "sync_type", "allow_delete", "throttle", "batch_size"],
69
+ hint: ` expecting "*" | true | { id_fields: string[], synced_field: string }`
70
+ },
71
+ {
72
+ rule: "subscribe",
73
+ sqlRule: "select",
74
+ methods: prostgles_types_1.RULE_METHODS.subscribe,
75
+ no_limits: { throttle: 0 },
76
+ table_only: true,
77
+ allowed_params: ["throttle"],
78
+ hint: ` expecting "*" | true | { throttle: number } \n Will use "select" rules`
79
+ }
80
+ ];
81
+ const PubSubManager_1 = require("./PubSubManager");
82
+ class PublishParser {
83
+ constructor(publish, publishMethods, publishRawSQL, dbo, db, prostgles) {
84
+ this.publish = publish;
85
+ this.publishMethods = publishMethods;
86
+ this.publishRawSQL = publishRawSQL;
87
+ this.dbo = dbo;
88
+ this.db = db;
89
+ this.prostgles = prostgles;
90
+ if (!this.dbo || !this.publish)
91
+ throw "INTERNAL ERROR: dbo and/or publish missing";
92
+ }
93
+ async getPublishParams(localParams, clientInfo) {
94
+ if (!this.dbo)
95
+ throw "dbo missing";
96
+ return {
97
+ ...(clientInfo || await this.prostgles.authHandler?.getClientInfo(localParams)),
98
+ dbo: this.dbo,
99
+ db: this.db,
100
+ socket: localParams.socket
101
+ };
102
+ }
103
+ async getMethods(socket) {
104
+ let methods = {};
105
+ const publishParams = await this.getPublishParams({ socket });
106
+ const _methods = await applyParamsIfFunc(this.publishMethods, publishParams);
107
+ if (_methods && Object.keys(_methods).length) {
108
+ (0, prostgles_types_1.getKeys)(_methods).map(key => {
109
+ if (_methods[key] && (typeof _methods[key] === "function" || typeof _methods[key].then === "function")) {
110
+ //@ts-ignore
111
+ methods[key] = _methods[key];
112
+ }
113
+ else {
114
+ throw `invalid publishMethods item -> ${key} \n Expecting a function or promise`;
115
+ }
116
+ });
117
+ }
118
+ return methods;
119
+ }
120
+ /**
121
+ * Parses the first level of publish. (If false then nothing if * then all tables and views)
122
+ * @param socket
123
+ * @param user
124
+ */
125
+ async getPublish(localParams, clientInfo) {
126
+ const publishParams = await this.getPublishParams(localParams, clientInfo);
127
+ let _publish = await applyParamsIfFunc(this.publish, publishParams);
128
+ if (_publish === "*") {
129
+ let publish = {};
130
+ this.prostgles.dboBuilder.tablesOrViews?.map(tov => {
131
+ publish[tov.name] = "*";
132
+ });
133
+ return publish;
134
+ }
135
+ return _publish;
136
+ }
137
+ async getValidatedRequestRuleWusr({ tableName, command, localParams }) {
138
+ const clientInfo = await this.prostgles.authHandler.getClientInfo(localParams);
139
+ return await this.getValidatedRequestRule({ tableName, command, localParams }, clientInfo);
140
+ }
141
+ async getValidatedRequestRule({ tableName, command, localParams }, clientInfo) {
142
+ if (!this.dbo)
143
+ throw "INTERNAL ERROR: dbo is missing";
144
+ if (!command || !tableName)
145
+ throw "command OR tableName are missing";
146
+ let rtm = RULE_TO_METHODS.find(rtms => rtms.methods.includes(command));
147
+ if (!rtm) {
148
+ throw "Invalid command: " + command;
149
+ }
150
+ /* Must be local request -> allow everything */
151
+ if (!localParams || (!localParams.socket && !localParams.httpReq)) {
152
+ return RULE_TO_METHODS.reduce((a, v) => ({
153
+ ...a,
154
+ [v.rule]: v.no_limits
155
+ }), {});
156
+ }
157
+ /* Must be from socket. Must have a publish */
158
+ if (!this.publish)
159
+ throw "publish is missing";
160
+ /* Get any publish errors for socket */
161
+ const schm = localParams?.socket?.prostgles?.schema?.[tableName]?.[command];
162
+ if (schm && schm.err)
163
+ throw schm.err;
164
+ let table_rule = await this.getTableRules({ tableName, localParams }, clientInfo);
165
+ if (!table_rule)
166
+ throw "Invalid or disallowed table: " + tableName;
167
+ if (command === "upsert") {
168
+ if (!table_rule.update || !table_rule.insert) {
169
+ throw `Invalid or disallowed command: upsert`;
170
+ }
171
+ }
172
+ if (rtm && table_rule && table_rule[rtm.rule]) {
173
+ return table_rule;
174
+ }
175
+ else
176
+ throw `Invalid or disallowed command: ${tableName}.${command}`;
177
+ }
178
+ async getTableRules({ tableName, localParams }, clientInfo) {
179
+ try {
180
+ if (!localParams || !tableName)
181
+ throw "publish OR socket OR dbo OR tableName are missing";
182
+ let _publish = await this.getPublish(localParams, clientInfo);
183
+ const raw_table_rules = _publish[tableName]; // applyParamsIfFunc(_publish[tableName], localParams, this.dbo, this.db, user);
184
+ if (!raw_table_rules)
185
+ return undefined;
186
+ let parsed_table = {};
187
+ /* Get view or table specific rules */
188
+ const tHandler = this.dbo[tableName];
189
+ if (!tHandler)
190
+ throw `${tableName} could not be found in dbo`;
191
+ const is_view = tHandler.is_view;
192
+ const MY_RULES = RULE_TO_METHODS.filter(r => {
193
+ /** Check PG User privileges */
194
+ const pgUserIsAllowedThis = tHandler.tableOrViewInfo.privileges[r.sqlRule];
195
+ const result = (!is_view || !r.table_only) && pgUserIsAllowedThis;
196
+ if (!pgUserIsAllowedThis && (0, DboBuilder_1.isPlainObject)(raw_table_rules) && raw_table_rules[r.sqlRule]) {
197
+ throw `Your postgres user is not allowed ${r.sqlRule} on table ${tableName}`;
198
+ }
199
+ return result;
200
+ });
201
+ /* All methods allowed. Add no limits for table rules */
202
+ if ([true, "*"].includes(raw_table_rules)) {
203
+ parsed_table = {};
204
+ MY_RULES.map(r => {
205
+ parsed_table[r.rule] = { ...r.no_limits };
206
+ });
207
+ /** Specific rules allowed */
208
+ }
209
+ else if ((0, DboBuilder_1.isPlainObject)(raw_table_rules) && (0, prostgles_types_1.getKeys)(raw_table_rules).length) {
210
+ const allRuleKeys = (0, prostgles_types_1.getKeys)(raw_table_rules);
211
+ const dissallowedRuleKeys = allRuleKeys.filter(m => !raw_table_rules[m]);
212
+ MY_RULES.map(r => {
213
+ /** Unless specifically disabled these are allowed */
214
+ if (["getInfo", "getColumns"].includes(r.rule) && !dissallowedRuleKeys.includes(r.rule)) {
215
+ parsed_table[r.rule] = r.no_limits;
216
+ return;
217
+ }
218
+ /** Add no_limit values for implied/ fully allowed methods */
219
+ if ([true, "*"].includes(raw_table_rules[r.rule]) && r.no_limits) {
220
+ parsed_table[r.rule] = Object.assign({}, r.no_limits);
221
+ /** Carry over detailed config */
222
+ }
223
+ else if ((0, DboBuilder_1.isPlainObject)(raw_table_rules[r.rule])) {
224
+ parsed_table[r.rule] = raw_table_rules[r.rule];
225
+ }
226
+ });
227
+ allRuleKeys.filter(m => parsed_table[m])
228
+ .find((method) => {
229
+ let rm = MY_RULES.find(r => r.rule === method || r.methods.includes(method));
230
+ if (!rm) {
231
+ let extraInfo = "";
232
+ if (is_view && RULE_TO_METHODS.find(r => !is_view && r.rule === method || r.methods.includes(method))) {
233
+ extraInfo = "You've specified table rules to a view\n";
234
+ }
235
+ throw `Invalid rule in publish.${tableName} -> ${method} \n${extraInfo}Expecting any of: ${MY_RULES.flatMap(r => [r.rule, ...r.methods]).join(", ")}`;
236
+ }
237
+ /* Check RULES for invalid params */
238
+ /* Methods do not have params -> They use them from rules */
239
+ if (method === rm.rule) {
240
+ let method_params = (0, prostgles_types_1.getKeys)(parsed_table[method]);
241
+ let iparam = method_params.find(p => !rm?.allowed_params.includes(p));
242
+ if (iparam) {
243
+ throw `Invalid setting in publish.${tableName}.${method} -> ${iparam}. \n Expecting any of: ${rm.allowed_params.join(", ")}`;
244
+ }
245
+ }
246
+ /* Add default params (if missing) */
247
+ if (method === "sync") {
248
+ if ([true, "*"].includes(parsed_table[method])) {
249
+ throw "Invalid sync rule. Expecting { id_fields: string[], synced_field: string } ";
250
+ }
251
+ if (typeof parsed_table[method]?.throttle !== "number") {
252
+ parsed_table[method].throttle = 100;
253
+ }
254
+ if (typeof parsed_table[method]?.batch_size !== "number") {
255
+ parsed_table[method].batch_size = PubSubManager_1.DEFAULT_SYNC_BATCH_SIZE;
256
+ }
257
+ }
258
+ /* Enable subscribe if not explicitly disabled */
259
+ const subKey = "subscribe";
260
+ if (method === "select" && !dissallowedRuleKeys.includes(subKey)) {
261
+ const sr = MY_RULES.find(r => r.rule === subKey);
262
+ if (sr) {
263
+ parsed_table[subKey] = { ...sr.no_limits };
264
+ parsed_table.subscribeOne = { ...sr.no_limits };
265
+ }
266
+ }
267
+ });
268
+ }
269
+ else {
270
+ throw "Unexpected publish";
271
+ }
272
+ const getImpliedMethods = (tableRules) => {
273
+ let res = { ...tableRules };
274
+ /* Add implied methods if not specifically dissallowed */
275
+ MY_RULES.map(r => {
276
+ /** THIS IS A MESS -> some methods cannot be dissallowed (unsync, unsubscribe...) */
277
+ r.methods.forEach(method => {
278
+ var _a;
279
+ const isAllowed = tableRules[r.rule] && tableRules[method] === undefined;
280
+ if (isAllowed) {
281
+ if (method === "updateBatch" && !tableRules.update) {
282
+ }
283
+ else if (method === "upsert" && (!tableRules.update || !tableRules.insert)) {
284
+ }
285
+ else {
286
+ (_a = res)[method] ?? (_a[method] = true);
287
+ }
288
+ }
289
+ });
290
+ });
291
+ return res;
292
+ };
293
+ parsed_table = getImpliedMethods(parsed_table);
294
+ return parsed_table;
295
+ }
296
+ catch (e) {
297
+ throw e;
298
+ }
299
+ }
300
+ /* Prepares schema for client. Only allowed views and commands will be present */
301
+ async getSchemaFromPublish(socket) {
302
+ let schema = {};
303
+ let tables = [];
304
+ try {
305
+ /* Publish tables and views based on socket */
306
+ const clientInfo = await this.prostgles.authHandler?.getClientInfo({ socket });
307
+ let _publish = await this.getPublish(socket, clientInfo);
308
+ if (_publish && Object.keys(_publish).length) {
309
+ let txKey = "tx";
310
+ if (!this.prostgles.opts.transactions)
311
+ txKey = "";
312
+ if (typeof this.prostgles.opts.transactions === "string")
313
+ txKey = this.prostgles.opts.transactions;
314
+ const tableNames = Object.keys(_publish).filter(k => !txKey || txKey !== k);
315
+ await Promise.all(tableNames
316
+ .map(async (tableName) => {
317
+ if (!this.dbo[tableName]) {
318
+ throw `Table ${tableName} does not exist
319
+ Expecting one of: ${this.prostgles.dboBuilder.tablesOrViews?.map(tov => tov.name).join(", ")}
320
+ DBO tables: ${Object.keys(this.dbo).filter(k => this.dbo[k].find).join(", ")}
321
+ `;
322
+ }
323
+ const table_rules = await this.getTableRules({ localParams: { socket }, tableName }, clientInfo);
324
+ if (table_rules && Object.keys(table_rules).length) {
325
+ schema[tableName] = {};
326
+ let methods = [];
327
+ let tableInfo;
328
+ let tableColumns;
329
+ if (typeof table_rules === "object") {
330
+ methods = (0, prostgles_types_1.getKeys)(table_rules);
331
+ }
332
+ await Promise.all(methods.filter(m => m !== "select").map(async (method) => {
333
+ if (method === "sync" && table_rules[method]) {
334
+ /* Pass sync info */
335
+ schema[tableName][method] = table_rules[method];
336
+ }
337
+ else if (table_rules[method]) {
338
+ schema[tableName][method] = {};
339
+ /* Test for issues with the common table CRUD methods () */
340
+ if (Prostgles_1.TABLE_METHODS.includes(method)) {
341
+ let err = null;
342
+ try {
343
+ let valid_table_command_rules = await this.getValidatedRequestRule({ tableName, command: method, localParams: { socket } }, clientInfo);
344
+ await this.dbo[tableName][method]({}, {}, {}, valid_table_command_rules, { socket, has_rules: true, testRule: true });
345
+ }
346
+ catch (e) {
347
+ err = "INTERNAL PUBLISH ERROR";
348
+ schema[tableName][method] = { err };
349
+ throw `publish.${tableName}.${method}: \n -> ${e}`;
350
+ }
351
+ }
352
+ if (method === "getInfo" || method === "getColumns") {
353
+ let tableRules = await this.getValidatedRequestRule({ tableName, command: method, localParams: { socket } }, clientInfo);
354
+ const res = await this.dbo[tableName][method](undefined, undefined, undefined, tableRules, { socket, has_rules: true });
355
+ if (method === "getInfo") {
356
+ tableInfo = res;
357
+ }
358
+ else if (method === "getColumns") {
359
+ tableColumns = res;
360
+ }
361
+ }
362
+ }
363
+ }));
364
+ if (tableInfo && tableColumns) {
365
+ tables.push({
366
+ name: tableName,
367
+ info: tableInfo,
368
+ columns: tableColumns
369
+ });
370
+ }
371
+ }
372
+ return true;
373
+ }));
374
+ }
375
+ }
376
+ catch (e) {
377
+ console.error("Prostgles \nERRORS IN PUBLISH: ", JSON.stringify(e));
378
+ throw e;
379
+ }
380
+ return { schema, tables };
381
+ }
382
+ }
383
+ exports.PublishParser = PublishParser;
384
+ function applyParamsIfFunc(maybeFunc, ...params) {
385
+ if ((maybeFunc !== null && maybeFunc !== undefined) &&
386
+ (typeof maybeFunc === "function" || typeof maybeFunc.then === "function")) {
387
+ return maybeFunc(...params);
388
+ }
389
+ return maybeFunc;
390
+ }