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

Type for an enum array is incorrectly generated #107

Open
alii opened this issue Sep 28, 2024 · 0 comments · May be fixed by #108
Open

Type for an enum array is incorrectly generated #107

alii opened this issue Sep 28, 2024 · 0 comments · May be fixed by #108
Labels
bug Something isn't working

Comments

@alii
Copy link
Collaborator

alii commented Sep 28, 2024

With postgres as a datasource, prisma will let you use an enum array as a value. Example:

enum UserPermissions {
  FOO
  BAR
}

model User {
  id String @id
  permissions UserPermissions[]
}

Unfortunately prisma-kysely generates the array type that the prisma client would return, but in reality the migration/shape prisma would use for this is actually a string, and it's stored as an arbitrary string format: {FOO,BAR}. An empty array would be stored as the string {}

Of course I wouldn't expect kysely to be selecting this string value as an array, so I think it would make the most sense for prisma-kysely to either say "we don't support enum array types" or (ideally) have it generate this array to a string type.

Here's an example of what we could generate:

export type EnumArrayInner<T extends string, All extends string> = T extends infer Single extends string ? T | `${Single},${Exclude<All, Single>}` : never
export type EnumArray<T extends string> = `{${EnumArrayInner<T, T>}}`;

export const UserPermissions = {
    FOO: "FOO",
    BAR: "BAR"
} as const;
export type UserPermissions = (typeof UserPermissions)[keyof typeof UserPermissions];

export type User = {
    id: string;
    permissions: EnumArray<UserPermissions>;
}

CleanShot 2024-09-28 at 14 34 43

alii added a commit to alii/prisma-kysely that referenced this issue Sep 28, 2024
@alii alii linked a pull request Sep 28, 2024 that will close this issue
@valtyr valtyr added the bug Something isn't working label Oct 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants