prostgles-server 2.0.332 → 2.0.334
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 +4 -4
- package/dist/DBSchemaBuilder.js.map +1 -1
- package/dist/TableConfig.d.ts +21 -10
- package/dist/TableConfig.d.ts.map +1 -1
- package/dist/TableConfig.js +5 -5
- package/dist/TableConfig.js.map +1 -1
- package/dist/validation.d.ts +10 -10
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +29 -30
- package/dist/validation.js.map +1 -1
- package/lib/DBSchemaBuilder.js +4 -4
- package/lib/DBSchemaBuilder.ts +3 -3
- package/lib/TableConfig.d.ts +21 -10
- package/lib/TableConfig.d.ts.map +1 -1
- package/lib/TableConfig.js +5 -5
- package/lib/TableConfig.ts +25 -15
- package/lib/validation.d.ts +10 -10
- package/lib/validation.d.ts.map +1 -1
- package/lib/validation.js +29 -30
- package/lib/validation.ts +113 -114
- package/package.json +1 -1
- package/tests/client/PID.txt +1 -1
- package/tests/isomorphic_queries.d.ts.map +1 -1
- package/tests/isomorphic_queries.js +5 -7
- package/tests/isomorphic_queries.ts +5 -7
- package/tests/server/index.js +10 -10
- package/tests/server/index.ts +10 -10
- package/tests/server/package-lock.json +1 -1
- package/tests/server/server.ts +2 -2
package/lib/TableConfig.js
CHANGED
|
@@ -205,11 +205,11 @@ class TableConfigurator {
|
|
|
205
205
|
const checkStatement = (0, validation_1.getPGCheckConstraint)({ schema: colConf.jsonbSchema, escapedFieldName: colNameEsc, nullable: !!colConf.nullable }, 0);
|
|
206
206
|
return ` ${colNameEsc} ${getColTypeDef(colConf, "JSONB")} CHECK(${checkStatement})`;
|
|
207
207
|
}
|
|
208
|
-
else if ("
|
|
209
|
-
if (!colConf.
|
|
210
|
-
throw new Error("colConf.
|
|
211
|
-
const type = colConf.
|
|
212
|
-
const checks = colConf.
|
|
208
|
+
else if ("enum" in colConf) {
|
|
209
|
+
if (!colConf.enum?.length)
|
|
210
|
+
throw new Error("colConf.enum Must not be empty");
|
|
211
|
+
const type = colConf.enum.every(v => Number.isFinite(v)) ? "NUMERIC" : "TEXT";
|
|
212
|
+
const checks = colConf.enum.map(v => `${colNameEsc} = ${(0, PubSubManager_1.asValue)(v)}`).join(" OR ");
|
|
213
213
|
return ` ${colNameEsc} ${type} ${colConf.nullable ? "" : "NOT NULL"} ${"defaultValue" in colConf ? ` DEFAULT ${(0, PubSubManager_1.asValue)(colConf.defaultValue)}` : ""} CHECK(${checks})`;
|
|
214
214
|
}
|
|
215
215
|
else {
|
package/lib/TableConfig.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { getKeys, asName, AnyObject, TableInfo, ALLOWED_EXTENSION, ALLOWED_CONT
|
|
|
2
2
|
import { isPlainObject, JoinInfo } from "./DboBuilder";
|
|
3
3
|
import { DB, DBHandlerServer, Joins, Prostgles } from "./Prostgles";
|
|
4
4
|
import { asValue } from "./PubSubManager";
|
|
5
|
-
import { getJSONBSchemaAsJSONSchema, getPGCheckConstraint,
|
|
5
|
+
import { getJSONBSchemaAsJSONSchema, getPGCheckConstraint, OneOf, ValidationSchema } from "./validation";
|
|
6
6
|
|
|
7
7
|
type ColExtraInfo = {
|
|
8
8
|
min?: string | number;
|
|
@@ -99,12 +99,12 @@ type SQLDefColumn = {
|
|
|
99
99
|
sqlDefinition?: string;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
type
|
|
103
|
-
defaultValue?:
|
|
102
|
+
type BaseColumnTypes = {
|
|
103
|
+
defaultValue?: any;
|
|
104
104
|
nullable?: boolean;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
type TextColumn =
|
|
107
|
+
type TextColumn = BaseColumnTypes & {
|
|
108
108
|
isText: true;
|
|
109
109
|
/**
|
|
110
110
|
* Value will be trimmed before update/insert
|
|
@@ -117,8 +117,8 @@ type TextColumn = TextColDef & {
|
|
|
117
117
|
lowerCased?: boolean;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
export type JSONBColumnDef =
|
|
121
|
-
jsonbSchema: ValidationSchema | Omit<
|
|
120
|
+
export type JSONBColumnDef = BaseColumnTypes & {
|
|
121
|
+
jsonbSchema: ValidationSchema | Omit<OneOf, "optional">;
|
|
122
122
|
|
|
123
123
|
/**
|
|
124
124
|
* If the new schema CHECK fails old rows the update old rows using this function
|
|
@@ -153,7 +153,7 @@ type ReferencedColumn = {
|
|
|
153
153
|
/**
|
|
154
154
|
* Will create a lookup table that this column will reference
|
|
155
155
|
*/
|
|
156
|
-
references?:
|
|
156
|
+
references?: BaseColumnTypes & {
|
|
157
157
|
|
|
158
158
|
|
|
159
159
|
tableName: string;
|
|
@@ -179,13 +179,23 @@ type NamedJoinColumn = {
|
|
|
179
179
|
joinDef: JoinDef[];
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
type
|
|
183
|
-
|
|
182
|
+
type Enum<T extends string | number = any> = {
|
|
183
|
+
enum: T[];
|
|
184
184
|
nullable?: boolean;
|
|
185
185
|
defaultValue?: T;
|
|
186
186
|
};
|
|
187
187
|
|
|
188
|
-
export type ColumnConfig<LANG_IDS = { en: 1 }> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef |
|
|
188
|
+
export type ColumnConfig<LANG_IDS = { en: 1 }> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef | Enum))>;
|
|
189
|
+
|
|
190
|
+
export type ColumnConfigs<LANG_IDS = { en: 1 }> = {
|
|
191
|
+
sql: string | BaseColumn<LANG_IDS> & SQLDefColumn;
|
|
192
|
+
join: BaseColumn<LANG_IDS> & NamedJoinColumn;
|
|
193
|
+
media: BaseColumn<LANG_IDS> & MediaColumn;
|
|
194
|
+
referenced: BaseColumn<LANG_IDS> & ReferencedColumn;
|
|
195
|
+
text: BaseColumn<LANG_IDS> & TextColumn;
|
|
196
|
+
jsonb: BaseColumn<LANG_IDS> & JSONBColumnDef;
|
|
197
|
+
enum: BaseColumn<LANG_IDS> & Enum;
|
|
198
|
+
}
|
|
189
199
|
|
|
190
200
|
type UnionKeys<T> = T extends T ? keyof T : never;
|
|
191
201
|
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
|
|
@@ -416,7 +426,7 @@ export default class TableConfigurator<LANG_IDS = { en: 1 }> {
|
|
|
416
426
|
if ("columns" in tableConf) {
|
|
417
427
|
const getColDef = (name: string, colConf: ColumnConfig): string => {
|
|
418
428
|
const colNameEsc = asName(name);
|
|
419
|
-
const getColTypeDef = (colConf:
|
|
429
|
+
const getColTypeDef = (colConf: BaseColumnTypes, pgType: "TEXT" | "JSONB") => {
|
|
420
430
|
const { nullable, defaultValue } = colConf;
|
|
421
431
|
return `${pgType} ${!nullable ? " NOT NULL " : ""} ${defaultValue ? ` DEFAULT ${asValue(defaultValue)} ` : ""}`
|
|
422
432
|
}
|
|
@@ -458,10 +468,10 @@ export default class TableConfigurator<LANG_IDS = { en: 1 }> {
|
|
|
458
468
|
const checkStatement = getPGCheckConstraint({ schema: colConf.jsonbSchema, escapedFieldName: colNameEsc, nullable: !!colConf.nullable }, 0)
|
|
459
469
|
return ` ${colNameEsc} ${getColTypeDef(colConf, "JSONB")} CHECK(${checkStatement})`;
|
|
460
470
|
|
|
461
|
-
} else if("
|
|
462
|
-
if(!colConf.
|
|
463
|
-
const type = colConf.
|
|
464
|
-
const checks = colConf.
|
|
471
|
+
} else if("enum" in colConf) {
|
|
472
|
+
if(!colConf.enum?.length) throw new Error("colConf.enum Must not be empty");
|
|
473
|
+
const type = colConf.enum.every(v => Number.isFinite(v))? "NUMERIC" : "TEXT";
|
|
474
|
+
const checks = colConf.enum.map(v => `${colNameEsc} = ${asValue(v)}`).join(" OR ");
|
|
465
475
|
return ` ${colNameEsc} ${type} ${colConf.nullable? "" : "NOT NULL"} ${"defaultValue" in colConf? ` DEFAULT ${asValue(colConf.defaultValue)}` : ""} CHECK(${checks})`;
|
|
466
476
|
|
|
467
477
|
} else {
|
package/lib/validation.d.ts
CHANGED
|
@@ -9,12 +9,12 @@ declare type BaseOptions = {
|
|
|
9
9
|
declare type SimpleType = BaseOptions & ({
|
|
10
10
|
type: "number" | "boolean" | "integer" | "string" | "any" | "number[]" | "boolean[]" | "integer[]" | "string[]" | "any[]" | ValidationSchema;
|
|
11
11
|
} | {
|
|
12
|
-
|
|
12
|
+
enum: readonly any[];
|
|
13
13
|
});
|
|
14
|
-
export declare type
|
|
15
|
-
|
|
14
|
+
export declare type OneOf = BaseOptions & {
|
|
15
|
+
oneOf: readonly ValidationSchema[];
|
|
16
16
|
};
|
|
17
|
-
declare type FieldType = SimpleType |
|
|
17
|
+
declare type FieldType = SimpleType | OneOf;
|
|
18
18
|
declare type GetType<T extends FieldType> = T extends {
|
|
19
19
|
type: ValidationSchema;
|
|
20
20
|
} ? SchemaObject<T["type"]> : T extends {
|
|
@@ -38,12 +38,12 @@ declare type GetType<T extends FieldType> = T extends {
|
|
|
38
38
|
} ? string[] : T extends {
|
|
39
39
|
type: "any[]";
|
|
40
40
|
} ? any[] : T extends {
|
|
41
|
-
|
|
42
|
-
} ? T["
|
|
41
|
+
enum: readonly any[];
|
|
42
|
+
} ? T["enum"][number] :
|
|
43
43
|
/** This needs fixing */
|
|
44
44
|
T extends {
|
|
45
|
-
|
|
46
|
-
} ? SchemaObject<T["
|
|
45
|
+
oneOf: readonly ValidationSchema[];
|
|
46
|
+
} ? SchemaObject<T["oneOf"][number]> : any;
|
|
47
47
|
export declare type ValidationSchema = Record<string, FieldType>;
|
|
48
48
|
export declare type SchemaObject<S extends ValidationSchema> = ({
|
|
49
49
|
[K in keyof S as S[K]["optional"] extends true ? K : never]?: GetType<S[K]>;
|
|
@@ -54,7 +54,7 @@ export declare function validate<T>(obj: T, key: keyof T, validation: FieldType)
|
|
|
54
54
|
export declare function validateSchema<S extends ValidationSchema>(schema: S, obj: SchemaObject<S>, objName?: string, optional?: boolean): void;
|
|
55
55
|
export declare function getPGCheckConstraint(args: {
|
|
56
56
|
escapedFieldName: string;
|
|
57
|
-
schema: ValidationSchema |
|
|
57
|
+
schema: ValidationSchema | OneOf;
|
|
58
58
|
nullable: boolean;
|
|
59
59
|
isRootQuery?: boolean;
|
|
60
60
|
optional?: boolean;
|
|
@@ -63,7 +63,7 @@ declare type ColOpts = {
|
|
|
63
63
|
nullable?: boolean;
|
|
64
64
|
};
|
|
65
65
|
export declare function getSchemaTSTypes(schema: ValidationSchema, leading?: string, isOneOf?: boolean): string;
|
|
66
|
-
export declare function getJSONBSchemaTSTypes(schema: ValidationSchema |
|
|
66
|
+
export declare function getJSONBSchemaTSTypes(schema: ValidationSchema | OneOf, colOpts: ColOpts, leading?: string, isOneOf?: boolean): string;
|
|
67
67
|
export declare function getJSONBSchemaAsJSONSchema(tableName: string, colName: string, columnConfig: BaseColumn<{
|
|
68
68
|
en: 1;
|
|
69
69
|
}> & JSONBColumnDef): AnyObject;
|
package/lib/validation.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAsC,MAAM,iBAAiB,CAAC;AAEhF,OAAO,EAAE,UAAU,EAAgB,cAAc,EAAE,MAAM,eAAe,CAAC;AAEzE,aAAK,WAAW,GAAG;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,aAAK,UAAU,GAAG,WAAW,GAAG,CAAC;IAC/B,IAAI,EACF,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,KAAK,GACnD,UAAU,GAAG,WAAW,GAA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: "string" } } },
|
|
10
|
-
o: {
|
|
10
|
+
o: {
|
|
11
|
+
oneOf: [
|
|
11
12
|
{ z: { type: "integer" } },
|
|
12
13
|
{ z1: { type: "integer" } }
|
|
13
|
-
]
|
|
14
|
+
]
|
|
15
|
+
}
|
|
14
16
|
};
|
|
15
17
|
const ss = {
|
|
16
18
|
a: true,
|
|
@@ -38,9 +40,9 @@ function validate(obj, key, validation) {
|
|
|
38
40
|
if (validation.type === "integer" && !Number.isInteger(val))
|
|
39
41
|
throw new Error(err);
|
|
40
42
|
}
|
|
41
|
-
else if ("
|
|
42
|
-
err += `on of: ${validation.
|
|
43
|
-
if (!validation.
|
|
43
|
+
else if ("enum" in validation && validation.enum) {
|
|
44
|
+
err += `on of: ${validation.enum}`;
|
|
45
|
+
if (!validation.enum.includes(val))
|
|
44
46
|
throw new Error(err);
|
|
45
47
|
}
|
|
46
48
|
return true;
|
|
@@ -70,18 +72,18 @@ function getPGCheckConstraint(args, depth) {
|
|
|
70
72
|
checks.push(`${valAsJson} IS NULL`);
|
|
71
73
|
if (t.optional)
|
|
72
74
|
checks.push(`${escapedFieldName} ? ${(0, PubSubManager_1.asValue)(k)} = FALSE`);
|
|
73
|
-
if ("
|
|
74
|
-
checks.push(`(${t.
|
|
75
|
+
if ("oneOf" in t) {
|
|
76
|
+
checks.push(`(${t.oneOf.map(subType => getPGCheckConstraint({ escapedFieldName: valAsJson, schema: subType, nullable, optional: t.optional }, depth + 1)).join(" OR ")})`);
|
|
75
77
|
}
|
|
76
|
-
else if ("
|
|
77
|
-
if (!t.
|
|
78
|
-
throw new Error(`Invalid ValidationSchema for property: ${k} of field ${escapedFieldName}:
|
|
78
|
+
else if ("enum" in t) {
|
|
79
|
+
if (!t.enum.length || t.enum.some(v => v === undefined || !["number", "boolean", "string", null].includes(typeof v))) {
|
|
80
|
+
throw new Error(`Invalid ValidationSchema for property: ${k} of field ${escapedFieldName}: enum cannot be empty AND can only contain: numbers, text, boolean, null`);
|
|
79
81
|
}
|
|
80
|
-
const oneOfHasNull = t.
|
|
82
|
+
const oneOfHasNull = t.enum.includes(null);
|
|
81
83
|
if (oneOfHasNull)
|
|
82
84
|
checks.push(`${valAsText} IS NULL`);
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
+
const _enum = t.enum.filter(o => o !== null);
|
|
86
|
+
_enum.forEach(o => {
|
|
85
87
|
checks.push(`(${valAsText})${jsToPGtypes[typeof o]} = ${(0, PubSubManager_1.asValue)(o)}`);
|
|
86
88
|
});
|
|
87
89
|
}
|
|
@@ -114,7 +116,7 @@ function getPGCheckConstraint(args, depth) {
|
|
|
114
116
|
const checks = [];
|
|
115
117
|
let typeChecks = "";
|
|
116
118
|
if (isOneOfTypes(s)) {
|
|
117
|
-
typeChecks = s.
|
|
119
|
+
typeChecks = s.oneOf.map(t => `(${getSchemaChecks(t)})`).join(" OR ");
|
|
118
120
|
}
|
|
119
121
|
else {
|
|
120
122
|
typeChecks = getSchemaChecks(s);
|
|
@@ -126,9 +128,9 @@ function getPGCheckConstraint(args, depth) {
|
|
|
126
128
|
}
|
|
127
129
|
exports.getPGCheckConstraint = getPGCheckConstraint;
|
|
128
130
|
const isOneOfTypes = (s) => {
|
|
129
|
-
if ("
|
|
130
|
-
if (!Array.isArray(s.
|
|
131
|
-
throw "Expecting
|
|
131
|
+
if ("oneOf" in s) {
|
|
132
|
+
if (!Array.isArray(s.oneOf)) {
|
|
133
|
+
throw "Expecting oneOf to be an array of types";
|
|
132
134
|
}
|
|
133
135
|
return true;
|
|
134
136
|
}
|
|
@@ -146,11 +148,11 @@ function getSchemaTSTypes(schema, leading = "", isOneOf = false) {
|
|
|
146
148
|
return nullType + getSchemaTSTypes(def.type, "", true);
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
|
-
else if ("
|
|
150
|
-
return nullType + def.
|
|
151
|
+
else if ("enum" in def) {
|
|
152
|
+
return nullType + def.enum.map(v => (0, PubSubManager_1.asValue)(v)).join(" | ");
|
|
151
153
|
}
|
|
152
|
-
else if ("
|
|
153
|
-
return (def.nullable ? `\n${leading} | null` : "") + def.
|
|
154
|
+
else if ("oneOf" in def) {
|
|
155
|
+
return (def.nullable ? `\n${leading} | null` : "") + def.oneOf.map(v => `\n${leading} | ` + getSchemaTSTypes(v, "", true)).join("");
|
|
154
156
|
}
|
|
155
157
|
else
|
|
156
158
|
throw "Unexpected getSchemaTSTypes";
|
|
@@ -168,7 +170,7 @@ function getSchemaTSTypes(schema, leading = "", isOneOf = false) {
|
|
|
168
170
|
exports.getSchemaTSTypes = getSchemaTSTypes;
|
|
169
171
|
function getJSONBSchemaTSTypes(schema, colOpts, leading = "", isOneOf = false) {
|
|
170
172
|
if (isOneOfTypes(schema)) {
|
|
171
|
-
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.
|
|
173
|
+
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.oneOf.map(s => `\n${leading} | ` + getSchemaTSTypes(s, "", true)).join("");
|
|
172
174
|
}
|
|
173
175
|
else {
|
|
174
176
|
return (colOpts.nullable ? `null | ` : "") + getSchemaTSTypes(schema, leading, isOneOf);
|
|
@@ -211,18 +213,15 @@ const getJSONSchemaObject = (objDef) => {
|
|
|
211
213
|
};
|
|
212
214
|
}
|
|
213
215
|
}
|
|
214
|
-
else if ("
|
|
216
|
+
else if ("enum" in itemSchema) {
|
|
215
217
|
item = {
|
|
216
|
-
type:
|
|
217
|
-
|
|
218
|
-
"type": typeof itemSchema.oneOf[0],
|
|
219
|
-
"enum": itemSchema.oneOf //.concat(nullable? [null] : [])
|
|
220
|
-
}
|
|
218
|
+
type: typeof itemSchema.enum[0],
|
|
219
|
+
"enum": itemSchema.enum //.concat(nullable? [null] : [])
|
|
221
220
|
};
|
|
222
221
|
}
|
|
223
|
-
else if ("
|
|
222
|
+
else if ("oneOf" in itemSchema) {
|
|
224
223
|
item = {
|
|
225
|
-
oneOf: itemSchema.
|
|
224
|
+
oneOf: itemSchema.oneOf.map(t => getJSONSchemaObject(t))
|
|
226
225
|
};
|
|
227
226
|
}
|
|
228
227
|
else {
|