@zenstackhq/sdk 3.8.3 → 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.d.cts CHANGED
@@ -108,6 +108,7 @@ declare class PrismaSchemaGenerator {
108
108
  private datasourceHasSchemasSetting;
109
109
  private getDefaultPostgresSchemaName;
110
110
  private isPrismaAttribute;
111
+ private isNativeTypeMappingAttribute;
111
112
  private getUnsupportedFieldType;
112
113
  private generateModelField;
113
114
  private isDefaultWithAuthInvocation;
@@ -156,6 +157,8 @@ declare class TsSchemaGenerator {
156
157
  private getSubModels;
157
158
  private createTypeDefObject;
158
159
  private createComputedFieldsObject;
160
+ private createFieldParamsObject;
161
+ private mapFunctionParamTypeToTSType;
159
162
  private createUpdatedAtObject;
160
163
  private mapFieldTypeToTSType;
161
164
  private createDataFieldObject;
package/dist/index.d.mts CHANGED
@@ -108,6 +108,7 @@ declare class PrismaSchemaGenerator {
108
108
  private datasourceHasSchemasSetting;
109
109
  private getDefaultPostgresSchemaName;
110
110
  private isPrismaAttribute;
111
+ private isNativeTypeMappingAttribute;
111
112
  private getUnsupportedFieldType;
112
113
  private generateModelField;
113
114
  private isDefaultWithAuthInvocation;
@@ -156,6 +157,8 @@ declare class TsSchemaGenerator {
156
157
  private getSubModels;
157
158
  private createTypeDefObject;
158
159
  private createComputedFieldsObject;
160
+ private createFieldParamsObject;
161
+ private mapFunctionParamTypeToTSType;
159
162
  private createUpdatedAtObject;
160
163
  private mapFieldTypeToTSType;
161
164
  private createDataFieldObject;
package/dist/index.mjs CHANGED
@@ -493,6 +493,9 @@ var PrismaSchemaGenerator = class {
493
493
  if (!attr.decl.ref) return false;
494
494
  return attr.decl.ref.attributes.some((a) => a.decl.ref?.name === "@@@prisma");
495
495
  }
496
+ isNativeTypeMappingAttribute(attr) {
497
+ return attr.decl.$refText.startsWith("@db.") && this.isPrismaAttribute(attr);
498
+ }
496
499
  getUnsupportedFieldType(fieldType) {
497
500
  if (fieldType.unsupported) {
498
501
  const value = getStringLiteral(fieldType.unsupported.value);
@@ -523,7 +526,11 @@ var PrismaSchemaGenerator = class {
523
526
  return AstUtils.streamAst(expr).some(isAuthInvocation);
524
527
  }
525
528
  makeFieldAttribute(attr) {
526
- const attrName = attr.decl.ref.name;
529
+ let attrName = attr.decl.ref.name;
530
+ if (this.isNativeTypeMappingAttribute(attr)) {
531
+ const dataSource = this.zmodel.declarations.find(isDataSource);
532
+ if (dataSource) attrName = attrName.replace("@db", `@${dataSource.name}`);
533
+ }
527
534
  return new FieldAttribute(attrName, attr.args.map((arg) => this.makeAttributeArg(arg)));
528
535
  }
529
536
  makeAttributeArg(arg) {
@@ -751,7 +758,24 @@ var TsSchemaGenerator = class {
751
758
  return ts.factory.createObjectLiteralExpression(fields, true);
752
759
  }
753
760
  createComputedFieldsObject(fields) {
754
- return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [ts.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, ts.factory.createTypeLiteralNode([ts.factory.createPropertySignature(void 0, "modelAlias", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))]), void 0)], ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type)), ts.factory.createBlock([ts.factory.createThrowStatement(ts.factory.createNewExpression(ts.factory.createIdentifier("Error"), void 0, [ts.factory.createStringLiteral("This is a stub for computed field")]))], true))), true);
761
+ return ts.factory.createObjectLiteralExpression(fields.map((field) => {
762
+ const params = [ts.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, ts.factory.createTypeLiteralNode([ts.factory.createPropertySignature(void 0, "modelAlias", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))]), void 0)];
763
+ if (field.params.length > 0) params.push(ts.factory.createParameterDeclaration(void 0, void 0, "args", void 0, ts.factory.createTypeLiteralNode(field.params.map((param) => ts.factory.createPropertySignature(void 0, param.name, param.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : void 0, ts.factory.createTypeReferenceNode(this.mapFunctionParamTypeToTSType(param.type))))), void 0));
764
+ return ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, params, ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type)), ts.factory.createBlock([ts.factory.createThrowStatement(ts.factory.createNewExpression(ts.factory.createIdentifier("Error"), void 0, [ts.factory.createStringLiteral("This is a stub for computed field")]))], true));
765
+ }), true);
766
+ }
767
+ createFieldParamsObject(params) {
768
+ return ts.factory.createObjectLiteralExpression(params.map((param) => ts.factory.createPropertyAssignment(param.name, ts.factory.createObjectLiteralExpression([
769
+ ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(param.name)),
770
+ ...param.optional ? [ts.factory.createPropertyAssignment("optional", ts.factory.createTrue())] : [],
771
+ ...param.type.array ? [ts.factory.createPropertyAssignment("array", ts.factory.createTrue())] : [],
772
+ ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(param.type.type ?? param.type.reference.$refText))
773
+ ]))), true);
774
+ }
775
+ mapFunctionParamTypeToTSType(type) {
776
+ let result = 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");
777
+ if (type.array) result = `${result}[]`;
778
+ return result;
755
779
  }
756
780
  createUpdatedAtObject(ignoreArg) {
757
781
  return ts.factory.createObjectLiteralExpression([ts.factory.createPropertyAssignment("ignore", ts.factory.createArrayLiteralExpression(ignoreArg.value.items.map((item) => ts.factory.createStringLiteral(item.target.$refText))))]);
@@ -794,6 +818,7 @@ var TsSchemaGenerator = class {
794
818
  objectFields.push(ts.factory.createPropertyAssignment("default", this.createDefaultTypeAssertion(defaultExpr)));
795
819
  }
796
820
  if (hasAttribute(field, "@computed")) objectFields.push(ts.factory.createPropertyAssignment("computed", ts.factory.createTrue()));
821
+ if (field.params.length > 0) objectFields.push(ts.factory.createPropertyAssignment("params", this.createFieldParamsObject(field.params)));
797
822
  if (isDataModel(field.type.reference?.ref)) objectFields.push(ts.factory.createPropertyAssignment("relation", this.createRelationObject(field, contextModel)));
798
823
  const fkFor = this.getForeignKeyFor(field, contextModel);
799
824
  if (fkFor && fkFor.length > 0) 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))))));
@@ -906,9 +931,9 @@ var TsSchemaGenerator = class {
906
931
  const relationName = this.getRelationName(field);
907
932
  for (const otherField of getAllFields(targetModel)) {
908
933
  if (otherField === field) continue;
909
- if (otherField.type.reference?.ref === sourceModel) if (relationName) {
934
+ if (otherField.type.reference?.ref === sourceModel) {
910
935
  if (this.getRelationName(otherField) === relationName) return otherField;
911
- } else return otherField;
936
+ }
912
937
  }
913
938
  }
914
939
  getRelationName(field) {