Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DtoOverrideType values with a custom import in unique index - import not included on inferred types #68

Open
zackdotcomputer opened this issue Mar 19, 2025 · 2 comments

Comments

@zackdotcomputer
Copy link

I have a type where one of the fields uses the @DtoOverrideType syntax to override the type of a field and add an import for that type (since it's an in-code enum rather than an in-db one).

This works fine in the primary DTO. However, this field is part of a unique index. When the generate outputs the <type><fields>UniqueInputDtoAttributes type, it remembers the type override and applies it correctly but loses the context of the custom import. That causes a compilation error.

@Brakebein
Copy link
Owner

You mean within the ConnectDto? It's possible. These override annotation were initially thought for Json types wíthout having unique fields in mind.

If I missunderstood the issue, please provide a minimum example.

@zackdotcomputer
Copy link
Author

Yes, it turns out this is related to unique indices that use DtoOverrideType. In my case, I have an enum value in the code called Instance that indicates what org a user belongs to. It is exported from the package @app/instances.

The relevant excerpt of my schema looks like:

model User {
  id  String  @id
  email  String  @db.Citext

  /// @DtoCastType(Instance, @app/instances)
  instance  String

  @@unique([email, instance], name: "user_email_instance_unique")
}

When we run generate, we get a correct DTO file:

import {WhitelabelInstance} from '@app/instances'

export interface UserDtoAttributes {
  id: string ;
  email: string ;
  instance: WhitelabelInstance ;
}

However, the connect-user.dto.ts file does not compile:

interface UserUserEmailInstanceUniqueUniqueInputDtoAttributes {
    email: string ;
instance: WhitelabelInstance ;
  }


export interface ConnectUserDtoAttributes {
  id?: string ;
user_email_instance_unique?: UserUserEmailInstanceUniqueUniqueInputDtoAttributes ;
}

The behavior previously (in 1.18.1, which I am upgrading from) was that the connect DTO would generate with the underlying, rather than overridden, type for instance:

interface UserUserEmailInstanceUniqueUniqueInputDtoAttributes {
    email: string ;
instance: string ;
  }


export interface ConnectUserDtoAttributes {
  id?: string ;
user_email_instance_unique?: UserUserEmailInstanceUniqueUniqueInputDtoAttributes ;
}

While clearly not ideal, this does at least compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants