@zenstackhq/sdk 3.4.3 → 3.4.5

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/index.cjs CHANGED
@@ -44,7 +44,9 @@ __export(model_utils_exports, {
44
44
  getAttribute: () => getAttribute,
45
45
  getAuthDecl: () => getAuthDecl,
46
46
  getContainingModel: () => getContainingModel,
47
+ getDelegateOriginModel: () => getDelegateOriginModel,
47
48
  getIdFields: () => getIdFields,
49
+ getOwnedFields: () => getOwnedFields,
48
50
  hasAttribute: () => hasAttribute,
49
51
  isDelegateModel: () => isDelegateModel,
50
52
  isFromStdlib: () => isFromStdlib,
@@ -89,6 +91,29 @@ function isDelegateModel(node) {
89
91
  return (0, import_ast.isDataModel)(node) && hasAttribute(node, "@@delegate");
90
92
  }
91
93
  __name(isDelegateModel, "isDelegateModel");
94
+ function getOwnedFields(model) {
95
+ const fields = [
96
+ ...model.fields
97
+ ];
98
+ for (const mixin of model.mixins) {
99
+ if (mixin.ref) {
100
+ fields.push(...getOwnedFields(mixin.ref));
101
+ }
102
+ }
103
+ return fields;
104
+ }
105
+ __name(getOwnedFields, "getOwnedFields");
106
+ function getDelegateOriginModel(field, contextModel) {
107
+ let base = contextModel.baseModel?.ref;
108
+ while (base) {
109
+ if (isDelegateModel(base) && getOwnedFields(base).some((f) => f.name === field.name)) {
110
+ return base.name;
111
+ }
112
+ base = base.baseModel?.ref;
113
+ }
114
+ return void 0;
115
+ }
116
+ __name(getDelegateOriginModel, "getDelegateOriginModel");
92
117
  function isUniqueField(field) {
93
118
  if (hasAttribute(field, "@unique")) {
94
119
  return true;
@@ -642,7 +667,7 @@ var PrismaSchemaGenerator = class {
642
667
  if (model_utils_exports.hasAttribute(field, "@computed")) {
643
668
  continue;
644
669
  }
645
- if (model_utils_exports.isIdField(field, decl) || !this.isInheritedFromDelegate(field, decl)) {
670
+ if (model_utils_exports.isIdField(field, decl) || !getDelegateOriginModel(field, decl)) {
646
671
  this.generateModelField(model, field, decl);
647
672
  }
648
673
  }
@@ -714,7 +739,7 @@ var PrismaSchemaGenerator = class {
714
739
  const type = new ModelFieldType(fieldType, isArray, field.type.optional);
715
740
  const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithAuthInvocation(attr)).filter((attr) => (
716
741
  // when building physical schema, exclude `@default` for id fields inherited from delegate base
717
- !(model_utils_exports.isIdField(field, contextModel) && this.isInheritedFromDelegate(field, contextModel) && attr.decl.$refText === "@default")
742
+ !(model_utils_exports.isIdField(field, contextModel) && getDelegateOriginModel(field, contextModel) && attr.decl.$refText === "@default")
718
743
  )).map((attr) => this.makeFieldAttribute(attr));
719
744
  const docs = [
720
745
  ...field.comments
@@ -732,9 +757,6 @@ var PrismaSchemaGenerator = class {
732
757
  }
733
758
  return import_langium.AstUtils.streamAst(expr).some(import_utils2.isAuthInvocation);
734
759
  }
735
- isInheritedFromDelegate(field, contextModel) {
736
- return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
737
- }
738
760
  makeFieldAttribute(attr) {
739
761
  const attrName = attr.decl.ref.name;
740
762
  return new FieldAttribute(attrName, attr.args.map((arg) => this.makeAttributeArg(arg)));
@@ -1099,8 +1121,11 @@ var TsSchemaGenerator = class {
1099
1121
  objectFields.push(ts.factory.createPropertyAssignment("omit", ts.factory.createTrue()));
1100
1122
  }
1101
1123
  if (contextModel && // id fields are duplicated in inherited models
1102
- !isIdField(field, contextModel) && field.$container !== contextModel && isDelegateModel(field.$container)) {
1103
- objectFields.push(ts.factory.createPropertyAssignment("originModel", ts.factory.createStringLiteral(field.$container.name)));
1124
+ !isIdField(field, contextModel)) {
1125
+ const delegateOrigin = getDelegateOriginModel(field, contextModel);
1126
+ if (delegateOrigin) {
1127
+ objectFields.push(ts.factory.createPropertyAssignment("originModel", ts.factory.createStringLiteral(delegateOrigin)));
1128
+ }
1104
1129
  }
1105
1130
  if (this.isDiscriminatorField(field)) {
1106
1131
  objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));