@postxl/schema 0.0.2
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/LICENSE +50 -0
- package/README.md +320 -0
- package/dist/enum/enum.brands.d.ts +11 -0
- package/dist/enum/enum.brands.js +19 -0
- package/dist/enum/enum.d.ts +49 -0
- package/dist/enum/enum.defaults.d.ts +41 -0
- package/dist/enum/enum.defaults.js +105 -0
- package/dist/enum/enum.js +6 -0
- package/dist/enum/enum.json-decoder.d.ts +61 -0
- package/dist/enum/enum.json-decoder.js +51 -0
- package/dist/enum/enum.transformer.d.ts +12 -0
- package/dist/enum/enum.transformer.js +76 -0
- package/dist/enum/enum.types.d.ts +14 -0
- package/dist/enum/enum.types.js +2 -0
- package/dist/enum/index.d.ts +6 -0
- package/dist/enum/index.js +22 -0
- package/dist/field/defaults.d.ts +12 -0
- package/dist/field/defaults.js +78 -0
- package/dist/field/discriminated-union.d.ts +125 -0
- package/dist/field/discriminated-union.js +207 -0
- package/dist/field/enum.d.ts +65 -0
- package/dist/field/enum.js +111 -0
- package/dist/field/field.d.ts +483 -0
- package/dist/field/field.js +68 -0
- package/dist/field/id.d.ts +152 -0
- package/dist/field/id.js +104 -0
- package/dist/field/index.d.ts +11 -0
- package/dist/field/index.js +27 -0
- package/dist/field/relation.d.ts +88 -0
- package/dist/field/relation.js +138 -0
- package/dist/field/scalar.d.ts +179 -0
- package/dist/field/scalar.js +197 -0
- package/dist/field/shared/brands.d.ts +38 -0
- package/dist/field/shared/brands.js +109 -0
- package/dist/field/shared/decoders.d.ts +17 -0
- package/dist/field/shared/decoders.js +88 -0
- package/dist/field/shared/seed.d.ts +32 -0
- package/dist/field/shared/seed.js +63 -0
- package/dist/field/shared/types.d.ts +73 -0
- package/dist/field/shared/types.js +2 -0
- package/dist/field/shared/utils.d.ts +26 -0
- package/dist/field/shared/utils.js +52 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +25 -0
- package/dist/model/index.d.ts +5 -0
- package/dist/model/index.js +21 -0
- package/dist/model/model.brands.d.ts +7 -0
- package/dist/model/model.brands.js +10 -0
- package/dist/model/model.defaults.d.ts +28 -0
- package/dist/model/model.defaults.js +304 -0
- package/dist/model/model.json-decoder.d.ts +94 -0
- package/dist/model/model.json-decoder.js +107 -0
- package/dist/model/model.transformer.d.ts +93 -0
- package/dist/model/model.transformer.js +183 -0
- package/dist/model/model.types.d.ts +37 -0
- package/dist/model/model.types.js +2 -0
- package/dist/project-schema/project-schema.brands.d.ts +10 -0
- package/dist/project-schema/project-schema.brands.js +17 -0
- package/dist/project-schema/project-schema.defaults.d.ts +16 -0
- package/dist/project-schema/project-schema.defaults.js +53 -0
- package/dist/project-schema/project-schema.json-decoder.d.ts +285 -0
- package/dist/project-schema/project-schema.json-decoder.js +145 -0
- package/dist/project-schema/project-schema.transformer.d.ts +284 -0
- package/dist/project-schema/project-schema.transformer.js +444 -0
- package/dist/project-schema/project-schema.types.d.ts +77 -0
- package/dist/project-schema/project-schema.types.js +2 -0
- package/dist/samples/la.schema.json +2055 -0
- package/dist/samples/mca.schema.json +830 -0
- package/dist/samples/ring.schema.json +879 -0
- package/dist/samples/sample-schemas.d.ts +1896 -0
- package/dist/samples/sample-schemas.js +20 -0
- package/dist/samples/simple.schema.json +250 -0
- package/dist/samples/subex.schema.json +1188 -0
- package/dist/samples/usp-msm.schema.json +2515 -0
- package/package.json +57 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fieldTransformer = exports.zFieldJSONInput = exports.createFieldJSONDecoder = void 0;
|
|
4
|
+
exports.isFieldDatabaseNative = isFieldDatabaseNative;
|
|
5
|
+
const zod_1 = require("zod");
|
|
6
|
+
const utils_1 = require("@postxl/utils");
|
|
7
|
+
const discriminated_union_1 = require("./discriminated-union");
|
|
8
|
+
const enum_1 = require("./enum");
|
|
9
|
+
const id_1 = require("./id");
|
|
10
|
+
const relation_1 = require("./relation");
|
|
11
|
+
const scalar_1 = require("./scalar");
|
|
12
|
+
/*
|
|
13
|
+
JSON Schema for a field
|
|
14
|
+
|
|
15
|
+
We have different kinds of fields:
|
|
16
|
+
- id: a field that is the primary key of the model
|
|
17
|
+
- scalar: a field that is not the primary key of the model
|
|
18
|
+
- relation: a field that is a relation to another model
|
|
19
|
+
- enum: a field that is an enum
|
|
20
|
+
- discriminatedUnion: a field that is a discriminated union
|
|
21
|
+
|
|
22
|
+
To differentiate between these kinds, we check the `type` of the field:
|
|
23
|
+
- if `name` is `id`, we know that it is an id field
|
|
24
|
+
- if `name` is not `id` and `type` is one of the database scalar types (e.g. `String`, `Int`, `DateTime`), we know that it is a scalar field
|
|
25
|
+
- else, we assume that it is a relation or enum field. Differentiation between these two cannot be done in the JSON schema of a field, but only on the ProjectSchema level.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* JSON Schema for a field
|
|
29
|
+
*/
|
|
30
|
+
const createFieldJSONDecoder = (params) => zod_1.z.union([
|
|
31
|
+
//
|
|
32
|
+
id_1.zFieldIdJSON.transform(id_1.fieldIdJSONTransformer),
|
|
33
|
+
scalar_1.zFieldScalarJSON.transform(scalar_1.fieldScalarJSONTransformer),
|
|
34
|
+
discriminated_union_1.zFieldDiscriminatedUnionJSON.transform(discriminated_union_1.fieldDiscriminatedUnionJSONTransformer),
|
|
35
|
+
// As relation and enum fields have flexible type names (i.e. the names of the referenced model/enum) and we use the
|
|
36
|
+
// type property to determine the type of the field, we need to handle these last
|
|
37
|
+
(0, relation_1.createRelationJSONDecoder)(params).transform(relation_1.fieldRelationJSONTransformer),
|
|
38
|
+
(0, enum_1.createEnumJSONDecoder)(params).transform(enum_1.fieldEnumJSONTransformer),
|
|
39
|
+
]);
|
|
40
|
+
exports.createFieldJSONDecoder = createFieldJSONDecoder;
|
|
41
|
+
const zFieldJSONInput = (params) => zod_1.z.union([
|
|
42
|
+
//
|
|
43
|
+
id_1.zFieldIdJSON,
|
|
44
|
+
scalar_1.zFieldScalarJSON,
|
|
45
|
+
(0, relation_1.createRelationJSONDecoder)(params),
|
|
46
|
+
(0, enum_1.createEnumJSONDecoder)(params),
|
|
47
|
+
]);
|
|
48
|
+
exports.zFieldJSONInput = zFieldJSONInput;
|
|
49
|
+
const fieldTransformer = (fieldInput, model, projectSchema, ctx) => {
|
|
50
|
+
switch (fieldInput.kind) {
|
|
51
|
+
case 'id':
|
|
52
|
+
return (0, id_1.fieldIdTransformer)({ fieldInput, model, projectSchema }, ctx);
|
|
53
|
+
case 'scalar':
|
|
54
|
+
return (0, scalar_1.fieldScalarTransformer)({ fieldInput, model, projectSchema }, ctx);
|
|
55
|
+
case 'discriminatedUnion':
|
|
56
|
+
return (0, discriminated_union_1.fieldDiscriminatedUnionTransformer)({ fieldInput, model, projectSchema }, ctx);
|
|
57
|
+
case 'relation':
|
|
58
|
+
return (0, relation_1.fieldRelationTransformer)({ fieldInput, model, projectSchema }, ctx);
|
|
59
|
+
case 'enum':
|
|
60
|
+
return (0, enum_1.fieldEnumTransformer)({ fieldInput, model, projectSchema }, ctx);
|
|
61
|
+
default:
|
|
62
|
+
throw new utils_1.ExhaustiveSwitchCheck(fieldInput);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
exports.fieldTransformer = fieldTransformer;
|
|
66
|
+
function isFieldDatabaseNative(field) {
|
|
67
|
+
return field.kind !== 'discriminatedUnion';
|
|
68
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import * as Branded from './shared/brands';
|
|
3
|
+
import { FieldSeedConfig } from './shared/seed';
|
|
4
|
+
import { FieldCommon, FieldTransformer } from './shared/types';
|
|
5
|
+
export type FieldKindId = 'id';
|
|
6
|
+
export declare const fieldKindId: FieldKindId;
|
|
7
|
+
export type FieldId = Omit<FieldCommon<FieldKindId>, 'name'> & {
|
|
8
|
+
kind: 'id';
|
|
9
|
+
/**
|
|
10
|
+
* Name of the id field. We enforce that this is always `id` to lower complexity.
|
|
11
|
+
*/
|
|
12
|
+
name: Branded.FieldIdName;
|
|
13
|
+
/**
|
|
14
|
+
* The branded TypeScript type of the id field, e.g. `UserId`.
|
|
15
|
+
*/
|
|
16
|
+
type: Branded.IdType;
|
|
17
|
+
idType: IdGenerationType;
|
|
18
|
+
unbrandedTypeName: Branded.ScalarType;
|
|
19
|
+
/**
|
|
20
|
+
* If true, the repository will expose an index for the field.
|
|
21
|
+
*/
|
|
22
|
+
hasIndex: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* If true, the field is automatically set to the current date and time when the item is created.
|
|
25
|
+
*/
|
|
26
|
+
isCreatedAt: false;
|
|
27
|
+
/**
|
|
28
|
+
* If true, the field is automatically set to the current date and time when the item is updated.
|
|
29
|
+
*/
|
|
30
|
+
isUpdatedAt: false;
|
|
31
|
+
seed: FieldSeedConfig;
|
|
32
|
+
};
|
|
33
|
+
export type IdGenerationType =
|
|
34
|
+
/**
|
|
35
|
+
* A UUID is generated/used for the id field.
|
|
36
|
+
*/
|
|
37
|
+
'UUID'
|
|
38
|
+
/**
|
|
39
|
+
* An incrementing number is used for the id field.
|
|
40
|
+
*/
|
|
41
|
+
| 'Increment';
|
|
42
|
+
/**
|
|
43
|
+
* JSON Schema for an id field
|
|
44
|
+
*/
|
|
45
|
+
export declare const zFieldIdJSON: z.ZodObject<{
|
|
46
|
+
name: z.ZodLiteral<"id">;
|
|
47
|
+
type: z.ZodUnion<[z.ZodLiteral<"String">, z.ZodLiteral<"Int">]>;
|
|
48
|
+
databaseName: z.ZodOptional<z.ZodString>;
|
|
49
|
+
excelName: z.ZodEffects<z.ZodOptional<z.ZodString>, string | undefined, string | undefined>;
|
|
50
|
+
label: z.ZodOptional<z.ZodString>;
|
|
51
|
+
description: z.ZodOptional<z.ZodString>;
|
|
52
|
+
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
53
|
+
faker: z.ZodOptional<z.ZodString>;
|
|
54
|
+
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, "many">]>>;
|
|
55
|
+
}, "strip", z.ZodTypeAny, {
|
|
56
|
+
type: "String" | "Int";
|
|
57
|
+
name: "id";
|
|
58
|
+
description?: string | undefined;
|
|
59
|
+
databaseName?: string | undefined;
|
|
60
|
+
excelName?: string | undefined;
|
|
61
|
+
label?: string | undefined;
|
|
62
|
+
isReadonly?: boolean | undefined;
|
|
63
|
+
faker?: string | undefined;
|
|
64
|
+
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
65
|
+
}, {
|
|
66
|
+
type: "String" | "Int";
|
|
67
|
+
name: "id";
|
|
68
|
+
description?: string | undefined;
|
|
69
|
+
databaseName?: string | undefined;
|
|
70
|
+
excelName?: string | undefined;
|
|
71
|
+
label?: string | undefined;
|
|
72
|
+
isReadonly?: boolean | undefined;
|
|
73
|
+
faker?: string | undefined;
|
|
74
|
+
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
75
|
+
}>;
|
|
76
|
+
export type FieldIdJSON = z.infer<typeof zFieldIdJSON>;
|
|
77
|
+
export declare const zFieldId: z.ZodEffects<z.ZodObject<{
|
|
78
|
+
name: z.ZodLiteral<"id">;
|
|
79
|
+
type: z.ZodUnion<[z.ZodLiteral<"String">, z.ZodLiteral<"Int">]>;
|
|
80
|
+
databaseName: z.ZodOptional<z.ZodString>;
|
|
81
|
+
excelName: z.ZodEffects<z.ZodOptional<z.ZodString>, string | undefined, string | undefined>;
|
|
82
|
+
label: z.ZodOptional<z.ZodString>;
|
|
83
|
+
description: z.ZodOptional<z.ZodString>;
|
|
84
|
+
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
85
|
+
faker: z.ZodOptional<z.ZodString>;
|
|
86
|
+
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, "many">]>>;
|
|
87
|
+
}, "strip", z.ZodTypeAny, {
|
|
88
|
+
type: "String" | "Int";
|
|
89
|
+
name: "id";
|
|
90
|
+
description?: string | undefined;
|
|
91
|
+
databaseName?: string | undefined;
|
|
92
|
+
excelName?: string | undefined;
|
|
93
|
+
label?: string | undefined;
|
|
94
|
+
isReadonly?: boolean | undefined;
|
|
95
|
+
faker?: string | undefined;
|
|
96
|
+
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
97
|
+
}, {
|
|
98
|
+
type: "String" | "Int";
|
|
99
|
+
name: "id";
|
|
100
|
+
description?: string | undefined;
|
|
101
|
+
databaseName?: string | undefined;
|
|
102
|
+
excelName?: string | undefined;
|
|
103
|
+
label?: string | undefined;
|
|
104
|
+
isReadonly?: boolean | undefined;
|
|
105
|
+
faker?: string | undefined;
|
|
106
|
+
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
107
|
+
}>, {
|
|
108
|
+
kind: "id";
|
|
109
|
+
name: "id" & z.BRAND<"PXL.FieldName"> & z.BRAND<"PXL.FieldName.Id">;
|
|
110
|
+
unbrandedTypeName: ((("string" | "number") & z.BRAND<"PXL.ScalarIdType">) & z.BRAND<"PXL.ScalarType">) & z.BRAND<"PXL.Type">;
|
|
111
|
+
databaseType: ("BigInt" | "String" | "Boolean" | "DateTime" | "Decimal" | "Float" | "Int" | "Json" | "Bytes") & z.BRAND<"PXL.DatabaseFieldType">;
|
|
112
|
+
databaseName: string & z.BRAND<"PXL.DatabaseFieldName">;
|
|
113
|
+
excelName: string & z.BRAND<"PXL.ExcelColumnName">;
|
|
114
|
+
description: string;
|
|
115
|
+
label: string;
|
|
116
|
+
hasIndex: true;
|
|
117
|
+
isReadonly: boolean;
|
|
118
|
+
isRequired: true;
|
|
119
|
+
isUnique: true;
|
|
120
|
+
isCreatedAt: false;
|
|
121
|
+
isUpdatedAt: false;
|
|
122
|
+
seed: FieldSeedConfig;
|
|
123
|
+
}, {
|
|
124
|
+
type: "String" | "Int";
|
|
125
|
+
name: "id";
|
|
126
|
+
description?: string | undefined;
|
|
127
|
+
databaseName?: string | undefined;
|
|
128
|
+
excelName?: string | undefined;
|
|
129
|
+
label?: string | undefined;
|
|
130
|
+
isReadonly?: boolean | undefined;
|
|
131
|
+
faker?: string | undefined;
|
|
132
|
+
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
133
|
+
}>;
|
|
134
|
+
export declare function fieldIdJSONTransformer(fieldInput: FieldIdJSON, ctx: z.RefinementCtx): {
|
|
135
|
+
kind: "id";
|
|
136
|
+
name: "id" & z.BRAND<"PXL.FieldName"> & z.BRAND<"PXL.FieldName.Id">;
|
|
137
|
+
unbrandedTypeName: ((("string" | "number") & z.BRAND<"PXL.ScalarIdType">) & z.BRAND<"PXL.ScalarType">) & z.BRAND<"PXL.Type">;
|
|
138
|
+
databaseType: ("BigInt" | "String" | "Boolean" | "DateTime" | "Decimal" | "Float" | "Int" | "Json" | "Bytes") & z.BRAND<"PXL.DatabaseFieldType">;
|
|
139
|
+
databaseName: string & z.BRAND<"PXL.DatabaseFieldName">;
|
|
140
|
+
excelName: string & z.BRAND<"PXL.ExcelColumnName">;
|
|
141
|
+
description: string;
|
|
142
|
+
label: string;
|
|
143
|
+
hasIndex: true;
|
|
144
|
+
isReadonly: boolean;
|
|
145
|
+
isRequired: true;
|
|
146
|
+
isUnique: true;
|
|
147
|
+
isCreatedAt: false;
|
|
148
|
+
isUpdatedAt: false;
|
|
149
|
+
seed: FieldSeedConfig;
|
|
150
|
+
};
|
|
151
|
+
export type FieldIdEnriched = ReturnType<typeof fieldIdJSONTransformer>;
|
|
152
|
+
export declare const fieldIdTransformer: FieldTransformer<FieldKindId, FieldIdEnriched, FieldId>;
|
package/dist/field/id.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.fieldIdTransformer = exports.zFieldId = exports.zFieldIdJSON = exports.fieldKindId = void 0;
|
|
37
|
+
exports.fieldIdJSONTransformer = fieldIdJSONTransformer;
|
|
38
|
+
const zod_1 = require("zod");
|
|
39
|
+
const utils_1 = require("@postxl/utils");
|
|
40
|
+
const Branded = __importStar(require("./shared/brands"));
|
|
41
|
+
const Decoders = __importStar(require("./shared/decoders"));
|
|
42
|
+
const seed_1 = require("./shared/seed");
|
|
43
|
+
exports.fieldKindId = 'id';
|
|
44
|
+
const zDatabaseType = zod_1.z
|
|
45
|
+
.union([zod_1.z.literal('String'), zod_1.z.literal('Int')])
|
|
46
|
+
.describe('The type of the field. This should either be `String` or `Int` in case name is `id`');
|
|
47
|
+
function databaseTypeToFieldTypeScalar(input) {
|
|
48
|
+
switch (input) {
|
|
49
|
+
case 'String':
|
|
50
|
+
return Branded.toScalarIdType('string');
|
|
51
|
+
case 'Int':
|
|
52
|
+
return Branded.toScalarIdType('number');
|
|
53
|
+
default:
|
|
54
|
+
throw new utils_1.ExhaustiveSwitchCheck(input);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* JSON Schema for an id field
|
|
59
|
+
*/
|
|
60
|
+
exports.zFieldIdJSON = zod_1.z.object({
|
|
61
|
+
name: zod_1.z.literal('id'),
|
|
62
|
+
type: zDatabaseType,
|
|
63
|
+
databaseName: Decoders.zFieldPropertyDatabaseName,
|
|
64
|
+
excelName: Decoders.zFieldPropertyExcelName,
|
|
65
|
+
label: Decoders.zFieldPropertyLabel,
|
|
66
|
+
description: Decoders.zFieldPropertyDescription,
|
|
67
|
+
isReadonly: Decoders.zFieldPropertyIsReadonly,
|
|
68
|
+
faker: Decoders.zFieldFaker,
|
|
69
|
+
seed: Decoders.zFieldSeed,
|
|
70
|
+
});
|
|
71
|
+
exports.zFieldId = exports.zFieldIdJSON.transform(fieldIdJSONTransformer);
|
|
72
|
+
function fieldIdJSONTransformer(fieldInput, ctx) {
|
|
73
|
+
return {
|
|
74
|
+
kind: exports.fieldKindId,
|
|
75
|
+
name: Branded.toFieldIdName(fieldInput.name),
|
|
76
|
+
unbrandedTypeName: databaseTypeToFieldTypeScalar(fieldInput.type),
|
|
77
|
+
databaseType: Branded.toDatabaseFieldType(fieldInput.type),
|
|
78
|
+
databaseName: Branded.toDatabaseFieldName(fieldInput.databaseName ?? (0, utils_1.toSnakeCase)(fieldInput.name)),
|
|
79
|
+
excelName: Branded.toExcelColumnName(fieldInput.excelName ?? fieldInput.label ?? fieldInput.name),
|
|
80
|
+
description: fieldInput.description ?? '',
|
|
81
|
+
label: fieldInput.label ?? 'Id',
|
|
82
|
+
hasIndex: true,
|
|
83
|
+
isReadonly: fieldInput.isReadonly ?? false,
|
|
84
|
+
isRequired: true,
|
|
85
|
+
isUnique: true,
|
|
86
|
+
isCreatedAt: false,
|
|
87
|
+
isUpdatedAt: false,
|
|
88
|
+
seed: (0, seed_1.seedTransformer)(fieldInput, ctx),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const fieldIdTransformer = ({ fieldInput, model }) => {
|
|
92
|
+
const idType = fieldInput.unbrandedTypeName === 'string' ? 'UUID' : 'Increment';
|
|
93
|
+
return {
|
|
94
|
+
...fieldInput,
|
|
95
|
+
modelName: model.name,
|
|
96
|
+
kind: 'id',
|
|
97
|
+
idType,
|
|
98
|
+
type: Branded.toIdType(model.name),
|
|
99
|
+
name: Branded.toFieldIdName(fieldInput.name),
|
|
100
|
+
label: fieldInput.label ?? fieldInput.name,
|
|
101
|
+
description: fieldInput.description === '' ? `The id of the ${model.name} item.` : fieldInput.description,
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
exports.fieldIdTransformer = fieldIdTransformer;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './discriminated-union';
|
|
2
|
+
export * from './defaults';
|
|
3
|
+
export * from './enum';
|
|
4
|
+
export * from './field';
|
|
5
|
+
export * from './id';
|
|
6
|
+
export * from './relation';
|
|
7
|
+
export * from './scalar';
|
|
8
|
+
export * from './shared/brands';
|
|
9
|
+
export * from './shared/seed';
|
|
10
|
+
export * from './shared/utils';
|
|
11
|
+
export * from './shared/types';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./discriminated-union"), exports);
|
|
18
|
+
__exportStar(require("./defaults"), exports);
|
|
19
|
+
__exportStar(require("./enum"), exports);
|
|
20
|
+
__exportStar(require("./field"), exports);
|
|
21
|
+
__exportStar(require("./id"), exports);
|
|
22
|
+
__exportStar(require("./relation"), exports);
|
|
23
|
+
__exportStar(require("./scalar"), exports);
|
|
24
|
+
__exportStar(require("./shared/brands"), exports);
|
|
25
|
+
__exportStar(require("./shared/seed"), exports);
|
|
26
|
+
__exportStar(require("./shared/utils"), exports);
|
|
27
|
+
__exportStar(require("./shared/types"), exports);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ModelName } from '../model';
|
|
3
|
+
import * as Branded from './shared/brands';
|
|
4
|
+
import { FieldCommon, FieldTransformer } from './shared/types';
|
|
5
|
+
export type FieldKindRelation = 'relation';
|
|
6
|
+
export declare const fieldKindRelation: FieldKindRelation;
|
|
7
|
+
export type FieldRelation = FieldCommon<FieldKindRelation> & {
|
|
8
|
+
type: Branded.IdType;
|
|
9
|
+
referencedModelName: ModelName;
|
|
10
|
+
/**
|
|
11
|
+
* The name of the field (in the Prisma schema) in the referenced model that
|
|
12
|
+
* contains the array of these items. If left empty, it defaults to "modelNamePlural"
|
|
13
|
+
* if only one relation to this model exists, otherwise an error is thrown.
|
|
14
|
+
*/
|
|
15
|
+
prismaRelationFieldName: Branded.DatabaseFieldName;
|
|
16
|
+
/**
|
|
17
|
+
* If true, when cloning an entity, this relation will be deep cloned (i.e., the referenced
|
|
18
|
+
* entity will also be cloned). If false or undefined, the clone will reference the same entity.
|
|
19
|
+
*
|
|
20
|
+
* Note: Setting this to true on a relation to a readonly model will cause a validation error.
|
|
21
|
+
*/
|
|
22
|
+
deepClone: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Name of the unbranded TypeScript type of the field, e.g. string
|
|
25
|
+
*
|
|
26
|
+
* Note: This should only be used in the type generator - all other generators
|
|
27
|
+
* should use the branded type "typeName"!
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
unbrandedType: Branded.ScalarType;
|
|
32
|
+
};
|
|
33
|
+
export declare const createRelationJSONDecoder: ({ modelNames }: {
|
|
34
|
+
modelNames: ModelName[];
|
|
35
|
+
}) => z.ZodObject<{
|
|
36
|
+
name: z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>;
|
|
37
|
+
type: z.ZodEffects<z.ZodString, string, string>;
|
|
38
|
+
databaseName: z.ZodOptional<z.ZodString>;
|
|
39
|
+
excelName: z.ZodEffects<z.ZodOptional<z.ZodString>, string | undefined, string | undefined>;
|
|
40
|
+
prismaRelationFieldName: z.ZodOptional<z.ZodString>;
|
|
41
|
+
deepClone: z.ZodOptional<z.ZodBoolean>;
|
|
42
|
+
label: z.ZodOptional<z.ZodString>;
|
|
43
|
+
description: z.ZodOptional<z.ZodString>;
|
|
44
|
+
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
45
|
+
isUnique: z.ZodOptional<z.ZodBoolean>;
|
|
46
|
+
}, "strip", z.ZodTypeAny, {
|
|
47
|
+
type: string;
|
|
48
|
+
name: string;
|
|
49
|
+
description?: string | undefined;
|
|
50
|
+
databaseName?: string | undefined;
|
|
51
|
+
excelName?: string | undefined;
|
|
52
|
+
label?: string | undefined;
|
|
53
|
+
isReadonly?: boolean | undefined;
|
|
54
|
+
isUnique?: boolean | undefined;
|
|
55
|
+
prismaRelationFieldName?: string | undefined;
|
|
56
|
+
deepClone?: boolean | undefined;
|
|
57
|
+
}, {
|
|
58
|
+
type: string;
|
|
59
|
+
name: string;
|
|
60
|
+
description?: string | undefined;
|
|
61
|
+
databaseName?: string | undefined;
|
|
62
|
+
excelName?: string | undefined;
|
|
63
|
+
label?: string | undefined;
|
|
64
|
+
isReadonly?: boolean | undefined;
|
|
65
|
+
isUnique?: boolean | undefined;
|
|
66
|
+
prismaRelationFieldName?: string | undefined;
|
|
67
|
+
deepClone?: boolean | undefined;
|
|
68
|
+
}>;
|
|
69
|
+
export type FieldRelationJSON = z.infer<ReturnType<typeof createRelationJSONDecoder>>;
|
|
70
|
+
export declare function fieldRelationJSONTransformer(fieldInput: FieldRelationJSON): {
|
|
71
|
+
kind: "relation";
|
|
72
|
+
type: string & z.BRAND<"PXL.IdType"> & z.BRAND<"PXL.Type">;
|
|
73
|
+
name: string & z.BRAND<"PXL.FieldName">;
|
|
74
|
+
referencedModelName: string & z.BRAND<"PXL.ModelName">;
|
|
75
|
+
label: string;
|
|
76
|
+
databaseName: string & z.BRAND<"PXL.DatabaseFieldName">;
|
|
77
|
+
prismaRelationFieldName: (string & z.BRAND<"PXL.DatabaseFieldName">) | undefined;
|
|
78
|
+
excelName: string & z.BRAND<"PXL.ExcelColumnName">;
|
|
79
|
+
deepClone: boolean;
|
|
80
|
+
description: string | undefined;
|
|
81
|
+
isReadonly: boolean;
|
|
82
|
+
isRequired: boolean;
|
|
83
|
+
isUnique: boolean;
|
|
84
|
+
validations: never[];
|
|
85
|
+
generation: string;
|
|
86
|
+
};
|
|
87
|
+
export type FieldRelationEnriched = ReturnType<typeof fieldRelationJSONTransformer>;
|
|
88
|
+
export declare const fieldRelationTransformer: FieldTransformer<FieldKindRelation, FieldRelationEnriched, FieldRelation>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.fieldRelationTransformer = exports.createRelationJSONDecoder = exports.fieldKindRelation = void 0;
|
|
37
|
+
exports.fieldRelationJSONTransformer = fieldRelationJSONTransformer;
|
|
38
|
+
const zod_1 = require("zod");
|
|
39
|
+
const utils_1 = require("@postxl/utils");
|
|
40
|
+
const model_1 = require("../model");
|
|
41
|
+
const Branded = __importStar(require("./shared/brands"));
|
|
42
|
+
const Decoders = __importStar(require("./shared/decoders"));
|
|
43
|
+
const utils_2 = require("./shared/utils");
|
|
44
|
+
exports.fieldKindRelation = 'relation';
|
|
45
|
+
const createRelationJSONDecoder = ({ modelNames }) => {
|
|
46
|
+
return zod_1.z.object({
|
|
47
|
+
name: Decoders.zFieldPropertyName.refine((name) => name.endsWith('Id'), (name) => ({ message: `Relation field name must end with 'Id' - received ${name}` })),
|
|
48
|
+
type: zod_1.z //
|
|
49
|
+
.string()
|
|
50
|
+
.describe('The name of the referenced model. If suffixed with "?", the field is optional.')
|
|
51
|
+
.transform((input, ctx) => {
|
|
52
|
+
const modelName = (0, model_1.toModelName)(input.endsWith('?') ? input.slice(0, -1) : input);
|
|
53
|
+
if (!modelNames.includes(modelName)) {
|
|
54
|
+
ctx.addIssue({
|
|
55
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
56
|
+
message: `Model ${modelName} is not defined in the project!`,
|
|
57
|
+
});
|
|
58
|
+
return zod_1.z.NEVER;
|
|
59
|
+
}
|
|
60
|
+
return input;
|
|
61
|
+
}),
|
|
62
|
+
databaseName: Decoders.zFieldPropertyDatabaseName,
|
|
63
|
+
excelName: Decoders.zFieldPropertyExcelName,
|
|
64
|
+
prismaRelationFieldName: zod_1.z
|
|
65
|
+
//
|
|
66
|
+
.string()
|
|
67
|
+
.optional()
|
|
68
|
+
.describe('The name of the field that references the related model (e.g. `User`, `name` property would be `userId`).'),
|
|
69
|
+
deepClone: zod_1.z
|
|
70
|
+
.boolean()
|
|
71
|
+
.optional()
|
|
72
|
+
.describe('If true, when cloning an entity, this relation will be deep cloned. If false or undefined, the clone will reference the same entity.'),
|
|
73
|
+
label: Decoders.zFieldPropertyLabel,
|
|
74
|
+
description: Decoders.zFieldPropertyDescription,
|
|
75
|
+
isReadonly: Decoders.zFieldPropertyIsReadonly,
|
|
76
|
+
isUnique: Decoders.zFieldPropertyIsUnique,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
exports.createRelationJSONDecoder = createRelationJSONDecoder;
|
|
80
|
+
function fieldRelationJSONTransformer(fieldInput) {
|
|
81
|
+
const { isRequired, type: modelName } = (0, utils_2.isFieldRequired)(fieldInput.type);
|
|
82
|
+
const referencedModelName = (0, model_1.toModelName)(modelName);
|
|
83
|
+
const type = Branded.toIdType(referencedModelName);
|
|
84
|
+
return {
|
|
85
|
+
kind: exports.fieldKindRelation,
|
|
86
|
+
type,
|
|
87
|
+
name: Branded.toFieldName(fieldInput.name),
|
|
88
|
+
referencedModelName,
|
|
89
|
+
label: (0, utils_1.toHumanReadable)(fieldInput.label ?? fieldInput.name),
|
|
90
|
+
databaseName: Branded.toDatabaseFieldName(fieldInput.databaseName ?? (0, utils_1.toSnakeCase)(fieldInput.name)),
|
|
91
|
+
prismaRelationFieldName: fieldInput.prismaRelationFieldName
|
|
92
|
+
? Branded.toDatabaseFieldName(fieldInput.prismaRelationFieldName)
|
|
93
|
+
: undefined,
|
|
94
|
+
excelName: Branded.toExcelColumnName(fieldInput.excelName ?? fieldInput.label ?? fieldInput.name),
|
|
95
|
+
deepClone: fieldInput.deepClone ?? false,
|
|
96
|
+
description: fieldInput.description,
|
|
97
|
+
isReadonly: fieldInput.isReadonly ?? false,
|
|
98
|
+
isRequired,
|
|
99
|
+
isUnique: fieldInput.isUnique ?? false,
|
|
100
|
+
validations: [],
|
|
101
|
+
generation: '',
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const fieldRelationTransformer = ({ fieldInput, model, projectSchema }, ctx) => {
|
|
105
|
+
const referencedModel = projectSchema.models.get(fieldInput.referencedModelName);
|
|
106
|
+
if (referencedModel === undefined) {
|
|
107
|
+
ctx.addIssue({
|
|
108
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
109
|
+
message: `Cannot find referenced model for ${model.name}.${fieldInput.name}!`,
|
|
110
|
+
});
|
|
111
|
+
return zod_1.z.NEVER;
|
|
112
|
+
}
|
|
113
|
+
// Validate that deepClone is not set to true for readonly models
|
|
114
|
+
if (fieldInput.deepClone && referencedModel.isReadonly) {
|
|
115
|
+
ctx.addIssue({
|
|
116
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
117
|
+
message: `Field ${model.name}.${fieldInput.name} has deepClone set to true, but the referenced model ${referencedModel.name} is readonly. Deep cloning readonly models is not allowed.`,
|
|
118
|
+
});
|
|
119
|
+
return zod_1.z.NEVER;
|
|
120
|
+
}
|
|
121
|
+
const idField = (0, utils_2.getIdField)(model);
|
|
122
|
+
if (idField === undefined) {
|
|
123
|
+
ctx.addIssue({
|
|
124
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
125
|
+
message: `Cannot find id field for ${model.name}!`,
|
|
126
|
+
});
|
|
127
|
+
return zod_1.z.NEVER;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
...fieldInput,
|
|
131
|
+
kind: 'relation',
|
|
132
|
+
modelName: model.name,
|
|
133
|
+
unbrandedType: idField.unbrandedTypeName,
|
|
134
|
+
databaseType: idField.databaseType,
|
|
135
|
+
prismaRelationFieldName: fieldInput.prismaRelationFieldName ?? Branded.toDatabaseFieldName((0, utils_1.pluralize)(model.name)),
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
exports.fieldRelationTransformer = fieldRelationTransformer;
|