@postxl/generator 0.44.5 → 0.46.0
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/generators/models/repository.generator.js +122 -32
- package/dist/lib/attributes.d.ts +5 -0
- package/dist/lib/schema/types.d.ts +1 -1
- package/dist/lib/utils/error.d.ts +4 -0
- package/dist/lib/utils/error.js +11 -1
- package/dist/prisma/attributes.js +5 -0
- package/dist/prisma/parse.js +79 -18
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@ const meta_1 = require("../../lib/meta");
|
|
|
6
6
|
const fields_1 = require("../../lib/schema/fields");
|
|
7
7
|
const schema_1 = require("../../lib/schema/schema");
|
|
8
8
|
const types_1 = require("../../lib/schema/types");
|
|
9
|
+
const types_2 = require("../../lib/types");
|
|
9
10
|
const jsdoc_1 = require("../../lib/utils/jsdoc");
|
|
10
11
|
const string_1 = require("../../lib/utils/string");
|
|
11
12
|
/**
|
|
@@ -341,7 +342,7 @@ function _generateMainBuildingBlocks_InMemoryOnly({ model, meta, schemaMeta, imp
|
|
|
341
342
|
throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
|
|
342
343
|
}
|
|
343
344
|
const mutationId = await execution.startUpdateMutation({
|
|
344
|
-
model: '
|
|
345
|
+
model: '${meta.actions.actionScopeConstType}',
|
|
345
346
|
entityId: item.id,
|
|
346
347
|
sourceObject: existingItem,
|
|
347
348
|
updateObject: item,
|
|
@@ -639,7 +640,7 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, schemaMeta, import
|
|
|
639
640
|
}
|
|
640
641
|
|
|
641
642
|
const mutationId = await execution.startUpdateMutation({
|
|
642
|
-
model: '
|
|
643
|
+
model: '${meta.actions.actionScopeConstType}',
|
|
643
644
|
entityId: item.id,
|
|
644
645
|
sourceObject: existingItem,
|
|
645
646
|
updateObject: item,
|
|
@@ -784,12 +785,9 @@ function generateUserRepositorySpecificBlocks_InDatabase({ model, meta, imports,
|
|
|
784
785
|
rootUserInitializeBlock: '',
|
|
785
786
|
};
|
|
786
787
|
}
|
|
787
|
-
|
|
788
|
-
from: meta.types.importPath,
|
|
789
|
-
items: [(0, types_1.toVariableName)('UserRole')],
|
|
790
|
-
});
|
|
788
|
+
const { rootUserId, rootUserValue } = generateSharedRootUserBlocks({ model, meta, imports });
|
|
791
789
|
return {
|
|
792
|
-
rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(
|
|
790
|
+
rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(${rootUserId})`,
|
|
793
791
|
getterBlock: `
|
|
794
792
|
// We initialize the root user in the init() function
|
|
795
793
|
private _rootUser!: ${meta.types.typeName}
|
|
@@ -806,16 +804,7 @@ function generateUserRepositorySpecificBlocks_InDatabase({ model, meta, imports,
|
|
|
806
804
|
}
|
|
807
805
|
|
|
808
806
|
const rawUser = await this.db.user.create({
|
|
809
|
-
data: {
|
|
810
|
-
id: ${meta.data.repository.className}.ROOT_USER_ID,
|
|
811
|
-
name: 'System',
|
|
812
|
-
email: 'system@postxl.com',
|
|
813
|
-
role: UserRole.Admin,
|
|
814
|
-
login: 'not-set',
|
|
815
|
-
familyName: 'System user',
|
|
816
|
-
age: 0,
|
|
817
|
-
countryId: null,
|
|
818
|
-
},
|
|
807
|
+
data: { ${rootUserValue} },
|
|
819
808
|
})
|
|
820
809
|
const newRootUser = this.toUser(rawUser)
|
|
821
810
|
this.set(newRootUser)
|
|
@@ -832,12 +821,9 @@ function generateUserRepositorySpecificBlocks_InMemoryOnly({ model, meta, import
|
|
|
832
821
|
rootUserInitializeBlock: '',
|
|
833
822
|
};
|
|
834
823
|
}
|
|
835
|
-
|
|
836
|
-
from: meta.types.importPath,
|
|
837
|
-
items: [(0, types_1.toVariableName)('UserRole')],
|
|
838
|
-
});
|
|
824
|
+
const { rootUserId, rootUserValue } = generateSharedRootUserBlocks({ model, meta, imports });
|
|
839
825
|
return {
|
|
840
|
-
rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(
|
|
826
|
+
rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(${rootUserId})`,
|
|
841
827
|
getterBlock: `
|
|
842
828
|
// We initialize the root user in the init() function
|
|
843
829
|
private _rootUser!: ${meta.types.typeName}
|
|
@@ -853,22 +839,126 @@ function generateUserRepositorySpecificBlocks_InMemoryOnly({ model, meta, import
|
|
|
853
839
|
return
|
|
854
840
|
}
|
|
855
841
|
|
|
856
|
-
const rawUser = {
|
|
857
|
-
id: ${meta.data.repository.className}.ROOT_USER_ID,
|
|
858
|
-
name: 'System',
|
|
859
|
-
email: 'system@postxl.com',
|
|
860
|
-
role: UserRole.Admin,
|
|
861
|
-
login: 'not-set',
|
|
862
|
-
familyName: 'System user',
|
|
863
|
-
age: 0,
|
|
864
|
-
countryId: null,
|
|
865
|
-
}
|
|
842
|
+
const rawUser = { ${rootUserValue} }
|
|
866
843
|
const newRootUser = this.verifyItem (rawUser)
|
|
867
844
|
this.set(newRootUser)
|
|
868
845
|
this._rootUser = newRootUser
|
|
869
846
|
}`,
|
|
870
847
|
};
|
|
871
848
|
}
|
|
849
|
+
function generateSharedRootUserBlocks({ model, meta, imports, }) {
|
|
850
|
+
var _a;
|
|
851
|
+
const providedDefault = model.attributes.systemUser;
|
|
852
|
+
const assignments = [];
|
|
853
|
+
let rootUserId = '';
|
|
854
|
+
for (const field of model.fields) {
|
|
855
|
+
let value = undefined;
|
|
856
|
+
if (providedDefault && field.name in providedDefault) {
|
|
857
|
+
value = providedDefault[field.name];
|
|
858
|
+
}
|
|
859
|
+
else if ((_a = field.attributes.examples) === null || _a === void 0 ? void 0 : _a.length) {
|
|
860
|
+
value = field.attributes.examples[0];
|
|
861
|
+
}
|
|
862
|
+
else if (!field.isRequired) {
|
|
863
|
+
value = null;
|
|
864
|
+
}
|
|
865
|
+
else if (field.kind === 'id') {
|
|
866
|
+
if (field.unbrandedTypeName === 'string') {
|
|
867
|
+
value = 'rootId';
|
|
868
|
+
}
|
|
869
|
+
else if (field.unbrandedTypeName === 'number') {
|
|
870
|
+
value = -1;
|
|
871
|
+
}
|
|
872
|
+
else {
|
|
873
|
+
throw new Error(`Could not generate root user: Unsupported id type ${field.unbrandedTypeName}!`);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
if (field.isRequired && !field.attributes.isCreatedAt && !field.attributes.isUpdatedAt) {
|
|
878
|
+
throw new Error(`Could not generate root user: No value for field ${field.name} provided!`);
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
switch (field.kind) {
|
|
885
|
+
case 'id': {
|
|
886
|
+
if (field.unbrandedTypeName === 'string') {
|
|
887
|
+
value = `'${value}'`;
|
|
888
|
+
}
|
|
889
|
+
else if (field.unbrandedTypeName === 'number') {
|
|
890
|
+
value = `${value}`;
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
throw new Error(`Could not generate root user: Unsupported id type ${field.unbrandedTypeName}!`);
|
|
894
|
+
}
|
|
895
|
+
rootUserId = value;
|
|
896
|
+
break;
|
|
897
|
+
}
|
|
898
|
+
case 'scalar': {
|
|
899
|
+
if (value === null) {
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
switch (field.tsTypeName) {
|
|
903
|
+
case 'string': {
|
|
904
|
+
value = `'${value}'`;
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
case 'number': {
|
|
908
|
+
value = `${value}`;
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
case 'boolean': {
|
|
912
|
+
value = value ? 'true' : 'false';
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
case 'Date': {
|
|
916
|
+
value = `${value}`;
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
default: {
|
|
920
|
+
throw new Error(`Could not generate root user: Unsupported scalar type ${field.tsTypeName}!`);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
break;
|
|
924
|
+
}
|
|
925
|
+
case 'relation': {
|
|
926
|
+
if (value === null) {
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
if (field.unbrandedTypeName === 'string') {
|
|
930
|
+
value = `'${value}'`;
|
|
931
|
+
}
|
|
932
|
+
else if (field.unbrandedTypeName === 'number') {
|
|
933
|
+
value = `${value}`;
|
|
934
|
+
}
|
|
935
|
+
else {
|
|
936
|
+
throw new Error(`Could not generate root user: Unsupported relation type ${field.unbrandedTypeName}!`);
|
|
937
|
+
}
|
|
938
|
+
break;
|
|
939
|
+
}
|
|
940
|
+
case 'enum': {
|
|
941
|
+
if (value === null) {
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
imports.addImport({
|
|
945
|
+
from: meta.types.importPath,
|
|
946
|
+
items: [field.enumerator.name],
|
|
947
|
+
});
|
|
948
|
+
value = `${field.enumerator.name}.${value}`;
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
default: {
|
|
952
|
+
throw new types_2.ExhaustiveSwitchCheck(field);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
assignments.push(`${field.name}: ${value}`);
|
|
956
|
+
}
|
|
957
|
+
return {
|
|
958
|
+
rootUserId,
|
|
959
|
+
rootUserValue: assignments.join(', '),
|
|
960
|
+
};
|
|
961
|
+
}
|
|
872
962
|
/**
|
|
873
963
|
* Generates code chunks responsible for verifying the ID validity of a model instance and generating the id
|
|
874
964
|
* value of a model with auto-generated id.
|
package/dist/lib/attributes.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ export type ModelAttributes = {
|
|
|
31
31
|
* Seed to use for random generation.
|
|
32
32
|
*/
|
|
33
33
|
randomSeed?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Schema tag: ´@@SystemUser()`
|
|
36
|
+
* The user that is used for system actions.
|
|
37
|
+
*/
|
|
38
|
+
systemUser?: object;
|
|
34
39
|
};
|
|
35
40
|
export type FieldAttributes = {
|
|
36
41
|
/**
|
|
@@ -150,7 +150,7 @@ export declare const toPath: (t: string) => FilePath;
|
|
|
150
150
|
/**
|
|
151
151
|
* Branded string values that can be used as import statement values in the generators
|
|
152
152
|
*/
|
|
153
|
-
export type ImportableTypes = FunctionName | ClassName | AnnotatedTypeName | VariableName;
|
|
153
|
+
export type ImportableTypes = FunctionName | ClassName | AnnotatedTypeName | VariableName | EnumName;
|
|
154
154
|
/**
|
|
155
155
|
* Branded string values that can be used as paths in import statements
|
|
156
156
|
*/
|
|
@@ -3,3 +3,7 @@
|
|
|
3
3
|
* By showing the error via console.log and throwing it again, we ensure that the error message is shown.
|
|
4
4
|
*/
|
|
5
5
|
export declare function throwError(message: string): never;
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the error message from an error object or any other object.
|
|
8
|
+
*/
|
|
9
|
+
export declare function extractError(error: unknown): string;
|
package/dist/lib/utils/error.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.throwError = void 0;
|
|
3
|
+
exports.extractError = exports.throwError = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Prisma generator often overwrites any error message because of some concurrency issues.
|
|
6
6
|
* By showing the error via console.log and throwing it again, we ensure that the error message is shown.
|
|
@@ -11,3 +11,13 @@ function throwError(message) {
|
|
|
11
11
|
throw new Error(m);
|
|
12
12
|
}
|
|
13
13
|
exports.throwError = throwError;
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the error message from an error object or any other object.
|
|
16
|
+
*/
|
|
17
|
+
function extractError(error) {
|
|
18
|
+
if (error instanceof Error) {
|
|
19
|
+
return error.message;
|
|
20
|
+
}
|
|
21
|
+
return JSON.stringify(error);
|
|
22
|
+
}
|
|
23
|
+
exports.extractError = extractError;
|
|
@@ -74,6 +74,10 @@ function getModelAttributes(model) {
|
|
|
74
74
|
schema: zod_1.default.string().optional(),
|
|
75
75
|
index: zod_1.default.array(zod_1.default.string()).optional(),
|
|
76
76
|
seed: zod_1.default.string().optional(),
|
|
77
|
+
systemUser: zod_1.default
|
|
78
|
+
.string()
|
|
79
|
+
.transform((t) => JSON.parse(t))
|
|
80
|
+
.optional(),
|
|
77
81
|
})
|
|
78
82
|
.transform((obj) => ({
|
|
79
83
|
ignore: obj.ignore,
|
|
@@ -81,6 +85,7 @@ function getModelAttributes(model) {
|
|
|
81
85
|
description: obj.description,
|
|
82
86
|
databaseSchema: obj.schema,
|
|
83
87
|
index: obj.index,
|
|
88
|
+
systemUser: obj.systemUser,
|
|
84
89
|
randomSeed: obj.seed !== undefined ? parseInt(obj.seed, 10) : undefined,
|
|
85
90
|
}));
|
|
86
91
|
const result = decoder.safeParse(attributes);
|
package/dist/prisma/parse.js
CHANGED
|
@@ -37,6 +37,7 @@ const REQUIRED_MODELS = ['User', 'Config', 'File', 'Action', 'Mutation'];
|
|
|
37
37
|
*/
|
|
38
38
|
function parsePrismaSchema({ datamodel: { enums: enumsRaw, models: modelsRaw }, config, }) {
|
|
39
39
|
ensureRequiredModelsExists(modelsRaw);
|
|
40
|
+
ensureConsistency({ models: modelsRaw, enums: enumsRaw });
|
|
40
41
|
// NOTE: We preprocess models and enums so that we can populate relationships.
|
|
41
42
|
const models = modelsRaw.map((dmmfModel) => parseModelCore({ dmmfModel, config }));
|
|
42
43
|
const enums = enumsRaw.map((dmmfEnum) => parseEnum({ dmmfEnum, config }));
|
|
@@ -49,10 +50,60 @@ exports.parsePrismaSchema = parsePrismaSchema;
|
|
|
49
50
|
function ensureRequiredModelsExists(models) {
|
|
50
51
|
for (const requiredModel of REQUIRED_MODELS) {
|
|
51
52
|
if (!models.find((m) => m.name === requiredModel)) {
|
|
52
|
-
(0, error_1.throwError)(`Required model ${requiredModel} not found in schema!`);
|
|
53
|
+
(0, error_1.throwError)(`Required model ${highlight(requiredModel)} not found in schema!`);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Validates:
|
|
59
|
+
* - That there are no duplicate model names
|
|
60
|
+
* - That model names are singular
|
|
61
|
+
* - That model attributes are valid
|
|
62
|
+
* - That field attributes are valid
|
|
63
|
+
* - That enum attributes are valid
|
|
64
|
+
*/
|
|
65
|
+
function ensureConsistency({ models, enums }) {
|
|
66
|
+
const errors = [];
|
|
67
|
+
const modelNames = models.map((m) => m.name);
|
|
68
|
+
const duplicateModelName = modelNames.find((name, i) => modelNames.indexOf(name) !== i);
|
|
69
|
+
if (duplicateModelName) {
|
|
70
|
+
errors.push(`Model ${duplicateModelName} is defined more than once.`);
|
|
71
|
+
}
|
|
72
|
+
for (const model of models) {
|
|
73
|
+
if ((0, string_1.isPlural)(model.name)) {
|
|
74
|
+
errors.push(`Model ${highlight(model.name)} is plural. Please use singular names for models.`);
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
(0, attributes_1.getModelAttributes)(model);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
errors.push(`Model ${highlight(model.name)} has invalid model attributes: ${(0, error_1.extractError)(e)}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const model of models) {
|
|
84
|
+
for (const field of model.fields) {
|
|
85
|
+
try {
|
|
86
|
+
(0, attributes_1.getFieldAttributes)(field);
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
errors.push(`Model ${highlight(model.name)} has invalid attributes for field ${highlight(field.name)}:
|
|
90
|
+
${(0, error_1.extractError)(e)}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const enumDef of enums) {
|
|
95
|
+
try {
|
|
96
|
+
(0, attributes_1.getEnumAttributes)(enumDef);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
errors.push(`Enum ${highlight(enumDef.name)} has invalid attributes:
|
|
100
|
+
${(0, error_1.extractError)(e)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (errors.length > 0) {
|
|
104
|
+
(0, error_1.throwError)(`${errors.length} ${(0, string_1.pluralize)('issue', errors.length)} detected in schema:\n * ${errors.join('\n * ')}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
56
107
|
function isModelNotIgnored(model) {
|
|
57
108
|
return model !== undefined && !model.attributes.ignore;
|
|
58
109
|
}
|
|
@@ -98,14 +149,14 @@ function parseModel({ dmmfModel, enums, models, config, }) {
|
|
|
98
149
|
continue;
|
|
99
150
|
}
|
|
100
151
|
if (dmmfField.relationFromFields.length > 1) {
|
|
101
|
-
|
|
152
|
+
(0, error_1.throwError)(`Field ${highlight(`${dmmfModel.name}.${dmmfField.relationName}`)} has more than one "from" field`);
|
|
102
153
|
}
|
|
103
154
|
const referencedModel = models.find((m) => m.sourceName === dmmfField.type);
|
|
104
155
|
if (!referencedModel) {
|
|
105
|
-
(0, error_1.throwError)(`
|
|
156
|
+
(0, error_1.throwError)(`Field ${highlight(`${dmmfModel.name}.${dmmfField.name}`)} references unknown model ${highlight(dmmfField.type)}.`);
|
|
106
157
|
}
|
|
107
158
|
if (dmmfField.relationOnDelete && dmmfField.relationOnDelete !== 'NoAction') {
|
|
108
|
-
(0, error_1.throwError)(`Investigate: "onDelete" attribute for relationship ${dmmfField.relationName} must be "NoAction": any deletes must be handled in the application layer, e.g. to update repository and search caches!`);
|
|
159
|
+
(0, error_1.throwError)(`Investigate model ${highlight(dmmfModel.name)}: "onDelete" attribute for relationship ${highlight(dmmfField.relationName)} must be "NoAction": any deletes must be handled in the application layer, e.g. to update repository and search caches!`);
|
|
109
160
|
}
|
|
110
161
|
relations[dmmfField.relationFromFields[0]] = referencedModel;
|
|
111
162
|
}
|
|
@@ -123,6 +174,7 @@ function parseModel({ dmmfModel, enums, models, config, }) {
|
|
|
123
174
|
dmmfField.kind !== 'object')
|
|
124
175
|
.map((dmmfField) => {
|
|
125
176
|
const attributes = (0, attributes_1.getFieldAttributes)(dmmfField);
|
|
177
|
+
const fieldName = highlight(`${dmmfModel.name}.${dmmfField.name}`);
|
|
126
178
|
const shared = {
|
|
127
179
|
name: Types.toFieldName((0, string_1.toCamelCase)(dmmfField.name)),
|
|
128
180
|
description: attributes.description,
|
|
@@ -137,7 +189,7 @@ function parseModel({ dmmfModel, enums, models, config, }) {
|
|
|
137
189
|
const refModel = relations[dmmfField.name];
|
|
138
190
|
const refField = relationFields[dmmfField.name];
|
|
139
191
|
if (!refField) {
|
|
140
|
-
(0, error_1.throwError)(
|
|
192
|
+
(0, error_1.throwError)(`${fieldName}: Relation field ${highlight(dmmfField.name)} not found.`);
|
|
141
193
|
}
|
|
142
194
|
return Object.assign(Object.assign({ kind: 'relation' }, shared), { relatedModelBacklinkFieldName: Types.toFieldName(refField.name), typeName: Types.toTypeName(dmmfField.type), unbrandedTypeName: getTsTypeForId(dmmfField), relationToModel: refModel });
|
|
143
195
|
}
|
|
@@ -148,7 +200,7 @@ function parseModel({ dmmfModel, enums, models, config, }) {
|
|
|
148
200
|
if (dmmfField.kind === 'scalar') {
|
|
149
201
|
let validation = undefined;
|
|
150
202
|
if (dmmfField.isList) {
|
|
151
|
-
(0, error_1.throwError)(
|
|
203
|
+
(0, error_1.throwError)(`${fieldName}: Array fields with scalars (e.g. String[]) aren't supported! Use a relation instead. `);
|
|
152
204
|
}
|
|
153
205
|
if (dmmfField.type === 'Int') {
|
|
154
206
|
validation = { type: 'int' };
|
|
@@ -161,11 +213,11 @@ function parseModel({ dmmfModel, enums, models, config, }) {
|
|
|
161
213
|
if (dmmfField.kind === 'enum') {
|
|
162
214
|
const fieldEnumDef = enums.find((e) => e.sourceName === dmmfField.type);
|
|
163
215
|
if (!fieldEnumDef) {
|
|
164
|
-
(0, error_1.throwError)(
|
|
216
|
+
(0, error_1.throwError)(`${fieldName}: Field references unknown enum ${highlight(dmmfField.type)}.`);
|
|
165
217
|
}
|
|
166
218
|
return Object.assign(Object.assign({ kind: 'enum' }, shared), { typeName: getTsTypeForEnum(dmmfField), enumerator: fieldEnumDef });
|
|
167
219
|
}
|
|
168
|
-
(0, error_1.throwError)(
|
|
220
|
+
(0, error_1.throwError)(`${fieldName} is not scalar, enum nor relation.`);
|
|
169
221
|
})
|
|
170
222
|
.filter((field) => !isFieldIgnored({ field }));
|
|
171
223
|
const { idField, defaultField, nameField, createdAtField, updatedAtField } = validateFields({ fields, model: core });
|
|
@@ -188,6 +240,7 @@ function validateFields({ fields, model: { name } }) {
|
|
|
188
240
|
let nameField = undefined;
|
|
189
241
|
let defaultField = undefined;
|
|
190
242
|
for (const field of fields) {
|
|
243
|
+
const fieldName = highlight(`${name}.${field.name}`);
|
|
191
244
|
switch (field.kind) {
|
|
192
245
|
case 'scalar':
|
|
193
246
|
if (field.name === 'name') {
|
|
@@ -195,20 +248,20 @@ function validateFields({ fields, model: { name } }) {
|
|
|
195
248
|
}
|
|
196
249
|
if (field.attributes.isCreatedAt) {
|
|
197
250
|
if (createdAtField) {
|
|
198
|
-
|
|
251
|
+
(0, error_1.throwError)(`${fieldName} has multiple createdAt fields`);
|
|
199
252
|
}
|
|
200
253
|
createdAtField = field;
|
|
201
254
|
}
|
|
202
255
|
if (field.attributes.isUpdatedAt) {
|
|
203
256
|
if (updatedAtField) {
|
|
204
|
-
|
|
257
|
+
(0, error_1.throwError)(`${fieldName} has multiple updatedAt fields`);
|
|
205
258
|
}
|
|
206
259
|
updatedAtField = field;
|
|
207
260
|
}
|
|
208
261
|
break;
|
|
209
262
|
case 'id':
|
|
210
263
|
if (idField) {
|
|
211
|
-
|
|
264
|
+
(0, error_1.throwError)(`${fieldName} has multiple id fields`);
|
|
212
265
|
}
|
|
213
266
|
idField = field;
|
|
214
267
|
break;
|
|
@@ -220,20 +273,20 @@ function validateFields({ fields, model: { name } }) {
|
|
|
220
273
|
//handle default case
|
|
221
274
|
if (field.attributes.isDefaultField && field.kind === 'scalar') {
|
|
222
275
|
if (defaultField !== undefined) {
|
|
223
|
-
|
|
276
|
+
(0, error_1.throwError)(`${fieldName} has multiple default fields`);
|
|
224
277
|
}
|
|
225
278
|
defaultField = field;
|
|
226
279
|
}
|
|
227
280
|
//handle name field
|
|
228
281
|
if (field.attributes.isLabel) {
|
|
229
282
|
if (labelField !== undefined) {
|
|
230
|
-
|
|
283
|
+
(0, error_1.throwError)(`${fieldName} has multiple name fields`);
|
|
231
284
|
}
|
|
232
285
|
labelField = field;
|
|
233
286
|
}
|
|
234
287
|
}
|
|
235
288
|
if (!idField) {
|
|
236
|
-
|
|
289
|
+
(0, error_1.throwError)(`Model ${highlight(name)} does not have an id field`);
|
|
237
290
|
}
|
|
238
291
|
return { idField, defaultField, nameField: (_a = labelField !== null && labelField !== void 0 ? labelField : nameField) !== null && _a !== void 0 ? _a : idField, createdAtField, updatedAtField };
|
|
239
292
|
}
|
|
@@ -309,12 +362,12 @@ function getTsTypeForScalar(field) {
|
|
|
309
362
|
return Types.toTypeName('number');
|
|
310
363
|
case 'Json':
|
|
311
364
|
case 'Bytes':
|
|
312
|
-
(0, error_1.throwError)(
|
|
365
|
+
(0, error_1.throwError)(`Field ${highlight(field.name)}: Type ${field.type} Not implemented yet`);
|
|
313
366
|
// While TypeScript understands that throwError never returns, eslint doesn't and complains.
|
|
314
367
|
// Hence we ignore the fallthrough error.
|
|
315
368
|
// eslint-disable-next-line no-fallthrough
|
|
316
369
|
default:
|
|
317
|
-
(0, error_1.throwError)(`Investigate: 'default' case in getTsTypeForScalar for field ${field.name} of type ${field.type}`);
|
|
370
|
+
(0, error_1.throwError)(`Investigate: 'default' case in getTsTypeForScalar for field ${highlight(field.name)} of type ${field.type}`);
|
|
318
371
|
}
|
|
319
372
|
}
|
|
320
373
|
/**
|
|
@@ -332,7 +385,7 @@ function getTsTypeForId(field) {
|
|
|
332
385
|
case 'Int':
|
|
333
386
|
return Types.toTypeName('number');
|
|
334
387
|
default:
|
|
335
|
-
(0, error_1.throwError)(`The id field ${field.name}
|
|
388
|
+
(0, error_1.throwError)(`The id field ${highlight(field.name)} is of type ${field.type} - but only BigInt, Boolean, Decimal, Float, Int and String are supported for Ids.`);
|
|
336
389
|
}
|
|
337
390
|
}
|
|
338
391
|
/**
|
|
@@ -347,9 +400,17 @@ function getTsTypeForEnum(field) {
|
|
|
347
400
|
case 'Decimal':
|
|
348
401
|
case 'Float':
|
|
349
402
|
case 'Int':
|
|
350
|
-
(0, error_1.throwError)(`The enum field ${field.name}
|
|
403
|
+
(0, error_1.throwError)(`The enum field ${highlight(field.name)} is of type ${field.type} - but only String fields are supported for enums so far.`);
|
|
351
404
|
break;
|
|
352
405
|
default:
|
|
353
406
|
return Types.toTypeName(field.type);
|
|
354
407
|
}
|
|
355
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Highlights a string cyan
|
|
411
|
+
*
|
|
412
|
+
* NOTE: We would normally use `chalk.cyan` here, but this causes an error in the generator, so we use this workaround.
|
|
413
|
+
*/
|
|
414
|
+
function highlight(str) {
|
|
415
|
+
return `\u001B[36m${str}\u001B[39m`;
|
|
416
|
+
}
|