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

Feature Request: External typescript types #83

Closed
fauh45 opened this issue Oct 19, 2023 · 5 comments
Closed

Feature Request: External typescript types #83

fauh45 opened this issue Oct 19, 2023 · 5 comments

Comments

@fauh45
Copy link

fauh45 commented Oct 19, 2023

While it is already possible to set typescript type declaration for columns in prisma schema using /// @kyselyType( ... ). I don't think there's a way to set the column type with typescript definition outside of the schema. This will be so useful for JSON column types, as sometimes it does contains a complex metadata structure, that's usually opaque to the programmer.

I suppose it'll be great to have something like,

types.ts

export type Metadata = {
    tracking_id: string,
    userLocation: {
        ...
    },
    ...
}

prisma.schema

model User {
    id String @id
    
    // Import would be based on the root of the project, or maybe the output of the file. This part might be the most complex part, resolving the module
    /// @kyselyType( import('./types.ts').Metadata )
   module Json
}
@valtyr
Copy link
Owner

valtyr commented Nov 1, 2023

Hey there @fauh45, this is a valid point. There's a solution on the roadmap which is tracked in #61. The idea is to allow users to include a "prelude", basically a string prepended to the generated file. This prelude could include import statements etc.

The funny thing is though is that we already support the exact syntax you suggested by total accident! If you make the import relative to the directory the generated file ends up in, it works great. This was absolutely unintentional which is hilarious, but actually a pretty powerful feature.

@valtyr
Copy link
Owner

valtyr commented Nov 1, 2023

I might just add this to the README to be fair 😆

@valtyr
Copy link
Owner

valtyr commented Nov 1, 2023

The schema:

datasource db {
    provider = "mysql"
    url      = "mysql://root:mysql@localhost:22332/test"
}

generator kysely {
    provider = "node ./dist/bin.js"
}

model User {
    id String @id

    /// @kyselyType( import('./jsonTypes.ts').Metadata )
    module Json
}

Gives the following result:

CleanShot 2023-11-01 at 10 50 03@2x

With the file jsonTypes.ts located in the output directory.

@fauh45
Copy link
Author

fauh45 commented Nov 1, 2023

Ups I didn't see #61 sorry!

And somehow the syntax I wrote just works with kysely-prisma? I should've tried generating what I made first lol. Well this revelation fixes my problem then, I could just import my types relative to output folder, which should be easy.

Also, you should absolutely put this to README haha, or if you want i can shoot a PR as well.

@valtyr
Copy link
Owner

valtyr commented May 8, 2024

Hey @fauh45 if you still want to create a PR that adds this to the README that would be great :)
Otherwise, I'm closing this issue for now.

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