RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Monorepo npx failure and empty types

botond-veress opened this issue · comments

Thank you for working on kysely. I really love working with it.

I found a couple of issues with the codegen cli though: I have a monorepo and I wanted to run kysely-codegen in one of the packages, but it doesn't seem to find the dependencies:

$ DATABASE_URL="..." npx kysely-codegen --dialect mysql --log-level debug 
• Loaded environment variables from .env file.
• Using dialect 'mysql'.
node:internal/modules/cjs/loader:1077
  const err = new Error(message);
              ^

Error: Cannot find module 'mysql2'

Please note I already added mysql2 as a dependency to that project.

$ cat package.json | jq '.dependencies.mysql2'
"^3.6.3"

Also, it looks like the dependency is in the root node_modules directory, which is most likely the issue.

Then I decided I would add the cli as a dev dependency to overcome the issue:

$ yarn add kysely-codegen --dev

The installation was successful, but running the CLI generated an empty db.t.ts file a couple of times. This is probably a duplicate of #91 .

$ DATABASE_URL="..." yarn kysely-codegen --dialect mysql --log-level debug --verify
• Loaded environment variables from .env file.
• Using dialect 'mysql'.
• Introspecting database...
  Found 55 public tables:
   - table1
   ...
   - table55
✓ Introspected 55 tables and generated ./node_modules/kysely-codegen/dist/db.d.ts in 502ms.
✨  Done in 1.19s.

It correctly listed all the tables from the database though. I removed and reinstalled the package, and that solved the issue.

P.S. I use node@18, npx@9.8.1 and yarn@1.22.19 workspaces.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

After further investigation and console.logs, it looks like it creates 2 db.d.ts files. One in the project's node_modules and one in the root. The file in the root node_modules is empty, however, that one is being used in the project is generated correctly:

$ stat -f %z ./node_modules/kysely-codegen/dist/db.d.ts
14128
$ stat -f %z ../../node_modules/kysely-codegen/dist/db.d.ts
21

So the only way this works, if the cli is being run in the project root directory.