prostgles-server 4.2.158 → 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 (145) hide show
  1. package/dist/Auth/AuthTypes.d.ts +4 -8
  2. package/dist/Auth/AuthTypes.d.ts.map +1 -1
  3. package/dist/Auth/setAuthProviders.d.ts.map +1 -1
  4. package/dist/Auth/setAuthProviders.js +4 -5
  5. package/dist/Auth/setAuthProviders.js.map +1 -1
  6. package/dist/Auth/setEmailProvider.js +1 -1
  7. package/dist/Auth/setEmailProvider.js.map +1 -1
  8. package/package.json +1 -1
  9. package/lib/Auth/AuthHandler.ts +0 -436
  10. package/lib/Auth/AuthTypes.ts +0 -285
  11. package/lib/Auth/getSafeReturnURL.ts +0 -35
  12. package/lib/Auth/sendEmail.ts +0 -83
  13. package/lib/Auth/setAuthProviders.ts +0 -129
  14. package/lib/Auth/setEmailProvider.ts +0 -85
  15. package/lib/Auth/setupAuthRoutes.ts +0 -161
  16. package/lib/DBEventsManager.ts +0 -178
  17. package/lib/DBSchemaBuilder.ts +0 -225
  18. package/lib/DboBuilder/DboBuilder.ts +0 -319
  19. package/lib/DboBuilder/DboBuilderTypes.ts +0 -361
  20. package/lib/DboBuilder/QueryBuilder/Functions.ts +0 -1153
  21. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +0 -288
  22. package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +0 -263
  23. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +0 -271
  24. package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +0 -136
  25. package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +0 -22
  26. package/lib/DboBuilder/QueryStreamer.ts +0 -250
  27. package/lib/DboBuilder/TableHandler/DataValidator.ts +0 -428
  28. package/lib/DboBuilder/TableHandler/TableHandler.ts +0 -205
  29. package/lib/DboBuilder/TableHandler/delete.ts +0 -115
  30. package/lib/DboBuilder/TableHandler/insert.ts +0 -183
  31. package/lib/DboBuilder/TableHandler/insertTest.ts +0 -78
  32. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +0 -62
  33. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +0 -134
  34. package/lib/DboBuilder/TableHandler/update.ts +0 -126
  35. package/lib/DboBuilder/TableHandler/updateBatch.ts +0 -49
  36. package/lib/DboBuilder/TableHandler/updateFile.ts +0 -48
  37. package/lib/DboBuilder/TableHandler/upsert.ts +0 -34
  38. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +0 -393
  39. package/lib/DboBuilder/ViewHandler/count.ts +0 -38
  40. package/lib/DboBuilder/ViewHandler/find.ts +0 -153
  41. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +0 -73
  42. package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +0 -74
  43. package/lib/DboBuilder/ViewHandler/getInfo.ts +0 -32
  44. package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +0 -84
  45. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +0 -96
  46. package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +0 -105
  47. package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +0 -208
  48. package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +0 -163
  49. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +0 -90
  50. package/lib/DboBuilder/ViewHandler/size.ts +0 -37
  51. package/lib/DboBuilder/ViewHandler/subscribe.ts +0 -118
  52. package/lib/DboBuilder/ViewHandler/validateViewRules.ts +0 -70
  53. package/lib/DboBuilder/dboBuilderUtils.ts +0 -222
  54. package/lib/DboBuilder/getColumns.ts +0 -114
  55. package/lib/DboBuilder/getCondition.ts +0 -201
  56. package/lib/DboBuilder/getSubscribeRelatedTables.ts +0 -190
  57. package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +0 -426
  58. package/lib/DboBuilder/insertNestedRecords.ts +0 -355
  59. package/lib/DboBuilder/parseUpdateRules.ts +0 -187
  60. package/lib/DboBuilder/prepareShortestJoinPaths.ts +0 -186
  61. package/lib/DboBuilder/runSQL.ts +0 -182
  62. package/lib/DboBuilder/runTransaction.ts +0 -50
  63. package/lib/DboBuilder/sqlErrCodeToMsg.ts +0 -254
  64. package/lib/DboBuilder/uploadFile.ts +0 -69
  65. package/lib/Event_Trigger_Tags.ts +0 -118
  66. package/lib/FileManager/FileManager.ts +0 -358
  67. package/lib/FileManager/getValidatedFileType.ts +0 -69
  68. package/lib/FileManager/initFileManager.ts +0 -187
  69. package/lib/FileManager/upload.ts +0 -62
  70. package/lib/FileManager/uploadStream.ts +0 -79
  71. package/lib/Filtering.ts +0 -463
  72. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +0 -502
  73. package/lib/JSONBValidation/validation.ts +0 -143
  74. package/lib/Logging.ts +0 -127
  75. package/lib/PostgresNotifListenManager.ts +0 -143
  76. package/lib/Prostgles.ts +0 -485
  77. package/lib/ProstglesTypes.ts +0 -196
  78. package/lib/PubSubManager/PubSubManager.ts +0 -609
  79. package/lib/PubSubManager/addSub.ts +0 -138
  80. package/lib/PubSubManager/addSync.ts +0 -141
  81. package/lib/PubSubManager/getCreatePubSubManagerError.ts +0 -72
  82. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +0 -662
  83. package/lib/PubSubManager/initPubSubManager.ts +0 -79
  84. package/lib/PubSubManager/notifListener.ts +0 -173
  85. package/lib/PubSubManager/orphanTriggerCheck.ts +0 -70
  86. package/lib/PubSubManager/pushSubData.ts +0 -55
  87. package/lib/PublishParser/PublishParser.ts +0 -162
  88. package/lib/PublishParser/getFileTableRules.ts +0 -124
  89. package/lib/PublishParser/getSchemaFromPublish.ts +0 -141
  90. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +0 -177
  91. package/lib/PublishParser/publishTypesAndUtils.ts +0 -399
  92. package/lib/RestApi.ts +0 -127
  93. package/lib/SchemaWatch/SchemaWatch.ts +0 -90
  94. package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +0 -3
  95. package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +0 -45
  96. package/lib/SchemaWatch/getWatchSchemaTagList.ts +0 -27
  97. package/lib/SyncReplication.ts +0 -557
  98. package/lib/TableConfig/TableConfig.ts +0 -468
  99. package/lib/TableConfig/getColumnDefinitionQuery.ts +0 -111
  100. package/lib/TableConfig/getConstraintDefinitionQueries.ts +0 -95
  101. package/lib/TableConfig/getFutureTableSchema.ts +0 -64
  102. package/lib/TableConfig/getPGIndexes.ts +0 -53
  103. package/lib/TableConfig/getTableColumnQueries.ts +0 -129
  104. package/lib/TableConfig/initTableConfig.ts +0 -326
  105. package/lib/index.ts +0 -13
  106. package/lib/initProstgles.ts +0 -319
  107. package/lib/onSocketConnected.ts +0 -102
  108. package/lib/runClientRequest.ts +0 -129
  109. package/lib/shortestPath.ts +0 -122
  110. package/lib/typeTests/DBoGenerated.d.ts +0 -320
  111. package/lib/typeTests/dboTypeCheck.ts +0 -81
  112. package/lib/utils.ts +0 -15
  113. package/tests/client/hooks.spec.ts +0 -205
  114. package/tests/client/index.ts +0 -139
  115. package/tests/client/package-lock.json +0 -637
  116. package/tests/client/package.json +0 -26
  117. package/tests/client/renderReactHook.ts +0 -177
  118. package/tests/client/tsconfig.json +0 -15
  119. package/tests/client/useProstgles.spec.ts +0 -120
  120. package/tests/clientFileTests.spec.ts +0 -102
  121. package/tests/clientOnlyQueries.spec.ts +0 -667
  122. package/tests/clientRestApi.spec.ts +0 -82
  123. package/tests/config_test/DBoGenerated.d.ts +0 -407
  124. package/tests/config_test/index.html +0 -109
  125. package/tests/config_test/index.js +0 -86
  126. package/tests/config_test/index.js.map +0 -1
  127. package/tests/config_test/index.ts +0 -91
  128. package/tests/config_test/init.sql +0 -48
  129. package/tests/config_test/package.json +0 -29
  130. package/tests/config_test/tsconfig.json +0 -23
  131. package/tests/config_testDBoGenerated.d.ts +0 -407
  132. package/tests/isomorphicQueries.spec.ts +0 -1493
  133. package/tests/server/DBoGenerated.d.ts +0 -537
  134. package/tests/server/index.html +0 -73
  135. package/tests/server/index.ts +0 -289
  136. package/tests/server/init.sql +0 -224
  137. package/tests/server/package-lock.json +0 -2164
  138. package/tests/server/package.json +0 -25
  139. package/tests/server/publishTypeCheck.ts +0 -136
  140. package/tests/server/server.ts +0 -35
  141. package/tests/server/testPublish.ts +0 -147
  142. package/tests/server/testTableConfig.ts +0 -156
  143. package/tests/server/tsconfig.json +0 -22
  144. package/tests/serverOnlyQueries.spec.ts +0 -32
  145. package/tests/test.sh +0 -20
