kysely-org / kysely-ctl

Command-line tool for Kysely
MIT License
96 stars 4 forks source link

Importing file with alias path is not working #109

Open JeromeDeLeon opened 1 month ago

JeromeDeLeon commented 1 month ago

Description

Currently, the kysely.config.ts file is importing the kysely instance I created on a separate file and the path is an alias path like @/path/to/kysely.

This throws an error on the CLI.

Looking at c12, it looks like it's exposing jitiOptions which also exposes the alias to allow alias path.

ERROR  Cannot find module '@/path/to/kysely'
Require stack:
- /repo/kysely.config.ts

  Require stack:
  - kysely.config.ts
  at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
  at Function.resolve (node:internal/modules/helpers:190:19)
  at _resolve (node_modules/jiti/dist/jiti.js:1:241814)
  at jiti (node_modules/jiti/dist/jiti.js:1:244531)
  at kysely.config.ts:2:15
  at evalModule (node_modules/jiti/dist/jiti.js:1:247313)
  at Object.jiti (node_modules/jiti/dist/jiti.js:1:245241)
  at resolveConfig (node_modules/c12/dist/shared/c12.cab0c9da.mjs:345:26)
  at loadConfig (node_modules/c12/dist/shared/c12.cab0c9da.mjs:147:29)
  at async getConfig (node_modules/kysely-ctl/dist/bin.js:226:24)

Workaround

Manually configure using dialect and dialectConfig essentially repeating the same logic.

Package Info

kysely v0.27.4
kysely-ctl v0.9.0
JeromeDeLeon commented 1 month ago

Another workaround is to use jiti inside the config and move .env.local to .env for Next.js application

import { fileURLToPath } from 'url';
import createJiti from 'jiti';
import { defineConfig } from 'kysely-ctl';

const moduleFileUrl = import.meta.url;

const jiti = createJiti(fileURLToPath(moduleFileUrl), {
  // Since `jiti` uses isolated environment, we need to provide an alias for the src directory
  // See: https://github.com/unjs/jiti/issues/104
  alias: { '@': fileURLToPath(new URL('./src', moduleFileUrl)) },
});

const kysely = jiti('./src/path/to/kysely.ts');

export default defineConfig({
  kysely,
  // other kysely-ctl config
});
gvanderclay commented 1 month ago

I encountered the same issue. Luckily @JeromeDeLeon's workaround works!