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.
- package/dist/Auth/setEmailProvider.js +2 -2
- package/dist/Auth/setEmailProvider.js.map +1 -1
- package/lib/Auth/AuthHandler.ts +436 -0
- package/lib/Auth/AuthTypes.ts +280 -0
- package/lib/Auth/getSafeReturnURL.ts +35 -0
- package/lib/Auth/sendEmail.ts +83 -0
- package/lib/Auth/setAuthProviders.ts +128 -0
- package/lib/Auth/setEmailProvider.ts +85 -0
- package/lib/Auth/setupAuthRoutes.ts +161 -0
- package/lib/DBEventsManager.ts +178 -0
- package/lib/DBSchemaBuilder.ts +225 -0
- package/lib/DboBuilder/DboBuilder.ts +319 -0
- package/lib/DboBuilder/DboBuilderTypes.ts +361 -0
- package/lib/DboBuilder/QueryBuilder/Functions.ts +1153 -0
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +288 -0
- package/lib/DboBuilder/QueryBuilder/getJoinQuery.ts +263 -0
- package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +271 -0
- package/lib/DboBuilder/QueryBuilder/getSelectQuery.ts +136 -0
- package/lib/DboBuilder/QueryBuilder/prepareHaving.ts +22 -0
- package/lib/DboBuilder/QueryStreamer.ts +250 -0
- package/lib/DboBuilder/TableHandler/DataValidator.ts +428 -0
- package/lib/DboBuilder/TableHandler/TableHandler.ts +205 -0
- package/lib/DboBuilder/TableHandler/delete.ts +115 -0
- package/lib/DboBuilder/TableHandler/insert.ts +183 -0
- package/lib/DboBuilder/TableHandler/insertTest.ts +78 -0
- package/lib/DboBuilder/TableHandler/onDeleteFromFileTable.ts +62 -0
- package/lib/DboBuilder/TableHandler/runInsertUpdateQuery.ts +134 -0
- package/lib/DboBuilder/TableHandler/update.ts +126 -0
- package/lib/DboBuilder/TableHandler/updateBatch.ts +49 -0
- package/lib/DboBuilder/TableHandler/updateFile.ts +48 -0
- package/lib/DboBuilder/TableHandler/upsert.ts +34 -0
- package/lib/DboBuilder/ViewHandler/ViewHandler.ts +393 -0
- package/lib/DboBuilder/ViewHandler/count.ts +38 -0
- package/lib/DboBuilder/ViewHandler/find.ts +153 -0
- package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +73 -0
- package/lib/DboBuilder/ViewHandler/getExistsFilters.ts +74 -0
- package/lib/DboBuilder/ViewHandler/getInfo.ts +32 -0
- package/lib/DboBuilder/ViewHandler/getTableJoinQuery.ts +84 -0
- package/lib/DboBuilder/ViewHandler/parseComplexFilter.ts +96 -0
- package/lib/DboBuilder/ViewHandler/parseFieldFilter.ts +105 -0
- package/lib/DboBuilder/ViewHandler/parseJoinPath.ts +208 -0
- package/lib/DboBuilder/ViewHandler/prepareSortItems.ts +163 -0
- package/lib/DboBuilder/ViewHandler/prepareWhere.ts +90 -0
- package/lib/DboBuilder/ViewHandler/size.ts +37 -0
- package/lib/DboBuilder/ViewHandler/subscribe.ts +118 -0
- package/lib/DboBuilder/ViewHandler/validateViewRules.ts +70 -0
- package/lib/DboBuilder/dboBuilderUtils.ts +222 -0
- package/lib/DboBuilder/getColumns.ts +114 -0
- package/lib/DboBuilder/getCondition.ts +201 -0
- package/lib/DboBuilder/getSubscribeRelatedTables.ts +190 -0
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +426 -0
- package/lib/DboBuilder/insertNestedRecords.ts +355 -0
- package/lib/DboBuilder/parseUpdateRules.ts +187 -0
- package/lib/DboBuilder/prepareShortestJoinPaths.ts +186 -0
- package/lib/DboBuilder/runSQL.ts +182 -0
- package/lib/DboBuilder/runTransaction.ts +50 -0
- package/lib/DboBuilder/sqlErrCodeToMsg.ts +254 -0
- package/lib/DboBuilder/uploadFile.ts +69 -0
- package/lib/Event_Trigger_Tags.ts +118 -0
- package/lib/FileManager/FileManager.ts +358 -0
- package/lib/FileManager/getValidatedFileType.ts +69 -0
- package/lib/FileManager/initFileManager.ts +187 -0
- package/lib/FileManager/upload.ts +62 -0
- package/lib/FileManager/uploadStream.ts +79 -0
- package/lib/Filtering.ts +463 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +502 -0
- package/lib/JSONBValidation/validation.ts +143 -0
- package/lib/Logging.ts +127 -0
- package/lib/PostgresNotifListenManager.ts +143 -0
- package/lib/Prostgles.ts +485 -0
- package/lib/ProstglesTypes.ts +196 -0
- package/lib/PubSubManager/PubSubManager.ts +609 -0
- package/lib/PubSubManager/addSub.ts +138 -0
- package/lib/PubSubManager/addSync.ts +141 -0
- package/lib/PubSubManager/getCreatePubSubManagerError.ts +72 -0
- package/lib/PubSubManager/getPubSubManagerInitQuery.ts +662 -0
- package/lib/PubSubManager/initPubSubManager.ts +79 -0
- package/lib/PubSubManager/notifListener.ts +173 -0
- package/lib/PubSubManager/orphanTriggerCheck.ts +70 -0
- package/lib/PubSubManager/pushSubData.ts +55 -0
- package/lib/PublishParser/PublishParser.ts +162 -0
- package/lib/PublishParser/getFileTableRules.ts +124 -0
- package/lib/PublishParser/getSchemaFromPublish.ts +141 -0
- package/lib/PublishParser/getTableRulesWithoutFileTable.ts +177 -0
- package/lib/PublishParser/publishTypesAndUtils.ts +399 -0
- package/lib/RestApi.ts +127 -0
- package/lib/SchemaWatch/SchemaWatch.ts +90 -0
- package/lib/SchemaWatch/createSchemaWatchEventTrigger.ts +3 -0
- package/lib/SchemaWatch/getValidatedWatchSchemaType.ts +45 -0
- package/lib/SchemaWatch/getWatchSchemaTagList.ts +27 -0
- package/lib/SyncReplication.ts +557 -0
- package/lib/TableConfig/TableConfig.ts +468 -0
- package/lib/TableConfig/getColumnDefinitionQuery.ts +111 -0
- package/lib/TableConfig/getConstraintDefinitionQueries.ts +95 -0
- package/lib/TableConfig/getFutureTableSchema.ts +64 -0
- package/lib/TableConfig/getPGIndexes.ts +53 -0
- package/lib/TableConfig/getTableColumnQueries.ts +129 -0
- package/lib/TableConfig/initTableConfig.ts +326 -0
- package/lib/index.ts +13 -0
- package/lib/initProstgles.ts +319 -0
- package/lib/onSocketConnected.ts +102 -0
- package/lib/runClientRequest.ts +129 -0
- package/lib/shortestPath.ts +122 -0
- package/lib/typeTests/DBoGenerated.d.ts +320 -0
- package/lib/typeTests/dboTypeCheck.ts +81 -0
- package/lib/utils.ts +15 -0
- package/package.json +1 -1
package/lib/Logging.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { AnyObject } from "prostgles-types";
|
|
2
|
+
import { LocalParams } from "./DboBuilder/DboBuilder";
|
|
3
|
+
import { TableHandler } from "./DboBuilder/TableHandler/TableHandler";
|
|
4
|
+
|
|
5
|
+
type ClientInfo = {
|
|
6
|
+
socketId: string | undefined;
|
|
7
|
+
sid: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export namespace EventTypes {
|
|
11
|
+
type DebugInfo = {
|
|
12
|
+
duration: number;
|
|
13
|
+
error?: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type Table = ClientInfo & DebugInfo & {
|
|
17
|
+
type: "table";
|
|
18
|
+
tableName: string;
|
|
19
|
+
command: keyof TableHandler;
|
|
20
|
+
txInfo: AnyObject | undefined;
|
|
21
|
+
data: AnyObject;
|
|
22
|
+
localParams: LocalParams | undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type SyncOneClient = ClientInfo & DebugInfo & {
|
|
26
|
+
type: "sync";
|
|
27
|
+
tableName: string;
|
|
28
|
+
localParams?: LocalParams;
|
|
29
|
+
connectedSocketIds: string[];
|
|
30
|
+
} & (
|
|
31
|
+
{
|
|
32
|
+
command: "syncData";
|
|
33
|
+
source: "client" | "trigger";
|
|
34
|
+
connectedSocketIds: string[];
|
|
35
|
+
lr: string;
|
|
36
|
+
} | {
|
|
37
|
+
command: "upsertData" | "pushData";
|
|
38
|
+
rows: number;
|
|
39
|
+
socketId: string;
|
|
40
|
+
} | {
|
|
41
|
+
command: "addTrigger";
|
|
42
|
+
state: "ok" | "fail";
|
|
43
|
+
/** If no socket id then it's a local subscribe */
|
|
44
|
+
socketId: string | undefined;
|
|
45
|
+
condition: string;
|
|
46
|
+
} | {
|
|
47
|
+
command: "addSync" | "unsync";
|
|
48
|
+
socketId: string;
|
|
49
|
+
condition: string;
|
|
50
|
+
} | {
|
|
51
|
+
command: "upsertSocket.disconnect";
|
|
52
|
+
socketId: string;
|
|
53
|
+
remainingSyncs: string;
|
|
54
|
+
remainingSubs: string;
|
|
55
|
+
connectedSocketIds: string[];
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
export type SyncMultiClient = {
|
|
59
|
+
type: "sync";
|
|
60
|
+
tableName: string;
|
|
61
|
+
localParams?: LocalParams;
|
|
62
|
+
connectedSocketIds: string[];
|
|
63
|
+
} & (
|
|
64
|
+
{
|
|
65
|
+
command: "notifListener";
|
|
66
|
+
op_name: string | undefined;
|
|
67
|
+
condition_ids_str: string | undefined;
|
|
68
|
+
tableTriggers: string[] | undefined;
|
|
69
|
+
tableSyncs: string;
|
|
70
|
+
state: "ok" | "error" | "no-triggers" | "invalid_condition_ids";
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
export type Sync = SyncOneClient | SyncMultiClient;
|
|
75
|
+
|
|
76
|
+
export type Connection = (ClientInfo & {
|
|
77
|
+
type: "connect" | "disconnect";
|
|
78
|
+
socketId: string;
|
|
79
|
+
connectedSocketIds: string[];
|
|
80
|
+
}) | (ClientInfo & DebugInfo & {
|
|
81
|
+
type: "connect.getClientSchema";
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
export type Method = ClientInfo & DebugInfo & {
|
|
85
|
+
type: "method";
|
|
86
|
+
args: any[];
|
|
87
|
+
localParams: LocalParams;
|
|
88
|
+
};
|
|
89
|
+
export type Auth = ClientInfo & DebugInfo & {
|
|
90
|
+
type: "auth";
|
|
91
|
+
} & (
|
|
92
|
+
| { command: "getClientInfo"; }
|
|
93
|
+
| { command: "login"; }
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
export type Debug = DebugInfo & {
|
|
97
|
+
type: "debug";
|
|
98
|
+
command:
|
|
99
|
+
| "initFileTable"
|
|
100
|
+
| "initTableConfig"
|
|
101
|
+
| "runSQLFile"
|
|
102
|
+
| "schemaChangeNotif"
|
|
103
|
+
| "TableConfig.runQueries.start"
|
|
104
|
+
| "TableConfig.runQueries.end"
|
|
105
|
+
| "refreshDBO.start"
|
|
106
|
+
| "refreshDBO.end"
|
|
107
|
+
| "tableConfigurator.init.start"
|
|
108
|
+
| "tableConfigurator.init.end"
|
|
109
|
+
| "initFileTable.start"
|
|
110
|
+
| "initFileTable.end"
|
|
111
|
+
| "initFileManager.runQuery"
|
|
112
|
+
| "DboBuilder.getTablesForSchemaPostgresSQL"
|
|
113
|
+
| "PubSubManager.create"
|
|
114
|
+
data?: AnyObject;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type EventInfo =
|
|
119
|
+
| EventTypes.Auth
|
|
120
|
+
| EventTypes.Table
|
|
121
|
+
| EventTypes.Method
|
|
122
|
+
| EventTypes.Sync
|
|
123
|
+
| EventTypes.SyncMultiClient
|
|
124
|
+
| EventTypes.Connection
|
|
125
|
+
| EventTypes.Debug;
|
|
126
|
+
|
|
127
|
+
export type TableEvent = EventTypes.Table;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Stefan L. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License. See LICENSE in the project root for license information.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
|
|
6
|
+
import { DB } from "./Prostgles";
|
|
7
|
+
import pg from "pg-promise/typescript/pg-subset";
|
|
8
|
+
import pgPromise from "pg-promise";
|
|
9
|
+
|
|
10
|
+
export type PrglNotifListener = (args: {
|
|
11
|
+
length: number;
|
|
12
|
+
processId: number;
|
|
13
|
+
channel: string;
|
|
14
|
+
payload: string;
|
|
15
|
+
name: string;
|
|
16
|
+
}) => void;
|
|
17
|
+
|
|
18
|
+
export class PostgresNotifListenManager {
|
|
19
|
+
connection?: pgPromise.IConnected<{}, pg.IClient>;
|
|
20
|
+
db_pg: DB;
|
|
21
|
+
notifListener: PrglNotifListener;
|
|
22
|
+
db_channel_name: string;
|
|
23
|
+
isListening: any;
|
|
24
|
+
client?: pg.IClient;
|
|
25
|
+
|
|
26
|
+
static create = (db_pg: DB, notifListener: PrglNotifListener, db_channel_name: string): Promise<PostgresNotifListenManager> => {
|
|
27
|
+
const res = new PostgresNotifListenManager(db_pg, notifListener, db_channel_name, true);
|
|
28
|
+
return res.init();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constructor(db_pg: DB, notifListener: PrglNotifListener, db_channel_name: string, noInit = false) {
|
|
32
|
+
if (!db_pg || !notifListener || !db_channel_name) throw "PostgresNotifListenManager: db_pg OR notifListener OR db_channel_name MISSING";
|
|
33
|
+
this.db_pg = db_pg;
|
|
34
|
+
this.notifListener = notifListener;
|
|
35
|
+
this.db_channel_name = db_channel_name;
|
|
36
|
+
|
|
37
|
+
if (!noInit) this.init()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async init(): Promise<PostgresNotifListenManager> {
|
|
41
|
+
|
|
42
|
+
this.connection = undefined;
|
|
43
|
+
|
|
44
|
+
this.isListening = await this.startListening();
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get isReady() {
|
|
49
|
+
return this.isListening;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
startListening() {
|
|
53
|
+
if (!this.db_pg || !this.notifListener) throw "PostgresNotifListenManager: db_pg OR notifListener missing";
|
|
54
|
+
|
|
55
|
+
return this.reconnect()
|
|
56
|
+
.catch(error => {
|
|
57
|
+
console.log('PostgresNotifListenManager: Failed Initial Connection:', error);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
destroyed = false;
|
|
62
|
+
destroy = async () => {
|
|
63
|
+
this.destroyed = true;
|
|
64
|
+
await this.stopListening();
|
|
65
|
+
this.connection?.done();
|
|
66
|
+
this.connection = undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
stopListening = async () => {
|
|
70
|
+
if (this.db_channel_name) {
|
|
71
|
+
try {
|
|
72
|
+
await this.connection?.none('UNLISTEN $1~', [this.db_channel_name])
|
|
73
|
+
await this.client?.query('UNLISTEN $1~', [this.db_channel_name])
|
|
74
|
+
} catch(error){
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
reconnect(delay: number | undefined = 0, maxAttempts: number | undefined = 0) {
|
|
81
|
+
if (!this.db_pg || !this.notifListener) throw "db_pg OR notifListener missing";
|
|
82
|
+
|
|
83
|
+
if (this.destroyed) {
|
|
84
|
+
return Promise.reject("Destroyed")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
delay = delay > 0 ? parseInt(delay + "") : 0;
|
|
88
|
+
maxAttempts = maxAttempts > 0 ? parseInt(maxAttempts + "") : 1;
|
|
89
|
+
|
|
90
|
+
const setListeners = (client: pg.IClient, notifListener: PrglNotifListener, db_channel_name: string) => {
|
|
91
|
+
client.on('notification', notifListener);
|
|
92
|
+
this.client = client;
|
|
93
|
+
if (!this.connection) throw "Connection missing";
|
|
94
|
+
return this.connection.none(
|
|
95
|
+
`/* prostgles-server internal query used for subscriptions and schema hot reload */ \nLISTEN $1~`, db_channel_name)
|
|
96
|
+
.catch(error => {
|
|
97
|
+
console.log("PostgresNotifListenManager: unexpected error: ", error); // unlikely to ever happen
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
removeListeners = (client: pg.IClient) => {
|
|
101
|
+
client.removeListener('notification', this.notifListener);
|
|
102
|
+
},
|
|
103
|
+
onConnectionLost = (err: any, e: pgPromise.ILostContext<pg.IClient>) => {
|
|
104
|
+
|
|
105
|
+
console.log('PostgresNotifListenManager: Connectivity Problem:', err);
|
|
106
|
+
this.connection = undefined; // prevent use of the broken connection
|
|
107
|
+
removeListeners(e.client);
|
|
108
|
+
|
|
109
|
+
this.reconnect(5000, 10) // retry 10 times, with 5-second intervals
|
|
110
|
+
.then(() => {
|
|
111
|
+
console.log('PostgresNotifListenManager: Successfully Reconnected');
|
|
112
|
+
})
|
|
113
|
+
.catch(() => {
|
|
114
|
+
// failed after 10 attempts
|
|
115
|
+
console.log('PostgresNotifListenManager: Connection Lost Permanently. No more retryies');
|
|
116
|
+
// process.exit(); // exiting the process
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
setTimeout(() => {
|
|
122
|
+
this.db_pg.connect({ direct: true, onLost: onConnectionLost })
|
|
123
|
+
.then(obj => {
|
|
124
|
+
this.connection = obj; // global connection is now available
|
|
125
|
+
resolve(obj);
|
|
126
|
+
return setListeners(obj.client, this.notifListener, this.db_channel_name);
|
|
127
|
+
})
|
|
128
|
+
.catch(error => {
|
|
129
|
+
/** Database was destroyed */
|
|
130
|
+
if(this.destroyed || error && error.code === "3D000") return;
|
|
131
|
+
console.log('PostgresNotifListenManager: Error Connecting:', error);
|
|
132
|
+
if (--maxAttempts) {
|
|
133
|
+
this.reconnect(delay, maxAttempts)
|
|
134
|
+
.then(resolve)
|
|
135
|
+
.catch(reject);
|
|
136
|
+
} else {
|
|
137
|
+
reject(error);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}, delay);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|