prostgles-server 3.0.86 → 3.0.88
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/.eslintignore +5 -0
- package/.eslintrc.json +30 -0
- package/dist/DBEventsManager.js +1 -1
- package/dist/DBEventsManager.js.map +1 -1
- package/dist/DBSchemaBuilder.d.ts.map +1 -1
- package/dist/DBSchemaBuilder.js +6 -6
- package/dist/DBSchemaBuilder.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/Functions.js +9 -9
- package/dist/DboBuilder/QueryBuilder/Functions.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.js +8 -7
- package/dist/DboBuilder/QueryBuilder/QueryBuilder.js.map +1 -1
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.d.ts.map +1 -1
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.js +4 -4
- package/dist/DboBuilder/QueryBuilder/makeSelectQuery.js.map +1 -1
- package/dist/DboBuilder/TableHandler.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler.js +18 -20
- package/dist/DboBuilder/TableHandler.js.map +1 -1
- package/dist/DboBuilder/ViewHandler.d.ts +0 -10
- package/dist/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/dist/DboBuilder/ViewHandler.js +50 -63
- package/dist/DboBuilder/ViewHandler.js.map +1 -1
- package/dist/DboBuilder/delete.js +0 -1
- package/dist/DboBuilder/delete.js.map +1 -1
- package/dist/DboBuilder/getColumns.js +5 -4
- package/dist/DboBuilder/getColumns.js.map +1 -1
- package/dist/DboBuilder/getCondition.d.ts.map +1 -1
- package/dist/DboBuilder/getCondition.js +6 -6
- package/dist/DboBuilder/getCondition.js.map +1 -1
- package/dist/DboBuilder/insert.js +15 -16
- package/dist/DboBuilder/insert.js.map +1 -1
- package/dist/DboBuilder/insertDataParse.d.ts.map +1 -1
- package/dist/DboBuilder/insertDataParse.js +9 -10
- package/dist/DboBuilder/insertDataParse.js.map +1 -1
- package/dist/DboBuilder/parseUpdateRules.js +2 -2
- package/dist/DboBuilder/parseUpdateRules.js.map +1 -1
- package/dist/DboBuilder/runSQL.d.ts.map +1 -1
- package/dist/DboBuilder/runSQL.js +5 -5
- package/dist/DboBuilder/runSQL.js.map +1 -1
- package/dist/DboBuilder/subscribe.js +3 -3
- package/dist/DboBuilder/subscribe.js.map +1 -1
- package/dist/DboBuilder/update.js +5 -6
- package/dist/DboBuilder/update.js.map +1 -1
- package/dist/DboBuilder/uploadFile.d.ts.map +1 -1
- package/dist/DboBuilder/uploadFile.js +1 -1
- package/dist/DboBuilder/uploadFile.js.map +1 -1
- package/dist/DboBuilder.d.ts.map +1 -1
- package/dist/DboBuilder.js +13 -14
- package/dist/DboBuilder.js.map +1 -1
- package/dist/FileManager.d.ts.map +1 -1
- package/dist/FileManager.js +3 -5
- package/dist/FileManager.js.map +1 -1
- package/dist/Filtering.js +7 -7
- package/dist/Filtering.js.map +1 -1
- package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts +3 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js +295 -0
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -0
- package/dist/JSONBValidation/validation.d.ts +108 -0
- package/dist/JSONBValidation/validation.d.ts.map +1 -0
- package/dist/JSONBValidation/validation.js +222 -0
- package/dist/JSONBValidation/validation.js.map +1 -0
- package/dist/PostgresNotifListenManager.js +1 -1
- package/dist/PostgresNotifListenManager.js.map +1 -1
- package/dist/Prostgles.d.ts.map +1 -1
- package/dist/Prostgles.js +20 -20
- package/dist/Prostgles.js.map +1 -1
- package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/PubSubManager.js +10 -8
- package/dist/PubSubManager/PubSubManager.js.map +1 -1
- package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/dist/PubSubManager/initPubSubManager.js +10 -7
- package/dist/PubSubManager/initPubSubManager.js.map +1 -1
- package/dist/PublishParser.d.ts.map +1 -1
- package/dist/PublishParser.js +122 -125
- package/dist/PublishParser.js.map +1 -1
- package/dist/SyncReplication.d.ts.map +1 -1
- package/dist/SyncReplication.js +19 -16
- package/dist/SyncReplication.js.map +1 -1
- package/dist/TableConfig.d.ts +9 -5
- package/dist/TableConfig.d.ts.map +1 -1
- package/dist/TableConfig.js +33 -12
- package/dist/TableConfig.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/shortestPath.js +11 -11
- package/dist/shortestPath.js.map +1 -1
- package/dist/validation.d.ts +50 -24
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +177 -53
- package/dist/validation.js.map +1 -1
- package/lib/AuthHandler.d.ts +11 -11
- package/lib/AuthHandler.d.ts.map +1 -1
- package/lib/DBEventsManager.js +1 -1
- package/lib/DBEventsManager.ts +1 -1
- package/lib/DBSchemaBuilder.d.ts +3 -3
- package/lib/DBSchemaBuilder.d.ts.map +1 -1
- package/lib/DBSchemaBuilder.js +6 -6
- package/lib/DBSchemaBuilder.ts +10 -9
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts +3 -3
- package/lib/DboBuilder/QueryBuilder/Functions.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/Functions.js +9 -9
- package/lib/DboBuilder/QueryBuilder/Functions.ts +13 -13
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts +3 -3
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.js +8 -7
- package/lib/DboBuilder/QueryBuilder/QueryBuilder.ts +12 -12
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.d.ts.map +1 -1
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.js +4 -4
- package/lib/DboBuilder/QueryBuilder/makeSelectQuery.ts +5 -5
- package/lib/DboBuilder/TableHandler.d.ts +1 -1
- package/lib/DboBuilder/TableHandler.d.ts.map +1 -1
- package/lib/DboBuilder/TableHandler.js +18 -20
- package/lib/DboBuilder/TableHandler.ts +21 -20
- package/lib/DboBuilder/ViewHandler.d.ts +1 -11
- package/lib/DboBuilder/ViewHandler.d.ts.map +1 -1
- package/lib/DboBuilder/ViewHandler.js +50 -63
- package/lib/DboBuilder/ViewHandler.ts +68 -97
- package/lib/DboBuilder/delete.js +0 -1
- package/lib/DboBuilder/delete.ts +1 -1
- package/lib/DboBuilder/getColumns.js +5 -4
- package/lib/DboBuilder/getColumns.ts +5 -5
- package/lib/DboBuilder/getCondition.d.ts.map +1 -1
- package/lib/DboBuilder/getCondition.js +6 -6
- package/lib/DboBuilder/getCondition.ts +7 -7
- package/lib/DboBuilder/insert.js +15 -16
- package/lib/DboBuilder/insert.ts +18 -18
- package/lib/DboBuilder/insertDataParse.d.ts.map +1 -1
- package/lib/DboBuilder/insertDataParse.js +9 -10
- package/lib/DboBuilder/insertDataParse.ts +42 -43
- package/lib/DboBuilder/parseUpdateRules.js +2 -2
- package/lib/DboBuilder/parseUpdateRules.ts +2 -2
- package/lib/DboBuilder/runSQL.d.ts.map +1 -1
- package/lib/DboBuilder/runSQL.js +5 -5
- package/lib/DboBuilder/runSQL.ts +6 -6
- package/lib/DboBuilder/subscribe.d.ts +1 -1
- package/lib/DboBuilder/subscribe.d.ts.map +1 -1
- package/lib/DboBuilder/subscribe.js +3 -3
- package/lib/DboBuilder/subscribe.ts +3 -3
- package/lib/DboBuilder/update.js +5 -6
- package/lib/DboBuilder/update.ts +6 -6
- package/lib/DboBuilder/uploadFile.d.ts.map +1 -1
- package/lib/DboBuilder/uploadFile.js +1 -1
- package/lib/DboBuilder/uploadFile.ts +2 -2
- package/lib/DboBuilder.d.ts +22 -22
- package/lib/DboBuilder.d.ts.map +1 -1
- package/lib/DboBuilder.js +13 -14
- package/lib/DboBuilder.ts +19 -19
- package/lib/FileManager.d.ts +6 -6
- package/lib/FileManager.d.ts.map +1 -1
- package/lib/FileManager.js +3 -5
- package/lib/FileManager.ts +7 -6
- package/lib/Filtering.d.ts +1 -1
- package/lib/Filtering.d.ts.map +1 -1
- package/lib/Filtering.js +7 -7
- package/lib/Filtering.ts +7 -7
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts +3 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.js +294 -0
- package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +293 -0
- package/lib/JSONBValidation/validation.d.ts +108 -0
- package/lib/JSONBValidation/validation.d.ts.map +1 -0
- package/lib/JSONBValidation/validation.js +221 -0
- package/lib/JSONBValidation/validation.ts +332 -0
- package/lib/PostgresNotifListenManager.d.ts +1 -1
- package/lib/PostgresNotifListenManager.d.ts.map +1 -1
- package/lib/PostgresNotifListenManager.js +1 -1
- package/lib/PostgresNotifListenManager.ts +1 -1
- package/lib/Prostgles.d.ts +14 -14
- package/lib/Prostgles.d.ts.map +1 -1
- package/lib/Prostgles.js +20 -19
- package/lib/Prostgles.ts +22 -21
- package/lib/PubSubManager/PubSubManager.d.ts +7 -7
- package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/PubSubManager.js +10 -8
- package/lib/PubSubManager/PubSubManager.ts +13 -8
- package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -1
- package/lib/PubSubManager/initPubSubManager.js +10 -7
- package/lib/PubSubManager/initPubSubManager.ts +12 -7
- package/lib/PublishParser.d.ts +32 -32
- package/lib/PublishParser.d.ts.map +1 -1
- package/lib/PublishParser.js +121 -124
- package/lib/PublishParser.ts +125 -127
- package/lib/SchemaWatch.d.ts +1 -1
- package/lib/SchemaWatch.d.ts.map +1 -1
- package/lib/SyncReplication.d.ts +5 -5
- package/lib/SyncReplication.d.ts.map +1 -1
- package/lib/SyncReplication.js +19 -16
- package/lib/SyncReplication.ts +470 -471
- package/lib/TableConfig.d.ts +28 -24
- package/lib/TableConfig.d.ts.map +1 -1
- package/lib/TableConfig.js +33 -12
- package/lib/TableConfig.ts +55 -21
- package/lib/index.js +1 -1
- package/lib/index.ts +1 -1
- package/lib/shortestPath.d.ts +1 -1
- package/lib/shortestPath.d.ts.map +1 -1
- package/lib/shortestPath.js +11 -11
- package/lib/shortestPath.ts +11 -11
- package/package.json +10 -6
- package/tests/client/PID.txt +1 -1
- package/tests/client/package-lock.json +53 -31
- package/tests/client/package.json +4 -1
- package/tests/isomorphic_queries.d.ts.map +1 -1
- package/tests/isomorphic_queries.js +28 -26
- package/tests/isomorphic_queries.ts +30 -29
- package/tests/server/DBoGenerated.d.ts +1 -1
- package/tests/server/index.js +8 -7
- package/tests/server/index.ts +10 -8
- package/tests/server/package-lock.json +76 -58
- package/tests/server/package.json +2 -2
- package/tests/server/server.ts +2 -3
- package/lib/validation.d.ts +0 -100
- package/lib/validation.d.ts.map +0 -1
- package/lib/validation.js +0 -280
- package/lib/validation.ts +0 -360
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { JSONSchema7, JSONSchema7TypeName } from "json-schema";
|
|
2
|
+
import { getKeys, isEmpty, isObject } from "prostgles-types";
|
|
3
|
+
import { asValue } from "../PubSubManager/PubSubManager";
|
|
4
|
+
import { BaseColumn, JSONBColumnDef, StrictUnion } from "../TableConfig";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const PrimitiveTypes = ["boolean" , "number", "integer", "string", "any"] as const;
|
|
8
|
+
const DATA_TYPES = [
|
|
9
|
+
...PrimitiveTypes,
|
|
10
|
+
...PrimitiveTypes.map(v => `${v}[]` as `${typeof v}[]`)
|
|
11
|
+
] as const;
|
|
12
|
+
type DataType = typeof DATA_TYPES[number];
|
|
13
|
+
|
|
14
|
+
export namespace JSONB {
|
|
15
|
+
|
|
16
|
+
export type BaseOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* False by default
|
|
19
|
+
*/
|
|
20
|
+
optional?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* False by default
|
|
23
|
+
*/
|
|
24
|
+
nullable?: boolean;
|
|
25
|
+
description?: string;
|
|
26
|
+
title?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type BasicType = BaseOptions & {
|
|
30
|
+
type: DataType;
|
|
31
|
+
allowedValues?: any[];
|
|
32
|
+
oneOf?: undefined;
|
|
33
|
+
arrayOf?: undefined;
|
|
34
|
+
enum?: undefined;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ObjectType = BaseOptions & {
|
|
38
|
+
type: ObjectSchema;
|
|
39
|
+
allowedValues?: undefined;
|
|
40
|
+
oneOf?: undefined;
|
|
41
|
+
arrayOf?: undefined;
|
|
42
|
+
enum?: undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type EnumType = BaseOptions & {
|
|
46
|
+
type?: undefined;
|
|
47
|
+
enum: readonly any[];
|
|
48
|
+
oneOf?: undefined;
|
|
49
|
+
arrayOf?: undefined;
|
|
50
|
+
allowedValues?: undefined;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type OneOf = BaseOptions & {
|
|
54
|
+
type?: undefined;
|
|
55
|
+
oneOf: readonly ObjectSchema[];
|
|
56
|
+
arrayOf?: undefined;
|
|
57
|
+
allowedValues?: undefined;
|
|
58
|
+
enum?: undefined;
|
|
59
|
+
}
|
|
60
|
+
export type ArrayOf = BaseOptions & {
|
|
61
|
+
type?: undefined;
|
|
62
|
+
arrayOf: ObjectSchema;
|
|
63
|
+
allowedValues?: undefined;
|
|
64
|
+
oneOf?: undefined;
|
|
65
|
+
enum?: undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export type FieldTypeObj =
|
|
70
|
+
| BasicType
|
|
71
|
+
| ObjectType
|
|
72
|
+
| EnumType
|
|
73
|
+
| OneOf
|
|
74
|
+
| ArrayOf;
|
|
75
|
+
|
|
76
|
+
export type FieldType =
|
|
77
|
+
| DataType
|
|
78
|
+
| FieldTypeObj;
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
export type GetType<T extends FieldType | Omit<FieldTypeObj, "optional">> =
|
|
82
|
+
| T extends { type: ObjectSchema } ? NestedSchemaObject<T["type"]> :
|
|
83
|
+
| T extends "number" | { type: "number" } ? number :
|
|
84
|
+
| T extends "boolean" | { type: "boolean" } ? boolean :
|
|
85
|
+
| T extends "integer" | { type: "integer" } ? number :
|
|
86
|
+
| T extends "string" | { type: "string" } ? string :
|
|
87
|
+
| T extends "any" | { type: "any" } ? any :
|
|
88
|
+
| T extends "number[]" | { type: "number[]" } ? number[] :
|
|
89
|
+
| T extends "boolean[]" | { type: "boolean[]" } ? boolean[] :
|
|
90
|
+
| T extends "integer[]" | { type: "integer[]" } ? number[] :
|
|
91
|
+
| T extends "string[]" | { type: "string[]" } ? string[] :
|
|
92
|
+
| T extends "any[]" | { type: "any[]" } ? any[] :
|
|
93
|
+
| T extends { enum: readonly any[] } ? T["enum"][number] :
|
|
94
|
+
|
|
95
|
+
| T extends { oneOf: readonly ObjectSchema[] } ? StrictUnion<NestedSchemaObject<T["oneOf"][number]>> :
|
|
96
|
+
|
|
97
|
+
| T extends { arrayOf: ObjectSchema } ? NestedSchemaObject<T["arrayOf"]>[] :
|
|
98
|
+
any;
|
|
99
|
+
|
|
100
|
+
type IsOptional<F extends FieldType> = F extends DataType? false : F extends { optional: true }? true : false;
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
export type ObjectSchema = Record<string, FieldType>;
|
|
104
|
+
export type JSONBSchema = Omit<FieldTypeObj, "optional">;
|
|
105
|
+
|
|
106
|
+
export type NestedSchemaObject<S extends ObjectSchema> = (
|
|
107
|
+
{
|
|
108
|
+
[K in keyof S as IsOptional<S[K]> extends true ? K : never]?: GetType<S[K]>
|
|
109
|
+
} & {
|
|
110
|
+
[K in keyof S as IsOptional<S[K]> extends true ? never : K]: GetType<S[K]>
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
export type SchemaObject<S extends JSONBSchema> = S["nullable"] extends true? (null | GetType<S>) : GetType<S>;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
/** tests */
|
|
118
|
+
const s: JSONB.JSONBSchema = {
|
|
119
|
+
type: {
|
|
120
|
+
a: { type: "boolean" },
|
|
121
|
+
c: { type: { c1: { type: "string" } } },
|
|
122
|
+
arr: { arrayOf: { d: "string" } },
|
|
123
|
+
o: {
|
|
124
|
+
oneOf: [
|
|
125
|
+
{ z: { type: "integer" } },
|
|
126
|
+
{ z1: { type: "integer" } }
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const ss: JSONB.SchemaObject<typeof s> = {
|
|
133
|
+
a: true,
|
|
134
|
+
arr: [{ d: "" }],
|
|
135
|
+
c: {
|
|
136
|
+
c1: ""
|
|
137
|
+
},
|
|
138
|
+
o: { z1: 23 }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const getFieldTypeObj = (rawFieldType: JSONB.FieldType): JSONB.FieldTypeObj => {
|
|
142
|
+
if(typeof rawFieldType === "string") return { type: rawFieldType };
|
|
143
|
+
|
|
144
|
+
return rawFieldType;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function validate<T>(obj: T, key: keyof T, rawFieldType: JSONB.FieldType): boolean {
|
|
148
|
+
let err = `The provided value for ${JSON.stringify(key)} is of invalid type. Expecting `;
|
|
149
|
+
const val = obj[key];
|
|
150
|
+
const fieldType = getFieldTypeObj(rawFieldType);
|
|
151
|
+
if ("type" in fieldType && fieldType.type) {
|
|
152
|
+
if (typeof fieldType.type !== "string") {
|
|
153
|
+
getKeys(fieldType.type).forEach(subKey => {
|
|
154
|
+
validate(val, subKey as any, (fieldType.type as JSONB.ObjectSchema)[subKey])
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
err += fieldType.type;
|
|
158
|
+
if (fieldType.type === "boolean" && typeof val !== fieldType.type) throw new Error(err)
|
|
159
|
+
if (fieldType.type === "string" && typeof val !== fieldType.type) throw new Error(err)
|
|
160
|
+
if (fieldType.type === "number" && !Number.isFinite(val)) throw new Error(err)
|
|
161
|
+
if (fieldType.type === "integer" && !Number.isInteger(val)) throw new Error(err);
|
|
162
|
+
|
|
163
|
+
} else if (fieldType.enum) {
|
|
164
|
+
err += `on of: ${fieldType.enum}`;
|
|
165
|
+
if (!fieldType.enum.includes(val)) throw new Error(err)
|
|
166
|
+
}
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function validateSchema<S extends JSONB.ObjectSchema>(schema: S, obj: JSONB.NestedSchemaObject<S>, objName?: string, optional = false) {
|
|
171
|
+
if ((!schema || isEmpty(schema)) && !optional) throw new Error(`Expecting ${objName} to be defined`);
|
|
172
|
+
getKeys(schema).forEach(k => validate(obj as any, k, schema[k]));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
type ColOpts = { nullable?: boolean };
|
|
177
|
+
|
|
178
|
+
export function getSchemaTSTypes(schema: JSONB.ObjectSchema, leading = "", isOneOf = false): string {
|
|
179
|
+
const getFieldType = (rawFieldType: JSONB.FieldType) => {
|
|
180
|
+
const fieldType = getFieldTypeObj(rawFieldType);
|
|
181
|
+
const nullType = (fieldType.nullable ? `null | ` : "");
|
|
182
|
+
if (fieldType?.type) {
|
|
183
|
+
if (typeof fieldType.type === "string") {
|
|
184
|
+
const correctType = fieldType.type.replace("integer", "number");
|
|
185
|
+
if (fieldType.allowedValues && fieldType.type.endsWith("[]")) {
|
|
186
|
+
return nullType + ` (${fieldType.allowedValues.map(v => JSON.stringify(v)).join(" | ")})[]`
|
|
187
|
+
}
|
|
188
|
+
return nullType + correctType
|
|
189
|
+
} else {
|
|
190
|
+
return nullType + getSchemaTSTypes(fieldType.type, "", true)
|
|
191
|
+
}
|
|
192
|
+
} else if (fieldType?.enum) {
|
|
193
|
+
return nullType + fieldType.enum.map(v => asValue(v)).join(" | ")
|
|
194
|
+
} else if (fieldType?.oneOf) {
|
|
195
|
+
return (fieldType.nullable ? `\n${leading} | null` : "") + fieldType.oneOf.map(v => `\n${leading} | ` + getSchemaTSTypes(v, "", true)).join("")
|
|
196
|
+
} else if (fieldType?.arrayOf) {
|
|
197
|
+
return (fieldType.nullable ? `\n${leading} | null` : "") + getSchemaTSTypes(fieldType.arrayOf, "", true) + "[]";
|
|
198
|
+
} else throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const spacing = isOneOf ? " " : " ";
|
|
202
|
+
|
|
203
|
+
const res = `${leading}{ \n` + getKeys(schema).map(k => {
|
|
204
|
+
const fieldType = getFieldTypeObj(schema[k]);
|
|
205
|
+
return `${leading}${spacing}${k}${fieldType.optional ? "?" : ""}: ` + getFieldType(fieldType) + ";";
|
|
206
|
+
}).join("\n") + ` \n${leading}}${isOneOf ? "" : ";"}`;
|
|
207
|
+
|
|
208
|
+
/** Keep single line */
|
|
209
|
+
if (isOneOf) return res.split("\n").join("");
|
|
210
|
+
|
|
211
|
+
return res;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, leading = "", isOneOf = false): string {
|
|
215
|
+
if (schema.arrayOf) {
|
|
216
|
+
return (colOpts.nullable ? `\n${leading} | null` : "") + getSchemaTSTypes(schema.arrayOf, leading, isOneOf) + "[]";
|
|
217
|
+
} else if (schema.enum) {
|
|
218
|
+
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.enum.map(v => asValue(v)).join(" | ")
|
|
219
|
+
} else if (schema.oneOf) {
|
|
220
|
+
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.oneOf.map(s => `\n${leading} | ` + getSchemaTSTypes(s, "", true)).join("")
|
|
221
|
+
} else {
|
|
222
|
+
if(typeof schema.type === "string"){
|
|
223
|
+
return (colOpts.nullable ? `null | ` : "") + schema.type;
|
|
224
|
+
} else if(schema.type){
|
|
225
|
+
return (colOpts.nullable ? `null | ` : "") + getSchemaTSTypes(schema.type, leading, isOneOf);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return "";
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
const getJSONSchemaObject = (rawType: JSONB.FieldType | JSONB.JSONBSchema, rootInfo?: { id: string }): JSONSchema7 => {
|
|
234
|
+
const { type, arrayOf, description, nullable, oneOf, title, ...t } =
|
|
235
|
+
typeof rawType === "string"? ({ type: rawType } satisfies JSONB.FieldTypeObj) :
|
|
236
|
+
rawType;
|
|
237
|
+
|
|
238
|
+
let result: JSONSchema7 = {};
|
|
239
|
+
const partialProps: Partial<JSONSchema7> = {
|
|
240
|
+
...((t.enum || t.allowedValues?.length) && { enum: t.allowedValues ?? t.enum!.slice(0) }),
|
|
241
|
+
...(!!description && { description }),
|
|
242
|
+
...(!!title && { title }),
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
if(t.enum?.length){
|
|
246
|
+
partialProps.type = typeof t.enum[0]! as any;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if(typeof type === "string" || arrayOf){
|
|
250
|
+
|
|
251
|
+
/** ARRAY */
|
|
252
|
+
if(type && typeof type !== "string") {
|
|
253
|
+
throw "Not expected";
|
|
254
|
+
}
|
|
255
|
+
if(arrayOf || type?.endsWith("[]")){
|
|
256
|
+
|
|
257
|
+
const arrayItems =
|
|
258
|
+
arrayOf? getJSONSchemaObject({ type: arrayOf }) :
|
|
259
|
+
type?.startsWith("any")? { type: undefined } :
|
|
260
|
+
{
|
|
261
|
+
type: type?.slice(0, -2) as JSONSchema7TypeName,
|
|
262
|
+
...(t.allowedValues && { enum: t.allowedValues }),
|
|
263
|
+
};
|
|
264
|
+
result = {
|
|
265
|
+
type: "array",
|
|
266
|
+
items: arrayItems,
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** PRIMITIVES */
|
|
270
|
+
} else {
|
|
271
|
+
result = {
|
|
272
|
+
type: type as JSONSchema7TypeName,
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** OBJECT */
|
|
277
|
+
} else if(isObject(type)){
|
|
278
|
+
result = {
|
|
279
|
+
type: "object",
|
|
280
|
+
required: getKeys(type).filter(k => {
|
|
281
|
+
const t = type[k];
|
|
282
|
+
return typeof t === "string" || !t.optional
|
|
283
|
+
}),
|
|
284
|
+
properties: getKeys(type).reduce((a, k) => {
|
|
285
|
+
return {
|
|
286
|
+
...a,
|
|
287
|
+
[k]: getJSONSchemaObject(type[k])
|
|
288
|
+
}
|
|
289
|
+
}, {}),
|
|
290
|
+
}
|
|
291
|
+
} else if(oneOf){
|
|
292
|
+
result = {
|
|
293
|
+
type: "object",
|
|
294
|
+
oneOf: oneOf.map(s => getJSONSchemaObject({ type: s }))
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (nullable) {
|
|
299
|
+
const nullDef = { type: "null" } as const;
|
|
300
|
+
if (result.oneOf) {
|
|
301
|
+
result.oneOf.push(nullDef)
|
|
302
|
+
} else if (result.enum && !result.enum.includes(null)) {
|
|
303
|
+
result.enum.push(null)
|
|
304
|
+
|
|
305
|
+
} else result = {
|
|
306
|
+
type: 'object',
|
|
307
|
+
oneOf: [result, nullDef]
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const rootSchema: JSONSchema7 | undefined = !rootInfo? undefined : {
|
|
312
|
+
"$id": rootInfo?.id,
|
|
313
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
...rootSchema,
|
|
318
|
+
...partialProps,
|
|
319
|
+
...result,
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export function getJSONBSchemaAsJSONSchema(tableName: string, colName: string, columnConfig: BaseColumn<{ en: 1 }> & JSONBColumnDef): JSONSchema7 {
|
|
324
|
+
|
|
325
|
+
const schema: JSONB.JSONBSchema = {
|
|
326
|
+
...columnConfig,
|
|
327
|
+
...(columnConfig.jsonbSchema ?? { type: columnConfig.jsonbSchemaType! })
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
return getJSONSchemaObject(schema, { id: `${tableName}.${colName}` })
|
|
331
|
+
}
|
|
332
|
+
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DB } from "./Prostgles";
|
|
2
2
|
import pg from "pg-promise/typescript/pg-subset";
|
|
3
3
|
import pgPromise from "pg-promise";
|
|
4
|
-
export
|
|
4
|
+
export type PrglNotifListener = (args: {
|
|
5
5
|
length: number;
|
|
6
6
|
processId: number;
|
|
7
7
|
channel: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostgresNotifListenManager.d.ts","sourceRoot":"","sources":["PostgresNotifListenManager.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,iCAAiC,CAAC;AACjD,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,
|
|
1
|
+
{"version":3,"file":"PostgresNotifListenManager.d.ts","sourceRoot":"","sources":["PostgresNotifListenManager.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,iCAAiC,CAAC;AACjD,OAAO,SAAS,MAAM,YAAY,CAAC;AAEnC,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CAChB,KAAK,IAAI,CAAC;AAEX,qBAAa,0BAA0B;IACnC,UAAU,CAAC,EAAE,SAAS,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;IAClD,KAAK,EAAE,EAAE,CAAC;IACV,aAAa,EAAE,iBAAiB,CAAC;IACjC,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,EAAE,GAAG,CAAC;IACjB,MAAM,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC;IAEpB,MAAM,CAAC,MAAM,6BAA8B,iBAAiB,mBAAmB,MAAM,KAAG,QAAQ,0BAA0B,CAAC,CAG1H;gBAEW,KAAK,EAAE,EAAE,EAAE,aAAa,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,UAAQ;IAS1F,IAAI,IAAI,OAAO,CAAC,0BAA0B,CAAC;IAQjD,OAAO;IAIP,cAAc;IA+Bd,SAAS,UAAS;IAClB,OAAO,aAMN;IAED,aAAa,aAKZ;IAED,SAAS,CAAC,KAAK,GAAE,MAAM,GAAG,SAAa,EAAE,WAAW,GAAE,MAAM,GAAG,SAAa;CAyD/E"}
|
|
@@ -13,7 +13,7 @@ class PostgresNotifListenManager {
|
|
|
13
13
|
isListening;
|
|
14
14
|
client;
|
|
15
15
|
static create = (db_pg, notifListener, db_channel_name) => {
|
|
16
|
-
|
|
16
|
+
const res = new PostgresNotifListenManager(db_pg, notifListener, db_channel_name, true);
|
|
17
17
|
return res.init();
|
|
18
18
|
};
|
|
19
19
|
constructor(db_pg, notifListener, db_channel_name, noInit = false) {
|
|
@@ -24,7 +24,7 @@ export class PostgresNotifListenManager {
|
|
|
24
24
|
client?: pg.IClient;
|
|
25
25
|
|
|
26
26
|
static create = (db_pg: DB, notifListener: PrglNotifListener, db_channel_name: string): Promise<PostgresNotifListenManager> => {
|
|
27
|
-
|
|
27
|
+
const res = new PostgresNotifListenManager(db_pg, notifListener, db_channel_name, true);
|
|
28
28
|
return res.init();
|
|
29
29
|
}
|
|
30
30
|
|
package/lib/Prostgles.d.ts
CHANGED
|
@@ -7,32 +7,32 @@ import AuthHandler, { Auth, SessionUser, AuthRequestParams } from "./AuthHandler
|
|
|
7
7
|
import TableConfigurator, { TableConfig } from "./TableConfig";
|
|
8
8
|
import { DboBuilder, DBHandlerServer, PRGLIOSocket } from "./DboBuilder";
|
|
9
9
|
export { DBHandlerServer };
|
|
10
|
-
export
|
|
10
|
+
export type PGP = pgPromise.IMain<{}, pg.IClient>;
|
|
11
11
|
import { AnyObject, FileColumnConfig } from "prostgles-types";
|
|
12
12
|
import { Publish, PublishMethods, PublishParams, PublishParser } from "./PublishParser";
|
|
13
13
|
import { DBEventsManager } from "./DBEventsManager";
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export type DB = pgPromise.IDatabase<{}, pg.IClient>;
|
|
15
|
+
type DbConnection = string | pg.IConnectionParameters<pg.IClient>;
|
|
16
|
+
type DbConnectionOpts = pg.IDefaults;
|
|
17
17
|
export declare const TABLE_METHODS: readonly ["update", "find", "findOne", "insert", "delete", "upsert"];
|
|
18
18
|
export declare const JOIN_TYPES: readonly ["one-many", "many-one", "one-one", "many-many"];
|
|
19
|
-
export
|
|
19
|
+
export type Join = {
|
|
20
20
|
tables: [string, string];
|
|
21
21
|
on: {
|
|
22
22
|
[key: string]: string;
|
|
23
23
|
}[];
|
|
24
24
|
type: typeof JOIN_TYPES[number];
|
|
25
25
|
};
|
|
26
|
-
export
|
|
27
|
-
|
|
26
|
+
export type Joins = Join[] | "inferred";
|
|
27
|
+
type Keywords = {
|
|
28
28
|
$and: string;
|
|
29
29
|
$or: string;
|
|
30
30
|
$not: string;
|
|
31
31
|
};
|
|
32
|
-
export
|
|
32
|
+
export type DeepPartial<T> = {
|
|
33
33
|
[P in keyof T]?: DeepPartial<T[P]>;
|
|
34
34
|
};
|
|
35
|
-
export
|
|
35
|
+
export type ExpressApp = {
|
|
36
36
|
get: (routePath: string, cb: (req: {
|
|
37
37
|
params: {
|
|
38
38
|
name: string;
|
|
@@ -77,7 +77,7 @@ export declare type ExpressApp = {
|
|
|
77
77
|
* 1. create a column in that table called media
|
|
78
78
|
* 2. create a lookup table lookup_media_{referencedTable} that joins referencedTable to the media table
|
|
79
79
|
*/
|
|
80
|
-
export
|
|
80
|
+
export type FileTableConfig = {
|
|
81
81
|
tableName?: string;
|
|
82
82
|
/**
|
|
83
83
|
* GET path used in serving media. defaults to /${tableName}
|
|
@@ -117,7 +117,7 @@ export declare type FileTableConfig = {
|
|
|
117
117
|
};
|
|
118
118
|
imageOptions?: ImageOptions;
|
|
119
119
|
};
|
|
120
|
-
export
|
|
120
|
+
export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUser> = {
|
|
121
121
|
dbConnection: DbConnection;
|
|
122
122
|
dbOptions?: DbConnectionOpts;
|
|
123
123
|
tsGeneratedTypesDir?: string;
|
|
@@ -180,12 +180,12 @@ export declare type ProstglesInitOptions<S = void, SUser extends SessionUser = S
|
|
|
180
180
|
fileTable?: FileTableConfig;
|
|
181
181
|
tableConfig?: TableConfig;
|
|
182
182
|
};
|
|
183
|
-
export
|
|
183
|
+
export type OnReady = {
|
|
184
184
|
dbo: DBHandlerServer;
|
|
185
185
|
db: DB;
|
|
186
186
|
};
|
|
187
|
-
|
|
188
|
-
export
|
|
187
|
+
type OnReadyCallback<S = void> = (dbo: DBOFullyTyped<S>, db: DB) => any;
|
|
188
|
+
export type InitResult = {
|
|
189
189
|
db: DBOFullyTyped;
|
|
190
190
|
_db: DB;
|
|
191
191
|
pgp: PGP;
|
package/lib/Prostgles.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Prostgles.d.ts","sourceRoot":"","sources":["Prostgles.ts"],"names":[],"mappings":";AAMA,OAAO,KAAK,SAAS,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,GAAG,QAAQ,iCAAiC,CAAC,CAAC;AACvD,OAAO,WAAW,EAAE,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,eAA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D,IAAI,UAAU,CAAC,CAAC,EAAE,UAAU,EAE3B;IACD,aAAa,CAAC,EAAE,aAAa,CAAC;IAE9B,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B,QAAQ;;;;;MAAoB;IAC5B,OAAO,CAAC,MAAM,CAAS;IAEvB,eAAe,CAAC,EAAE,eAAe,CAAC;IAGlC,WAAW,CAAC,EAAE,WAAW,CAAC;IAE1B,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IAEtC,OAAO,CAAC,SAAS,EAAE,MAAM;gBAIb,MAAM,EAAE,oBAAoB;IA+BxC,SAAS,UAAS;IAEZ,cAAc,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IA+B9D,OAAO;IAIP,aAAa;;;;IAMb,OAAO,CAAC,WAAW;IASnB,gBAAgB,eAKf;IAED;;OAEG;IACH,aAAa,CAAC,KAAK,UAAQ;IAgB3B;;OAEG;IACH,UAAU,uEAUT;IAED,OAAO,CAAC,eAAe,CAyBtB;IAGD,aAAa,sBAiBZ;IAED,WAAW,UAAS;IACpB,0BAA0B,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC;IAGtC,IAAI,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,UAAU,CAAC;IAoInD,UAAU,CAAC,QAAQ,EAAE,MAAM;IA0BjC,gBAAgB,EAAE,GAAG,EAAE,CAAM;IACvB,eAAe;IAkHrB,gBAAgB,WAAkB,GAAG,mBAoGpC;CACF;AAqCD,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAE1D"}
|
package/lib/Prostgles.js
CHANGED
|
@@ -34,10 +34,8 @@ exports.isSuperUser = exports.Prostgles = exports.JOIN_TYPES = exports.TABLE_MET
|
|
|
34
34
|
const promise = __importStar(require("bluebird"));
|
|
35
35
|
const pgPromise = __importStar(require("pg-promise"));
|
|
36
36
|
const FileManager_1 = __importDefault(require("./FileManager"));
|
|
37
|
-
const
|
|
38
|
-
const version = pkgj.version;
|
|
37
|
+
const { version } = require('../package.json');
|
|
39
38
|
const AuthHandler_1 = __importDefault(require("./AuthHandler"));
|
|
40
|
-
console.log("Add a basic auth mode where user and sessions table are created");
|
|
41
39
|
const TableConfig_1 = __importDefault(require("./TableConfig"));
|
|
42
40
|
const utils_1 = require("./utils");
|
|
43
41
|
const DboBuilder_1 = require("./DboBuilder");
|
|
@@ -47,7 +45,7 @@ const PublishParser_1 = require("./PublishParser");
|
|
|
47
45
|
const DBEventsManager_1 = require("./DBEventsManager");
|
|
48
46
|
exports.TABLE_METHODS = ["update", "find", "findOne", "insert", "delete", "upsert"];
|
|
49
47
|
function getDbConnection(dbConnection, options, debugQueries = false, onNotice) {
|
|
50
|
-
|
|
48
|
+
const pgp = pgPromise({
|
|
51
49
|
promiseLib: promise,
|
|
52
50
|
...(debugQueries ? {
|
|
53
51
|
query: function (ctx) {
|
|
@@ -63,7 +61,8 @@ function getDbConnection(dbConnection, options, debugQueries = false, onNotice)
|
|
|
63
61
|
}
|
|
64
62
|
} : {}),
|
|
65
63
|
...((onNotice || debugQueries) ? {
|
|
66
|
-
connect: function (client, dc,
|
|
64
|
+
connect: function ({ client, dc, useCount }) {
|
|
65
|
+
const isFresh = !useCount;
|
|
67
66
|
if (isFresh && !client.listeners('notice').length) {
|
|
68
67
|
client.on('notice', function (msg) {
|
|
69
68
|
if (onNotice) {
|
|
@@ -124,7 +123,9 @@ class Prostgles {
|
|
|
124
123
|
port: 5432,
|
|
125
124
|
application_name: "prostgles_app"
|
|
126
125
|
},
|
|
127
|
-
onReady: () => {
|
|
126
|
+
onReady: () => {
|
|
127
|
+
//empty
|
|
128
|
+
},
|
|
128
129
|
schema: "public",
|
|
129
130
|
watchSchema: false,
|
|
130
131
|
watchSchemaType: "DDL_trigger",
|
|
@@ -164,7 +165,7 @@ class Prostgles {
|
|
|
164
165
|
if (!params.io)
|
|
165
166
|
console.warn("io missing. WebSockets will not be set up");
|
|
166
167
|
// TODO: find an exact keyof T<->arr TS matching method
|
|
167
|
-
|
|
168
|
+
const config = [
|
|
168
169
|
"transactions", "joins", "tsGeneratedTypesDir",
|
|
169
170
|
"onReady", "dbConnection", "dbOptions", "publishMethods", "io",
|
|
170
171
|
"publish", "schema", "publishRawSQL", "wsChannelNamePrefix", "onSocketConnect",
|
|
@@ -454,7 +455,7 @@ class Prostgles {
|
|
|
454
455
|
this.checkDb();
|
|
455
456
|
if (!this.dbo)
|
|
456
457
|
throw "dbo missing";
|
|
457
|
-
|
|
458
|
+
const publishParser = new PublishParser_1.PublishParser(this.opts.publish, this.opts.publishMethods, this.opts.publishRawSQL, this.dbo, this.db, this);
|
|
458
459
|
this.publishParser = publishParser;
|
|
459
460
|
if (!this.opts.io)
|
|
460
461
|
return;
|
|
@@ -476,7 +477,7 @@ class Prostgles {
|
|
|
476
477
|
this.connectedSockets.push(socket);
|
|
477
478
|
if (!this.db || !this.dbo)
|
|
478
479
|
throw new Error("db/dbo missing");
|
|
479
|
-
|
|
480
|
+
const { dbo, db } = this;
|
|
480
481
|
try {
|
|
481
482
|
if (this.opts.onSocketConnect) {
|
|
482
483
|
try {
|
|
@@ -494,16 +495,16 @@ class Prostgles {
|
|
|
494
495
|
Checks request against publish and if OK run it with relevant publish functions. Local (server) requests do not check the policy
|
|
495
496
|
*/
|
|
496
497
|
socket.removeAllListeners(prostgles_types_1.CHANNELS.DEFAULT);
|
|
497
|
-
socket.on(prostgles_types_1.CHANNELS.DEFAULT, async ({ tableName, command, param1, param2, param3 }, cb = (...
|
|
498
|
+
socket.on(prostgles_types_1.CHANNELS.DEFAULT, async ({ tableName, command, param1, param2, param3 }, cb = (..._callback) => { }) => {
|
|
498
499
|
try { /* Channel name will only include client-sent params so we ignore table_rules enforced params */
|
|
499
500
|
if (!socket || !this.authHandler || !this.publishParser || !this.dbo) {
|
|
500
501
|
console.error("socket or authhandler missing??!!");
|
|
501
502
|
throw "socket or authhandler missing??!!";
|
|
502
503
|
}
|
|
503
504
|
const clientInfo = await this.authHandler.getClientInfo({ socket });
|
|
504
|
-
|
|
505
|
+
const valid_table_command_rules = await this.publishParser.getValidatedRequestRule({ tableName, command, localParams: { socket } }, clientInfo);
|
|
505
506
|
if (valid_table_command_rules) {
|
|
506
|
-
|
|
507
|
+
const res = await this.dbo[tableName][command](param1, param2, param3, valid_table_command_rules, { socket, isRemoteRequest: true });
|
|
507
508
|
cb(null, res);
|
|
508
509
|
}
|
|
509
510
|
else
|
|
@@ -529,7 +530,7 @@ class Prostgles {
|
|
|
529
530
|
;
|
|
530
531
|
});
|
|
531
532
|
socket.removeAllListeners(prostgles_types_1.CHANNELS.METHOD);
|
|
532
|
-
socket.on(prostgles_types_1.CHANNELS.METHOD, async ({ method, params }, cb = (...
|
|
533
|
+
socket.on(prostgles_types_1.CHANNELS.METHOD, async ({ method, params }, cb = (..._callback) => { }) => {
|
|
533
534
|
try {
|
|
534
535
|
const methods = await this.publishParser?.getAllowedMethods(socket);
|
|
535
536
|
if (!methods || !methods[method]) {
|
|
@@ -561,11 +562,11 @@ class Prostgles {
|
|
|
561
562
|
}
|
|
562
563
|
pushSocketSchema = async (socket) => {
|
|
563
564
|
try {
|
|
564
|
-
|
|
565
|
+
const { auth, userData } = await this.authHandler?.makeSocketAuth(socket) || {};
|
|
565
566
|
// let needType = this.publishRawSQL && typeof this.publishRawSQL === "function";
|
|
566
567
|
// let DATA_TYPES = !needType? [] : await this.db.any("SELECT oid, typname FROM pg_type");
|
|
567
568
|
// let USER_TABLES = !needType? [] : await this.db.any("SELECT relid, relname FROM pg_catalog.pg_statio_user_tables");
|
|
568
|
-
const {
|
|
569
|
+
const { db, publishParser } = this;
|
|
569
570
|
let fullSchema;
|
|
570
571
|
let publishValidationError;
|
|
571
572
|
let rawSQL = false;
|
|
@@ -585,12 +586,12 @@ class Prostgles {
|
|
|
585
586
|
if (this.opts.publishRawSQL && typeof this.opts.publishRawSQL === "function") {
|
|
586
587
|
const canRunSQL = async () => {
|
|
587
588
|
const publishParams = await this.publishParser?.getPublishParams({ socket });
|
|
588
|
-
|
|
589
|
+
const res = await this.opts.publishRawSQL?.(publishParams);
|
|
589
590
|
return Boolean(res && typeof res === "boolean" || res === "*");
|
|
590
591
|
};
|
|
591
592
|
if (await canRunSQL()) {
|
|
592
593
|
socket.removeAllListeners(prostgles_types_1.CHANNELS.SQL);
|
|
593
|
-
socket.on(prostgles_types_1.CHANNELS.SQL, async ({ query, params, options }, cb = (...
|
|
594
|
+
socket.on(prostgles_types_1.CHANNELS.SQL, async ({ query, params, options }, cb = (..._callback) => { }) => {
|
|
594
595
|
if (!this.dbo?.sql)
|
|
595
596
|
throw "Internal error: sql handler missing";
|
|
596
597
|
this.dbo.sql(query, params, options, { socket }).then(res => {
|
|
@@ -609,9 +610,9 @@ class Prostgles {
|
|
|
609
610
|
}
|
|
610
611
|
}
|
|
611
612
|
const { schema, tables } = fullSchema ?? { schema: {}, tables: [] };
|
|
612
|
-
|
|
613
|
+
const joinTables2 = [];
|
|
613
614
|
if (this.opts.joins) {
|
|
614
|
-
|
|
615
|
+
const _joinTables2 = this.dboBuilder.getJoinPaths()
|
|
615
616
|
.filter(jp => ![jp.t1, jp.t2].find(t => !schema[t] || !schema[t].findOne)).map(jp => [jp.t1, jp.t2].sort());
|
|
616
617
|
_joinTables2.map(jt => {
|
|
617
618
|
if (!joinTables2.find(_jt => _jt.join() === jt.join())) {
|