@postxl/generator 0.54.0 → 0.56.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.
@@ -136,34 +136,43 @@ function parseModel({ dmmfModel, enums, models, config, }) {
136
136
  if (core.attributes.ignore) {
137
137
  return undefined;
138
138
  }
139
- // NOTE: We assume that each relation may only reference one field. Because of this,
140
- // we can "relate" a given relation to a scalar field used in the relation.
141
- // Since Prisma doesn't mark those fields as relations, we need to preprocess
142
- // relations and then figure out which scalar fields are used in relations.
143
- const relations = {};
139
+ // NOTE: We assume that each relation may only reference one field.
140
+ // Because of this, we can "relate" a given relation to a scalar field
141
+ // used as a foreign key.
142
+ //
143
+ // Since Prisma doesn't mark foreign-key fields as relations,
144
+ // we need to preprocess relations and then figure out which scalar
145
+ // fields are actually foreign-keys.
146
+ const referencedModels = {};
147
+ /**
148
+ * A map of models that are referenced in any way in the relations.
149
+ */
150
+ const relatedModels = {};
144
151
  for (const dmmfField of dmmfModel.fields) {
145
- if (dmmfField.kind !== 'object' ||
146
- !dmmfField.relationName ||
147
- !dmmfField.relationFromFields ||
148
- !dmmfField.relationToFields) {
152
+ if (dmmfField.kind !== 'object' || !dmmfField.relationName) {
149
153
  continue;
150
154
  }
151
- if (dmmfField.relationFromFields.length > 1) {
152
- (0, error_1.throwError)(`Field ${highlight(`${dmmfModel.name}.${dmmfField.relationName}`)} has more than one "from" field`);
153
- }
154
155
  const referencedModel = models.find((m) => m.sourceName === dmmfField.type);
155
156
  if (!referencedModel) {
156
157
  (0, error_1.throwError)(`Field ${highlight(`${dmmfModel.name}.${dmmfField.name}`)} references unknown model ${highlight(dmmfField.type)}.`);
157
158
  }
159
+ if (!dmmfField.relationFromFields || dmmfField.relationFromFields.length === 0) {
160
+ // NOTE: This field has no foreign-key values in this model so it must be a back-relation.
161
+ relatedModels[dmmfField.type] = referencedModel;
162
+ continue;
163
+ }
164
+ if (dmmfField.relationFromFields.length > 1) {
165
+ (0, error_1.throwError)(`Field ${highlight(`${dmmfModel.name}.${dmmfField.relationName}`)} has more than one "from" field`);
166
+ }
158
167
  if (dmmfField.relationOnDelete && dmmfField.relationOnDelete !== 'NoAction') {
159
168
  (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!`);
160
169
  }
161
170
  // NOTE: At this point, we only have the `ModelCore`. After all models are parsed, we need to updated
162
171
  // the relations with the full `Model`. This is done in the `linkModels` function.
163
- relations[dmmfField.relationFromFields[0]] = referencedModel;
172
+ referencedModels[dmmfField.relationFromFields[0]] = referencedModel;
164
173
  }
165
174
  const relationFields = dmmfModel.fields
166
- .filter((f) => f.kind === 'object' && f.relationToFields && f.relationToFields.length === 1)
175
+ .filter((f) => { var _a; return f.kind === 'object' && ((_a = f.relationToFields) === null || _a === void 0 ? void 0 : _a.length) === 1; })
167
176
  .reduce((acc, f) => {
168
177
  if (f.relationFromFields && f.relationFromFields[0]) {
169
178
  acc[f.relationFromFields[0]] = f;
@@ -171,9 +180,14 @@ function parseModel({ dmmfModel, enums, models, config, }) {
171
180
  return acc;
172
181
  }, {});
173
182
  const fields = dmmfModel.fields
174
- .filter((dmmfField) =>
175
- // if field is a "backlink", i.e. the receiver of a relation, we ignore it
176
- dmmfField.kind !== 'object')
183
+ .filter((dmmfField) => {
184
+ // NOTE: This is a relation field that we'll handle when we process its foreign-key. If it's a back relation
185
+ // then it won't have a foreign-key and we simply ignore it.
186
+ if (dmmfField.kind === 'object') {
187
+ return false;
188
+ }
189
+ return true;
190
+ })
177
191
  .map((dmmfField) => {
178
192
  const attributes = (0, attributes_1.getFieldAttributes)(dmmfField);
179
193
  const fieldName = highlight(`${dmmfModel.name}.${dmmfField.name}`);
@@ -187,17 +201,19 @@ function parseModel({ dmmfModel, enums, models, config, }) {
187
201
  schemaType: dmmfField.type,
188
202
  };
189
203
  // NOTE: We mark scalar fields which are used in relations as relation fields by Purple Schema standards.
190
- if (dmmfField.name in relations) {
191
- const refModel = relations[dmmfField.name];
204
+ if (dmmfField.name in referencedModels) {
205
+ const refModel = referencedModels[dmmfField.name];
192
206
  const refField = relationFields[dmmfField.name];
193
207
  if (!refField) {
194
208
  (0, error_1.throwError)(`${fieldName}: Relation field ${highlight(dmmfField.name)} not found.`);
195
209
  }
196
- return Object.assign(Object.assign({ kind: 'relation' }, shared), { relatedModelBacklinkFieldName: Types.toFieldName(refField.name), typeName: Types.toTypeName(dmmfField.type), unbrandedTypeName: getTsTypeForId(dmmfField), relationToModel: refModel });
210
+ const _field = Object.assign(Object.assign({ kind: 'relation' }, shared), { relationFieldName: Types.toFieldName(refField.name), unbrandedTypeName: getTsTypeForId(dmmfField), relationToModel: refModel });
211
+ return _field;
197
212
  }
198
213
  if (dmmfField.isId) {
199
214
  const isGeneratedField = isAutoIncrementField(dmmfField) || isUUIDField(dmmfField);
200
- return Object.assign(Object.assign({ kind: 'id' }, shared), { isUnique: isUniqueField(dmmfField), isGenerated: isGeneratedField, unbrandedTypeName: getTsTypeForId(dmmfField), model: core });
215
+ const _field = Object.assign(Object.assign({ kind: 'id' }, shared), { isUnique: isUniqueField(dmmfField), isGenerated: isGeneratedField, unbrandedTypeName: getTsTypeForId(dmmfField), model: core });
216
+ return _field;
201
217
  }
202
218
  if (dmmfField.kind === 'scalar') {
203
219
  let validation = undefined;
@@ -210,14 +226,16 @@ function parseModel({ dmmfModel, enums, models, config, }) {
210
226
  if (dmmfField.type === 'Float') {
211
227
  validation = { type: 'float' };
212
228
  }
213
- return Object.assign(Object.assign({ kind: 'scalar', validation }, shared), { isUnique: isUniqueField(dmmfField), isGenerated: isAutoIncrementField(dmmfField), tsTypeName: getTsTypeForScalar(dmmfField) });
229
+ const _field = Object.assign(Object.assign({ kind: 'scalar', validation }, shared), { isUnique: isUniqueField(dmmfField), isGenerated: isAutoIncrementField(dmmfField), tsTypeName: getTsTypeForScalar(dmmfField) });
230
+ return _field;
214
231
  }
215
232
  if (dmmfField.kind === 'enum') {
216
233
  const fieldEnumDef = enums.find((e) => e.sourceName === dmmfField.type);
217
234
  if (!fieldEnumDef) {
218
235
  (0, error_1.throwError)(`${fieldName}: Field references unknown enum ${highlight(dmmfField.type)}.`);
219
236
  }
220
- return Object.assign(Object.assign({ kind: 'enum' }, shared), { typeName: getTsTypeForEnum(dmmfField), enumerator: fieldEnumDef });
237
+ const _field = Object.assign(Object.assign({ kind: 'enum' }, shared), { typeName: getTsTypeForEnum(dmmfField), enumerator: fieldEnumDef });
238
+ return _field;
221
239
  }
222
240
  (0, error_1.throwError)(`${fieldName} is not scalar, enum nor relation.`);
223
241
  })
@@ -228,7 +246,7 @@ function parseModel({ dmmfModel, enums, models, config, }) {
228
246
  nameField,
229
247
  fields,
230
248
  createdAtField,
231
- updatedAtField });
249
+ updatedAtField, relatedModels: Object.values(relatedModels) });
232
250
  }
233
251
  /**
234
252
  * Checks that there is exactly one id field and that there is at most one default field.
@@ -290,7 +308,13 @@ function validateFields({ fields, model: { name } }) {
290
308
  if (!idField) {
291
309
  (0, error_1.throwError)(`Model ${highlight(name)} does not have an id field`);
292
310
  }
293
- return { idField, defaultField, nameField: (_a = labelField !== null && labelField !== void 0 ? labelField : nameField) !== null && _a !== void 0 ? _a : idField, createdAtField, updatedAtField };
311
+ return {
312
+ idField,
313
+ defaultField,
314
+ nameField: (_a = labelField !== null && labelField !== void 0 ? labelField : nameField) !== null && _a !== void 0 ? _a : idField,
315
+ createdAtField,
316
+ updatedAtField,
317
+ };
294
318
  }
295
319
  function isAutoIncrementField(fieldDmmf) {
296
320
  if (fieldDmmf.default === undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.54.0",
3
+ "version": "0.56.0",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {