prostgles-server 3.0.110 → 3.0.112
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/DBSchemaBuilder.js +1 -1
- package/dist/DBSchemaBuilder.js.map +1 -1
- package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -1
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js +48 -6
- package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -1
- package/dist/JSONBValidation/validation.d.ts +2 -2
- package/dist/JSONBValidation/validation.d.ts.map +1 -1
- package/dist/JSONBValidation/validation.js +22 -2
- package/dist/JSONBValidation/validation.js.map +1 -1
- package/lib/DBSchemaBuilder.js +1 -1
- package/lib/DBSchemaBuilder.ts +1 -1
- package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -1
- package/lib/JSONBValidation/validate_jsonb_schema_sql.js +48 -6
- package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +48 -6
- package/lib/JSONBValidation/validation.d.ts +2 -2
- package/lib/JSONBValidation/validation.d.ts.map +1 -1
- package/lib/JSONBValidation/validation.js +22 -2
- package/lib/JSONBValidation/validation.ts +22 -3
- package/package.json +1 -1
- package/tests/client/PID.txt +1 -1
- package/tests/server/package-lock.json +1 -1
package/dist/DBSchemaBuilder.js
CHANGED
|
@@ -16,7 +16,7 @@ const getDBSchema = (dboBuilder) => {
|
|
|
16
16
|
if (colConf) {
|
|
17
17
|
if ((0, prostgles_types_1.isObject)(colConf) && (colConf.jsonbSchema || colConf.jsonbSchemaType)) {
|
|
18
18
|
const schema = colConf.jsonbSchema || { ...colConf, type: colConf.jsonbSchemaType };
|
|
19
|
-
type = (0, validation_1.getJSONBSchemaTSTypes)(schema, { nullable: colConf.nullable }, " ");
|
|
19
|
+
type = (0, validation_1.getJSONBSchemaTSTypes)(schema, { nullable: colConf.nullable }, " ", dboBuilder.tablesOrViews ?? []);
|
|
20
20
|
}
|
|
21
21
|
else if ((0, prostgles_types_1.isObject)(colConf) && "enum" in colConf) {
|
|
22
22
|
if (!colConf.enum)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DBSchemaBuilder.js","sourceRoot":"","sources":["../lib/DBSchemaBuilder.ts"],"names":[],"mappings":";;;AAAA,qDAAuF;AACvF,wBAA0B;AAE1B,6CAA4F;AAE5F,6DAAqE;AAG9D,MAAM,WAAW,GAAG,CAAC,UAAsB,EAAU,EAAE;IAC5D,MAAM,MAAM,GAAa,EAAE,CAAC;IAG5B,kFAAkF;IAClF,UAAU,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC5F,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAC/E,MAAM,UAAU,GAAG,CAAC,CAAsB,EAAE,EAAE;YAC5C,IAAI,IAAI,GAAW,CAAC,CAAC,CAAC,WAAW,CAAA,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,IAAA,6BAAgB,EAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAA;YACvF,MAAM,OAAO,GAAG,UAAU,CAAC,SAAS,CAAC,iBAAiB,EAAE,eAAe,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC;YAC1F,IAAG,OAAO,EAAC;gBACT,IAAG,IAAA,0BAAQ,EAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,WAAW,IAAI,OAA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I,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE;iBACrC;gBACD,UAAU,EAAE,IAAI;gBAChB,OAAO,EAAE,IAAI;gBACb,MAAM,EAAE;oBACN,YAAY,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE;iBAC1B;aACF;YACD,IAAI,EAAE;gBACJ,MAAM,EAAE;oBACN,YAAY,EAAE,GAAY;oBAC1B,YAAY,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE;iBAC1B;aACF;SACF,CAAA;QACD,MAAM,IAAI,GAAsB,CAAC,CAAA;QAEjC,MAAM,CAAC,GAA6B,CAAQ,CAAC;QAE7C,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,IAAK,EAAE,CAAC;QAErB,OAAO,GAAG,CAAC;IACb,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate_jsonb_schema_sql.d.ts","sourceRoot":"","sources":["../../lib/JSONBValidation/validate_jsonb_schema_sql.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,wBAAwB,yBAAmC,CAAC;AACzE,eAAO,MAAM,yBAAyB,
|
|
1
|
+
{"version":3,"file":"validate_jsonb_schema_sql.d.ts","sourceRoot":"","sources":["../../lib/JSONBValidation/validate_jsonb_schema_sql.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,wBAAwB,yBAAmC,CAAC;AACzE,eAAO,MAAM,yBAAyB,QAyYrC,CAAC"}
|
|
@@ -44,7 +44,7 @@ BEGIN
|
|
|
44
44
|
IF ARRAY[jsonb_schema] <@ allowed_types THEN
|
|
45
45
|
schema = jsonb_build_object('type', jsonb_schema);
|
|
46
46
|
/* { "type": ... } */
|
|
47
|
-
ELSIF BTRIM(jsonb_schema) ILIKE '{%' THEN
|
|
47
|
+
ELSIF BTRIM(replace(jsonb_schema,E'\n','')) ILIKE '{%' THEN
|
|
48
48
|
schema = jsonb_schema::JSONB;
|
|
49
49
|
ELSE
|
|
50
50
|
RAISE EXCEPTION $$Invalid schema. Expecting 'typename' or { "type": "typename" } but received: %, %$$, jsonb_schema, path USING HINT = path, COLUMN = colname;
|
|
@@ -72,6 +72,46 @@ BEGIN
|
|
|
72
72
|
RAISE EXCEPTION 'Data not in allowed enum list (%), %', schema->'enum', path USING HINT = path, COLUMN = colname;
|
|
73
73
|
END IF;
|
|
74
74
|
|
|
75
|
+
ELSIF schema ? 'lookup' THEN
|
|
76
|
+
IF NOT ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
77
|
+
$sch$
|
|
78
|
+
{
|
|
79
|
+
"oneOfType": [
|
|
80
|
+
{
|
|
81
|
+
"type": { "enum": ["schema"] },
|
|
82
|
+
"object": { "enum": ["table", "column"] },
|
|
83
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
84
|
+
"filter": { "optional": true, "type": "any" }
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"type": { "enum": ["data"] },
|
|
88
|
+
"table": "string",
|
|
89
|
+
"column": "string",
|
|
90
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
91
|
+
"filter": { "optional": true, "type": "any" },
|
|
92
|
+
"isFullRow": { "optional": true, "type": {
|
|
93
|
+
"displayColumns": { "optional": true, "type": "string[]" },
|
|
94
|
+
"searchColumns": { "optional": true, "type": "string[]" }
|
|
95
|
+
}},
|
|
96
|
+
"showInRowCard": { "optional": true, "type": "any" }
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
$sch$,
|
|
101
|
+
schema->'lookup',
|
|
102
|
+
checked_path || '.schema'::TEXT
|
|
103
|
+
) THEN
|
|
104
|
+
|
|
105
|
+
RETURN FALSE;
|
|
106
|
+
END IF;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
RETURN ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
110
|
+
CASE WHEN (schema->'lookup'->'isArray')::BOOLEAN THEN 'any[]' ELSE 'any' END,
|
|
111
|
+
data,
|
|
112
|
+
checked_path
|
|
113
|
+
);
|
|
114
|
+
|
|
75
115
|
ELSIF schema ? 'type' THEN
|
|
76
116
|
|
|
77
117
|
IF jsonb_typeof(schema->'type') = 'string' THEN
|
|
@@ -164,17 +204,14 @@ BEGIN
|
|
|
164
204
|
SELECT key, value
|
|
165
205
|
FROM jsonb_each(schema->'type')
|
|
166
206
|
LOOP
|
|
207
|
+
|
|
167
208
|
optional = COALESCE((sub_schema.value->>'optional')::BOOLEAN, FALSE);
|
|
168
209
|
IF NOT (data ? sub_schema.key) THEN
|
|
169
|
-
|
|
170
210
|
IF NOT optional THEN
|
|
171
211
|
RAISE EXCEPTION 'Types not matching. Required property (%) is missing. %',sub_schema.key , path USING HINT = path, COLUMN = colname;
|
|
172
|
-
ELSE
|
|
173
|
-
RETURN true;
|
|
174
212
|
END IF;
|
|
175
|
-
END IF;
|
|
176
213
|
|
|
177
|
-
|
|
214
|
+
ELSIF NOT ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
178
215
|
-- sub_schema.value::TEXT,
|
|
179
216
|
CASE WHEN jsonb_typeof(sub_schema.value) = 'string' THEN TRIM(both '"' from sub_schema.value::TEXT) ELSE sub_schema.value::TEXT END,
|
|
180
217
|
data->sub_schema.key,
|
|
@@ -184,6 +221,8 @@ BEGIN
|
|
|
184
221
|
END IF;
|
|
185
222
|
|
|
186
223
|
END LOOP;
|
|
224
|
+
|
|
225
|
+
RETURN TRUE;
|
|
187
226
|
ELSE
|
|
188
227
|
RAISE EXCEPTION 'Unexpected schema.type ( % ), %',jsonb_typeof(schema->'type'), path USING HINT = path, COLUMN = colname;
|
|
189
228
|
END IF;
|
|
@@ -354,5 +393,8 @@ SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "type": { "a": { "enum": ["a"] } }
|
|
|
354
393
|
|
|
355
394
|
SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "arrayOfType": { "a": { "enum": ["a"] } } }', '[{ "a": "a"}]');
|
|
356
395
|
|
|
396
|
+
|
|
397
|
+
SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "lookup": { "type": "data", "table": "tblName", "column": "colName" } }', '{}');
|
|
398
|
+
|
|
357
399
|
`;
|
|
358
400
|
//# sourceMappingURL=validate_jsonb_schema_sql.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate_jsonb_schema_sql.js","sourceRoot":"","sources":["../../lib/JSONBValidation/validate_jsonb_schema_sql.ts"],"names":[],"mappings":";;;AAAA,kEAA+D;AAGlD,QAAA,wBAAwB,GAAG,uBAAgC,CAAC;AAC5D,QAAA,yBAAyB,GAAG;;;IAGrC,6BAAa,CAAC,kCAAkC;;;4BAGxB,gCAAwB;;6BAEvB,gCAAwB
|
|
1
|
+
{"version":3,"file":"validate_jsonb_schema_sql.js","sourceRoot":"","sources":["../../lib/JSONBValidation/validate_jsonb_schema_sql.ts"],"names":[],"mappings":";;;AAAA,kEAA+D;AAGlD,QAAA,wBAAwB,GAAG,uBAAgC,CAAC;AAC5D,QAAA,yBAAyB,GAAG;;;IAGrC,6BAAa,CAAC,kCAAkC;;;4BAGxB,gCAAwB;;6BAEvB,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aA8DxC,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAiCxB,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA2BlB,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;oBA8EvB,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAgC/B,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;eA4BtB,gCAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6EAgCsC,gCAAwB;;;;;;oEAMjC,gCAAwB;;;;;;;;;;;;;;;sBAetE,gCAAwB;;;;;;;;;;;;SAYrC,gCAAwB;;;;;;SAMxB,gCAAwB;;;;;SAKxB,gCAAwB;;;;SAIxB,gCAAwB;;;;;SAKxB,gCAAwB;;;;;;;;;;;;;;;;SAgBxB,gCAAwB;;;;;SAKxB,gCAAwB;;;;;SAKxB,gCAAwB;;;;;SAKxB,gCAAwB;;SAExB,gCAAwB;;SAExB,gCAAwB;;;SAGxB,gCAAwB;;CAEhC,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { JSONB } from "prostgles-types";
|
|
1
|
+
import { JSONB, TableSchema } from "prostgles-types";
|
|
2
2
|
export declare function validate<T>(obj: T, key: keyof T, rawFieldType: JSONB.FieldType): boolean;
|
|
3
3
|
export declare function validateSchema<S extends JSONB.ObjectType["type"]>(schema: S, obj: JSONB.GetObjectType<S>, objName?: string, optional?: boolean): void;
|
|
4
4
|
type ColOpts = {
|
|
5
5
|
nullable?: boolean;
|
|
6
6
|
};
|
|
7
|
-
export declare function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading
|
|
7
|
+
export declare function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading: string | undefined, tables: TableSchema[]): string;
|
|
8
8
|
export {};
|
|
9
9
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../lib/JSONBValidation/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../lib/JSONBValidation/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAYjF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,SAAS,GAAG,OAAO,CAqBxF;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,UAAQ,QAG5I;AAGD,KAAK,OAAO,GAAG;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAGtC,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,oBAAK,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAqEnI"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getJSONBSchemaTSTypes = exports.validateSchema = exports.validate = void 0;
|
|
4
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
+
const DboBuilder_1 = require("../DboBuilder");
|
|
5
6
|
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
6
7
|
const getFieldTypeObj = (rawFieldType) => {
|
|
7
8
|
if (typeof rawFieldType === "string")
|
|
@@ -42,13 +43,17 @@ function validateSchema(schema, obj, objName, optional = false) {
|
|
|
42
43
|
(0, prostgles_types_1.getKeys)(schema).forEach(k => validate(obj, k, schema[k]));
|
|
43
44
|
}
|
|
44
45
|
exports.validateSchema = validateSchema;
|
|
45
|
-
function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "") {
|
|
46
|
+
function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "", tables) {
|
|
46
47
|
const getFieldType = (rawFieldType, isOneOf = false, innerLeading = "", depth = 0) => {
|
|
47
48
|
const fieldType = getFieldTypeObj(rawFieldType);
|
|
48
49
|
const nullType = (fieldType.nullable ? `null | ` : "");
|
|
49
50
|
/** Primitives */
|
|
50
51
|
if (typeof fieldType?.type === "string") {
|
|
51
|
-
const correctType = fieldType.type
|
|
52
|
+
const correctType = fieldType.type
|
|
53
|
+
.replace("integer", "number")
|
|
54
|
+
.replace("time", "string")
|
|
55
|
+
.replace("timestamp", "string")
|
|
56
|
+
.replace("Date", "string");
|
|
52
57
|
if (fieldType.allowedValues && fieldType.type.endsWith("[]")) {
|
|
53
58
|
return nullType + ` (${fieldType.allowedValues.map(v => JSON.stringify(v)).join(" | ")})[]`;
|
|
54
59
|
}
|
|
@@ -86,6 +91,21 @@ function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "") {
|
|
|
86
91
|
const { keysEnum, values } = fieldType.record;
|
|
87
92
|
return `${fieldType.nullable ? `null |` : ""} Record<${keysEnum?.map(v => (0, PubSubManager_1.asValue)(v)).join(" | ") ?? "string"}, ${!values ? "any" : getFieldType(values, true, undefined, depth + 1)}>`;
|
|
88
93
|
}
|
|
94
|
+
else if (fieldType?.lookup) {
|
|
95
|
+
const l = fieldType.lookup;
|
|
96
|
+
const isSChema = l.type === "schema";
|
|
97
|
+
let type = isSChema ? "string" : "";
|
|
98
|
+
if (!isSChema) {
|
|
99
|
+
const cols = tables.find(t => t.name === l.table)?.columns;
|
|
100
|
+
if (!l.isFullRow) {
|
|
101
|
+
type = (0, DboBuilder_1.postgresToTsType)(cols?.find(c => c.name === l.column)?.udt_name ?? "text");
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
type = !cols ? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable ? "null | " : ""} ${(0, DboBuilder_1.postgresToTsType)(c.udt_name)}; `).join(" ")} }`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return `${fieldType.nullable ? `null |` : ""} ${type}${l.isArray ? "[]" : ""}`;
|
|
108
|
+
}
|
|
89
109
|
else
|
|
90
110
|
throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2);
|
|
91
111
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../lib/JSONBValidation/validation.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../lib/JSONBValidation/validation.ts"],"names":[],"mappings":";;;AAAA,qDAAiF;AACjF,8CAAiD;AACjD,kEAAyD;AAIzD,MAAM,eAAe,GAAG,CAAC,YAA6B,EAAsB,EAAE;IAC5E,IAAG,OAAO,YAAY,KAAK,QAAQ;QAAE,OAAO,EAAE,IAAI,EAAE,YAAY,EAAE,CAAC;IAEnE,OAAO,YAAY,CAAC;AACtB,CAAC,CAAA;AAED,SAAgB,QAAQ,CAAI,GAAM,EAAE,GAAY,EAAE,YAA6B;IAC7E,IAAI,GAAG,GAAG,0BAA0B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,iCAAiC,CAAC;IACzF,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;IACrB,MAAM,SAAS,GAAG,eAAe,CAAC,YAAY,CAAC,CAAC;IAChD,IAAI,MAAM,IAAI,SAAS,IAAI,SAAS,CAAC,IAAI,EAAE;QACzC,IAAI,OAAO,SAAS,CAAC,IAAI,KAAK,QAAQ,EAAE;YACtC,IAAA,yBAAO,EAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;gBACvC,QAAQ,CAAC,GAAG,EAAE,MAAa,EAAG,SAAS,CAAC,IAAiC,CAAC,MAAM,CAAC,CAAC,CAAA;YACpF,CAAC,CAAC,CAAC;SACJ;QACD,GAAG,IAAI,SAAS,CAAC,IAAI,CAAC;QACtB,IAAI,SAAS,CAAC,IAAI,KAAK,SAAS,IAAI,OAAO,GAAG,KAAK,SAAS,CAAC,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAA;QACvF,IAAI,SAAS,CAAC,IAAI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
|
19
19
|
if (colConf) {
|
|
20
20
|
if ((0, prostgles_types_1.isObject)(colConf) && (colConf.jsonbSchema || colConf.jsonbSchemaType)) {
|
|
21
21
|
const schema = colConf.jsonbSchema || { ...colConf, type: colConf.jsonbSchemaType };
|
|
22
|
-
type = (0, validation_1.getJSONBSchemaTSTypes)(schema, { nullable: colConf.nullable }, " ");
|
|
22
|
+
type = (0, validation_1.getJSONBSchemaTSTypes)(schema, { nullable: colConf.nullable }, " ", dboBuilder.tablesOrViews ?? []);
|
|
23
23
|
}
|
|
24
24
|
else if ((0, prostgles_types_1.isObject)(colConf) && "enum" in colConf) {
|
|
25
25
|
if (!colConf.enum)
|
package/lib/DBSchemaBuilder.ts
CHANGED
|
@@ -20,7 +20,7 @@ export const getDBSchema = (dboBuilder: DboBuilder): string => {
|
|
|
20
20
|
if(isObject(colConf) && (colConf.jsonbSchema || colConf.jsonbSchemaType)){
|
|
21
21
|
const schema: JSONB.JSONBSchema = colConf.jsonbSchema || { ...colConf, type: colConf.jsonbSchemaType };
|
|
22
22
|
|
|
23
|
-
type = getJSONBSchemaTSTypes(schema, { nullable: colConf.nullable }, " ");
|
|
23
|
+
type = getJSONBSchemaTSTypes(schema, { nullable: colConf.nullable }, " ", dboBuilder.tablesOrViews ?? []);
|
|
24
24
|
} else if(isObject(colConf) && "enum" in colConf){
|
|
25
25
|
if(!colConf.enum) throw "colConf.enum missing"
|
|
26
26
|
const types = colConf.enum.map(t => typeof t === "number"? t : JSON.stringify(t));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate_jsonb_schema_sql.d.ts","sourceRoot":"","sources":["validate_jsonb_schema_sql.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,wBAAwB,yBAAmC,CAAC;AACzE,eAAO,MAAM,yBAAyB,
|
|
1
|
+
{"version":3,"file":"validate_jsonb_schema_sql.d.ts","sourceRoot":"","sources":["validate_jsonb_schema_sql.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,wBAAwB,yBAAmC,CAAC;AACzE,eAAO,MAAM,yBAAyB,QAyYrC,CAAC"}
|
|
@@ -44,7 +44,7 @@ BEGIN
|
|
|
44
44
|
IF ARRAY[jsonb_schema] <@ allowed_types THEN
|
|
45
45
|
schema = jsonb_build_object('type', jsonb_schema);
|
|
46
46
|
/* { "type": ... } */
|
|
47
|
-
ELSIF BTRIM(jsonb_schema) ILIKE '{%' THEN
|
|
47
|
+
ELSIF BTRIM(replace(jsonb_schema,E'\n','')) ILIKE '{%' THEN
|
|
48
48
|
schema = jsonb_schema::JSONB;
|
|
49
49
|
ELSE
|
|
50
50
|
RAISE EXCEPTION $$Invalid schema. Expecting 'typename' or { "type": "typename" } but received: %, %$$, jsonb_schema, path USING HINT = path, COLUMN = colname;
|
|
@@ -72,6 +72,46 @@ BEGIN
|
|
|
72
72
|
RAISE EXCEPTION 'Data not in allowed enum list (%), %', schema->'enum', path USING HINT = path, COLUMN = colname;
|
|
73
73
|
END IF;
|
|
74
74
|
|
|
75
|
+
ELSIF schema ? 'lookup' THEN
|
|
76
|
+
IF NOT ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
77
|
+
$sch$
|
|
78
|
+
{
|
|
79
|
+
"oneOfType": [
|
|
80
|
+
{
|
|
81
|
+
"type": { "enum": ["schema"] },
|
|
82
|
+
"object": { "enum": ["table", "column"] },
|
|
83
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
84
|
+
"filter": { "optional": true, "type": "any" }
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"type": { "enum": ["data"] },
|
|
88
|
+
"table": "string",
|
|
89
|
+
"column": "string",
|
|
90
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
91
|
+
"filter": { "optional": true, "type": "any" },
|
|
92
|
+
"isFullRow": { "optional": true, "type": {
|
|
93
|
+
"displayColumns": { "optional": true, "type": "string[]" },
|
|
94
|
+
"searchColumns": { "optional": true, "type": "string[]" }
|
|
95
|
+
}},
|
|
96
|
+
"showInRowCard": { "optional": true, "type": "any" }
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
$sch$,
|
|
101
|
+
schema->'lookup',
|
|
102
|
+
checked_path || '.schema'::TEXT
|
|
103
|
+
) THEN
|
|
104
|
+
|
|
105
|
+
RETURN FALSE;
|
|
106
|
+
END IF;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
RETURN ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
110
|
+
CASE WHEN (schema->'lookup'->'isArray')::BOOLEAN THEN 'any[]' ELSE 'any' END,
|
|
111
|
+
data,
|
|
112
|
+
checked_path
|
|
113
|
+
);
|
|
114
|
+
|
|
75
115
|
ELSIF schema ? 'type' THEN
|
|
76
116
|
|
|
77
117
|
IF jsonb_typeof(schema->'type') = 'string' THEN
|
|
@@ -164,17 +204,14 @@ BEGIN
|
|
|
164
204
|
SELECT key, value
|
|
165
205
|
FROM jsonb_each(schema->'type')
|
|
166
206
|
LOOP
|
|
207
|
+
|
|
167
208
|
optional = COALESCE((sub_schema.value->>'optional')::BOOLEAN, FALSE);
|
|
168
209
|
IF NOT (data ? sub_schema.key) THEN
|
|
169
|
-
|
|
170
210
|
IF NOT optional THEN
|
|
171
211
|
RAISE EXCEPTION 'Types not matching. Required property (%) is missing. %',sub_schema.key , path USING HINT = path, COLUMN = colname;
|
|
172
|
-
ELSE
|
|
173
|
-
RETURN true;
|
|
174
212
|
END IF;
|
|
175
|
-
END IF;
|
|
176
213
|
|
|
177
|
-
|
|
214
|
+
ELSIF NOT ${exports.VALIDATE_SCHEMA_FUNCNAME}(
|
|
178
215
|
-- sub_schema.value::TEXT,
|
|
179
216
|
CASE WHEN jsonb_typeof(sub_schema.value) = 'string' THEN TRIM(both '"' from sub_schema.value::TEXT) ELSE sub_schema.value::TEXT END,
|
|
180
217
|
data->sub_schema.key,
|
|
@@ -184,6 +221,8 @@ BEGIN
|
|
|
184
221
|
END IF;
|
|
185
222
|
|
|
186
223
|
END LOOP;
|
|
224
|
+
|
|
225
|
+
RETURN TRUE;
|
|
187
226
|
ELSE
|
|
188
227
|
RAISE EXCEPTION 'Unexpected schema.type ( % ), %',jsonb_typeof(schema->'type'), path USING HINT = path, COLUMN = colname;
|
|
189
228
|
END IF;
|
|
@@ -354,4 +393,7 @@ SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "type": { "a": { "enum": ["a"] } }
|
|
|
354
393
|
|
|
355
394
|
SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "arrayOfType": { "a": { "enum": ["a"] } } }', '[{ "a": "a"}]');
|
|
356
395
|
|
|
396
|
+
|
|
397
|
+
SELECT ${exports.VALIDATE_SCHEMA_FUNCNAME}('{ "lookup": { "type": "data", "table": "tblName", "column": "colName" } }', '{}');
|
|
398
|
+
|
|
357
399
|
`;
|
|
@@ -43,7 +43,7 @@ BEGIN
|
|
|
43
43
|
IF ARRAY[jsonb_schema] <@ allowed_types THEN
|
|
44
44
|
schema = jsonb_build_object('type', jsonb_schema);
|
|
45
45
|
/* { "type": ... } */
|
|
46
|
-
ELSIF BTRIM(jsonb_schema) ILIKE '{%' THEN
|
|
46
|
+
ELSIF BTRIM(replace(jsonb_schema,E'\n','')) ILIKE '{%' THEN
|
|
47
47
|
schema = jsonb_schema::JSONB;
|
|
48
48
|
ELSE
|
|
49
49
|
RAISE EXCEPTION $$Invalid schema. Expecting 'typename' or { "type": "typename" } but received: %, %$$, jsonb_schema, path USING HINT = path, COLUMN = colname;
|
|
@@ -71,6 +71,46 @@ BEGIN
|
|
|
71
71
|
RAISE EXCEPTION 'Data not in allowed enum list (%), %', schema->'enum', path USING HINT = path, COLUMN = colname;
|
|
72
72
|
END IF;
|
|
73
73
|
|
|
74
|
+
ELSIF schema ? 'lookup' THEN
|
|
75
|
+
IF NOT ${VALIDATE_SCHEMA_FUNCNAME}(
|
|
76
|
+
$sch$
|
|
77
|
+
{
|
|
78
|
+
"oneOfType": [
|
|
79
|
+
{
|
|
80
|
+
"type": { "enum": ["schema"] },
|
|
81
|
+
"object": { "enum": ["table", "column"] },
|
|
82
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
83
|
+
"filter": { "optional": true, "type": "any" }
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"type": { "enum": ["data"] },
|
|
87
|
+
"table": "string",
|
|
88
|
+
"column": "string",
|
|
89
|
+
"isArray": { "type": "boolean", "optional": true },
|
|
90
|
+
"filter": { "optional": true, "type": "any" },
|
|
91
|
+
"isFullRow": { "optional": true, "type": {
|
|
92
|
+
"displayColumns": { "optional": true, "type": "string[]" },
|
|
93
|
+
"searchColumns": { "optional": true, "type": "string[]" }
|
|
94
|
+
}},
|
|
95
|
+
"showInRowCard": { "optional": true, "type": "any" }
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
$sch$,
|
|
100
|
+
schema->'lookup',
|
|
101
|
+
checked_path || '.schema'::TEXT
|
|
102
|
+
) THEN
|
|
103
|
+
|
|
104
|
+
RETURN FALSE;
|
|
105
|
+
END IF;
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
RETURN ${VALIDATE_SCHEMA_FUNCNAME}(
|
|
109
|
+
CASE WHEN (schema->'lookup'->'isArray')::BOOLEAN THEN 'any[]' ELSE 'any' END,
|
|
110
|
+
data,
|
|
111
|
+
checked_path
|
|
112
|
+
);
|
|
113
|
+
|
|
74
114
|
ELSIF schema ? 'type' THEN
|
|
75
115
|
|
|
76
116
|
IF jsonb_typeof(schema->'type') = 'string' THEN
|
|
@@ -163,17 +203,14 @@ BEGIN
|
|
|
163
203
|
SELECT key, value
|
|
164
204
|
FROM jsonb_each(schema->'type')
|
|
165
205
|
LOOP
|
|
206
|
+
|
|
166
207
|
optional = COALESCE((sub_schema.value->>'optional')::BOOLEAN, FALSE);
|
|
167
208
|
IF NOT (data ? sub_schema.key) THEN
|
|
168
|
-
|
|
169
209
|
IF NOT optional THEN
|
|
170
210
|
RAISE EXCEPTION 'Types not matching. Required property (%) is missing. %',sub_schema.key , path USING HINT = path, COLUMN = colname;
|
|
171
|
-
ELSE
|
|
172
|
-
RETURN true;
|
|
173
211
|
END IF;
|
|
174
|
-
END IF;
|
|
175
212
|
|
|
176
|
-
|
|
213
|
+
ELSIF NOT ${VALIDATE_SCHEMA_FUNCNAME}(
|
|
177
214
|
-- sub_schema.value::TEXT,
|
|
178
215
|
CASE WHEN jsonb_typeof(sub_schema.value) = 'string' THEN TRIM(both '"' from sub_schema.value::TEXT) ELSE sub_schema.value::TEXT END,
|
|
179
216
|
data->sub_schema.key,
|
|
@@ -183,6 +220,8 @@ BEGIN
|
|
|
183
220
|
END IF;
|
|
184
221
|
|
|
185
222
|
END LOOP;
|
|
223
|
+
|
|
224
|
+
RETURN TRUE;
|
|
186
225
|
ELSE
|
|
187
226
|
RAISE EXCEPTION 'Unexpected schema.type ( % ), %',jsonb_typeof(schema->'type'), path USING HINT = path, COLUMN = colname;
|
|
188
227
|
END IF;
|
|
@@ -353,4 +392,7 @@ SELECT ${VALIDATE_SCHEMA_FUNCNAME}('{ "type": { "a": { "enum": ["a"] } } }', '{
|
|
|
353
392
|
|
|
354
393
|
SELECT ${VALIDATE_SCHEMA_FUNCNAME}('{ "arrayOfType": { "a": { "enum": ["a"] } } }', '[{ "a": "a"}]');
|
|
355
394
|
|
|
395
|
+
|
|
396
|
+
SELECT ${VALIDATE_SCHEMA_FUNCNAME}('{ "lookup": { "type": "data", "table": "tblName", "column": "colName" } }', '{}');
|
|
397
|
+
|
|
356
398
|
`;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { JSONB } from "prostgles-types";
|
|
1
|
+
import { JSONB, TableSchema } from "prostgles-types";
|
|
2
2
|
export declare function validate<T>(obj: T, key: keyof T, rawFieldType: JSONB.FieldType): boolean;
|
|
3
3
|
export declare function validateSchema<S extends JSONB.ObjectType["type"]>(schema: S, obj: JSONB.GetObjectType<S>, objName?: string, optional?: boolean): void;
|
|
4
4
|
type ColOpts = {
|
|
5
5
|
nullable?: boolean;
|
|
6
6
|
};
|
|
7
|
-
export declare function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading
|
|
7
|
+
export declare function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading: string, tables: TableSchema[]): string;
|
|
8
8
|
export {};
|
|
9
9
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAYjF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,SAAS,GAAG,OAAO,CAqBxF;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,UAAQ,QAG5I;AAGD,KAAK,OAAO,GAAG;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAGtC,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,KAAK,CAAC,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,QAAK,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,CAqEnI"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getJSONBSchemaTSTypes = exports.validateSchema = exports.validate = void 0;
|
|
4
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
+
const DboBuilder_1 = require("../DboBuilder");
|
|
5
6
|
const PubSubManager_1 = require("../PubSubManager/PubSubManager");
|
|
6
7
|
const getFieldTypeObj = (rawFieldType) => {
|
|
7
8
|
if (typeof rawFieldType === "string")
|
|
@@ -42,13 +43,17 @@ function validateSchema(schema, obj, objName, optional = false) {
|
|
|
42
43
|
(0, prostgles_types_1.getKeys)(schema).forEach(k => validate(obj, k, schema[k]));
|
|
43
44
|
}
|
|
44
45
|
exports.validateSchema = validateSchema;
|
|
45
|
-
function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "") {
|
|
46
|
+
function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "", tables) {
|
|
46
47
|
const getFieldType = (rawFieldType, isOneOf = false, innerLeading = "", depth = 0) => {
|
|
47
48
|
const fieldType = getFieldTypeObj(rawFieldType);
|
|
48
49
|
const nullType = (fieldType.nullable ? `null | ` : "");
|
|
49
50
|
/** Primitives */
|
|
50
51
|
if (typeof fieldType?.type === "string") {
|
|
51
|
-
const correctType = fieldType.type
|
|
52
|
+
const correctType = fieldType.type
|
|
53
|
+
.replace("integer", "number")
|
|
54
|
+
.replace("time", "string")
|
|
55
|
+
.replace("timestamp", "string")
|
|
56
|
+
.replace("Date", "string");
|
|
52
57
|
if (fieldType.allowedValues && fieldType.type.endsWith("[]")) {
|
|
53
58
|
return nullType + ` (${fieldType.allowedValues.map(v => JSON.stringify(v)).join(" | ")})[]`;
|
|
54
59
|
}
|
|
@@ -86,6 +91,21 @@ function getJSONBSchemaTSTypes(schema, colOpts, outerLeading = "") {
|
|
|
86
91
|
const { keysEnum, values } = fieldType.record;
|
|
87
92
|
return `${fieldType.nullable ? `null |` : ""} Record<${keysEnum?.map(v => (0, PubSubManager_1.asValue)(v)).join(" | ") ?? "string"}, ${!values ? "any" : getFieldType(values, true, undefined, depth + 1)}>`;
|
|
88
93
|
}
|
|
94
|
+
else if (fieldType?.lookup) {
|
|
95
|
+
const l = fieldType.lookup;
|
|
96
|
+
const isSChema = l.type === "schema";
|
|
97
|
+
let type = isSChema ? "string" : "";
|
|
98
|
+
if (!isSChema) {
|
|
99
|
+
const cols = tables.find(t => t.name === l.table)?.columns;
|
|
100
|
+
if (!l.isFullRow) {
|
|
101
|
+
type = (0, DboBuilder_1.postgresToTsType)(cols?.find(c => c.name === l.column)?.udt_name ?? "text");
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
type = !cols ? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable ? "null | " : ""} ${(0, DboBuilder_1.postgresToTsType)(c.udt_name)}; `).join(" ")} }`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return `${fieldType.nullable ? `null |` : ""} ${type}${l.isArray ? "[]" : ""}`;
|
|
108
|
+
}
|
|
89
109
|
else
|
|
90
110
|
throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2);
|
|
91
111
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { getKeys, isEmpty, isObject, JSONB } from "prostgles-types";
|
|
1
|
+
import { getKeys, isEmpty, isObject, JSONB, TableSchema } from "prostgles-types";
|
|
2
|
+
import { postgresToTsType } from "../DboBuilder";
|
|
2
3
|
import { asValue } from "../PubSubManager/PubSubManager";
|
|
3
4
|
|
|
4
5
|
|
|
@@ -41,7 +42,7 @@ export function validateSchema<S extends JSONB.ObjectType["type"]>(schema: S, ob
|
|
|
41
42
|
type ColOpts = { nullable?: boolean };
|
|
42
43
|
|
|
43
44
|
|
|
44
|
-
export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading = ""): string {
|
|
45
|
+
export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpts, outerLeading = "", tables: TableSchema[]): string {
|
|
45
46
|
|
|
46
47
|
const getFieldType = (rawFieldType: JSONB.FieldType, isOneOf = false, innerLeading = "", depth = 0): string => {
|
|
47
48
|
const fieldType = getFieldTypeObj(rawFieldType);
|
|
@@ -49,7 +50,12 @@ export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpt
|
|
|
49
50
|
|
|
50
51
|
/** Primitives */
|
|
51
52
|
if (typeof fieldType?.type === "string") {
|
|
52
|
-
const correctType = fieldType.type
|
|
53
|
+
const correctType = fieldType.type
|
|
54
|
+
.replace("integer", "number")
|
|
55
|
+
.replace("time", "string")
|
|
56
|
+
.replace("timestamp", "string")
|
|
57
|
+
.replace("Date", "string");
|
|
58
|
+
|
|
53
59
|
if (fieldType.allowedValues && fieldType.type.endsWith("[]")) {
|
|
54
60
|
return nullType + ` (${fieldType.allowedValues.map(v => JSON.stringify(v)).join(" | ")})[]`
|
|
55
61
|
}
|
|
@@ -88,6 +94,19 @@ export function getJSONBSchemaTSTypes(schema: JSONB.JSONBSchema, colOpts: ColOpt
|
|
|
88
94
|
const { keysEnum, values } = fieldType.record;
|
|
89
95
|
return `${fieldType.nullable ? `null |` : ""} Record<${keysEnum?.map(v => asValue(v)).join(" | ") ?? "string"}, ${!values? "any" : getFieldType(values, true, undefined, depth + 1)}>`
|
|
90
96
|
|
|
97
|
+
} else if(fieldType?.lookup){
|
|
98
|
+
const l = fieldType.lookup
|
|
99
|
+
const isSChema = l.type === "schema";
|
|
100
|
+
let type = isSChema? "string" : "";
|
|
101
|
+
if(!isSChema){
|
|
102
|
+
const cols = tables.find(t => t.name === l.table)?.columns
|
|
103
|
+
if(!l.isFullRow){
|
|
104
|
+
type = postgresToTsType(cols?.find(c => c.name === l.column)?.udt_name ?? "text");
|
|
105
|
+
} else {
|
|
106
|
+
type = !cols? "any" : `{ ${cols.map(c => `${JSON.stringify(c.name)}: ${c.is_nullable? "null | " : "" } ${postgresToTsType(c.udt_name)}; `).join(" ")} }`
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return `${fieldType.nullable ? `null |` : ""} ${type}${l.isArray? "[]" : ""}`;
|
|
91
110
|
} else throw "Unexpected getSchemaTSTypes: " + JSON.stringify({ fieldType, schema }, null, 2)
|
|
92
111
|
}
|
|
93
112
|
|
package/package.json
CHANGED
package/tests/client/PID.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
25798
|