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,319 +0,0 @@
1
-
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Stefan L. All rights reserved.
4
- * Licensed under the MIT License. See LICENSE in the project root for license information.
5
- *--------------------------------------------------------------------------------------------*/
6
-
7
- import {
8
- PG_COLUMN_UDT_DATA_TYPE,
9
- SQLOptions,
10
- getJoinHandlers,
11
- isDefined,
12
- tryCatch
13
- } from "prostgles-types";
14
- import { getDBSchema } from "../DBSchemaBuilder";
15
- import {
16
- DB, Prostgles
17
- } from "../Prostgles";
18
- import { Join } from "../ProstglesTypes";
19
- import { PubSubManager } from "../PubSubManager/PubSubManager";
20
- import { getCreatePubSubManagerError } from "../PubSubManager/getCreatePubSubManagerError";
21
- import {
22
- DbTableInfo,
23
- PublishParser
24
- } from "../PublishParser/PublishParser";
25
- import { Graph } from "../shortestPath";
26
- import { clone } from "../utils";
27
- import { DBHandlerServer, DbTxTableHandlers, LocalParams, TX, TableSchema, TxCB } from "./DboBuilderTypes";
28
- import { QueryStreamer } from "./QueryStreamer";
29
- import { TableHandler } from "./TableHandler/TableHandler";
30
- import { JoinPaths, ViewHandler } from "./ViewHandler/ViewHandler";
31
- import { parseJoinPath } from "./ViewHandler/parseJoinPath";
32
- import { PGConstraint, getCanExecute, getConstraints, getErrorAsObject, getSerializedClientErrorFromPGError } from "./dboBuilderUtils";
33
- import { getTablesForSchemaPostgresSQL } from "./getTablesForSchemaPostgresSQL";
34
- import { prepareShortestJoinPaths } from "./prepareShortestJoinPaths";
35
- import { cacheDBTypes, runSQL } from "./runSQL";
36
- import { runClientTransactionStatement } from "./runTransaction";
37
-
38
- export * from "./DboBuilderTypes";
39
- export * from "./dboBuilderUtils";
40
-
41
- type OidInfo = {
42
- /**
43
- * Oid
44
- */
45
- relid: number;
46
- relname: string;
47
- schemaname: string;
48
- };
49
-
50
- type TableOidInfo = OidInfo & {
51
- pkey_columns: string[] | null;
52
- };
53
-
54
- type TableOidColumnInfo = OidInfo & {
55
- column_name: string;
56
- udt_name: string;
57
- ordinal_position: number;
58
- };
59
-
60
-
61
- export class DboBuilder {
62
- tablesOrViews?: TableSchema[];
63
- /**
64
- * Used in obtaining column names for error messages
65
- */
66
- constraints?: PGConstraint[];
67
-
68
- db: DB;
69
-
70
- dbo: DBHandlerServer;
71
-
72
- /**
73
- * Undefined if cannot create table triggers
74
- */
75
- private _pubSubManager?: PubSubManager;
76
-
77
- /**
78
- * Used for db.sql field type details
79
- */
80
- DATA_TYPES: { oid: string, typname: PG_COLUMN_UDT_DATA_TYPE }[] | undefined;
81
- DATA_TYPES_DBKEY = "";
82
- USER_TABLES: TableOidInfo[] | undefined;
83
- USER_TABLE_COLUMNS: TableOidColumnInfo[] | undefined;
84
-
85
- queryStreamer: QueryStreamer;
86
-
87
- get tables(): DbTableInfo[] {
88
- return (this.tablesOrViews ?? [])
89
- .map(({ name, columns }) => {
90
- const info = this.dbo[name]?.tableOrViewInfo;
91
- if(!info) return undefined;
92
- return {
93
- name,
94
- columns,
95
- info
96
- }})
97
- .filter(isDefined);
98
- }
99
-
100
- getPubSubManager = async (): Promise<PubSubManager> => {
101
- if (!this._pubSubManager) {
102
-
103
- const canExecute = await getCanExecute(this.db)
104
- if (!canExecute) throw "PubSubManager based subscriptions not possible: Cannot run EXECUTE statements on this connection";
105
-
106
- const { pubSubManager, error, hasError } = await tryCatch(async () => {
107
- const pubSubManager = await PubSubManager.create({
108
- dboBuilder: this,
109
- });
110
- return { pubSubManager };
111
- });
112
- this._pubSubManager = pubSubManager;
113
- if (hasError || !this._pubSubManager ) {
114
- await this.prostgles.opts.onLog?.({
115
- type: "debug",
116
- command: "PubSubManager.create",
117
- duration: 0,
118
- error: getErrorAsObject(error)
119
- });
120
- throw "Could not create this._pubSubManager check logs";
121
- }
122
- }
123
-
124
- return this._pubSubManager;
125
- }
126
-
127
- tsTypesDefinition?: string;
128
- joinGraph?: Graph;
129
- private shortestJoinPaths: JoinPaths = [];
130
-
131
- prostgles: Prostgles;
132
- publishParser?: PublishParser;
133
-
134
- onSchemaChange?: (event: { command: string; query: string }) => void;
135
-
136
- private constructor(prostgles: Prostgles) {
137
- this.prostgles = prostgles;
138
- if (!this.prostgles.db) throw "db missing"
139
- this.db = this.prostgles.db;
140
- this.dbo = {} as unknown as DBHandlerServer;
141
- this.queryStreamer = new QueryStreamer(this);
142
- }
143
-
144
- private init = async () => {
145
-
146
- await this.build();
147
- /* If watchSchema is enabled then PubSubManager must be created (if possible) because it creates the event trigger */
148
- if (
149
- this.prostgles.schemaWatch?.type.watchType === "DDL_trigger"
150
- ) {
151
- await this.getPubSubManager()
152
- }
153
-
154
- return this;
155
- }
156
-
157
- public static create = async (prostgles: Prostgles): Promise<DboBuilder> => {
158
- const res = new DboBuilder(prostgles)
159
- return await res.init();
160
- }
161
-
162
-
163
- destroy() {
164
- this._pubSubManager?.destroy();
165
- }
166
-
167
- _joins?: Join[];
168
- get joins(): Join[] {
169
- return clone(this._joins ?? []).filter(j => j.tables[0] !== j.tables[1]) as Join[];
170
- }
171
-
172
- set joins(j: Join[]) {
173
- this._joins = clone(j);
174
- }
175
-
176
- getAllJoinPaths() {
177
- return this.shortestJoinPaths;
178
- }
179
-
180
- prepareShortestJoinPaths = async () => {
181
- const { joins, shortestJoinPaths, joinGraph } = await prepareShortestJoinPaths(this);
182
- this.joinGraph = joinGraph;
183
- this.joins = joins;
184
- this.shortestJoinPaths = shortestJoinPaths;
185
- }
186
-
187
- runSQL = async (query: string, params: any, options: SQLOptions | undefined, localParams?: LocalParams) => {
188
- return runSQL.bind(this)(query, params, options, localParams).catch(error => Promise.reject(getSerializedClientErrorFromPGError(error, { type: "sql" })));
189
- }
190
-
191
- canSubscribe = false;
192
- checkingCanSubscribe = false;
193
- async build(): Promise<DBHandlerServer> {
194
- if(!this.canSubscribe && !this.checkingCanSubscribe){
195
- this.checkingCanSubscribe = true;
196
- const subscribeError = await getCreatePubSubManagerError(this);
197
- if(subscribeError){
198
- console.error(
199
- "Could not initiate PubSubManager. Realtime data/Subscriptions will not work. Error: ",
200
- subscribeError
201
- );
202
- this.canSubscribe = false;
203
- } else {
204
- this.canSubscribe = true;
205
- }
206
- this.checkingCanSubscribe = false;
207
- }
208
- const start = Date.now();
209
- const tablesOrViewsReq = await getTablesForSchemaPostgresSQL(this, this.prostgles.opts.schema);
210
- await this.prostgles.opts.onLog?.({
211
- type: "debug",
212
- command: "DboBuilder.getTablesForSchemaPostgresSQL",
213
- data: tablesOrViewsReq.durations,
214
- duration: Date.now() - start,
215
- })
216
- this.tablesOrViews = tablesOrViewsReq.result;
217
-
218
- this.constraints = await getConstraints(this.db, this.prostgles.opts.schema);
219
- await this.prepareShortestJoinPaths();
220
-
221
- this.dbo = {};
222
- this.tablesOrViews.map(tov => {
223
- const columnsForTypes = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
224
-
225
-
226
- const filterKeywords = Object.values(this.prostgles.keywords);
227
- const $filterCol = columnsForTypes.find(c => filterKeywords.includes(c.name));
228
- if ($filterCol) {
229
- throw `DboBuilder init error: \n\nTable ${JSON.stringify(tov.name)} column ${JSON.stringify($filterCol.name)} is colliding with Prostgles filtering functionality ($filter keyword)
230
- Please provide a replacement keyword name using the $filter_keyName init option.
231
- Alternatively you can rename the table column\n`;
232
- }
233
-
234
- this.dbo[tov.escaped_identifier] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, undefined, this.shortestJoinPaths);
235
-
236
- if (this.shortestJoinPaths && this.shortestJoinPaths.find(jp => [jp.t1, jp.t2].includes(tov.name))) {
237
-
238
- const table = tov.name;
239
-
240
- this.dbo.innerJoin ??= {};
241
- this.dbo.leftJoin ??= {};
242
- this.dbo.innerJoinOne ??= {};
243
- this.dbo.leftJoinOne ??= {};
244
-
245
- const joinHandlers = getJoinHandlers(table);
246
- //@ts-ignore
247
- this.dbo.leftJoin[table] = joinHandlers.leftJoin;
248
- this.dbo.innerJoin[table] = joinHandlers.innerJoin;
249
- this.dbo.leftJoinOne[table] = joinHandlers.leftJoinOne;
250
- this.dbo.innerJoinOne[table] = joinHandlers.innerJoinOne;
251
- }
252
- });
253
-
254
- if (this.prostgles.opts.transactions) {
255
- let txKey = "tx";
256
- if (typeof this.prostgles.opts.transactions === "string") txKey = this.prostgles.opts.transactions;
257
-
258
- (this.dbo[txKey] as unknown as TX) = (cb: TxCB) => this.getTX(cb);
259
- }
260
-
261
- if (!this.dbo.sql) {
262
-
263
- this.dbo.sql = this.runSQL;
264
- } else {
265
- console.warn(`Could not create dbo.sql handler because there is already a table named "sql"`)
266
- }
267
-
268
- this.tsTypesDefinition = [
269
- `/* SCHEMA DEFINITON. Table names have been altered to work with Typescript */`,
270
- `/* DBO Definition */`,
271
- getDBSchema(this)
272
- ].join("\n");
273
-
274
- return this.dbo;
275
- }
276
-
277
- getShortestJoinPath = (viewHandler: ViewHandler, target: string): JoinPaths[number] | undefined => {
278
- const source = viewHandler.name;
279
- if(source === target){
280
- const joinPath = parseJoinPath({
281
- rawPath: target,
282
- rootTable: source,
283
- viewHandler
284
- });
285
-
286
- if(!joinPath) return undefined;
287
-
288
- return {
289
- t1: source,
290
- t2: target,
291
- path: [source]
292
- }
293
- }
294
-
295
- const jp = this.shortestJoinPaths.find(jp => jp.t1 === source && jp.t2 === target);
296
- return jp;
297
- }
298
-
299
- getTX = async (cb: TxCB) => {
300
- const transaction = await this.db.tx(t => {
301
- const dbTX: DbTxTableHandlers & Pick<DBHandlerServer, "sql"> = {};
302
- this.tablesOrViews?.map(tov => {
303
- const handlerClass = tov.is_view ? ViewHandler : TableHandler;
304
- dbTX[tov.name] = new handlerClass(this.db, tov, this, { t, dbTX }, this.shortestJoinPaths);
305
- });
306
- dbTX.sql = (q, args, opts, localP) => this.runSQL(q, args, opts, { tx: { dbTX, t }, ...(localP ?? {}) })
307
-
308
- return cb(dbTX, t);
309
- });
310
-
311
- return transaction;
312
- }
313
-
314
- cacheDBTypes = cacheDBTypes.bind(this);
315
-
316
- runClientTransactionStatement = (statement: string) => {
317
- return runClientTransactionStatement(statement, this.prostgles.opts.dbConnection as any);
318
- }
319
- }
@@ -1,361 +0,0 @@
1
-
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Stefan L. All rights reserved.
4
- * Licensed under the MIT License. See LICENSE in the project root for license information.
5
- *--------------------------------------------------------------------------------------------*/
6
-
7
- import * as pgPromise from 'pg-promise';
8
- import {
9
- AnyObject,
10
- ColumnInfo,
11
- DbJoinMaker,
12
- EXISTS_KEY,
13
- RawJoinPath,
14
- SQLHandler,
15
- TableInfo as TInfo,
16
- UserLike
17
- } from "prostgles-types";
18
- import { BasicSession, ExpressReq } from "../Auth/AuthTypes";
19
- import { BasicCallback } from "../PubSubManager/PubSubManager";
20
- import {
21
- PublishAllOrNothing
22
- } from "../PublishParser/PublishParser";
23
- import { FieldSpec, } from "./QueryBuilder/Functions";
24
- import { TableHandler } from "./TableHandler/TableHandler";
25
- import { ParsedJoinPath } from "./ViewHandler/parseJoinPath";
26
- import pg = require('pg-promise/typescript/pg-subset');
27
-
28
-
29
- type PGP = pgPromise.IMain<{}, pg.IClient>;
30
-
31
-
32
- export type TableSchemaColumn = ColumnInfo & {
33
- privileges: Partial<Record<"INSERT" | "REFERENCES" | "SELECT" | "UPDATE", true>>;
34
- }
35
-
36
- export type TableSchema = {
37
- schema: string;
38
- name: string;
39
- escaped_identifier: string;
40
- oid: number;
41
- comment: string;
42
- columns: TableSchemaColumn[];
43
- is_view: boolean;
44
- view_definition: string | null;
45
- parent_tables: string[];
46
- privileges: {
47
- insert: boolean;
48
- select: boolean;
49
- update: boolean;
50
- delete: boolean;
51
- };
52
- /** Cannot add triggers to hyperTables */
53
- isHyperTable?: boolean;
54
- }
55
-
56
- export type SortItem = {
57
- asc: boolean;
58
- nulls?: "first" | "last";
59
- nullEmpty?: boolean;
60
- key: string;
61
- nested?: {
62
- table: string;
63
- selectItemAlias: string;
64
- isNumeric: boolean;
65
- wrapperQuerySortItem: string;
66
- joinAlias: string;
67
- };
68
- } & ({
69
- type: "query";
70
- fieldQuery: string;
71
- } | {
72
- type: "position";
73
- fieldPosition: number;
74
- });
75
-
76
- export type Media = {
77
- id?: string;
78
- title?: string;
79
- extension?: string;
80
- content_type?: string;
81
- content_length?: number;
82
- url?: string;
83
- added?: Date;
84
- signed_url?: string;
85
- signed_url_expires?: number;
86
- name?: string;
87
- original_name?: string;
88
- etag?: string;
89
- deleted?: string | null;
90
- deleted_from_storage?: string | null;
91
- };
92
-
93
- export type ParsedMedia = Required<Pick<Media, "extension" | "content_type">>;
94
-
95
- export type TxCB<TH = DbTxTableHandlers> = {
96
- (t: TH & Pick<DBHandlerServer, "sql">, _t: pgPromise.ITask<{}>): (any | void);
97
- }
98
- export type TX<TH = TableHandlers> = {
99
- (t: TxCB<TH>): Promise<(any | void)>;
100
- }
101
-
102
- export type TableHandlers = {
103
- [key: string]: Partial<TableHandler>;
104
- }
105
- export type DbTxTableHandlers = {
106
- [key: string]: Omit<Partial<TableHandler>, "dbTx"> | Omit<TableHandler, "dbTx">;
107
- }
108
-
109
-
110
- export type DBHandlerServerExtra<TH = TableHandlers, WithTransactions = true> = {
111
- sql: SQLHandler;
112
- } & (
113
- WithTransactions extends true? { tx: TX<TH> } :
114
- Record<string, never>
115
- );
116
-
117
- // export type DBHandlerServer<TH = TableHandlers> =
118
- // TH &
119
- // Partial<DbJoinMaker> & {
120
- // sql?: SQLHandler
121
- // } & {
122
- // tx?: TX<TH>
123
- // }
124
-
125
- export type DBHandlerServer<TH = TableHandlers> =
126
- TH &
127
- Partial<DbJoinMaker> & {
128
- sql?: SQLHandler
129
- } & {
130
- tx?: TX<TH>
131
- }
132
-
133
-
134
- export const pgp: PGP = pgPromise({
135
- // ,query: function (e) { console.log({psql: e.query, params: e.params}); }
136
- });
137
-
138
- export type TableInfo = TInfo & {
139
- schema: string;
140
- name: string;
141
- oid: number;
142
- comment: string;
143
- columns: ColumnInfo[];
144
- }
145
-
146
- export type ViewInfo = TableInfo & {
147
- parent_tables: string[]
148
- }
149
-
150
- export type TableOrViewInfo = TableInfo & ViewInfo & {
151
- is_view: boolean;
152
- }
153
-
154
- export type PRGLIOSocket = {
155
- readonly id: string;
156
-
157
- readonly handshake: {
158
- query?: Record<string, string | string[] | undefined>;
159
- /**
160
- * IP Address
161
- */
162
- address: string;
163
- headers?: AnyObject & { cookie?: string; }; // e.g.: "some_arg=dwdaw; otherarg=23232"
164
- auth?: Record<string, any>;
165
- }
166
-
167
- readonly on: (channel: string, params: any, cb?: (err: any, res?: any) => void) => any;// Promise<void>;
168
-
169
- readonly emit: (channel: string, message?: any, cb?: BasicCallback) => any;
170
-
171
- readonly once: (channel: string, cb: (_data: any, cb: BasicCallback) => void) => void;
172
-
173
- readonly removeAllListeners: (channel: string) => void;
174
-
175
- readonly disconnect: () => void;
176
-
177
- readonly request: {
178
- url?: string;
179
- connection: { remoteAddress?: string; }
180
- }
181
-
182
- /** Used for session caching */
183
- __prglCache?: {
184
- session: BasicSession;
185
- user: UserLike;
186
- clientUser: UserLike;
187
- }
188
-
189
- _user?: AnyObject
190
-
191
- /** Used for publish error caching */
192
- prostgles?: AnyObject;
193
- };
194
-
195
- export type LocalParams = {
196
- httpReq?: ExpressReq;
197
- socket?: PRGLIOSocket;
198
- func?: () => any;
199
- isRemoteRequest?: {
200
- user?: UserLike | undefined;
201
- };
202
- testRule?: boolean;
203
- tableAlias?: string;
204
- // subOne?: boolean;
205
-
206
- tx?: {
207
- dbTX: TableHandlers;
208
- t: pgPromise.ITask<{}>;
209
- }
210
-
211
- /** Used to exclude certain logs */
212
- noLog?: boolean;
213
-
214
- returnQuery?: boolean | "noRLS" | "where-condition";
215
- returnNewQuery?: boolean;
216
- /** Used for count/size queries */
217
- bypassLimit?: boolean;
218
-
219
- nestedInsert?: {
220
- depth: number;
221
- previousData: AnyObject;
222
- previousTable: string;
223
- referencingColumn?: string;
224
- }
225
- }
226
-
227
-
228
- export type Aggregation = {
229
- field: string,
230
- query: string,
231
- alias: string,
232
- getQuery: (alias: string) => string;
233
- };
234
-
235
- export type Filter = AnyObject | { $and: Filter[] } | { $or: Filter[] };
236
-
237
- export type JoinInfo = {
238
- /**
239
- * If true then all joins involve unique columns and the result is a 1 to 1 join
240
- */
241
- expectOne?: boolean,
242
- paths: {
243
-
244
- /**
245
- * The table that JOIN ON columns refer to.
246
- * columns in index = 1 refer to this table. index = 0 columns refer to previous JoinInfo.table
247
- */
248
- table: string,
249
-
250
- /**
251
- * Source and target JOIN ON column groups for each existing constraint
252
- * Each inner array group will be combined with AND and outer arrays with OR to allow multiple references to the same table
253
- * e.g.: [[source_table_column: string, table_column: string]]
254
- */
255
- on: [string, string][][],
256
-
257
- /**
258
- * Source table name
259
- */
260
- source: string,
261
-
262
- /**
263
- * Target table name
264
- */
265
- target: string
266
- }[]
267
- }
268
-
269
-
270
- export type CommonTableRules = {
271
-
272
- /**
273
- * True by default. Allows clients to get column information on any columns that are allowed in (select, insert, update) field rules.
274
- */
275
- getColumns?: PublishAllOrNothing;
276
-
277
- /**
278
- * True by default. Allows clients to get table information (oid, comment, label, has_media).
279
- */
280
- getInfo?: PublishAllOrNothing
281
- }
282
-
283
- export type ValidatedTableRules = CommonTableRules & {
284
-
285
- /* All columns of the view/table. Includes computed fields as well */
286
- allColumns: FieldSpec[];
287
-
288
- select: {
289
- /* Fields you can select */
290
- fields: string[];
291
-
292
- /* Fields you can select */
293
- orderByFields: string[];
294
-
295
- /* Filter applied to every select */
296
- filterFields: string[];
297
-
298
- /* Filter applied to every select */
299
- forcedFilter: any;
300
-
301
- /* Max limit allowed for each select. 1000 by default. If null then an unlimited select is allowed when providing { limit: null } */
302
- maxLimit: number | null;
303
- },
304
- update: {
305
- /* Fields you can update */
306
- fields: string[];
307
-
308
- /* Fields you can return after updating */
309
- returningFields: string[];
310
-
311
- /* Fields you can use in filtering when updating */
312
- filterFields: string[];
313
-
314
- /* Filter applied to every update. Filter fields cannot be updated */
315
- forcedFilter: any;
316
-
317
- /* Data applied to every update */
318
- forcedData: any;
319
- },
320
- insert: {
321
- /* Fields you can insert */
322
- fields: string[];
323
-
324
- /* Fields you can return after inserting. Will return select.fields by default */
325
- returningFields: string[];
326
-
327
- /* Data applied to every insert */
328
- forcedData: any;
329
- },
330
- delete: {
331
- /* Fields to filter by when deleting */
332
- filterFields: string[];
333
-
334
- /* Filter applied to every deletes */
335
- forcedFilter: any;
336
-
337
- /* Fields you can return after deleting */
338
- returningFields: string[];
339
- }
340
- }
341
-
342
- export type ExistsFilterConfig = {
343
- existType: EXISTS_KEY;
344
- /**
345
- * Target table filter. target table is the last table from tables
346
- */
347
- targetTableFilter: Filter;
348
-
349
- } & ({
350
- isJoined: true;
351
- /**
352
- * list of join tables in their order
353
- * If table path starts with "**" then get shortest join to first table
354
- * e.g.: "**.users" means finding the shortest join from root table to users table
355
- */
356
- path: RawJoinPath;
357
- parsedPath: ParsedJoinPath[]
358
- } | {
359
- isJoined: false;
360
- targetTable: string;
361
- });