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,208 +0,0 @@
1
- import { JoinPath, RawJoinPath, reverseJoinOn } from "prostgles-types";
2
- import { ViewHandler } from "./ViewHandler";
3
-
4
- type parseJoinPathArgs = {
5
- rawPath: RawJoinPath;
6
- rootTable: string;
7
- viewHandler: ViewHandler;
8
- allowMultiOrJoin?: boolean;
9
- addShortestJoinIfMissing?: boolean;
10
- }
11
- export type ParsedJoinPath = { table: string; on: Record<string, string>[] };
12
-
13
- /**
14
- * Return a valid join path
15
- */
16
- export const parseJoinPath = ({ rawPath, rootTable, viewHandler, allowMultiOrJoin = false, addShortestJoinIfMissing }: parseJoinPathArgs): ParsedJoinPath[] => {
17
- const result: ParsedJoinPath[] = [];
18
- let cleanPath = typeof rawPath === "string"? [{ table: rawPath }] : rawPath;
19
- if(addShortestJoinIfMissing && cleanPath[0] !== "**"){
20
- cleanPath = ["**", ...cleanPath]
21
- }
22
- cleanPath.forEach((item, i) => {
23
- const prevTable = result.at(-1)?.table ?? rootTable;
24
- if(!prevTable) throw `prevTable missing`;
25
-
26
- const pushJoinPath = (targetPath: JoinPath) => {
27
- const getShortestJoin = i === 1 && cleanPath[0] === "**";
28
- const joinInfo = getJoins(viewHandler, prevTable, [targetPath], { allowMultiOrJoin, getShortestJoin });
29
-
30
- joinInfo.paths.forEach(path => {
31
-
32
- /** Check if join tables are valid */
33
- if (!viewHandler.dboBuilder.dbo[path.table]) {
34
- throw { stack: ["prepareExistCondition()"], message: `Invalid or dissallowed table in join path: ${path.table}` };
35
- }
36
- result.push({
37
- table: path.table,
38
- on: path.on.map(constraint => Object.fromEntries(constraint))
39
- });
40
- })
41
- }
42
-
43
- /** Shortest join */
44
- if(item === "**"){
45
-
46
- } else if(typeof item === "string"){
47
- const table = item;
48
- pushJoinPath({ table })
49
- } else {
50
- pushJoinPath(item);
51
- }
52
- });
53
-
54
- const missingPath = result.find(r => !r.on.length || r.on.some(v => !Object.keys(v).length));
55
- if(missingPath){
56
- throw `Missing join on condition for: ${missingPath.table}`
57
- }
58
-
59
- return result;
60
- }
61
-
62
- import { JoinInfo } from "../DboBuilder";
63
-
64
- type Opts = {
65
- allowMultiOrJoin?: boolean;
66
- getShortestJoin?: boolean;
67
- }
68
-
69
- /**
70
- * Returns all tables and fields required to join from source table to target table
71
- * Respecting the path.on condition
72
- */
73
- function getJoins(viewHandler: ViewHandler, source: string, path: JoinPath[], { allowMultiOrJoin = true, getShortestJoin }: Opts = {}): JoinInfo {
74
- const [lastItem] = path;
75
- if(!lastItem){
76
- throw `Empty path`;
77
- }
78
- if(getShortestJoin && path.length !== 1){
79
- throw `getShortestJoin requires exactly 1 path item`
80
- }
81
- const target = lastItem.table;
82
- if (!viewHandler.joinPaths) {
83
- throw `Join info missing`;
84
- }
85
-
86
- /* Self join */
87
- if (source === target) {
88
- const tableHandler = viewHandler.dboBuilder.tablesOrViews?.find(t => t.name === source);
89
- if (!tableHandler) throw `Table not found for joining ${source}`;
90
-
91
- const fcols = tableHandler.columns.filter(c => c.references?.some(({ ftable }) => ftable === viewHandler.name));
92
- if(!fcols.length){
93
- throw `There is no self-join foreign key relationship for table ${JSON.stringify(target)}`
94
- }
95
- let allOnJoins: [string, string][][] = [];
96
- fcols.forEach(fc => {
97
- fc.references!.forEach(({ fcols, cols }) => {
98
- const fieldArr = fcols.map((fcol, i) => [fcol, cols[i]!] as [string, string]);
99
- allOnJoins.push(fieldArr);
100
- })
101
- });
102
- allOnJoins = [
103
- ...allOnJoins,
104
- /** Reverse as well */
105
- ...allOnJoins.map(constraint => (constraint).map(([left, right]) => [right, left] as [string, string]))
106
- ]
107
- return {
108
- paths: [{
109
- source,
110
- target,
111
- table: target,
112
- on: getValidOn(lastItem.on, allOnJoins.map(v => Object.fromEntries(v)))
113
- }],
114
- expectOne: false
115
- }
116
- }
117
-
118
- /* Find the join path between tables */
119
- const tableConfigJoinInfo = viewHandler.dboBuilder?.prostgles?.tableConfigurator?.getJoinInfo(source, target);
120
- if (tableConfigJoinInfo) return tableConfigJoinInfo;
121
-
122
- const actualPath = getShortestJoin?
123
- viewHandler.joinPaths.find(j => {
124
- return j.t1 === source && j.t2 === target
125
- })?.path.map(table => ({ table, on: undefined })).slice(1) :
126
- viewHandler.joinPaths.find(j => {
127
- return j.path.join() === [{ table: source }, ...path].map(p => p.table).join()
128
- })? path : undefined;
129
-
130
- if(getShortestJoin && actualPath?.length && lastItem.on?.length){
131
- actualPath[actualPath.length-1]!.on = lastItem.on;
132
- }
133
-
134
- if (!actualPath) {
135
- throw `Joining ${source} <-...-> ${target} dissallowed or missing`;
136
- }
137
-
138
- /* Make the join chain info */
139
- const paths: JoinInfo["paths"] = [];
140
- actualPath.forEach((tablePath, i, arr) => {
141
- const prevTable = arr[i - 1]!;
142
- const t1 = i === 0 ? source : prevTable.table;
143
-
144
- viewHandler.joins ??= viewHandler.dboBuilder.joins;
145
-
146
- /* Get join options */
147
- const join = viewHandler.joins.find(j => j.tables.includes(t1) && j.tables.includes(tablePath.table));
148
- if (!join) {
149
- throw `Joining ${t1} <-> ${tablePath} dissallowed or missing`;
150
- }
151
- const isLtr = join.tables[0] === t1;
152
- const joinOn = isLtr? join.on : reverseJoinOn(join.on);
153
-
154
- const on = getValidOn(tablePath.on, joinOn);
155
- paths.push({
156
- source,
157
- target,
158
- table: tablePath.table,
159
- on
160
- });
161
- });
162
- const expectOne = false;
163
- // paths.map(({ source, target, on }, i) => {
164
- // if(expectOne && on.length === 1){
165
- // const sourceCol = on[0][1];
166
- // const targetCol = on[0][0];
167
-
168
- // const sCol = this.dboBuilder.dbo[source].columns.find(c => c.name === sourceCol)
169
- // const tCol = this.dboBuilder.dbo[target].columns.find(c => c.name === targetCol)
170
- // console.log({ sourceCol, targetCol, sCol, source, tCol, target, on})
171
- // expectOne = sCol.is_pkey && tCol.is_pkey
172
- // }
173
- // })
174
-
175
- const isMultiOrJoin = paths.find(p => p.on.length > 1);
176
- if(!allowMultiOrJoin && isMultiOrJoin){
177
- throw `Table ${JSON.stringify(source)} can join to ${JSON.stringify(target)} through multiple constraints. Must chose one of ${JSON.stringify(isMultiOrJoin.on)}`
178
- }
179
- return {
180
- paths,
181
- expectOne
182
- };
183
- }
184
-
185
- const getValidOn = (requested: JoinPath["on"], possible: ParsedJoinPath["on"]) => {
186
-
187
- if(!requested){
188
- return possible.map(v => Object.entries(v));
189
- }
190
- if(!requested.length){
191
- throw `Invalid requested "tablePath.on". Cannot be empty`
192
- }
193
- const isValid = requested.every(requestedConstraint => {
194
- return possible.some(possibleConstraint => conditionsMatch(possibleConstraint, requestedConstraint));
195
- });
196
-
197
- if(!isValid){
198
- throw `Invalid path specified for join: ${JSON.stringify(requested)}. Allowed paths: ${JSON.stringify(possible)}`
199
- }
200
-
201
- return requested.map(v => Object.entries(v));
202
- }
203
-
204
- const conditionsMatch = (c1: Record<string, string>, c2: Record<string, string>) => {
205
- const keys1 = Object.keys(c1);
206
- const keys2 = Object.keys(c2);
207
- return keys1.sort().join() === keys2.sort().join() && keys1.every(key => c1[key] === c2[key]);
208
- }
@@ -1,163 +0,0 @@
1
- import { OrderBy, asName, isDefined, isEmpty, isObject } from "prostgles-types/dist";
2
- import { SortItem } from "../DboBuilder";
3
- import { NewQueryJoin, SelectItemValidated, asNameAlias } from "../QueryBuilder/QueryBuilder";
4
-
5
- /* This relates only to SELECT */
6
- export const prepareSortItems = (
7
- rawOrderBy: OrderBy | undefined,
8
- allowed_cols: string[],
9
- tableAlias: string | undefined,
10
- select: SelectItemValidated[],
11
- joinQueries: NewQueryJoin[],
12
- ): SortItem[] => {
13
-
14
- if (!rawOrderBy) return [];
15
-
16
- let orderBy: { key: string, asc: boolean, nulls?: "first" | "last", nullEmpty?: boolean }[] = [];
17
- if (isObject(rawOrderBy)) {
18
- orderBy = parseOrderObj(rawOrderBy);
19
- } else if (typeof rawOrderBy === "string") {
20
- /* string */
21
- orderBy = [{ key: rawOrderBy, asc: true }];
22
- } else if (Array.isArray(rawOrderBy)) {
23
-
24
- /* Order by is formed of a list of ascending field names */
25
- const _orderBy = (rawOrderBy as any[]);
26
- if (_orderBy && !_orderBy.find(v => typeof v !== "string")) {
27
- /* [string] */
28
- orderBy = _orderBy.map(key => ({ key, asc: true }));
29
- } else if (_orderBy.find(v => isObject(v) && !isEmpty(v))) {
30
- orderBy = _orderBy.map(v => parseOrderObj(v, true)[0]!);
31
- } else return throwErr(rawOrderBy);
32
- } else return throwErr(rawOrderBy);
33
-
34
- if (!orderBy || !orderBy.length) return [];
35
-
36
- const validatedAggAliases = select.filter(s =>
37
- s.type !== "joinedColumn" &&
38
- (!s.fields.length || s.fields.every(f => allowed_cols.includes(f)))
39
- ).map(s => s.alias)
40
-
41
- const sortableNestedColumns = joinQueries.flatMap(jq => jq.select.map(selectItem => {
42
- const joinAlias = jq.tableAlias ?? jq.table;
43
- return {
44
- ...jq,
45
- selectItem,
46
- joinAlias,
47
- key: `${joinAlias}.${selectItem.alias}`
48
- }
49
- }));
50
- const bad_param = orderBy.find(({ key }) =>
51
- !sortableNestedColumns.some(v => v.key === key) &&
52
- !validatedAggAliases.includes(key) &&
53
- !allowed_cols.includes(key)
54
- );
55
- if (bad_param) {
56
- throw "Invalid/disallowed orderBy fields or params: " + bad_param.key;
57
- }
58
-
59
- const selectedAliases = select.filter(s => s.selected).map(s => s.alias);
60
-
61
- const result: SortItem[] = orderBy.map(({ key, asc, nulls, nullEmpty = false }) => {
62
-
63
- const nestedField = sortableNestedColumns.find(f => f.key === key);
64
- if (nestedField) {
65
- const { table, selectItem, joinAlias } = nestedField;
66
-
67
- const comparableDataTypeCast = ["uuid", "xml"].includes(selectItem.column_udt_type ?? "")? "::TEXT" : "";
68
- const sortItemAlias = asName(`prostgles_nested_sort_${selectItem.alias}`)
69
-
70
- return {
71
- key,
72
- type: "query",
73
- asc,
74
- nulls,
75
- nullEmpty,
76
- nested: {
77
- table,
78
- joinAlias,
79
- selectItemAlias: selectItem.alias,
80
- isNumeric: selectItem.tsDataType === "number",
81
- wrapperQuerySortItem: `${asc? "MIN" : "MAX"}(${asNameAlias(selectItem.alias, joinAlias)}${comparableDataTypeCast}) as ${sortItemAlias}`,
82
- },
83
- fieldQuery: `${asName(joinAlias)}.${sortItemAlias + (asc? "" : " DESC")} ${nulls? `NULLS ${nulls === "last"? "LAST" : "FIRST" }` : ""}`,
84
- }
85
- }
86
- /* Order by column index when possible to bypass name collision when ordering by a computed column.
87
- (Postgres will sort by existing columns wheundefined possible)
88
- */
89
-
90
- const index = selectedAliases.indexOf(key) + 1;
91
- let colKey = (index > 0 && !nullEmpty) ? index : [tableAlias, key].filter(isDefined).map(asName).join(".");
92
- if (nullEmpty) {
93
- colKey = `nullif(trim(${colKey}::text), '')`
94
- }
95
-
96
- if (typeof colKey === "number") {
97
- return {
98
- key,
99
- type: "position",
100
- asc,
101
- nulls,
102
- nullEmpty,
103
- fieldPosition: colKey
104
- }
105
- }
106
-
107
- return {
108
- key,
109
- type: "query",
110
- fieldQuery: colKey,
111
- nulls,
112
- nullEmpty,
113
- asc,
114
- }
115
- });
116
-
117
- return result;
118
- }
119
-
120
- const throwErr = (rawOrderBy: any) => {
121
- throw "\nInvalid orderBy option -> " + JSON.stringify(rawOrderBy) +
122
- "Expecting: \
123
- { key2: false, \"nested.key2\": false, key1: true } \
124
- { key1: 1, key2: -1 } \
125
- [{ key1: true }, { key2: false }] \
126
- [{ key: 'colName', asc: true, nulls: 'first', nullEmpty: true }]"
127
- };
128
-
129
- const parseOrderObj = (orderBy: any, expectOne = false): { key: string, asc: boolean, nulls?: "first" | "last", nullEmpty?: boolean }[] => {
130
- if (!isObject(orderBy)) return throwErr(orderBy);
131
-
132
- const keys = Object.keys(orderBy);
133
- if (keys.length && keys.find(k => ["key", "asc", "nulls", "nullEmpty"].includes(k))) {
134
- const { key, asc, nulls, nullEmpty = false } = orderBy;
135
- if (
136
- !["string"].includes(typeof key) ||
137
- !["boolean"].includes(typeof asc) ||
138
- !["first", "last", undefined, null].includes(nulls) ||
139
- !["boolean"].includes(typeof nullEmpty)
140
- ) {
141
- throw `Invalid orderBy option (${JSON.stringify(orderBy, null, 2)}) \n
142
- Expecting { key: string, asc?: boolean, nulls?: 'first' | 'last' | null | undefined, nullEmpty?: boolean } `
143
- }
144
- return [{ key, asc, nulls, nullEmpty }];
145
- }
146
-
147
- if (expectOne && keys.length > 1) {
148
- throw "\nInvalid orderBy " + JSON.stringify(orderBy) +
149
- "\nEach orderBy array element cannot have more than one key";
150
- }
151
- /* { key2: true, key1: false } */
152
- if (!Object.values(orderBy).find(v => ![true, false].includes(<any>v))) {
153
- return keys.map(key => ({ key, asc: Boolean(orderBy[key]) }))
154
-
155
- /* { key2: -1, key1: 1 } */
156
- } else if (!Object.values(orderBy).find(v => ![-1, 1].includes(<any>v))) {
157
- return keys.map(key => ({ key, asc: orderBy[key] === 1 }))
158
-
159
- /* { key2: "asc", key1: "desc" } */
160
- } else if (!Object.values(orderBy).find(v => !["asc", "desc"].includes(<any>v))) {
161
- return keys.map(key => ({ key, asc: orderBy[key] === "asc" }))
162
- } else return throwErr(orderBy);
163
- };
@@ -1,90 +0,0 @@
1
- import { AnyObject, FieldFilter, getKeys, isDefined, isObject } from "prostgles-types/dist";
2
- import { ViewHandler } from "./ViewHandler";
3
- import { ExistsFilterConfig, Filter, LocalParams } from "../DboBuilder";
4
- import { SelectItem } from "../QueryBuilder/QueryBuilder";
5
- import { TableRule } from "../../PublishParser/PublishParser";
6
- import { getCondition } from "../getCondition";
7
-
8
- export type PrepareWhereParams = {
9
- filter?: Filter;
10
- select: SelectItem[] | undefined;
11
- forcedFilter?: AnyObject;
12
- filterFields?: FieldFilter;
13
- addWhere?: boolean;
14
- tableAlias?: string,
15
- localParams: LocalParams | undefined,
16
- tableRule: TableRule | undefined,
17
- isHaving?: boolean;
18
- };
19
-
20
- export async function prepareWhere(this: ViewHandler, params: PrepareWhereParams): Promise<{ condition: string; where: string; filter: AnyObject; exists: ExistsFilterConfig[]; }> {
21
- const { filter, select, forcedFilter, filterFields: ff, addWhere: addKeywords = true, tableAlias, localParams, tableRule } = params;
22
- const { $and: $and_key, $or: $or_key } = this.dboBuilder.prostgles.keywords;
23
-
24
- let filterFields = ff;
25
- /* Local update allow all. TODO -> FIX THIS */
26
- if (!ff && !tableRule) filterFields = "*";
27
-
28
- const exists: ExistsFilterConfig[] = [];
29
-
30
- const parseFullFilter = async (f: any, parentFilter: any = null, isForcedFilterBypass: boolean): Promise<string> => {
31
- if (!f) throw "Invalid/missing group filter provided";
32
- if (!isObject(f)) throw "\nInvalid filter\nExpecting an object but got -> " + JSON.stringify(f);
33
- let result = "";
34
- const keys = getKeys(f);
35
- if (!keys.length) {
36
- return result;
37
- }
38
- if ((keys.includes($and_key) || keys.includes($or_key))) {
39
- if (keys.length > 1) throw `\ngroup filter must contain only one array property. e.g.: { ${$and_key}: [...] } OR { ${$or_key}: [...] } `;
40
- if (parentFilter && Object.keys(parentFilter).includes("")) throw "group filter ($and/$or) can only be placed at the root or within another group filter";
41
- }
42
-
43
- const { [$and_key]: $and, [$or_key]: $or } = f,
44
- group: AnyObject[] = $and || $or;
45
-
46
- if (group && group.length) {
47
- const operand = $and ? " AND " : " OR ";
48
- const conditions = (await Promise.all(
49
- group.map(async gf => await parseFullFilter(gf, group, isForcedFilterBypass))
50
- )).filter(c => c);
51
-
52
- if (conditions?.length) {
53
- if (conditions.length === 1) return conditions.join(operand);
54
- else return ` ( ${conditions.sort().join(operand)} ) `;
55
- }
56
- } else if (!group) {
57
-
58
- /** forcedFilters do not get checked against publish and are treated as server-side requests */
59
- const cond = await getCondition.bind(this)({
60
- filter: { ...f },
61
- select,
62
- allowed_colnames: isForcedFilterBypass ? this.column_names.slice(0) : this.parseFieldFilter(filterFields),
63
- tableAlias,
64
- localParams: isForcedFilterBypass ? undefined : localParams,
65
- tableRules: isForcedFilterBypass ? undefined : tableRule,
66
- isHaving: params.isHaving,
67
- });
68
- result = cond.condition;
69
- exists.push(...cond.exists);
70
- }
71
- return result;
72
- }
73
-
74
- /* A forced filter condition will not check if the existsJoined filter tables have been published */
75
- const forcedFilterCond = forcedFilter ? await parseFullFilter(forcedFilter, null, true) : undefined;
76
- const filterCond = await parseFullFilter(filter, null, false);
77
- let cond = [
78
- forcedFilterCond, filterCond
79
- ].filter(c => c).join(" AND ");
80
-
81
- const finalFilter = forcedFilter ? {
82
- [$and_key]: [forcedFilter, filter].filter(isDefined)
83
- } : { ...filter };
84
-
85
- const condition = cond;
86
- if (cond && addKeywords) {
87
- cond = `WHERE ${cond}`;
88
- }
89
- return { condition, where: cond || "", filter: finalFilter, exists };
90
- }
@@ -1,37 +0,0 @@
1
- import { SelectParams } from "prostgles-types";
2
- import { TableRule } from "../../PublishParser/publishTypesAndUtils";
3
- import { Filter, LocalParams } from "../DboBuilderTypes";
4
- import { getErrorAsObject, getSerializedClientErrorFromPGError, withUserRLS } from "../dboBuilderUtils";
5
- import { ViewHandler } from "./ViewHandler";
6
- export async function size(this: ViewHandler, _filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise<string> {
7
- const filter = _filter || {};
8
- const start = Date.now();
9
- try {
10
- const result = await this.find(filter, { ...selectParams, limit: 2 }, undefined, table_rules, localParams)
11
- .then(async _allowed => {
12
-
13
- const q: string = await this.find(
14
- filter, { ...selectParams, limit: selectParams?.limit ?? Number.MAX_SAFE_INTEGER },
15
- undefined,
16
- table_rules,
17
- { ...localParams, returnQuery: "noRLS", bypassLimit: true }
18
- ) as any;
19
- const query = withUserRLS(
20
- localParams,
21
- `${withUserRLS(localParams, "")}
22
- SELECT sum(pg_column_size((prgl_size_query.*))) as size
23
- FROM (
24
- ${q}
25
- ) prgl_size_query
26
- `
27
- );
28
-
29
- return (this.tx?.t || this.db).one(query).then(({ size }) => size || '0');
30
- });
31
- await this._log({ command: "size", localParams, data: { filter, selectParams }, duration: Date.now() - start });
32
- return result;
33
- } catch (e) {
34
- await this._log({ command: "size", localParams, data: { filter, selectParams }, duration: Date.now() - start, error: getErrorAsObject(e) });
35
- throw getSerializedClientErrorFromPGError(e, { type: "tableMethod", localParams, view: this });
36
- }
37
- }
@@ -1,118 +0,0 @@
1
- import { AnyObject, SubscribeParams, SubscriptionChannels } from "prostgles-types";
2
- import { TableRule } from "../../PublishParser/PublishParser";
3
- import { Filter, LocalParams, getClientErrorFromPGError, getErrorAsObject, getSerializedClientErrorFromPGError } from "../DboBuilder";
4
- import { getSubscribeRelatedTables } from "../getSubscribeRelatedTables";
5
- import { NewQuery } from "../QueryBuilder/QueryBuilder";
6
- import { ViewHandler } from "./ViewHandler";
7
-
8
- type OnData = (items: AnyObject[]) => any;
9
- export type LocalFuncs = {
10
- onData: OnData
11
- onError?: (error: any) => void;
12
- } | OnData;
13
-
14
- export const getOnDataFunc = (localFuncs: LocalFuncs | undefined): OnData | undefined => {
15
- return typeof localFuncs === "function"? localFuncs : localFuncs?.onData;
16
- }
17
- export const matchesLocalFuncs = (localFuncs1: LocalFuncs | undefined, localFuncs2: LocalFuncs | undefined) => {
18
- return localFuncs1 && localFuncs2 && getOnDataFunc(localFuncs1) === getOnDataFunc(localFuncs2);
19
- }
20
- export const parseLocalFuncs = (localFuncs1: LocalFuncs | undefined): Extract<LocalFuncs, { onData: OnData }> | undefined=> {
21
- return !localFuncs1? undefined : typeof localFuncs1 === "function"? {
22
- onData: localFuncs1
23
- } : localFuncs1;
24
- }
25
-
26
- async function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFuncs: LocalFuncs): Promise<{ unsubscribe: () => any }>
27
- async function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFuncs: undefined, table_rules: TableRule | undefined, localParams: LocalParams): Promise<SubscriptionChannels>
28
- async function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFuncs?: LocalFuncs, table_rules?: TableRule, localParams?: LocalParams): Promise<{ unsubscribe: () => any } | SubscriptionChannels>
29
- {
30
- const start = Date.now();
31
- try {
32
-
33
- if(!this.dboBuilder.canSubscribe){
34
- throw "Cannot subscribe. PubSubManager not initiated";
35
- }
36
-
37
- if (this.tx) {
38
- throw "subscribe not allowed within transactions";
39
- }
40
- if (!localParams && !localFuncs) {
41
- throw " missing data. provide -> localFunc | localParams { socket } ";
42
- }
43
- if (localParams?.socket && localFuncs) {
44
- console.error({ localParams, localFuncs })
45
- throw " Cannot have localFunc AND socket ";
46
- }
47
-
48
- const { throttle = 0, throttleOpts, ...selectParams } = params;
49
-
50
- /** Ensure request is valid */
51
- await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, localParams);
52
-
53
- // TODO: Implement comprehensive canSubscribe check
54
- // if (!this.dboBuilder.prostgles.isSuperUser) {
55
- // throw "Subscribe not possible. Must be superuser";
56
- // }
57
-
58
- const newQuery: NewQuery = await this.find(filter, { ...selectParams, limit: 0 }, undefined, table_rules, { ...localParams, returnNewQuery: true }) as any;
59
- const viewOptions = await getSubscribeRelatedTables.bind(this)({
60
- filter, selectParams,
61
- table_rules, localParams,
62
- newQuery
63
- });
64
-
65
- const commonSubOpts = {
66
- table_info: this.tableOrViewInfo,
67
- viewOptions,
68
- table_rules,
69
- condition: newQuery.whereOpts.condition,
70
- table_name: this.name,
71
- filter: { ...filter },
72
- params: { ...selectParams },
73
- throttle,
74
- throttleOpts,
75
- last_throttled: 0,
76
- } as const;
77
-
78
- const pubSubManager = await this.dboBuilder.getPubSubManager();
79
- if (!localFuncs) {
80
-
81
- const { socket } = localParams ?? {};
82
- const result = await pubSubManager.addSub({
83
- ...commonSubOpts,
84
- socket,
85
- localFuncs: undefined,
86
- socket_id: socket?.id,
87
- });
88
-
89
- await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start });
90
- return result;
91
- } else {
92
-
93
- const { channelName, sendFirstData } = await pubSubManager.addSub({
94
- ...commonSubOpts,
95
- socket: undefined,
96
- localFuncs,
97
- socket_id: undefined,
98
- });
99
-
100
- const unsubscribe = async () => {
101
- const pubSubManager = await this.dboBuilder.getPubSubManager();
102
- pubSubManager.removeLocalSub(channelName, localFuncs)
103
- };
104
- await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start });
105
- const res: { unsubscribe: () => any } = Object.freeze({ unsubscribe });
106
- /** Send first data after subscription is initialised to prevent race conditions */
107
- setTimeout(() => {
108
- sendFirstData?.();
109
- }, 0);
110
- return res;
111
- }
112
- } catch (e) {
113
- await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start, error: getErrorAsObject(e) });
114
- throw getSerializedClientErrorFromPGError(e, { type: "tableMethod", localParams, view: this });
115
- }
116
- }
117
-
118
- export { subscribe };
@@ -1,70 +0,0 @@
1
- import { AnyObject, FieldFilter } from "prostgles-types/dist";
2
- import { UpdateRule } from "../../PublishParser/PublishParser";
3
- import { ViewHandler } from "./ViewHandler";
4
-
5
- export async function validateViewRules(
6
- this: ViewHandler,
7
- args: {
8
- fields?: FieldFilter,
9
- filterFields?: FieldFilter,
10
- returningFields?: FieldFilter,
11
- forcedFilter?: AnyObject,
12
- dynamicFields?: UpdateRule["dynamicFields"],
13
- rule: "update" | "select" | "insert" | "delete"
14
- }
15
- ) {
16
- const {
17
- fields,
18
- filterFields,
19
- returningFields,
20
- forcedFilter,
21
- dynamicFields,
22
- rule,
23
- } = args;
24
-
25
- /* Safely test publish rules */
26
- if (fields) {
27
- try {
28
- const _fields = this.parseFieldFilter(fields);
29
- if (this.is_media && rule === "insert" && !_fields.includes("id")) {
30
- throw "Must allow id insert for media table"
31
- }
32
- } catch (e) {
33
- throw ` issue with publish.${this.name}.${rule}.fields: \nVALUE: ` + JSON.stringify(fields, null, 2) + "\nERROR: " + JSON.stringify(e, null, 2);
34
- }
35
- }
36
- if (filterFields) {
37
- try {
38
- this.parseFieldFilter(filterFields);
39
- } catch (e) {
40
- throw ` issue with publish.${this.name}.${rule}.filterFields: \nVALUE: ` + JSON.stringify(filterFields, null, 2) + "\nERROR: " + JSON.stringify(e, null, 2);
41
- }
42
- }
43
- if (returningFields) {
44
- try {
45
- this.parseFieldFilter(returningFields);
46
- } catch (e) {
47
- throw ` issue with publish.${this.name}.${rule}.returningFields: \nVALUE: ` + JSON.stringify(returningFields, null, 2) + "\nERROR: " + JSON.stringify(e, null, 2);
48
- }
49
- }
50
- if (forcedFilter) {
51
- try {
52
- await this.find(forcedFilter, { limit: 0 });
53
- } catch (e) {
54
- throw ` issue with publish.${this.name}.${rule}.forcedFilter: \nVALUE: ` + JSON.stringify(forcedFilter, null, 2) + "\nERROR: " + JSON.stringify(e, null, 2);
55
- }
56
- }
57
- if (dynamicFields) {
58
- for await (const dfieldRule of dynamicFields) {
59
- try {
60
- const { fields, filter } = dfieldRule;
61
- this.parseFieldFilter(fields);
62
- await this.find(filter, { limit: 0 });
63
- } catch (e) {
64
- throw ` issue with publish.${this.name}.${rule}.dynamicFields: \nVALUE: ` + JSON.stringify(dfieldRule, null, 2) + "\nERROR: " + JSON.stringify(e, null, 2);
65
- }
66
- }
67
- }
68
-
69
- return true;
70
- }