prostgles-server 4.2.159 → 4.2.161

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 (107) hide show
  1. package/dist/Auth/setEmailProvider.js +2 -2
  2. package/dist/Auth/setEmailProvider.js.map +1 -1
  3. package/lib/Auth/AuthHandler.ts +436 -0
  4. package/lib/Auth/AuthTypes.ts +280 -0
  5. package/lib/Auth/getSafeReturnURL.ts +35 -0
  6. package/lib/Auth/sendEmail.ts +83 -0
  7. package/lib/Auth/setAuthProviders.ts +128 -0
  8. package/lib/Auth/setEmailProvider.ts +85 -0
  9. package/lib/Auth/setupAuthRoutes.ts +161 -0
  10. package/lib/DBEventsManager.ts +178 -0
  11. package/lib/DBSchemaBuilder.ts +225 -0
  12. package/lib/DboBuilder/DboBuilder.ts +319 -0
  13. package/lib/DboBuilder/DboBuilderTypes.ts +361 -0
  14. package/lib/DboBuilder/QueryBuilder/Functions.ts +1153 -0
  15. package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +288 -0
  16. package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +263 -0
  17. package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +271 -0
  18. package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +136 -0
  19. package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +22 -0
  20. package/lib/DboBuilder/QueryStreamer.ts +250 -0
  21. package/lib/DboBuilder/TableHandler/DataValidator.ts +428 -0
  22. package/lib/DboBuilder/TableHandler/TableHandler.ts +205 -0
  23. package/lib/DboBuilder/TableHandler/delete.ts +115 -0
  24. package/lib/DboBuilder/TableHandler/insert.ts +183 -0
  25. package/lib/DboBuilder/TableHandler/insertTest.ts +78 -0
  26. package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +62 -0
  27. package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +134 -0
  28. package/lib/DboBuilder/TableHandler/update.ts +126 -0
  29. package/lib/DboBuilder/TableHandler/updateBatch.ts +49 -0
  30. package/lib/DboBuilder/TableHandler/updateFile.ts +48 -0
  31. package/lib/DboBuilder/TableHandler/upsert.ts +34 -0
  32. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +393 -0
  33. package/lib/DboBuilder/ViewHandler/count.ts +38 -0
  34. package/lib/DboBuilder/ViewHandler/find.ts +153 -0
  35. package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +73 -0
  36. package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +74 -0
  37. package/lib/DboBuilder/ViewHandler/getInfo.ts +32 -0
  38. package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +84 -0
  39. package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +96 -0
  40. package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +105 -0
  41. package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +208 -0
  42. package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +163 -0
  43. package/lib/DboBuilder/ViewHandler/prepareWhere.ts +90 -0
  44. package/lib/DboBuilder/ViewHandler/size.ts +37 -0
  45. package/lib/DboBuilder/ViewHandler/subscribe.ts +118 -0
  46. package/lib/DboBuilder/ViewHandler/validateViewRules.ts +70 -0
  47. package/lib/DboBuilder/dboBuilderUtils.ts +222 -0
  48. package/lib/DboBuilder/getColumns.ts +114 -0
  49. package/lib/DboBuilder/getCondition.ts +201 -0
  50. package/lib/DboBuilder/getSubscribeRelatedTables.ts +190 -0
  51. package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +426 -0
  52. package/lib/DboBuilder/insertNestedRecords.ts +355 -0
  53. package/lib/DboBuilder/parseUpdateRules.ts +187 -0
  54. package/lib/DboBuilder/prepareShortestJoinPaths.ts +186 -0
  55. package/lib/DboBuilder/runSQL.ts +182 -0
  56. package/lib/DboBuilder/runTransaction.ts +50 -0
  57. package/lib/DboBuilder/sqlErrCodeToMsg.ts +254 -0
  58. package/lib/DboBuilder/uploadFile.ts +69 -0
  59. package/lib/Event_Trigger_Tags.ts +118 -0
  60. package/lib/FileManager/FileManager.ts +358 -0
  61. package/lib/FileManager/getValidatedFileType.ts +69 -0
  62. package/lib/FileManager/initFileManager.ts +187 -0
  63. package/lib/FileManager/upload.ts +62 -0
  64. package/lib/FileManager/uploadStream.ts +79 -0
  65. package/lib/Filtering.ts +463 -0
  66. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +502 -0
  67. package/lib/JSONBValidation/validation.ts +143 -0
  68. package/lib/Logging.ts +127 -0
  69. package/lib/PostgresNotifListenManager.ts +143 -0
  70. package/lib/Prostgles.ts +485 -0
  71. package/lib/ProstglesTypes.ts +196 -0
  72. package/lib/PubSubManager/PubSubManager.ts +609 -0
  73. package/lib/PubSubManager/addSub.ts +138 -0
  74. package/lib/PubSubManager/addSync.ts +141 -0
  75. package/lib/PubSubManager/getCreatePubSubManagerError.ts +72 -0
  76. package/lib/PubSubManager/getPubSubManagerInitQuery.ts +662 -0
  77. package/lib/PubSubManager/initPubSubManager.ts +79 -0
  78. package/lib/PubSubManager/notifListener.ts +173 -0
  79. package/lib/PubSubManager/orphanTriggerCheck.ts +70 -0
  80. package/lib/PubSubManager/pushSubData.ts +55 -0
  81. package/lib/PublishParser/PublishParser.ts +162 -0
  82. package/lib/PublishParser/getFileTableRules.ts +124 -0
  83. package/lib/PublishParser/getSchemaFromPublish.ts +141 -0
  84. package/lib/PublishParser/getTableRulesWithoutFileTable.ts +177 -0
  85. package/lib/PublishParser/publishTypesAndUtils.ts +399 -0
  86. package/lib/RestApi.ts +127 -0
  87. package/lib/SchemaWatch/SchemaWatch.ts +90 -0
  88. package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +3 -0
  89. package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +45 -0
  90. package/lib/SchemaWatch/getWatchSchemaTagList.ts +27 -0
  91. package/lib/SyncReplication.ts +557 -0
  92. package/lib/TableConfig/TableConfig.ts +468 -0
  93. package/lib/TableConfig/getColumnDefinitionQuery.ts +111 -0
  94. package/lib/TableConfig/getConstraintDefinitionQueries.ts +95 -0
  95. package/lib/TableConfig/getFutureTableSchema.ts +64 -0
  96. package/lib/TableConfig/getPGIndexes.ts +53 -0
  97. package/lib/TableConfig/getTableColumnQueries.ts +129 -0
  98. package/lib/TableConfig/initTableConfig.ts +326 -0
  99. package/lib/index.ts +13 -0
  100. package/lib/initProstgles.ts +319 -0
  101. package/lib/onSocketConnected.ts +102 -0
  102. package/lib/runClientRequest.ts +129 -0
  103. package/lib/shortestPath.ts +122 -0
  104. package/lib/typeTests/DBoGenerated.d.ts +320 -0
  105. package/lib/typeTests/dboTypeCheck.ts +81 -0
  106. package/lib/utils.ts +15 -0
  107. package/package.json +1 -1
@@ -0,0 +1,208 @@
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
+ }
@@ -0,0 +1,163 @@
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
+ };
@@ -0,0 +1,90 @@
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
+ }
@@ -0,0 +1,37 @@
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
+ }
@@ -0,0 +1,118 @@
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 };
@@ -0,0 +1,70 @@
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
+ }