@zenstackhq/sdk 3.4.6 → 3.5.0-beta.1

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
@@ -887,11 +887,12 @@ var TsSchemaGenerator = class {
887
887
  __name(this, "TsSchemaGenerator");
888
888
  }
889
889
  usedExpressionUtils = false;
890
+ usedAttributeApplication = false;
891
+ usedFieldDefault = false;
890
892
  async generate(model, options) {
891
893
  import_node_fs.default.mkdirSync(options.outDir, {
892
894
  recursive: true
893
895
  });
894
- this.usedExpressionUtils = false;
895
896
  this.generateSchema(model, options);
896
897
  if (options.generateModelTypes !== false) {
897
898
  this.generateModelsAndTypeDefs(model, options);
@@ -915,6 +916,9 @@ var TsSchemaGenerator = class {
915
916
  });
916
917
  }
917
918
  for (const { lite, file } of targets) {
919
+ this.usedExpressionUtils = false;
920
+ this.usedAttributeApplication = false;
921
+ this.usedFieldDefault = false;
918
922
  const statements = [];
919
923
  this.generateSchemaStatements(model, statements, lite);
920
924
  this.generateBannerComments(statements);
@@ -929,6 +933,12 @@ var TsSchemaGenerator = class {
929
933
  const schemaClass = this.createSchemaClass(model, lite);
930
934
  const schemaImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(void 0, void 0, ts.factory.createNamedImports([
931
935
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
936
+ ...this.usedAttributeApplication ? [
937
+ ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("AttributeApplication"))
938
+ ] : [],
939
+ ...this.usedFieldDefault ? [
940
+ ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("FieldDefault"))
941
+ ] : [],
932
942
  ...this.usedExpressionUtils ? [
933
943
  ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
934
944
  ] : []
@@ -982,6 +992,14 @@ var TsSchemaGenerator = class {
982
992
  createAsConst(expr) {
983
993
  return ts.factory.createAsExpression(expr, ts.factory.createTypeReferenceNode("const"));
984
994
  }
995
+ createAttributesTypeAssertion(expr) {
996
+ this.usedAttributeApplication = true;
997
+ return ts.factory.createAsExpression(expr, ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode("AttributeApplication"))));
998
+ }
999
+ createDefaultTypeAssertion(expr) {
1000
+ this.usedFieldDefault = true;
1001
+ return ts.factory.createAsExpression(expr, ts.factory.createTypeReferenceNode("FieldDefault"));
1002
+ }
985
1003
  createProviderObject(model) {
986
1004
  const dsProvider = this.getDataSourceProvider(model);
987
1005
  const defaultSchema = this.getDataSourceDefaultSchema(model);
@@ -1024,7 +1042,7 @@ var TsSchemaGenerator = class {
1024
1042
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm, lite))), true)),
1025
1043
  // attributes
1026
1044
  ...allAttributes.length > 0 ? [
1027
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
1045
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true)))
1028
1046
  ] : [],
1029
1047
  // idFields
1030
1048
  ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(getIdFields(dm).map((idField) => ts.factory.createStringLiteral(idField)))),
@@ -1061,7 +1079,7 @@ var TsSchemaGenerator = class {
1061
1079
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
1062
1080
  // attributes
1063
1081
  ...allAttributes.length > 0 ? [
1064
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
1082
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true)))
1065
1083
  ] : []
1066
1084
  ];
1067
1085
  return ts.factory.createObjectLiteralExpression(fields, true);
@@ -1131,39 +1149,39 @@ var TsSchemaGenerator = class {
1131
1149
  objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
1132
1150
  }
1133
1151
  if (!lite && field.attributes.length > 0) {
1134
- objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
1152
+ objectFields.push(ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr))))));
1135
1153
  }
1136
1154
  const defaultValue = this.getFieldMappedDefault(field);
1137
1155
  if (defaultValue !== void 0) {
1156
+ let defaultExpr;
1138
1157
  if (defaultValue === null) {
1139
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("_null")));
1158
+ defaultExpr = this.createExpressionUtilsCall("_null");
1140
1159
  } else if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
1141
1160
  if ("call" in defaultValue) {
1142
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1161
+ defaultExpr = this.createExpressionUtilsCall("call", [
1143
1162
  ts.factory.createStringLiteral(defaultValue.call),
1144
1163
  ...defaultValue.args.length > 0 ? [
1145
1164
  ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createExpressionUtilsCall("literal", [
1146
1165
  this.createLiteralNode(arg)
1147
1166
  ])))
1148
1167
  ] : []
1149
- ])));
1168
+ ]);
1150
1169
  } else if ("authMember" in defaultValue) {
1151
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1170
+ defaultExpr = this.createExpressionUtilsCall("member", [
1152
1171
  this.createExpressionUtilsCall("call", [
1153
1172
  ts.factory.createStringLiteral("auth")
1154
1173
  ]),
1155
1174
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
1156
- ])));
1175
+ ]);
1157
1176
  } else {
1158
1177
  throw new Error(`Unsupported default value type for field ${field.name}`);
1159
1178
  }
1179
+ } else if (Array.isArray(defaultValue)) {
1180
+ defaultExpr = ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)));
1160
1181
  } else {
1161
- if (Array.isArray(defaultValue)) {
1162
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)))));
1163
- } else {
1164
- objectFields.push(ts.factory.createPropertyAssignment("default", this.createLiteralNode(defaultValue)));
1165
- }
1182
+ defaultExpr = this.createLiteralNode(defaultValue);
1166
1183
  }
1184
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createDefaultTypeAssertion(defaultExpr)));
1167
1185
  }
1168
1186
  if (hasAttribute(field, "@computed")) {
1169
1187
  objectFields.push(ts.factory.createPropertyAssignment("computed", ts.factory.createTrue()));
@@ -1173,7 +1191,7 @@ var TsSchemaGenerator = class {
1173
1191
  }
1174
1192
  const fkFor = this.getForeignKeyFor(field);
1175
1193
  if (fkFor && fkFor.length > 0) {
1176
- objectFields.push(ts.factory.createPropertyAssignment("foreignKeyFor", ts.factory.createArrayLiteralExpression(fkFor.map((fk) => ts.factory.createStringLiteral(fk)), true)));
1194
+ objectFields.push(ts.factory.createPropertyAssignment("foreignKeyFor", ts.factory.createAsExpression(ts.factory.createArrayLiteralExpression(fkFor.map((fk) => ts.factory.createStringLiteral(fk)), true), ts.factory.createTypeOperatorNode(ts.SyntaxKind.ReadonlyKeyword, ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))))));
1177
1195
  }
1178
1196
  return ts.factory.createObjectLiteralExpression(objectFields, true);
1179
1197
  }
@@ -1415,12 +1433,12 @@ var TsSchemaGenerator = class {
1415
1433
  ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
1416
1434
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
1417
1435
  ...field.attributes.length > 0 ? [
1418
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true))
1436
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true)))
1419
1437
  ] : []
1420
1438
  ], true))), true))
1421
1439
  ] : [],
1422
1440
  ...e.attributes.length > 0 ? [
1423
- ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true))
1441
+ ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true)))
1424
1442
  ] : []
1425
1443
  ], true);
1426
1444
  }