drizzle-team / drizzle-kit-mirror

Docs and issues repository for drizzle-kit
290 stars 17 forks source link

Typescript Path Alias support #113

Open HegarGarcia opened 1 year ago

HegarGarcia commented 1 year ago

Currently drizzle-kit doesn't support Typescript path aliases, this prevents the usage of the package on big codebases where most of the imports are through aliases.

Using absolute paths on the imports works without a problem, but when using any @/... path everything breaks. This also applies to the drizzle.config.ts.

image

Right now I'm working around this problema by denying all imports not coming from drizzle-orm with ESLint but that is not a good solution long term.

Leaving the snippet for anyone having issues with this.

const override = {
  files: ["src/db/schema.ts"],
  rules: {
    "@typescript-eslint/no-restricted-imports": [
      "error",
      {
        patterns: ["*", "!drizzle-orm", "!drizzle-orm/mysql-core"],
      },
    ],
  },
};
AkisArou commented 1 year ago

I have the same problem in an nx monorepo. The fast solution/workaround I've implemented is: npm i -D tsconfig-paths ts-node

npx ts-node --project ./tsconfig.base.json -r tsconfig-paths/register node_modules/drizzle-kit/index.cjs generate:pg --config ./packages//shared/data-access/database/src/lib/drizzle.config.ts

bestickley commented 1 year ago

If you're using TS 5.0+ and cannot use ts-node, tsx also works well too: tsx node_modules/drizzle-kit/index.cjs studio --config ./src/db/drizzle.config.ts

michaldziuba03 commented 1 year ago

I have same problem in my project with Nx monorepo.

iolyd commented 1 year ago

Met the same problem with a SvelteKit project and for some reason tsx wouldn't account for the path aliases inside the root tsconfigs' "extends": "./.svelte-kit/tsconfig.json". A quick fix for the moment is to reference Sveltekit's generated config directly:

"scripts": {
  "db:run": "tsx --tsconfig ./.svelte-kit/tsconfig.json node_modules/drizzle-kit/index.cjs",
  "db:gen": "pnpm db:run generate:pg",
  "db:studio": "pnpm db:run studio"
}

Note that for this to work in CI/CD flows, you have to build your sveltekit app first to generate the config.


Update: drizzle's team decided to move index.cjs to bin.cjs, so the above should now be:

 "db:run": "tsx --tsconfig ./.svelte-kit/tsconfig.json node_modules/drizzle-kit/bin.cjs",
sjunepark commented 11 months ago

Same issue with using sveltekit($env/static/private)