kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

Pass client instance #870

Closed francescovenica closed 7 months ago

francescovenica commented 7 months ago

Hello,

I just found out about this amazing tool, I wanted to double check if it is possible somehow to pass a client to the query builder, so instead of having something like:

const dialect = new PostgresDialect({
  pool: new Pool({
    database: 'test',
    host: 'localhost',
    user: 'admin',
    port: 5434,
    max: 10,
  })
})

// Database interface is passed to Kysely's constructor, and from now on, Kysely 
// knows your database structure.
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how 
// to communicate with your database.
export const db = new Kysely<Database>({
  dialect,
})

having something like:

export const db = new Kysely<Database>({
  client,
})

or even something like:

export const db = new Kysely<Database>()
db.connection(client).whatever()

similar to what Knex provide, many thanks

igalklebanov commented 7 months ago

Hey 👋

The core PostgreSQL dialect only accepts a pool instance. You can use https://github.com/jtlapp/kysely-pg-client instead, or implement your own dialect, while reusing the existing core PostgreSQL functionality, except for the driver.

francescovenica commented 7 months ago

great, many thanks! :)

francescovenica commented 7 months ago

actually I don't think that library works, typescript is complaining a bit, I'll try to fork it