prostgles-types 4.0.113 → 4.0.115
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/.vscode/settings.json +10 -1
- package/dist/auth.d.ts +21 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/files.d.ts +5 -5
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +13 -1
- package/dist/files.js.map +1 -1
- package/dist/filters.d.ts +82 -14
- package/dist/filters.d.ts.map +1 -1
- package/dist/filters.js +90 -45
- package/dist/filters.js.map +1 -1
- package/dist/index.d.ts +335 -18
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +124 -59
- package/dist/index.js.map +1 -1
- package/dist/insertUpdateUtils.d.ts.map +1 -1
- package/dist/insertUpdateUtils.js +2 -0
- package/dist/insertUpdateUtils.js.map +1 -1
- package/dist/jsonb.d.ts +33 -10
- package/dist/jsonb.d.ts.map +1 -1
- package/dist/jsonb.js +50 -38
- package/dist/jsonb.js.map +1 -1
- package/dist/md5.d.ts.map +1 -1
- package/dist/md5.js +52 -19
- package/dist/md5.js.map +1 -1
- package/dist/replication.d.ts +47 -0
- package/dist/replication.d.ts.map +1 -1
- package/dist/util.d.ts +47 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +122 -65
- package/dist/util.js.map +1 -1
- package/lib/auth.ts +11 -13
- package/lib/files.ts +87 -75
- package/lib/filters.ts +228 -193
- package/lib/index.ts +604 -466
- package/lib/insertUpdateUtils.ts +10 -10
- package/lib/jsonb.ts +242 -205
- package/lib/md5.ts +31 -28
- package/lib/replication.ts +39 -33
- package/lib/util.ts +217 -175
- package/package.json +2 -1
- package/tsconfig.json +1 -1
package/lib/insertUpdateUtils.ts
CHANGED
|
@@ -4,20 +4,22 @@ import { ExactlyOne } from "./util";
|
|
|
4
4
|
export type KeysOfType<T, U> = { [K in keyof T]: T[K] extends U ? K : never }[keyof T];
|
|
5
5
|
export type RequiredKeys<T> = Exclude<KeysOfType<T, Exclude<T[keyof T], undefined>>, undefined>;
|
|
6
6
|
export type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
|
|
7
|
-
export type PartialBy<T, K extends keyof T | string> = Omit<T, K> &
|
|
7
|
+
export type PartialBy<T, K extends keyof T | string> = Omit<T, K> &
|
|
8
|
+
Partial<Pick<T, Extract<K, keyof T>>>;
|
|
8
9
|
|
|
9
|
-
export const FUNC_ENDING_HINT = "$func" as const;
|
|
10
|
+
export const FUNC_ENDING_HINT = "$func" as const;
|
|
10
11
|
type DataOrFuncValuesObject<ObjectType> = {
|
|
11
12
|
[Key in keyof ObjectType & string]:
|
|
12
13
|
| { [K in Key]: ObjectType[Key] }
|
|
13
14
|
| { [K in `${Key}.${typeof FUNC_ENDING_HINT}`]: Record<string, any[]> };
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
-
type PropertyValueIntersection<O> =
|
|
16
|
+
|
|
17
|
+
type PropertyValueIntersection<O> =
|
|
18
|
+
{
|
|
17
19
|
[K in keyof O]: (x: O[K]) => void;
|
|
18
|
-
}[keyof O] extends (x: infer I) => void
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
}[keyof O] extends (x: infer I) => void ?
|
|
21
|
+
I
|
|
22
|
+
: never;
|
|
21
23
|
|
|
22
24
|
// export type UpsertDataToPGCast<TD> = PropertyValueIntersection<DataOrFuncValuesObject<Required<TD>>>;
|
|
23
25
|
export type UpsertDataToPGCast<TD extends AnyObject = AnyObject> = {
|
|
@@ -44,7 +46,7 @@ const mixed: UpsertDataToPGCast<Schema> = {
|
|
|
44
46
|
col1: 2,
|
|
45
47
|
col2: { func: [] },
|
|
46
48
|
};
|
|
47
|
-
|
|
49
|
+
|
|
48
50
|
const badKey: UpsertDataToPGCast<Schema> = {
|
|
49
51
|
//@ts-expect-error
|
|
50
52
|
badkey: { func: [] },
|
|
@@ -54,5 +56,3 @@ const badKey: UpsertDataToPGCast<Schema> = {
|
|
|
54
56
|
const wrong: UpsertDataToPGCast<Schema> = {
|
|
55
57
|
col2: { func: [] },
|
|
56
58
|
};
|
|
57
|
-
|
|
58
|
-
|
package/lib/jsonb.ts
CHANGED
|
@@ -2,17 +2,21 @@ import { getKeys, getObjectEntries, isObject, StrictUnion } from "./util";
|
|
|
2
2
|
import type { JSONSchema7, JSONSchema7TypeName } from "json-schema";
|
|
3
3
|
import { AnyObject } from "./filters";
|
|
4
4
|
|
|
5
|
-
export const PrimitiveTypes = [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
export const PrimitiveTypes = [
|
|
6
|
+
"boolean",
|
|
7
|
+
"number",
|
|
8
|
+
"integer",
|
|
9
|
+
"string",
|
|
10
|
+
"Date",
|
|
11
|
+
"time",
|
|
12
|
+
"timestamp",
|
|
13
|
+
"any",
|
|
10
14
|
] as const;
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
export const PrimitiveArrayTypes = PrimitiveTypes.map((v) => `${v}[]` as `${typeof v}[]`);
|
|
16
|
+
export const DATA_TYPES = [...PrimitiveTypes, ...PrimitiveArrayTypes] as const;
|
|
17
|
+
type DataType = (typeof DATA_TYPES)[number];
|
|
13
18
|
|
|
14
19
|
export namespace JSONB {
|
|
15
|
-
|
|
16
20
|
export type BaseOptions = {
|
|
17
21
|
/**
|
|
18
22
|
* False by default
|
|
@@ -24,51 +28,52 @@ export namespace JSONB {
|
|
|
24
28
|
nullable?: boolean;
|
|
25
29
|
description?: string;
|
|
26
30
|
title?: string;
|
|
27
|
-
};
|
|
31
|
+
};
|
|
28
32
|
|
|
29
33
|
export type Lookup = BaseOptions & {
|
|
30
34
|
type?: "Lookup" | "Lookup[]";
|
|
31
|
-
lookup:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
35
|
+
lookup:
|
|
36
|
+
| {
|
|
37
|
+
type:
|
|
38
|
+
| "data"
|
|
39
|
+
/**
|
|
40
|
+
* This is used as edit-mode (to generate lookup of type data)
|
|
41
|
+
*/
|
|
42
|
+
| "data-def";
|
|
43
|
+
table: string;
|
|
44
|
+
column: string;
|
|
45
|
+
filter?: AnyObject;
|
|
46
|
+
isArray?: boolean;
|
|
47
|
+
isFullRow?: {
|
|
48
|
+
/**
|
|
49
|
+
* Columns used to display the selected row in the dropdown
|
|
50
|
+
*/
|
|
51
|
+
displayColumns?: string[];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Columns used to search
|
|
55
|
+
*/
|
|
56
|
+
searchColumns?: string[];
|
|
57
|
+
/**
|
|
58
|
+
* If true then a button will be shown
|
|
59
|
+
* in the row card footer to access this action
|
|
60
|
+
*/
|
|
61
|
+
showInRowCard?: {
|
|
62
|
+
/**
|
|
63
|
+
* Action button text. Defaults to the method name
|
|
64
|
+
*/
|
|
65
|
+
actionLabel?: string;
|
|
66
|
+
actionColor?: "danger" | "warn" | "action";
|
|
67
|
+
actionStyle?: AnyObject;
|
|
68
|
+
actionClass?: string;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
| {
|
|
72
|
+
type: "schema";
|
|
73
|
+
isArray?: boolean;
|
|
74
|
+
object: "column" | "table";
|
|
75
|
+
filter?: { table?: string; tsDataType?: string; udt_name?: string };
|
|
76
|
+
};
|
|
72
77
|
allowedValues?: undefined;
|
|
73
78
|
oneOf?: undefined;
|
|
74
79
|
oneOfType?: undefined;
|
|
@@ -86,7 +91,7 @@ export namespace JSONB {
|
|
|
86
91
|
arrayOfType?: undefined;
|
|
87
92
|
enum?: undefined;
|
|
88
93
|
};
|
|
89
|
-
|
|
94
|
+
|
|
90
95
|
export type ObjectType = BaseOptions & {
|
|
91
96
|
type: ObjectSchema;
|
|
92
97
|
allowedValues?: undefined;
|
|
@@ -95,8 +100,8 @@ export namespace JSONB {
|
|
|
95
100
|
arrayOf?: undefined;
|
|
96
101
|
arrayOfType?: undefined;
|
|
97
102
|
enum?: undefined;
|
|
98
|
-
}
|
|
99
|
-
|
|
103
|
+
};
|
|
104
|
+
|
|
100
105
|
export type EnumType = BaseOptions & {
|
|
101
106
|
type?: undefined;
|
|
102
107
|
enum: readonly any[];
|
|
@@ -106,34 +111,40 @@ export namespace JSONB {
|
|
|
106
111
|
arrayOfType?: undefined;
|
|
107
112
|
allowedValues?: undefined;
|
|
108
113
|
};
|
|
109
|
-
|
|
114
|
+
|
|
110
115
|
export type OneOf = BaseOptions & {
|
|
111
116
|
type?: undefined;
|
|
112
117
|
arrayOf?: undefined;
|
|
113
118
|
arrayOfType?: undefined;
|
|
114
119
|
allowedValues?: undefined;
|
|
115
120
|
enum?: undefined;
|
|
116
|
-
} & (
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
} & (
|
|
122
|
+
| {
|
|
123
|
+
oneOf?: undefined;
|
|
124
|
+
oneOfType: readonly ObjectSchema[];
|
|
125
|
+
}
|
|
126
|
+
| {
|
|
127
|
+
oneOf: FieldType[];
|
|
128
|
+
oneOfType?: undefined;
|
|
129
|
+
}
|
|
130
|
+
);
|
|
123
131
|
export type ArrayOf = BaseOptions & {
|
|
124
132
|
type?: undefined;
|
|
125
133
|
allowedValues?: undefined;
|
|
126
134
|
oneOf?: undefined;
|
|
127
135
|
oneOfType?: undefined;
|
|
128
136
|
enum?: undefined;
|
|
129
|
-
} & (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
} & (
|
|
138
|
+
| {
|
|
139
|
+
arrayOf?: undefined;
|
|
140
|
+
arrayOfType: ObjectSchema;
|
|
141
|
+
}
|
|
142
|
+
| {
|
|
143
|
+
arrayOf: FieldType;
|
|
144
|
+
arrayOfType?: undefined;
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
|
|
137
148
|
export type RecordType = BaseOptions & {
|
|
138
149
|
type?: undefined;
|
|
139
150
|
allowedValues?: undefined;
|
|
@@ -146,102 +157,102 @@ export namespace JSONB {
|
|
|
146
157
|
keysEnum?: readonly string[];
|
|
147
158
|
values?: FieldType;
|
|
148
159
|
partial?: boolean;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
160
|
+
};
|
|
161
|
+
};
|
|
151
162
|
|
|
152
163
|
export type FieldTypeObj = StrictUnion<
|
|
153
|
-
|
|
|
154
|
-
| ObjectType
|
|
155
|
-
| EnumType
|
|
156
|
-
| OneOf
|
|
157
|
-
| ArrayOf
|
|
158
|
-
| RecordType
|
|
159
|
-
| Lookup
|
|
164
|
+
BasicType | ObjectType | EnumType | OneOf | ArrayOf | RecordType | Lookup
|
|
160
165
|
>;
|
|
161
166
|
|
|
162
|
-
export type FieldType =
|
|
163
|
-
| DataType
|
|
164
|
-
| FieldTypeObj;
|
|
165
|
-
|
|
167
|
+
export type FieldType = DataType | FieldTypeObj;
|
|
166
168
|
|
|
167
|
-
export type GetType<T extends FieldType | Omit<FieldTypeObj, "optional">> = GetWNullType<
|
|
168
|
-
|
|
169
|
-
|
|
169
|
+
export type GetType<T extends FieldType | Omit<FieldTypeObj, "optional">> = GetWNullType<
|
|
170
|
+
T extends DataType ? { type: T } : T
|
|
171
|
+
>;
|
|
172
|
+
type GetWNullType<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">> =
|
|
173
|
+
T extends { nullable: true } ? null | _GetType<T> : _GetType<T>;
|
|
174
|
+
type GetAllowedValues<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">, TType> =
|
|
175
|
+
T extends { allowedValues: readonly any[] } ? T["allowedValues"][number] : TType;
|
|
170
176
|
|
|
171
177
|
type _GetType<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">> =
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
178
|
+
T extends { type: ObjectSchema } ? GetObjectType<T["type"]>
|
|
179
|
+
: T extends { type: "number" } ? GetAllowedValues<T, number>
|
|
180
|
+
: T extends { type: "boolean" } ? GetAllowedValues<T, boolean>
|
|
181
|
+
: T extends { type: "integer" } ? GetAllowedValues<T, number>
|
|
182
|
+
: T extends { type: "string" } ? GetAllowedValues<T, string>
|
|
183
|
+
: T extends { type: "time" } ? GetAllowedValues<T, string>
|
|
184
|
+
: T extends { type: "timestamp" } ? GetAllowedValues<T, string>
|
|
185
|
+
: T extends { type: "Date" } ? GetAllowedValues<T, string>
|
|
186
|
+
: T extends { type: "any" } ? GetAllowedValues<T, any>
|
|
187
|
+
: T extends { type: "number[]" } ? GetAllowedValues<T, number>[]
|
|
188
|
+
: T extends { type: "boolean[]" } ? GetAllowedValues<T, boolean>[]
|
|
189
|
+
: T extends { type: "integer[]" } ? GetAllowedValues<T, number>[]
|
|
190
|
+
: T extends { type: "time[]" } ? GetAllowedValues<T, string>[]
|
|
191
|
+
: T extends { type: "timestamp[]" } ? GetAllowedValues<T, string>[]
|
|
192
|
+
: T extends { type: "Date[]" } ? GetAllowedValues<T, string>[]
|
|
193
|
+
: T extends { type: "string[]" } ? GetAllowedValues<T, string>[]
|
|
194
|
+
: T extends { type: "any[]" } ? GetAllowedValues<T, any>[]
|
|
195
|
+
: T extends { enum: readonly any[] | any[] } ? T["enum"][number]
|
|
196
|
+
: T extends { record: RecordType["record"] } ?
|
|
197
|
+
Record<
|
|
198
|
+
T["record"] extends { keysEnum: readonly string[] } ? T["record"]["keysEnum"][number]
|
|
199
|
+
: string,
|
|
200
|
+
T["record"] extends { values: FieldType } ? GetType<T["record"]["values"]> : any
|
|
201
|
+
>
|
|
202
|
+
: T extends { oneOf: readonly FieldType[] | FieldType[] } ? GetType<T["oneOf"][number]>
|
|
203
|
+
: T extends { oneOfType: readonly ObjectSchema[] | ObjectSchema[] } ?
|
|
204
|
+
GetObjectType<T["oneOfType"][number]>
|
|
205
|
+
: T extends { arrayOf: FieldType } ? GetType<T["arrayOf"]>[]
|
|
206
|
+
: T extends { arrayOfType: ObjectSchema } ? GetObjectType<T["arrayOfType"]>[]
|
|
207
|
+
: any;
|
|
208
|
+
|
|
209
|
+
type IsOptional<F extends FieldType> =
|
|
210
|
+
F extends DataType ? false
|
|
211
|
+
: F extends { optional: true } ? true
|
|
212
|
+
: false;
|
|
202
213
|
|
|
203
214
|
type ObjectSchema = Record<string, FieldType>;
|
|
204
|
-
export type JSONBSchema<T extends FieldTypeObj = FieldTypeObj> = Omit<T, "optional"> & {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
{
|
|
208
|
-
[K in keyof S as IsOptional<S[K]> extends true ? K : never]?: GetType<S[K]>
|
|
209
|
-
} & {
|
|
210
|
-
[K in keyof S as IsOptional<S[K]> extends true ? never : K]: GetType<S[K]>
|
|
211
|
-
}
|
|
212
|
-
);
|
|
213
|
-
export type GetSchemaType<S extends JSONBSchema> = S["nullable"] extends true? (null | GetType<S>) : GetType<S>;
|
|
215
|
+
export type JSONBSchema<T extends FieldTypeObj = FieldTypeObj> = Omit<T, "optional"> & {
|
|
216
|
+
defaultValue?: any;
|
|
217
|
+
};
|
|
214
218
|
|
|
219
|
+
export type GetObjectType<S extends ObjectSchema> = {
|
|
220
|
+
[K in keyof S as IsOptional<S[K]> extends true ? K : never]?: GetType<S[K]>;
|
|
221
|
+
} & {
|
|
222
|
+
[K in keyof S as IsOptional<S[K]> extends true ? never : K]: GetType<S[K]>;
|
|
223
|
+
};
|
|
224
|
+
export type GetSchemaType<S extends JSONBSchema> =
|
|
225
|
+
S["nullable"] extends true ? null | GetType<S> : GetType<S>;
|
|
215
226
|
}
|
|
216
227
|
|
|
217
|
-
|
|
218
228
|
/** tests */
|
|
219
|
-
const t: JSONB.GetType<{ arrayOfType: { a: "number" } }> = [
|
|
220
|
-
{ a: 2 }
|
|
221
|
-
]
|
|
229
|
+
const t: JSONB.GetType<{ arrayOfType: { a: "number" } }> = [{ a: 2 }];
|
|
222
230
|
|
|
223
231
|
/** StrictUnion was removed because it doesn't work with object | string */
|
|
224
|
-
const _oneOf: JSONB.GetType<{
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
232
|
+
const _oneOf: JSONB.GetType<{
|
|
233
|
+
nullable: true;
|
|
234
|
+
oneOf: [
|
|
235
|
+
{ enum: ["n"] },
|
|
236
|
+
{ type: { a: "number" } },
|
|
237
|
+
{ type: { a: { type: "string"; allowedValues: ["a"] } } },
|
|
238
|
+
];
|
|
239
|
+
}> = {
|
|
240
|
+
a: "a",
|
|
241
|
+
};
|
|
231
242
|
|
|
232
243
|
const _a: JSONB.GetType<{ type: { a: "number" } }> = {
|
|
233
|
-
a: 2
|
|
234
|
-
}
|
|
244
|
+
a: 2,
|
|
245
|
+
};
|
|
235
246
|
|
|
236
|
-
const _r: JSONB.GetType<{ record: { keysEnum: ["a", "b"]
|
|
247
|
+
const _r: JSONB.GetType<{ record: { keysEnum: ["a", "b"]; values: "integer[]" } }> = {
|
|
237
248
|
a: [2],
|
|
238
|
-
b: [221]
|
|
239
|
-
}
|
|
249
|
+
b: [221],
|
|
250
|
+
};
|
|
240
251
|
|
|
241
252
|
const _dd: JSONB.JSONBSchema = {
|
|
242
253
|
enum: [1],
|
|
243
|
-
type: "any"
|
|
244
|
-
}
|
|
254
|
+
type: "any",
|
|
255
|
+
};
|
|
245
256
|
|
|
246
257
|
const s = {
|
|
247
258
|
type: {
|
|
@@ -249,118 +260,144 @@ const s = {
|
|
|
249
260
|
c: { type: { c1: { type: "string" } } },
|
|
250
261
|
arr: { arrayOfType: { d: "string" } },
|
|
251
262
|
o: {
|
|
252
|
-
oneOfType: [
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
} as const;// satisfies JSONB.JSONBSchema;
|
|
263
|
+
oneOfType: [{ z: { type: "integer" } }, { z1: { type: "integer" } }],
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
} as const; // satisfies JSONB.JSONBSchema;
|
|
259
267
|
|
|
260
268
|
const _ss: JSONB.GetType<typeof s> = {
|
|
261
269
|
a: true,
|
|
262
270
|
arr: [{ d: "" }],
|
|
263
271
|
c: {
|
|
264
|
-
c1: ""
|
|
272
|
+
c1: "",
|
|
265
273
|
},
|
|
266
|
-
o: { z1: 23 }
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export const getJSONSchemaObject = (
|
|
270
|
-
|
|
274
|
+
o: { z1: 23 },
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export const getJSONSchemaObject = (
|
|
278
|
+
rawType: JSONB.FieldType | JSONB.JSONBSchema,
|
|
279
|
+
rootInfo?: { id: string }
|
|
280
|
+
): JSONSchema7 => {
|
|
281
|
+
const {
|
|
282
|
+
type,
|
|
283
|
+
arrayOf,
|
|
284
|
+
arrayOfType,
|
|
285
|
+
description,
|
|
286
|
+
nullable,
|
|
287
|
+
oneOf,
|
|
288
|
+
oneOfType,
|
|
289
|
+
title,
|
|
290
|
+
record,
|
|
291
|
+
...t
|
|
292
|
+
} = typeof rawType === "string" ? ({ type: rawType } as JSONB.FieldTypeObj) : rawType;
|
|
271
293
|
|
|
272
294
|
let result: JSONSchema7 = {};
|
|
273
295
|
const partialProps: Partial<JSONSchema7> = {
|
|
274
|
-
...((t.enum ||
|
|
296
|
+
...((t.enum ||
|
|
297
|
+
(t.allowedValues?.length && (typeof type !== "string" || !type.endsWith("[]")))) && {
|
|
298
|
+
enum: t.allowedValues?.slice(0) ?? t.enum!.slice(0),
|
|
299
|
+
}),
|
|
275
300
|
...(!!description && { description }),
|
|
276
301
|
...(!!title && { title }),
|
|
277
302
|
};
|
|
278
303
|
|
|
279
|
-
if(t.enum?.length){
|
|
304
|
+
if (t.enum?.length) {
|
|
280
305
|
partialProps.type = typeof t.enum[0]! as any;
|
|
281
306
|
}
|
|
282
307
|
|
|
283
|
-
if(typeof type === "string" || arrayOf || arrayOfType){
|
|
284
|
-
|
|
308
|
+
if (typeof type === "string" || arrayOf || arrayOfType) {
|
|
285
309
|
/** ARRAY */
|
|
286
|
-
if(type && typeof type !== "string") {
|
|
310
|
+
if (type && typeof type !== "string") {
|
|
287
311
|
throw "Not expected";
|
|
288
312
|
}
|
|
289
|
-
if(arrayOf || arrayOfType || type?.endsWith("[]")){
|
|
290
|
-
const arrayItems =
|
|
291
|
-
|
|
292
|
-
type?.startsWith("any")? { type: undefined }
|
|
293
|
-
{
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
313
|
+
if (arrayOf || arrayOfType || type?.endsWith("[]")) {
|
|
314
|
+
const arrayItems =
|
|
315
|
+
arrayOf || arrayOfType ? getJSONSchemaObject(arrayOf || { type: arrayOfType })
|
|
316
|
+
: type?.startsWith("any") ? { type: undefined }
|
|
317
|
+
: {
|
|
318
|
+
type: type?.slice(0, -2) as JSONSchema7TypeName,
|
|
319
|
+
...(t.allowedValues && { enum: t.allowedValues.slice(0) }),
|
|
320
|
+
};
|
|
297
321
|
result = {
|
|
298
322
|
type: "array",
|
|
299
323
|
items: arrayItems,
|
|
300
|
-
}
|
|
324
|
+
};
|
|
301
325
|
|
|
302
|
-
|
|
326
|
+
/** PRIMITIVES */
|
|
303
327
|
} else {
|
|
304
328
|
result = {
|
|
305
329
|
type: type as JSONSchema7TypeName,
|
|
306
|
-
}
|
|
330
|
+
};
|
|
307
331
|
}
|
|
308
332
|
|
|
309
|
-
|
|
310
|
-
} else if(isObject(type)){
|
|
333
|
+
/** OBJECT */
|
|
334
|
+
} else if (isObject(type)) {
|
|
311
335
|
result = {
|
|
312
336
|
type: "object",
|
|
313
|
-
required: getKeys(type).filter(k => {
|
|
337
|
+
required: getKeys(type).filter((k) => {
|
|
314
338
|
const t = type[k]!;
|
|
315
339
|
return typeof t === "string" || !t.optional;
|
|
316
340
|
}),
|
|
317
|
-
properties: getObjectEntries(type).reduce((a, [k, v]) => {
|
|
341
|
+
properties: getObjectEntries(type).reduce((a, [k, v]) => {
|
|
318
342
|
return {
|
|
319
343
|
...a,
|
|
320
|
-
[k]: getJSONSchemaObject(v)
|
|
321
|
-
}
|
|
344
|
+
[k]: getJSONSchemaObject(v),
|
|
345
|
+
};
|
|
322
346
|
}, {}),
|
|
323
|
-
}
|
|
324
|
-
} else if(oneOf || oneOfType){
|
|
325
|
-
const _oneOf = oneOf || oneOfType!.map(type => ({ type }))
|
|
347
|
+
};
|
|
348
|
+
} else if (oneOf || oneOfType) {
|
|
349
|
+
const _oneOf = oneOf || oneOfType!.map((type) => ({ type }));
|
|
326
350
|
result = {
|
|
327
|
-
oneOf: _oneOf.map(t => getJSONSchemaObject(t))
|
|
328
|
-
}
|
|
329
|
-
} else if(record){
|
|
351
|
+
oneOf: _oneOf.map((t) => getJSONSchemaObject(t)),
|
|
352
|
+
};
|
|
353
|
+
} else if (record) {
|
|
330
354
|
result = {
|
|
331
355
|
type: "object",
|
|
332
|
-
...(record.values &&
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
356
|
+
...(record.values &&
|
|
357
|
+
!record.keysEnum && { additionalProperties: getJSONSchemaObject(record.values) }),
|
|
358
|
+
...(record.keysEnum && {
|
|
359
|
+
properties: record.keysEnum.reduce(
|
|
360
|
+
(a, v) => ({
|
|
361
|
+
...a,
|
|
362
|
+
[v]: !record.values ? { type: {} } : getJSONSchemaObject(record.values),
|
|
363
|
+
}),
|
|
364
|
+
{}
|
|
365
|
+
),
|
|
366
|
+
}),
|
|
367
|
+
};
|
|
338
368
|
}
|
|
339
369
|
|
|
340
370
|
if (nullable) {
|
|
341
371
|
const nullDef = { type: "null" } as const;
|
|
342
372
|
if (result.oneOf) {
|
|
343
|
-
result.oneOf.push(nullDef)
|
|
373
|
+
result.oneOf.push(nullDef);
|
|
344
374
|
} else if (result.enum && !result.enum.includes(null)) {
|
|
345
|
-
result.enum.push(null)
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
375
|
+
result.enum.push(null);
|
|
376
|
+
} else
|
|
377
|
+
result = {
|
|
378
|
+
oneOf: [result, nullDef],
|
|
379
|
+
};
|
|
350
380
|
}
|
|
351
381
|
|
|
352
|
-
const rootSchema: JSONSchema7 | undefined =
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
382
|
+
const rootSchema: JSONSchema7 | undefined =
|
|
383
|
+
!rootInfo ? undefined : (
|
|
384
|
+
{
|
|
385
|
+
$id: rootInfo?.id,
|
|
386
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
387
|
+
}
|
|
388
|
+
);
|
|
356
389
|
|
|
357
390
|
return {
|
|
358
391
|
...rootSchema,
|
|
359
392
|
...partialProps,
|
|
360
393
|
...result,
|
|
361
|
-
}
|
|
394
|
+
};
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
export function getJSONBSchemaAsJSONSchema(
|
|
398
|
+
tableName: string,
|
|
399
|
+
colName: string,
|
|
400
|
+
schema: JSONB.JSONBSchema
|
|
401
|
+
): JSONSchema7 {
|
|
402
|
+
return getJSONSchemaObject(schema, { id: `${tableName}.${colName}` });
|
|
362
403
|
}
|
|
363
|
-
|
|
364
|
-
export function getJSONBSchemaAsJSONSchema(tableName: string, colName: string, schema: JSONB.JSONBSchema): JSONSchema7 {
|
|
365
|
-
return getJSONSchemaObject(schema, { id: `${tableName}.${colName}` })
|
|
366
|
-
}
|