Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate using kysely config or non connection string #231

Open
noudadrichem opened this issue Jan 8, 2025 · 1 comment
Open

Generate using kysely config or non connection string #231

noudadrichem opened this issue Jan 8, 2025 · 1 comment

Comments

@noudadrichem
Copy link

Awesome package! Really happy with its functionality! I do have some optimisation requests.

I have a setup where we use kysely-ctl and a kysely-config like so:

import { defineConfig } from "kysely-ctl";
import { db } from "./src/lib/database/index";

export default defineConfig({
    kysely: db,
}

The db is setup using Postgres dialect that is not connecting via a connection string but via credentials.

export const db = new Kysely<DB>({
    log: ["error"],
    dialect: new PostgresDialect({
        pool: new pg.Pool({
            host: ENVIRONMENT.DB_HOST,
            port: ENVIRONMENT.DB_PORT,
            user: ENVIRONMENT.DB_USERNAME,
            password: ENVIRONMENT.DB_PASSWORD,
            database: ENVIRONMENT.DB_NAME,
        }),
    }),,
    plugins,
});

I now have to maintain both DB_... variables as a DATABASE_URL in my env file.

It would be very lovely if codegen works by using the Kysely config as well by introspecting the DB defined in that config

@ctrlplusb
Copy link

you could execute via code, building the url.

i run migrations and then codegen in one shot.

import { Config } from "@mono/core/config"
import { Cli } from "kysely-codegen"
import { migrate } from "../src/db/migrate"

console.log("Running database migrations...")
await migrate()
console.log("Migrations complete")

console.log("Generating database schema...")
const codegenCli = new Cli()
const schemaPath = "./src/db/schema.ts"
await codegenCli.generate({
  camelCase: true,
  outFile: schemaPath,
  dialectName: "mssql",
  excludePattern: "*.(spt|kysely|MSreplication)_*",
  url: `Server=${Config.db.host},${Config.db.port};Database=${Config.db.name};User Id=${Config.db.username};Password=${Config.db.password};TrustServerCertificate=true;`,
})
console.log(`Generated ${schemaPath}`)

console.log("Done.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants