@postxl/generator 0.45.0 → 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.
|
@@ -785,10 +785,6 @@ function generateUserRepositorySpecificBlocks_InDatabase({ model, meta, imports,
|
|
|
785
785
|
rootUserInitializeBlock: '',
|
|
786
786
|
};
|
|
787
787
|
}
|
|
788
|
-
imports.addImport({
|
|
789
|
-
from: meta.types.importPath,
|
|
790
|
-
items: [(0, types_1.toVariableName)('UserRole')],
|
|
791
|
-
});
|
|
792
788
|
const { rootUserId, rootUserValue } = generateSharedRootUserBlocks({ model, meta, imports });
|
|
793
789
|
return {
|
|
794
790
|
rootUserNameConst: `public static ROOT_USER_ID = ${meta.types.toBrandedIdTypeFnName}(${rootUserId})`,
|
|
@@ -868,7 +864,7 @@ function generateSharedRootUserBlocks({ model, meta, imports, }) {
|
|
|
868
864
|
}
|
|
869
865
|
else if (field.kind === 'id') {
|
|
870
866
|
if (field.unbrandedTypeName === 'string') {
|
|
871
|
-
value =
|
|
867
|
+
value = 'rootId';
|
|
872
868
|
}
|
|
873
869
|
else if (field.unbrandedTypeName === 'number') {
|
|
874
870
|
value = -1;
|
|
@@ -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;
|
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
|
+
}
|