fdarian / prisma-generator-drizzle

A Prisma generator for generating Drizzle schema with ease
90 stars 10 forks source link

Check tsconfig and add .ts to imports #18

Closed hilja closed 5 months ago

hilja commented 5 months ago

Screenshot 2024-01-12 at 17 16 01

Not a big problem, but if you’ve got something like this in tsonfig you need to import with .ts extension. Could also be a setting.

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2022",
    "allowImportingTsExtensions": true,
  }
}
hilja commented 5 months ago

Ah, this won't work https://github.com/drizzle-team/drizzle-orm/issues/849 I ran the generator yesterday evening and didn't try to migrate until this morning and noticed it can't find the tables.

hilja commented 5 months ago

This is the way:

"scripts": {
  "generate": "NODE_OPTIONS='--import tsx' drizzle-kit generate:pg"
}

A bit off-topic but, is the following supposed to work?

export default defineConfig({
  schema: './drizzle/schema/schema.ts',
})

It can't find any tables from the schema.ts, but glob works fine :)

export default defineConfig({
  schema: './drizzle/schema/*',
})
fdarian commented 5 months ago

@hilja do you mind making a repository where I can reproduce the issue?

So far I'm importing the generated models in a next.js project (using the tsconfig created from create-next-app) and a simple ts project works fine

hilja commented 5 months ago

Here's a quick repro:

The app I was testing with is loosely based on the epic stack https://github.com/epicweb-dev/epic-stack

So I hacked up prisma-generator-drizzle in there https://github.com/hilja/drizzle-test. Note that the app won't actually work because I used postgres (epic-stack is sqlite). But the generator should.

Setup:

git clone git@github.com:hilja/drizzle-test.git
cd drizzle-test
npm i
npm prisma generate

After generating the drizzle schema, the imports in prisma/drizzle/schema should error: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Adding .ts or .js manually fixes it.

And when trying to drizzle-kit generate:pg with npm run generate, it can’t find any tables:

0 tables

No schema changes, nothing to migrate 😴
daniel-nagy commented 5 months ago

I have the same issue. To make things easy I believe this library could just always add the .js extension to the import path. I believe that would work in all cases but I could be wrong.

daniel-nagy commented 5 months ago

I just opened a PR to add the js extension. @hilja as a temporary workaround you can use my fork by installing it from a git URL.

yarn add daniel-nagy/prisma-generator-drizzle#js-ext-pkg
fdarian commented 5 months ago

@daniel-nagy @hilja I just patched the import logic to determine based on the project's moduleResolution, let me know if it's not working

fdarian commented 5 months ago

@daniel-nagy @hilja I just patched the import logic to determine based on the project's moduleResolution, let me know if it's not working

It breaks on my repo 😅

I couldn't find any better approach, so I just provide the config to override