@@ -1,141 +0,0 @@
1
- import { DBSchemaTable, MethodKey, TableInfo, TableSchemaForClient, getKeys, pickKeys } from "prostgles-types";
2
- import { AuthResult, ExpressReq } from "../Auth/AuthTypes";
3
- import { getErrorAsObject, PRGLIOSocket } from "../DboBuilder/DboBuilder";
4
- import { PublishObject, PublishParser } from "./PublishParser"
5
- import { TABLE_METHODS } from "../Prostgles";
6
-
7
- type Args = ({
8
- socket: PRGLIOSocket;
9
- httpReq?: undefined;
10
- } | {
11
- httpReq: ExpressReq;
12
- socket?: undefined;
13
- }) & {
14
- userData: AuthResult | undefined;
15
- }
16
- export async function getSchemaFromPublish(this: PublishParser, { userData, ...clientReq }: Args): Promise<{ schema: TableSchemaForClient; tables: DBSchemaTable[] }> {
17
- const schema: TableSchemaForClient = {};
18
- let tables: DBSchemaTable[] = []
19
-
20
- try {
21
- /* Publish tables and views based on socket */
22
- const clientInfo = userData ?? await this.prostgles.authHandler?.getClientInfo(clientReq);
23
-
24
- let _publish: PublishObject | undefined;
25
- try {
26
- _publish = await this.getPublish(clientReq, clientInfo);
27
- } catch(err){
28
- console.error("Error within then Publish function ", err)
29
- throw err;
30
- }
31
-
32
-
33
- if (_publish && Object.keys(_publish).length) {
34
- let txKey = "tx";
35
- if (!this.prostgles.opts.transactions) txKey = "";
36
- if (typeof this.prostgles.opts.transactions === "string") txKey = this.prostgles.opts.transactions;
37
-
38
- const tableNames = Object.keys(_publish).filter(k => !txKey || txKey !== k);
39
-
40
- const fileTableName = this.prostgles.fileManager?.tableName;
41
- if(fileTableName && this.dbo[fileTableName]?.is_media && !tableNames.includes(fileTableName)){
42
- const isReferenced = this.prostgles.dboBuilder.tablesOrViews?.some(t => t.columns.some(c => c.references?.some(r => r.ftable === fileTableName)))
43
- if(isReferenced){
44
- tableNames.unshift(fileTableName);
45
- }
46
- }
47
- await Promise.all(tableNames
48
- .map(async tableName => {
49
- if (!this.dbo[tableName]) {
50
- const errMsg = [
51
- `Table ${tableName} does not exist`,
52
- `Expecting one of: ${JSON.stringify(this.prostgles.dboBuilder.tablesOrViews?.map(tov => tov.name))}`,
53
- `DBO tables: ${JSON.stringify(Object.keys(this.dbo).filter(k => (this.dbo[k] as any).find))}`,
54
- ].join("\n");
55
- throw errMsg;
56
- }
57
-
58
- const table_rules = await this.getTableRules({ localParams: clientReq, tableName }, clientInfo);
59
-
60
- if (table_rules && Object.keys(table_rules).length) {
61
- schema[tableName] = {};
62
- const tableSchema = schema[tableName]!;
63
- let methods: MethodKey[] = [];
64
- let tableInfo: TableInfo | undefined;
65
- let tableColumns: DBSchemaTable["columns"] | undefined;
66
-
67
- if (typeof table_rules === "object") {
68
- methods = getKeys(table_rules) as any;
69
- }
70
-
71
- if(!this.prostgles.dboBuilder.canSubscribe){
72
- methods = methods.filter(m => !["subscribe", "subscribeOne", "sync", "unsubscribe", "unsync"].includes(m));
73
- }
74
-
75
- await Promise.all(methods.filter(m => m !== "select" as any)
76
- .map(async method => {
77
- if (method === "sync" && table_rules[method]) {
78
-
79
- /* Pass sync info */
80
- tableSchema[method] = table_rules[method];
81
- } else if ((table_rules as any)[method]) {
82
-
83
- tableSchema[method] = method === "insert"? pickKeys(table_rules.insert!, ["allowedNestedInserts"]) : {};
84
-
85
- /* Test for issues with the common table CRUD methods () */
86
- if (TABLE_METHODS.includes(method as any)) {
87
-
88
- try {
89
- const valid_table_command_rules = await this.getValidatedRequestRule({ tableName, command: method, localParams: clientReq }, clientInfo);
90
- if(this.prostgles.opts.testRulesOnConnect){
91
- await (this.dbo[tableName] as any)[method]({}, {}, {}, valid_table_command_rules, { ...clientReq, isRemoteRequest: true, testRule: true });
92
- }
93
-
94
- } catch (e) {
95
- tableSchema[method] = {
96
- err: "Internal publish error. Check server logs"
97
- };
98
-
99
- throw {
100
- ...getErrorAsObject(e),
101
- publish_path: `publish.${tableName}.${method}: \n -> ${e}`
102
- };
103
- }
104
- }
105
-
106
- if (method === "getInfo" || method === "getColumns") {
107
- const tableRules = await this.getValidatedRequestRule({ tableName, command: method, localParams: clientReq }, clientInfo);
108
- const res = await (this.dbo[tableName] as any)[method](undefined, undefined, undefined, tableRules, { ...clientReq, isRemoteRequest: true });
109
- if (method === "getInfo") {
110
- tableInfo = res;
111
- } else if (method === "getColumns") {
112
- tableColumns = res;
113
- }
114
- }
115
- }
116
- }));
117
-
118
- if (tableInfo && tableColumns) {
119
-
120
- tables.push({
121
- name: tableName,
122
- info: tableInfo,
123
- columns: tableColumns
124
- })
125
- }
126
- }
127
-
128
- return true;
129
- })
130
- );
131
- }
132
-
133
-
134
- } catch (e) {
135
- console.error("Prostgles \nERRORS IN PUBLISH: ", JSON.stringify(e));
136
- throw e;
137
- }
138
-
139
- tables = tables.sort((a, b) => a.name.localeCompare(b.name));
140
- return { schema, tables };
141
- }
@@ -1,177 +0,0 @@
1
- import { getKeys, isObject } from "prostgles-types";
2
- import { AuthResult } from "../Auth/AuthTypes";
3
- import { TableHandler } from "../DboBuilder/TableHandler/TableHandler";
4
- import { ViewHandler } from "../DboBuilder/ViewHandler/ViewHandler";
5
- import { DEFAULT_SYNC_BATCH_SIZE } from "../PubSubManager/PubSubManager";
6
- import { PublishParser } from "./PublishParser";
7
- import {
8
- DboTable, ParsedPublishTable, PublishObject, PublishTableRule,
9
- PublishViewRule, RULE_TO_METHODS, SubscribeRule
10
- } from "./publishTypesAndUtils";
11
-
12
- export async function getTableRulesWithoutFileTable(this: PublishParser, { tableName, localParams }: DboTable, clientInfo?: AuthResult, overridenPublish?: PublishObject): Promise<ParsedPublishTable | undefined> {
13
-
14
- if (!localParams || !tableName) throw { stack: ["getTableRules()"], message: "publish OR socket OR dbo OR tableName are missing" };
15
-
16
- const _publish = overridenPublish ?? await this.getPublish(localParams, clientInfo);
17
-
18
- const raw_table_rules = _publish[tableName];
19
- if (!raw_table_rules || isObject(raw_table_rules) && Object.values(raw_table_rules).every(v => !v)) {
20
- return undefined;
21
- }
22
-
23
- let parsed_table: ParsedPublishTable = {};
24
-
25
- /* Get view or table specific rules */
26
- const tHandler = (this.dbo[tableName] as TableHandler | ViewHandler);
27
- const is_view = tHandler.is_view;
28
- /**
29
- * Allow subscribing to a view if it has primary key columns from other tables
30
- */
31
- const canSubscribe = (!is_view || tHandler.columns.some(c => c.references));
32
- if (!tHandler) {
33
- throw { stack: ["getTableRules()"], message: `${tableName} could not be found in dbo` };
34
- }
35
-
36
- const MY_RULES = RULE_TO_METHODS.filter(r => {
37
-
38
- /** Check PG User privileges */
39
- const pgUserIsAllowedThis = tHandler.tableOrViewInfo.privileges[r.sqlRule];
40
- let result = (!is_view || !r.table_only) && pgUserIsAllowedThis;
41
-
42
- if (!pgUserIsAllowedThis && isObject(raw_table_rules) && (raw_table_rules as PublishTableRule)[r.sqlRule]) {
43
- throw `Your postgres user is not allowed ${r.sqlRule} on table ${tableName}`;
44
- }
45
-
46
- // TODO: Implement comprehensive canSubscribe check
47
- // if ((r.rule === "subscribe" || r.rule === "sync") && !this.prostgles.isSuperUser) {
48
- // result = false;
49
- // if (isObject(raw_table_rules) && (raw_table_rules as PublishTableRule)[r.rule]) {
50
- // throw `Cannot publish realtime rule ${tableName}.${r.rule}. Superuser is required for this`
51
- // }
52
- // }
53
-
54
- if(r.rule === "subscribe" && !canSubscribe){
55
- result = false;
56
- }
57
-
58
- return result;
59
- });
60
-
61
-
62
-
63
- /* All methods allowed. Add no limits for table rules */
64
- if ([true, "*"].includes(raw_table_rules as any)) {
65
- parsed_table = {};
66
- MY_RULES.filter(r => r.no_limits).forEach(r => {
67
- parsed_table[r.rule] = { ...r.no_limits as object } as any;
68
- });
69
-
70
- /** Specific rules allowed */
71
- } else if (isObject(raw_table_rules) && getKeys(raw_table_rules).length) {
72
- const allRuleKeys: (keyof PublishViewRule | keyof PublishTableRule)[] = getKeys(raw_table_rules);
73
- const dissallowedRuleKeys = allRuleKeys.filter(m => !(raw_table_rules as PublishTableRule)[m])
74
-
75
- MY_RULES.map(r => {
76
- /** Unless specifically disabled these are allowed */
77
- if (["getInfo", "getColumns"].includes(r.rule) && !dissallowedRuleKeys.includes(r.rule as any)) {
78
- parsed_table[r.rule] = r.no_limits as any;
79
- return;
80
- }
81
-
82
- /** Add no_limit values for implied/ fully allowed methods */
83
- if ([true, "*"].includes((raw_table_rules as PublishTableRule)[r.rule] as any) && r.no_limits) {
84
- parsed_table[r.rule] = Object.assign({}, r.no_limits) as any;
85
-
86
- /** Carry over detailed config */
87
- } else if (isObject((raw_table_rules as any)[r.rule])) {
88
- parsed_table[r.rule] = (raw_table_rules as any)[r.rule]
89
- }
90
- });
91
-
92
- allRuleKeys.filter(m => parsed_table[m])
93
- .forEach((method) => {
94
- const rule = parsed_table[method];
95
-
96
- const rm = MY_RULES.find(r => r.rule === method || (r.methods as readonly string[]).includes(method));
97
- if (!rm) {
98
- let extraInfo = "";
99
- if (is_view && RULE_TO_METHODS.find(r => !is_view && r.rule === method || (r.methods as any).includes(method))) {
100
- extraInfo = "You've specified table rules to a view\n";
101
- }
102
- throw `Invalid rule in publish.${tableName} -> ${method} \n${extraInfo}Expecting any of: ${MY_RULES.flatMap(r => [r.rule, ...r.methods]).join(", ")}`;
103
- }
104
-
105
- /* Check RULES for invalid params */
106
- /* Methods do not have params -> They use them from rules */
107
- if (method === rm.rule && isObject(rule)) {
108
- const method_params = Object.keys(rule);
109
- const allowed_params = Object.keys(rm?.allowed_params)
110
- const iparam = method_params.find(p => !allowed_params.includes(p));
111
- if (iparam) {
112
- throw `Invalid setting in publish.${tableName}.${method} -> ${iparam}. \n Expecting any of: ${allowed_params.join(", ")}`;
113
- }
114
- }
115
-
116
- /* Add default params (if missing) */
117
- if (method === "sync") {
118
-
119
- if ([true, "*"].includes(parsed_table[method] as any)) {
120
- throw "Invalid sync rule. Expecting { id_fields: string[], synced_field: string } ";
121
- }
122
-
123
- if (typeof parsed_table[method]?.throttle !== "number") {
124
- parsed_table[method]!.throttle = 100;
125
- }
126
- if (typeof parsed_table[method]?.batch_size !== "number") {
127
- parsed_table[method]!.batch_size = DEFAULT_SYNC_BATCH_SIZE;
128
- }
129
- }
130
-
131
- /* Enable subscribe if not explicitly disabled OR if VIEW with referenced tables */
132
- const subKey = "subscribe" as const;
133
-
134
- if (method === "select" && !dissallowedRuleKeys.includes(subKey)) {
135
- const sr = MY_RULES.find(r => r.rule === subKey);
136
- if (sr && canSubscribe) {
137
- parsed_table[subKey] = { ...sr.no_limits as SubscribeRule };
138
- parsed_table.subscribeOne = { ...sr.no_limits as SubscribeRule };
139
- }
140
- }
141
- });
142
-
143
- } else {
144
- throw "Unexpected publish"
145
- }
146
-
147
- const getImpliedMethods = (tableRules: ParsedPublishTable): ParsedPublishTable => {
148
- const res = { ...tableRules };
149
-
150
- /* Add implied methods if not specifically dissallowed */
151
- MY_RULES.map(r => {
152
-
153
- /** THIS IS A MESS -> some methods cannot be dissallowed (unsync, unsubscribe...) */
154
- r.methods.forEach(method => {
155
- const isAllowed = tableRules[r.rule] && (tableRules as any)[method] === undefined;
156
- if (isAllowed) {
157
-
158
- if (method === "updateBatch" && (!tableRules.update || tableRules.update.checkFilter || tableRules.update.postValidate)) {
159
- // not allowed
160
-
161
- } else if (method === "upsert" && (!tableRules.update || !tableRules.insert)) {
162
- // not allowed
163
-
164
- } else {
165
- (res as any)[method] ??= true;
166
- }
167
- }
168
- });
169
- });
170
-
171
- return res;
172
- }
173
-
174
- parsed_table = getImpliedMethods(parsed_table);
175
-
176
- return parsed_table;
177
- }