RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
770 stars 69 forks source link

Connecting to a database without HTTPS #1

Closed anthonyalayo closed 2 years ago

anthonyalayo commented 2 years ago

Hey there,

Just got here from reddit, thanks a lot for this! I tried using kysely-codegen with a localhost postgres, but I'm seeing this error:

❯ kysely-codegen                                                                              at 08:56:58 PM

Loaded environment variables from .env file.
Error: The server does not support SSL connections

After updating my DATABASE_URL to end with localhost:5432/postgres?sslmode=disable" as an attempt to workaround it, I saw a success message:

❯ kysely-codegen                                                                              at 09:01:58 PM
Loaded environment variables from .env file.
Introspected 4 models and generated ./node_modules/kysely-codegen/dist/index.d.ts in 165ms.

But the resulting object is empty:

export type DB = {};

For reference, my same .env file does work with prisma and the schema.prisma file has been generated.

RobinBlomberg commented 2 years ago

Hello there!

Thanks for the issue.

Honestly, I'm not well-versed enough in SSL to know what the best thing to do here is. For my local Heroku setup, it only works with ssl: { rejectUnauthorized: false } and no sslmode=disable, but I want it to work for everyone of course.

In any case, it seems like you successfully did find a workaround, since it apparently did introspect 4 models. The fact that DB is still empty indicates that the file was either written to the wrong place, or not written at all, which I could not replicate on my machine with pnpm or npm.

I just released kysely-codegen@0.1.2 which adds some debugging support:

Aside from that, try specifying a custom --out-file. Maybe something goes wrong with the path to node_modules or something.

RobinBlomberg commented 2 years ago

I think I've fixed the SSL bug in kysely-codegen@0.2.0. Not the best solution, but it now works for me with:

I can't replicate the file generation bug though, but maybe we can work it out together.

anthonyalayo commented 2 years ago

I've tried to pull the latest to give it another shot, but now kysely-codegen seems to hang here?

❯ npm install kysely-codegen                                                          took 9s at 11:40:56 AM
(##################) ⠴ reify:better-sqlite3: timing reifyNode:node_modules/kysely-codegen Completed in 690ms
RobinBlomberg commented 2 years ago

Weird - it worked for me just now. Seems like it's failing to install better-sqlite3. Could you try removing node_modules and try again?

Also, even better, I just now published kysely-codegen@0.2.3 that doesn't install the drivers automatically, so it won't try to install better-sqlite3 if you're not using it.

anthonyalayo commented 2 years ago

Everything worked! Thanks!

Also I needed to change what it said on the readme:

kysely-codegen --dialect=pg

to this:

❯ kysely-codegen --dialect=postgres 
RobinBlomberg commented 2 years ago

Oh right! Good call!

Perfect - happy it works! 🥳

RobinBlomberg commented 2 years ago

Heads up! Just thought of this, but there might be some problems with generated columns (i.e. { id: Generated<number> }) when inserting new rows.

I made an issue, but it may require some changes in the Kysely introspector: https://github.com/RobinBlomberg/kysely-codegen/issues/2