@zenstackhq/sdk 3.9.0-beta.1 → 3.9.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/index.cjs CHANGED
@@ -527,6 +527,9 @@ var PrismaSchemaGenerator = class {
527
527
  if (!attr.decl.ref) return false;
528
528
  return attr.decl.ref.attributes.some((a) => a.decl.ref?.name === "@@@prisma");
529
529
  }
530
+ isNativeTypeMappingAttribute(attr) {
531
+ return attr.decl.$refText.startsWith("@db.") && this.isPrismaAttribute(attr);
532
+ }
530
533
  getUnsupportedFieldType(fieldType) {
531
534
  if (fieldType.unsupported) {
532
535
  const value = (0, _zenstackhq_language_utils.getStringLiteral)(fieldType.unsupported.value);
@@ -557,7 +560,11 @@ var PrismaSchemaGenerator = class {
557
560
  return langium.AstUtils.streamAst(expr).some(_zenstackhq_language_utils.isAuthInvocation);
558
561
  }
559
562
  makeFieldAttribute(attr) {
560
- const attrName = attr.decl.ref.name;
563
+ let attrName = attr.decl.ref.name;
564
+ if (this.isNativeTypeMappingAttribute(attr)) {
565
+ const dataSource = this.zmodel.declarations.find(_zenstackhq_language_ast.isDataSource);
566
+ if (dataSource) attrName = attrName.replace("@db", `@${dataSource.name}`);
567
+ }
561
568
  return new FieldAttribute(attrName, attr.args.map((arg) => this.makeAttributeArg(arg)));
562
569
  }
563
570
  makeAttributeArg(arg) {
@@ -785,7 +792,24 @@ var TsSchemaGenerator = class {
785
792
  return typescript.factory.createObjectLiteralExpression(fields, true);
786
793
  }
787
794
  createComputedFieldsObject(fields) {
788
- return typescript.factory.createObjectLiteralExpression(fields.map((field) => typescript.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [typescript.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, typescript.factory.createTypeLiteralNode([typescript.factory.createPropertySignature(void 0, "modelAlias", void 0, typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword))]), void 0)], typescript.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type)), typescript.factory.createBlock([typescript.factory.createThrowStatement(typescript.factory.createNewExpression(typescript.factory.createIdentifier("Error"), void 0, [typescript.factory.createStringLiteral("This is a stub for computed field")]))], true))), true);
795
+ return typescript.factory.createObjectLiteralExpression(fields.map((field) => {
796
+ const params = [typescript.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, typescript.factory.createTypeLiteralNode([typescript.factory.createPropertySignature(void 0, "modelAlias", void 0, typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword))]), void 0)];
797
+ if (field.params.length > 0) params.push(typescript.factory.createParameterDeclaration(void 0, void 0, "args", void 0, typescript.factory.createTypeLiteralNode(field.params.map((param) => typescript.factory.createPropertySignature(void 0, param.name, param.optional ? typescript.factory.createToken(typescript.SyntaxKind.QuestionToken) : void 0, typescript.factory.createTypeReferenceNode(this.mapFunctionParamTypeToTSType(param.type))))), void 0));
798
+ return typescript.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, params, typescript.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type)), typescript.factory.createBlock([typescript.factory.createThrowStatement(typescript.factory.createNewExpression(typescript.factory.createIdentifier("Error"), void 0, [typescript.factory.createStringLiteral("This is a stub for computed field")]))], true));
799
+ }), true);
800
+ }
801
+ createFieldParamsObject(params) {
802
+ return typescript.factory.createObjectLiteralExpression(params.map((param) => typescript.factory.createPropertyAssignment(param.name, typescript.factory.createObjectLiteralExpression([
803
+ typescript.factory.createPropertyAssignment("name", typescript.factory.createStringLiteral(param.name)),
804
+ ...param.optional ? [typescript.factory.createPropertyAssignment("optional", typescript.factory.createTrue())] : [],
805
+ ...param.type.array ? [typescript.factory.createPropertyAssignment("array", typescript.factory.createTrue())] : [],
806
+ typescript.factory.createPropertyAssignment("type", typescript.factory.createStringLiteral(param.type.type ?? param.type.reference.$refText))
807
+ ]))), true);
808
+ }
809
+ mapFunctionParamTypeToTSType(type) {
810
+ let result = (0, ts_pattern.match)(type.type).with("String", () => "string").with("Boolean", () => "boolean").with("Int", () => "number").with("Float", () => "number").with("BigInt", () => "bigint").with("Decimal", () => "number").with("DateTime", () => "Date").otherwise(() => "unknown");
811
+ if (type.array) result = `${result}[]`;
812
+ return result;
789
813
  }
790
814
  createUpdatedAtObject(ignoreArg) {
791
815
  return typescript.factory.createObjectLiteralExpression([typescript.factory.createPropertyAssignment("ignore", typescript.factory.createArrayLiteralExpression(ignoreArg.value.items.map((item) => typescript.factory.createStringLiteral(item.target.$refText))))]);
@@ -828,6 +852,7 @@ var TsSchemaGenerator = class {
828
852
  objectFields.push(typescript.factory.createPropertyAssignment("default", this.createDefaultTypeAssertion(defaultExpr)));
829
853
  }
830
854
  if (hasAttribute(field, "@computed")) objectFields.push(typescript.factory.createPropertyAssignment("computed", typescript.factory.createTrue()));
855
+ if (field.params.length > 0) objectFields.push(typescript.factory.createPropertyAssignment("params", this.createFieldParamsObject(field.params)));
831
856
  if ((0, _zenstackhq_language_ast.isDataModel)(field.type.reference?.ref)) objectFields.push(typescript.factory.createPropertyAssignment("relation", this.createRelationObject(field, contextModel)));
832
857
  const fkFor = this.getForeignKeyFor(field, contextModel);
833
858
  if (fkFor && fkFor.length > 0) objectFields.push(typescript.factory.createPropertyAssignment("foreignKeyFor", typescript.factory.createAsExpression(typescript.factory.createArrayLiteralExpression(fkFor.map((fk) => typescript.factory.createStringLiteral(fk)), true), typescript.factory.createTypeOperatorNode(typescript.SyntaxKind.ReadonlyKeyword, typescript.factory.createArrayTypeNode(typescript.factory.createKeywordTypeNode(typescript.SyntaxKind.StringKeyword))))));
@@ -940,9 +965,9 @@ var TsSchemaGenerator = class {
940
965
  const relationName = this.getRelationName(field);
941
966
  for (const otherField of (0, _zenstackhq_language_utils.getAllFields)(targetModel)) {
942
967
  if (otherField === field) continue;
943
- if (otherField.type.reference?.ref === sourceModel) if (relationName) {
968
+ if (otherField.type.reference?.ref === sourceModel) {
944
969
  if (this.getRelationName(otherField) === relationName) return otherField;
945
- } else return otherField;
970
+ }
946
971
  }
947
972
  }
948
973
  getRelationName(field) {