RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
794 stars 72 forks source link

CLI throwing error for any command #9

Closed Woody88 closed 2 years ago

Woody88 commented 2 years ago

I am trying to test kysely-codegen with SQLite but the CLI keeps throwing Cannot find module 'pg' for any commands that I pass it.

Click to expand! ```bash > pnpm kysely-codegen --dialect sqlite node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'pg' Require stack: - /home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/dialects/postgres.js - /home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/dialect-manager.js - /home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/cli.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (/home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/dialects/postgres.js:5:14) at Module._compile (node:internal/modules/cjs/loader:1103:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/dialects/postgres.js', '/home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/dialect-manager.js', '/home/woody88/Code/Typescript/meetapp/node_modules/.pnpm/kysely-codegen@0.3.0_eb47a1c54283209bc69ae17fc7e73157/node_modules/kysely-codegen/dist/cli.js' ] } ```
DavesBorges commented 2 years ago

Perhaps is something to do with your .env I suppose

Woody88 commented 2 years ago

I got the chance to have a look at the code today. The problem is in the way the DialectManager constructs each respective dialect.

https://github.com/RobinBlomberg/kysely-codegen/blob/5e40c284c4a3fad154c382fb1128c0cd45b3c58e/src/dialect-manager.ts#L1-L16

https://github.com/RobinBlomberg/kysely-codegen/blob/5e40c284c4a3fad154c382fb1128c0cd45b3c58e/src/dialects/postgres.ts#L2

https://github.com/RobinBlomberg/kysely-codegen/blob/5e40c284c4a3fad154c382fb1128c0cd45b3c58e/src/dialects/sqlite.ts#L1

They should be dynamically imported. I fixed it by ensuring that the instantiate returns a Promise so that I may import it dynamically.

https://github.com/RobinBlomberg/kysely-codegen/blob/5e40c284c4a3fad154c382fb1128c0cd45b3c58e/src/dialect.ts#L103-L106

In addition, I am not sure if it's worth parsing the connection string. I think that it can be passed down to the appropriate dialect and let it handle it.

Lastly, The URL object will break for SQLite because it needs the file: scheme or will throw an exception. https://github.com/RobinBlomberg/kysely-codegen/blob/5e40c284c4a3fad154c382fb1128c0cd45b3c58e/src/connection-string-parser.ts#L64

RobinBlomberg commented 2 years ago

I'm implementing dynamic imports in #12, which I hope should solve this. It might not solve everything (I'm still not sure how I want to handle connection strings), but it's a start!