prostgles-server 4.2.192 → 4.2.194
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/AuthHandler.d.ts +8 -13
- package/dist/Auth/AuthHandler.d.ts.map +1 -1
- package/dist/Auth/AuthHandler.js +34 -89
- package/dist/Auth/AuthHandler.js.map +1 -1
- package/dist/Auth/AuthTypes.d.ts +16 -6
- package/dist/Auth/AuthTypes.d.ts.map +1 -1
- package/dist/Auth/authProviders/setOAuthProviders.js +1 -1
- package/dist/Auth/authProviders/setOAuthProviders.js.map +1 -1
- package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js +1 -1
- package/dist/Auth/endpoints/getConfirmEmailRequestHandler.js.map +1 -1
- package/dist/Auth/endpoints/getRegisterRequestHandler.js +1 -1
- package/dist/Auth/endpoints/getRegisterRequestHandler.js.map +1 -1
- package/dist/Auth/setupAuthRoutes.d.ts.map +1 -1
- package/dist/Auth/setupAuthRoutes.js +16 -10
- package/dist/Auth/setupAuthRoutes.js.map +1 -1
- package/dist/Auth/utils/getUserFromRequest.d.ts +7 -0
- package/dist/Auth/utils/getUserFromRequest.d.ts.map +1 -0
- package/dist/Auth/utils/getUserFromRequest.js +66 -0
- package/dist/Auth/utils/getUserFromRequest.js.map +1 -0
- package/dist/DboBuilder/DboBuilder.d.ts +1 -1
- package/dist/DboBuilder/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/DboBuilder.js +6 -1
- package/dist/DboBuilder/DboBuilder.js.map +1 -1
- package/dist/DboBuilder/DboBuilderTypes.d.ts +15 -7
- package/dist/DboBuilder/DboBuilderTypes.d.ts.map +1 -1
- package/dist/DboBuilder/DboBuilderTypes.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/getNewQuery.js +2 -2
- package/dist/DboBuilder/QueryBuilder/getNewQuery.js.map +1 -1
- package/dist/DboBuilder/QueryStreamer.js +1 -1
- package/dist/DboBuilder/QueryStreamer.js.map +1 -1
- package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler/TableHandler.js +2 -3
- package/dist/DboBuilder/TableHandler/TableHandler.js.map +1 -1
- package/dist/DboBuilder/TableHandler/insert.js +2 -2
- package/dist/DboBuilder/TableHandler/update.js +1 -1
- package/dist/DboBuilder/TableHandler/update.js.map +1 -1
- package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +0 -4
- package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler/ViewHandler.js +37 -24
- package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/ViewHandler/find.js +1 -1
- package/dist/DboBuilder/ViewHandler/find.js.map +1 -1
- package/dist/DboBuilder/ViewHandler/getExistsCondition.js +4 -4
- package/dist/DboBuilder/ViewHandler/getExistsCondition.js.map +1 -1
- package/dist/DboBuilder/ViewHandler/subscribe.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler/subscribe.js +9 -15
- package/dist/DboBuilder/ViewHandler/subscribe.js.map +1 -1
- package/dist/DboBuilder/dboBuilderUtils.d.ts.map +1 -1
- package/dist/DboBuilder/dboBuilderUtils.js +3 -1
- package/dist/DboBuilder/dboBuilderUtils.js.map +1 -1
- package/dist/DboBuilder/insertNestedRecords.d.ts +4 -3
- package/dist/DboBuilder/insertNestedRecords.d.ts.map +1 -1
- package/dist/DboBuilder/insertNestedRecords.js +12 -12
- package/dist/DboBuilder/insertNestedRecords.js.map +1 -1
- package/dist/DboBuilder/runSQL.d.ts +3 -2
- package/dist/DboBuilder/runSQL.d.ts.map +1 -1
- package/dist/DboBuilder/runSQL.js +12 -15
- package/dist/DboBuilder/runSQL.js.map +1 -1
- package/dist/FileManager/initFileManager.d.ts.map +1 -1
- package/dist/FileManager/initFileManager.js +5 -4
- package/dist/FileManager/initFileManager.js.map +1 -1
- package/dist/Prostgles.d.ts +3 -2
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +8 -16
- package/dist/Prostgles.js.map +1 -1
- package/dist/ProstglesTypes.d.ts +2 -2
- package/dist/ProstglesTypes.d.ts.map +1 -1
- package/dist/ProstglesTypes.js +1 -6
- package/dist/ProstglesTypes.js.map +1 -1
- package/dist/PubSubManager/PubSubManager.js +2 -2
- package/dist/PubSubManager/PubSubManager.js.map +1 -1
- package/dist/PubSubManager/addSync.d.ts.map +1 -1
- package/dist/PubSubManager/addSync.js +1 -3
- package/dist/PubSubManager/addSync.js.map +1 -1
- package/dist/PublishParser/PublishParser.d.ts +11 -18
- package/dist/PublishParser/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser/PublishParser.js +28 -27
- package/dist/PublishParser/PublishParser.js.map +1 -1
- package/dist/PublishParser/getFileTableRules.d.ts +2 -3
- package/dist/PublishParser/getFileTableRules.d.ts.map +1 -1
- package/dist/PublishParser/getFileTableRules.js +18 -20
- package/dist/PublishParser/getFileTableRules.js.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.d.ts +2 -9
- package/dist/PublishParser/getSchemaFromPublish.d.ts.map +1 -1
- package/dist/PublishParser/getSchemaFromPublish.js +5 -5
- package/dist/PublishParser/getSchemaFromPublish.js.map +1 -1
- package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts +1 -1
- package/dist/PublishParser/getTableRulesWithoutFileTable.d.ts.map +1 -1
- package/dist/PublishParser/getTableRulesWithoutFileTable.js +30 -33
- package/dist/PublishParser/getTableRulesWithoutFileTable.js.map +1 -1
- package/dist/PublishParser/publishTypesAndUtils.d.ts +6 -11
- package/dist/PublishParser/publishTypesAndUtils.d.ts.map +1 -1
- package/dist/PublishParser/publishTypesAndUtils.js.map +1 -1
- package/dist/RestApi.d.ts +1 -1
- package/dist/RestApi.d.ts.map +1 -1
- package/dist/RestApi.js +19 -16
- package/dist/RestApi.js.map +1 -1
- package/dist/initProstgles.d.ts.map +1 -1
- package/dist/initProstgles.js +11 -15
- package/dist/initProstgles.js.map +1 -1
- package/dist/onSocketConnected.d.ts.map +1 -1
- package/dist/onSocketConnected.js +5 -5
- package/dist/onSocketConnected.js.map +1 -1
- package/dist/runClientRequest.d.ts +14 -29
- package/dist/runClientRequest.d.ts.map +1 -1
- package/dist/runClientRequest.js +20 -34
- package/dist/runClientRequest.js.map +1 -1
- package/lib/Auth/AuthHandler.ts +46 -103
- package/lib/Auth/AuthTypes.ts +19 -8
- package/lib/Auth/authProviders/setOAuthProviders.ts +1 -1
- package/lib/Auth/endpoints/getConfirmEmailRequestHandler.ts +1 -1
- package/lib/Auth/endpoints/getRegisterRequestHandler.ts +1 -1
- package/lib/Auth/setupAuthRoutes.ts +17 -13
- package/lib/Auth/utils/getUserFromRequest.ts +71 -0
- package/lib/DboBuilder/DboBuilder.ts +7 -3
- package/lib/DboBuilder/DboBuilderTypes.ts +19 -17
- package/lib/DboBuilder/QueryBuilder/getNewQuery.ts +2 -2
- package/lib/DboBuilder/QueryStreamer.ts +1 -1
- package/lib/DboBuilder/TableHandler/TableHandler.ts +2 -3
- package/lib/DboBuilder/TableHandler/insert.ts +2 -2
- package/lib/DboBuilder/TableHandler/update.ts +1 -1
- package/lib/DboBuilder/ViewHandler/ViewHandler.ts +38 -37
- package/lib/DboBuilder/ViewHandler/find.ts +1 -1
- package/lib/DboBuilder/ViewHandler/getExistsCondition.ts +4 -4
- package/lib/DboBuilder/ViewHandler/subscribe.ts +22 -41
- package/lib/DboBuilder/dboBuilderUtils.ts +3 -1
- package/lib/DboBuilder/insertNestedRecords.ts +18 -16
- package/lib/DboBuilder/runSQL.ts +14 -16
- package/lib/FileManager/initFileManager.ts +16 -12
- package/lib/Prostgles.ts +10 -24
- package/lib/ProstglesTypes.ts +9 -31
- package/lib/PubSubManager/PubSubManager.ts +3 -3
- package/lib/PubSubManager/addSync.ts +1 -3
- package/lib/PublishParser/PublishParser.ts +35 -45
- package/lib/PublishParser/getFileTableRules.ts +24 -48
- package/lib/PublishParser/getSchemaFromPublish.ts +12 -23
- package/lib/PublishParser/getTableRulesWithoutFileTable.ts +30 -41
- package/lib/PublishParser/publishTypesAndUtils.ts +8 -21
- package/lib/RestApi.ts +43 -31
- package/lib/initProstgles.ts +51 -64
- package/lib/onSocketConnected.ts +12 -9
- package/lib/runClientRequest.ts +50 -66
- package/package.json +3 -3
|
@@ -16,25 +16,20 @@ import {
|
|
|
16
16
|
|
|
17
17
|
export async function getTableRulesWithoutFileTable(
|
|
18
18
|
this: PublishParser,
|
|
19
|
-
{ tableName,
|
|
19
|
+
{ tableName, clientReq }: DboTable,
|
|
20
20
|
clientInfo?: AuthResult,
|
|
21
21
|
overridenPublish?: PublishObject
|
|
22
22
|
): Promise<ParsedPublishTable | undefined> {
|
|
23
|
-
|
|
24
|
-
if (!localParams || !tableName)
|
|
25
|
-
throw new Error("publish OR socket OR dbo OR tableName are missing");
|
|
23
|
+
if (!tableName) throw new Error("publish OR socket OR dbo OR tableName are missing");
|
|
26
24
|
|
|
27
|
-
const
|
|
25
|
+
const publish = overridenPublish ?? (clientReq && (await this.getPublish(clientReq, clientInfo)));
|
|
28
26
|
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
!raw_table_rules ||
|
|
32
|
-
(isObject(raw_table_rules) && Object.values(raw_table_rules).every((v) => !v))
|
|
33
|
-
) {
|
|
27
|
+
const rawTableRule = publish?.[tableName];
|
|
28
|
+
if (!rawTableRule || (isObject(rawTableRule) && Object.values(rawTableRule).every((v) => !v))) {
|
|
34
29
|
return undefined;
|
|
35
30
|
}
|
|
36
31
|
|
|
37
|
-
let
|
|
32
|
+
let parsedTableRule: ParsedPublishTable = {};
|
|
38
33
|
|
|
39
34
|
/* Get view or table specific rules */
|
|
40
35
|
const tHandler = this.dbo[tableName] as TableHandler | ViewHandler | undefined;
|
|
@@ -57,8 +52,8 @@ export async function getTableRulesWithoutFileTable(
|
|
|
57
52
|
|
|
58
53
|
if (
|
|
59
54
|
!pgUserIsAllowedThis &&
|
|
60
|
-
isObject(
|
|
61
|
-
(
|
|
55
|
+
isObject(rawTableRule) &&
|
|
56
|
+
(rawTableRule as PublishTableRule)[r.sqlRule]
|
|
62
57
|
) {
|
|
63
58
|
throw `Your postgres user is not allowed ${r.sqlRule} on table ${tableName}`;
|
|
64
59
|
}
|
|
@@ -79,19 +74,16 @@ export async function getTableRulesWithoutFileTable(
|
|
|
79
74
|
});
|
|
80
75
|
|
|
81
76
|
/* All methods allowed. Add no limits for table rules */
|
|
82
|
-
if ([true, "*"].includes(
|
|
83
|
-
|
|
77
|
+
if ([true, "*"].includes(rawTableRule as any)) {
|
|
78
|
+
parsedTableRule = {};
|
|
84
79
|
MY_RULES.filter((r) => r.no_limits).forEach((r) => {
|
|
85
|
-
|
|
80
|
+
parsedTableRule[r.rule] = { ...(r.no_limits as object) } as any;
|
|
86
81
|
});
|
|
87
82
|
|
|
88
83
|
/** Specific rules allowed */
|
|
89
|
-
} else if (isObject(
|
|
90
|
-
const allRuleKeys: (keyof PublishViewRule | keyof PublishTableRule)[] =
|
|
91
|
-
|
|
92
|
-
const dissallowedRuleKeys = allRuleKeys.filter(
|
|
93
|
-
(m) => !(raw_table_rules as PublishTableRule)[m]
|
|
94
|
-
);
|
|
84
|
+
} else if (isObject(rawTableRule) && getKeys(rawTableRule).length) {
|
|
85
|
+
const allRuleKeys: (keyof PublishViewRule | keyof PublishTableRule)[] = getKeys(rawTableRule);
|
|
86
|
+
const dissallowedRuleKeys = allRuleKeys.filter((m) => !(rawTableRule as PublishTableRule)[m]);
|
|
95
87
|
|
|
96
88
|
MY_RULES.map((r) => {
|
|
97
89
|
/** Unless specifically disabled these are allowed */
|
|
@@ -99,27 +91,24 @@ export async function getTableRulesWithoutFileTable(
|
|
|
99
91
|
["getInfo", "getColumns"].includes(r.rule) &&
|
|
100
92
|
!dissallowedRuleKeys.includes(r.rule as any)
|
|
101
93
|
) {
|
|
102
|
-
|
|
94
|
+
parsedTableRule[r.rule] = r.no_limits as any;
|
|
103
95
|
return;
|
|
104
96
|
}
|
|
105
97
|
|
|
106
98
|
/** Add no_limit values for implied/ fully allowed methods */
|
|
107
|
-
if (
|
|
108
|
-
[
|
|
109
|
-
r.no_limits
|
|
110
|
-
) {
|
|
111
|
-
parsed_table[r.rule] = Object.assign({}, r.no_limits) as any;
|
|
99
|
+
if ([true, "*"].includes((rawTableRule as PublishTableRule)[r.rule] as any) && r.no_limits) {
|
|
100
|
+
parsedTableRule[r.rule] = Object.assign({}, r.no_limits) as any;
|
|
112
101
|
|
|
113
102
|
/** Carry over detailed config */
|
|
114
|
-
} else if (isObject((
|
|
115
|
-
|
|
103
|
+
} else if (isObject((rawTableRule as any)[r.rule])) {
|
|
104
|
+
parsedTableRule[r.rule] = (rawTableRule as any)[r.rule];
|
|
116
105
|
}
|
|
117
106
|
});
|
|
118
107
|
|
|
119
108
|
allRuleKeys
|
|
120
|
-
.filter((m) =>
|
|
109
|
+
.filter((m) => parsedTableRule[m])
|
|
121
110
|
.forEach((method) => {
|
|
122
|
-
const rule =
|
|
111
|
+
const rule = parsedTableRule[method];
|
|
123
112
|
|
|
124
113
|
const ruleInfo = MY_RULES.find(
|
|
125
114
|
(r) => r.rule === method || (r.methods as readonly string[]).includes(method)
|
|
@@ -150,15 +139,15 @@ export async function getTableRulesWithoutFileTable(
|
|
|
150
139
|
|
|
151
140
|
/* Add default params (if missing) */
|
|
152
141
|
if (method === "sync") {
|
|
153
|
-
if ([true, "*"].includes(
|
|
142
|
+
if ([true, "*"].includes(parsedTableRule[method] as any)) {
|
|
154
143
|
throw "Invalid sync rule. Expecting { id_fields: string[], synced_field: string } ";
|
|
155
144
|
}
|
|
156
145
|
|
|
157
|
-
if (typeof
|
|
158
|
-
|
|
146
|
+
if (typeof parsedTableRule[method]?.throttle !== "number") {
|
|
147
|
+
parsedTableRule[method]!.throttle = 100;
|
|
159
148
|
}
|
|
160
|
-
if (typeof
|
|
161
|
-
|
|
149
|
+
if (typeof parsedTableRule[method]?.batch_size !== "number") {
|
|
150
|
+
parsedTableRule[method]!.batch_size = DEFAULT_SYNC_BATCH_SIZE;
|
|
162
151
|
}
|
|
163
152
|
}
|
|
164
153
|
|
|
@@ -168,8 +157,8 @@ export async function getTableRulesWithoutFileTable(
|
|
|
168
157
|
if (method === "select" && !dissallowedRuleKeys.includes(subKey)) {
|
|
169
158
|
const sr = MY_RULES.find((r) => r.rule === subKey);
|
|
170
159
|
if (sr && canSubscribe) {
|
|
171
|
-
|
|
172
|
-
|
|
160
|
+
parsedTableRule[subKey] = { ...(sr.no_limits as SubscribeRule) };
|
|
161
|
+
parsedTableRule.subscribeOne = { ...(sr.no_limits as SubscribeRule) };
|
|
173
162
|
}
|
|
174
163
|
}
|
|
175
164
|
});
|
|
@@ -203,7 +192,7 @@ export async function getTableRulesWithoutFileTable(
|
|
|
203
192
|
return res;
|
|
204
193
|
};
|
|
205
194
|
|
|
206
|
-
|
|
195
|
+
parsedTableRule = getImpliedMethods(parsedTableRule);
|
|
207
196
|
|
|
208
|
-
return
|
|
197
|
+
return parsedTableRule;
|
|
209
198
|
}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { AnyObject, DBSchema, FullFilter, Method, RULE_METHODS } from "prostgles-types";
|
|
2
2
|
import type { DBOFullyTyped, PublishFullyTyped } from "../DBSchemaBuilder";
|
|
3
|
-
import {
|
|
4
|
-
CommonTableRules,
|
|
5
|
-
Filter,
|
|
6
|
-
LocalParams,
|
|
7
|
-
PRGLIOSocket,
|
|
8
|
-
TableOrViewInfo,
|
|
9
|
-
} from "../DboBuilder/DboBuilder";
|
|
3
|
+
import { CommonTableRules, Filter, LocalParams, TableOrViewInfo } from "../DboBuilder/DboBuilder";
|
|
10
4
|
import { DB, DBHandlerServer } from "../Prostgles";
|
|
11
5
|
|
|
12
6
|
export type PublishMethods<S = void, SUser extends SessionUser = SessionUser> = (
|
|
@@ -15,20 +9,13 @@ export type PublishMethods<S = void, SUser extends SessionUser = SessionUser> =
|
|
|
15
9
|
|
|
16
10
|
export type Awaitable<T> = T | Promise<T>;
|
|
17
11
|
|
|
18
|
-
type
|
|
19
|
-
socket?: any;
|
|
20
|
-
httpReq?: any;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type DboTable = Request & {
|
|
12
|
+
export type DboTable = {
|
|
24
13
|
tableName: string;
|
|
25
|
-
|
|
14
|
+
clientReq: AuthClientRequest | undefined;
|
|
15
|
+
};
|
|
16
|
+
export type DboTableCommand = DboTable & {
|
|
17
|
+
command: string;
|
|
26
18
|
};
|
|
27
|
-
export type DboTableCommand = Request &
|
|
28
|
-
DboTable & {
|
|
29
|
-
command: string;
|
|
30
|
-
localParams: LocalParams;
|
|
31
|
-
};
|
|
32
19
|
|
|
33
20
|
export const RULE_TO_METHODS = [
|
|
34
21
|
{
|
|
@@ -147,8 +134,8 @@ export const RULE_TO_METHODS = [
|
|
|
147
134
|
] as const;
|
|
148
135
|
|
|
149
136
|
import { FieldFilter, SelectParams } from "prostgles-types";
|
|
137
|
+
import { AuthClientRequest, SessionUser } from "../Auth/AuthTypes";
|
|
150
138
|
import { TableSchemaColumn } from "../DboBuilder/DboBuilderTypes";
|
|
151
|
-
import { SessionUser } from "../Auth/AuthTypes";
|
|
152
139
|
|
|
153
140
|
export type InsertRequestData = {
|
|
154
141
|
data: object | object[];
|
|
@@ -449,7 +436,7 @@ export type PublishParams<S = void, SUser extends SessionUser = SessionUser> = {
|
|
|
449
436
|
dbo: DBOFullyTyped<S>;
|
|
450
437
|
db: DB;
|
|
451
438
|
user?: SUser["user"];
|
|
452
|
-
|
|
439
|
+
clientReq: AuthClientRequest;
|
|
453
440
|
tables: DbTableInfo[];
|
|
454
441
|
};
|
|
455
442
|
export type RequestParams = { dbo?: DBHandlerServer; socket?: any };
|
package/lib/RestApi.ts
CHANGED
|
@@ -58,14 +58,14 @@ export class RestApi {
|
|
|
58
58
|
schema: `${routePrefix}/schema`,
|
|
59
59
|
};
|
|
60
60
|
this.expressApp = expressApp;
|
|
61
|
-
expressApp.post(this.routes.db, jsonParser, this.
|
|
61
|
+
expressApp.post(this.routes.db, jsonParser, this.onPostTableCommand);
|
|
62
62
|
expressApp.post(this.routes.sql, jsonParser, this.onPostSql);
|
|
63
63
|
expressApp.post(this.routes.methods, jsonParser, this.onPostMethod);
|
|
64
64
|
expressApp.post(this.routes.schema, jsonParser, this.onPostSchema);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
destroy = () => {
|
|
68
|
-
this.expressApp.removeListener(this.routes.db, this.
|
|
68
|
+
this.expressApp.removeListener(this.routes.db, this.onPostTableCommand);
|
|
69
69
|
this.expressApp.removeListener(this.routes.sql, this.onPostSql);
|
|
70
70
|
this.expressApp.removeListener(this.routes.methods, this.onPostMethod);
|
|
71
71
|
};
|
|
@@ -74,54 +74,62 @@ export class RestApi {
|
|
|
74
74
|
const params = req.body || [];
|
|
75
75
|
|
|
76
76
|
try {
|
|
77
|
-
const data = await runClientMethod.bind(this.prostgles)(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
const data = await runClientMethod.bind(this.prostgles)(
|
|
78
|
+
{
|
|
79
|
+
method,
|
|
80
|
+
params,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
res,
|
|
84
|
+
httpReq: req,
|
|
85
|
+
}
|
|
86
|
+
);
|
|
83
87
|
res.json(data);
|
|
84
88
|
} catch (rawError) {
|
|
85
89
|
const error = getSerializedClientErrorFromPGError(rawError, {
|
|
86
90
|
type: "method",
|
|
87
|
-
localParams: { httpReq: req },
|
|
91
|
+
localParams: { clientReq: { httpReq: req, res } },
|
|
88
92
|
});
|
|
89
93
|
res.status(400).json({ error });
|
|
90
94
|
}
|
|
91
95
|
};
|
|
92
96
|
onPostSchema = async (req: ExpressReq, res: ExpressRes) => {
|
|
93
97
|
try {
|
|
94
|
-
const data = await this.prostgles.getClientSchema({ httpReq: req });
|
|
98
|
+
const data = await this.prostgles.getClientSchema({ httpReq: req, res });
|
|
95
99
|
res.json(data);
|
|
96
100
|
} catch (rawError) {
|
|
97
101
|
const error = getSerializedClientErrorFromPGError(rawError, {
|
|
98
102
|
type: "method",
|
|
99
|
-
localParams: { httpReq: req },
|
|
103
|
+
localParams: { clientReq: { httpReq: req, res } },
|
|
100
104
|
});
|
|
101
105
|
res.status(400).json({ error });
|
|
102
106
|
}
|
|
103
107
|
};
|
|
104
108
|
onPostSql = async (req: ExpressReq, res: ExpressRes) => {
|
|
105
|
-
const [query,
|
|
109
|
+
const [query, params, options] = req.body || [];
|
|
106
110
|
try {
|
|
107
|
-
const data = await runClientSqlRequest.bind(this.prostgles)(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
const data = await runClientSqlRequest.bind(this.prostgles)(
|
|
112
|
+
{
|
|
113
|
+
query,
|
|
114
|
+
params,
|
|
115
|
+
options,
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
res,
|
|
119
|
+
httpReq: req,
|
|
120
|
+
}
|
|
121
|
+
);
|
|
114
122
|
res.json(data);
|
|
115
123
|
} catch (rawError) {
|
|
116
124
|
const error = getSerializedClientErrorFromPGError(rawError, {
|
|
117
125
|
type: "sql",
|
|
118
|
-
localParams: { httpReq: req },
|
|
126
|
+
localParams: { clientReq: { httpReq: req, res } },
|
|
119
127
|
});
|
|
120
128
|
res.status(400).json({ error });
|
|
121
129
|
}
|
|
122
130
|
};
|
|
123
131
|
|
|
124
|
-
|
|
132
|
+
onPostTableCommand = async (req: ExpressReq, res: ExpressRes) => {
|
|
125
133
|
const { params } = req;
|
|
126
134
|
const { tableName, command } = params;
|
|
127
135
|
if (!tableName || typeof tableName !== "string") {
|
|
@@ -135,20 +143,24 @@ export class RestApi {
|
|
|
135
143
|
|
|
136
144
|
try {
|
|
137
145
|
const [param1, param2, param3] = req.body || [];
|
|
138
|
-
const data = await runClientRequest.bind(this.prostgles)(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
146
|
+
const data = await runClientRequest.bind(this.prostgles)(
|
|
147
|
+
{
|
|
148
|
+
tableName,
|
|
149
|
+
command,
|
|
150
|
+
param1,
|
|
151
|
+
param2,
|
|
152
|
+
param3,
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
httpReq: req,
|
|
156
|
+
res,
|
|
157
|
+
}
|
|
158
|
+
);
|
|
147
159
|
res.json(data);
|
|
148
160
|
} catch (rawError) {
|
|
149
161
|
const error = getSerializedClientErrorFromPGError(rawError, {
|
|
150
162
|
type: "tableMethod",
|
|
151
|
-
localParams: { httpReq: req },
|
|
163
|
+
localParams: { clientReq: { httpReq: req, res } },
|
|
152
164
|
view: this.prostgles.dboBuilder.dbo[tableName],
|
|
153
165
|
});
|
|
154
166
|
res.status(400).json({ error });
|
package/lib/initProstgles.ts
CHANGED
|
@@ -70,14 +70,12 @@ export type InitResult = {
|
|
|
70
70
|
options: ProstglesInitOptions;
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
-
const clientOnlyUpdateKeys = [
|
|
74
|
-
"auth",
|
|
75
|
-
] as const satisfies (keyof UpdateableOptions)[];
|
|
73
|
+
const clientOnlyUpdateKeys = ["auth"] as const satisfies (keyof UpdateableOptions)[];
|
|
76
74
|
|
|
77
75
|
export const initProstgles = async function (
|
|
78
76
|
this: Prostgles,
|
|
79
77
|
onReady: OnReadyCallbackBasic,
|
|
80
|
-
reason: OnInitReason
|
|
78
|
+
reason: OnInitReason
|
|
81
79
|
): Promise<InitResult> {
|
|
82
80
|
this.loaded = false;
|
|
83
81
|
|
|
@@ -96,16 +94,14 @@ export const initProstgles = async function (
|
|
|
96
94
|
try {
|
|
97
95
|
const url = new URL(connString);
|
|
98
96
|
existingAppName =
|
|
99
|
-
url.searchParams.get("application_name") ??
|
|
100
|
-
url.searchParams.get("ApplicationName") ??
|
|
101
|
-
"";
|
|
97
|
+
url.searchParams.get("application_name") ?? url.searchParams.get("ApplicationName") ?? "";
|
|
102
98
|
} catch (e) {}
|
|
103
99
|
}
|
|
104
100
|
|
|
105
101
|
const conObj =
|
|
106
|
-
typeof this.opts.dbConnection === "string"
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
typeof this.opts.dbConnection === "string" ?
|
|
103
|
+
{ connectionString: this.opts.dbConnection }
|
|
104
|
+
: this.opts.dbConnection;
|
|
109
105
|
const application_name = `prostgles ${this.appId} ${existingAppName}`;
|
|
110
106
|
|
|
111
107
|
/* 1. Connect to db */
|
|
@@ -149,14 +145,7 @@ export const initProstgles = async function (
|
|
|
149
145
|
/* 3.9 Check auth config */
|
|
150
146
|
await this.initAuthHandler();
|
|
151
147
|
|
|
152
|
-
this.publishParser = new PublishParser(
|
|
153
|
-
this.opts.publish,
|
|
154
|
-
this.opts.publishMethods as any,
|
|
155
|
-
this.opts.publishRawSQL,
|
|
156
|
-
this.dbo!,
|
|
157
|
-
this.db,
|
|
158
|
-
this as any,
|
|
159
|
-
);
|
|
148
|
+
this.publishParser = new PublishParser(this);
|
|
160
149
|
this.dboBuilder.publishParser = this.publishParser;
|
|
161
150
|
|
|
162
151
|
/* 4. Set publish and auth listeners */
|
|
@@ -221,9 +210,7 @@ export const initProstgles = async function (
|
|
|
221
210
|
* While others also affect the server and onReady should be called
|
|
222
211
|
*/
|
|
223
212
|
if (
|
|
224
|
-
getKeys(newOpts).every((updatedKey) =>
|
|
225
|
-
clientOnlyUpdateKeys.includes(updatedKey as any),
|
|
226
|
-
)
|
|
213
|
+
getKeys(newOpts).every((updatedKey) => clientOnlyUpdateKeys.includes(updatedKey as any))
|
|
227
214
|
) {
|
|
228
215
|
await this.setSocketEvents();
|
|
229
216
|
} else {
|
|
@@ -279,52 +266,52 @@ const getDbConnection = function ({
|
|
|
279
266
|
const onQueryOrError:
|
|
280
267
|
| undefined
|
|
281
268
|
| ((error: any, ctx: pgPromise.IEventContext<pg.IClient>) => void) =
|
|
282
|
-
!onQuery && !DEBUG_MODE
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
269
|
+
!onQuery && !DEBUG_MODE ?
|
|
270
|
+
undefined
|
|
271
|
+
: (error, ctx) => {
|
|
272
|
+
if (onQuery) {
|
|
273
|
+
onQuery(error, ctx);
|
|
274
|
+
} else if (DEBUG_MODE) {
|
|
275
|
+
if (error) {
|
|
276
|
+
console.error(error, ctx);
|
|
277
|
+
} else {
|
|
278
|
+
console.log(ctx);
|
|
293
279
|
}
|
|
294
|
-
}
|
|
280
|
+
}
|
|
281
|
+
};
|
|
295
282
|
|
|
296
283
|
const pgp: PGP = pgPromise({
|
|
297
|
-
...(onQueryOrError
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
...(onNotice || DEBUG_MODE
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
284
|
+
...(onQueryOrError ?
|
|
285
|
+
{
|
|
286
|
+
query: (ctx) => onQueryOrError(undefined, ctx),
|
|
287
|
+
error: onQueryOrError,
|
|
288
|
+
}
|
|
289
|
+
: {}),
|
|
290
|
+
...(onNotice || DEBUG_MODE ?
|
|
291
|
+
{
|
|
292
|
+
connect: function ({ client, useCount }) {
|
|
293
|
+
const isFresh = !useCount;
|
|
294
|
+
if (isFresh && !client.listeners("notice").length) {
|
|
295
|
+
client.on("notice", function (msg) {
|
|
296
|
+
if (onNotice) {
|
|
297
|
+
onNotice(msg, msg?.message);
|
|
298
|
+
} else {
|
|
299
|
+
console.log("notice: %j", msg?.message);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if (isFresh && !client.listeners("error").length) {
|
|
304
|
+
client.on("error", function (msg) {
|
|
305
|
+
if (onNotice) {
|
|
306
|
+
onNotice(msg, msg?.message);
|
|
307
|
+
} else {
|
|
308
|
+
console.log("error: %j", msg?.message);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
}
|
|
314
|
+
: {}),
|
|
328
315
|
});
|
|
329
316
|
// pgp.pg.defaults.max = 70;
|
|
330
317
|
|
package/lib/onSocketConnected.ts
CHANGED
|
@@ -26,7 +26,7 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
26
26
|
if (this.opts.onSocketConnect) {
|
|
27
27
|
try {
|
|
28
28
|
const getUser = async () => {
|
|
29
|
-
return await this.authHandler?.
|
|
29
|
+
return await this.authHandler?.getUserFromRequest({ socket });
|
|
30
30
|
};
|
|
31
31
|
await this.opts.onSocketConnect({
|
|
32
32
|
socket,
|
|
@@ -55,7 +55,7 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
55
55
|
}
|
|
56
56
|
) => {
|
|
57
57
|
runClientRequest
|
|
58
|
-
.bind(this)(
|
|
58
|
+
.bind(this)(args, { socket })
|
|
59
59
|
.then((res) => {
|
|
60
60
|
cb(null, res);
|
|
61
61
|
})
|
|
@@ -79,7 +79,7 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
79
79
|
|
|
80
80
|
if (this.opts.onSocketDisconnect) {
|
|
81
81
|
const getUser = async () => {
|
|
82
|
-
return await this.authHandler?.
|
|
82
|
+
return await this.authHandler?.getUserFromRequest({ socket });
|
|
83
83
|
};
|
|
84
84
|
this.opts.onSocketDisconnect({ socket, dbo: dbo as any, db, getUser });
|
|
85
85
|
}
|
|
@@ -95,12 +95,15 @@ export async function onSocketConnected(this: Prostgles, socket: PRGLIOSocket) {
|
|
|
95
95
|
}
|
|
96
96
|
) => {
|
|
97
97
|
runClientMethod
|
|
98
|
-
.bind(this)(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
.bind(this)(
|
|
99
|
+
{
|
|
100
|
+
method,
|
|
101
|
+
params,
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
socket,
|
|
105
|
+
}
|
|
106
|
+
)
|
|
104
107
|
.then((res) => {
|
|
105
108
|
cb(null, res);
|
|
106
109
|
})
|