Is there a type for a Drizzle connection?

My use case is the following: I'm building a library dealing with a Neon database, but the connection shoud be created by the app using the library. So, all the functions of the lib expects as a their first parameter the actual connection to the database. How can I type this parameter so that the function accepts both a connection created with the Neon HTTP and Websockets drivers? Neon HTTP driver
// Setting up a connection with Neon HTTP driver
// See the Drizzle doc:
// - https://orm.drizzle.team/docs/connect-neon page
// - Section "Step 2 - Initialize the driver and make a query"
// - Tab "Neon HTTP"
import { drizzle } from 'drizzle-orm/neon-http' // <-- neon-http here
const db = drizzle(url)
// Setting up a connection with Neon HTTP driver
// See the Drizzle doc:
// - https://orm.drizzle.team/docs/connect-neon page
// - Section "Step 2 - Initialize the driver and make a query"
// - Tab "Neon HTTP"
import { drizzle } from 'drizzle-orm/neon-http' // <-- neon-http here
const db = drizzle(url)
Neon Websockets driver
// Setting up a connection with Neon HTTP driver
// See the Drizzle doc:
// - https://orm.drizzle.team/docs/connect-neon page
// - Section "Step 2 - Initialize the driver and make a query"
// - Tab "Neon Websockets"
import { drizzle } from 'drizzle-orm/neon-serverless' // <-- neon-serverless here
import ws from 'ws'
export const db = drizzle({ connection: url, ws: ws })
// Setting up a connection with Neon HTTP driver
// See the Drizzle doc:
// - https://orm.drizzle.team/docs/connect-neon page
// - Section "Step 2 - Initialize the driver and make a query"
// - Tab "Neon Websockets"
import { drizzle } from 'drizzle-orm/neon-serverless' // <-- neon-serverless here
import ws from 'ws'
export const db = drizzle({ connection: url, ws: ws })
I tried the following but it leads to a typescript error which I issued (https://github.com/drizzle-team/drizzle-orm/issues/3334) :
import { drizzle as http } from 'drizzle-orm/neon-http'
import { drizzle as ws } from 'drizzle-orm/neon-serverless'

export type DB = ReturnType<typeof http> | ReturnType<typeof ws>
import { drizzle as http } from 'drizzle-orm/neon-http'
import { drizzle as ws } from 'drizzle-orm/neon-serverless'

export type DB = ReturnType<typeof http> | ReturnType<typeof ws>
Many thanks in advance!
GitHub
[BUG]: Typescript error with returning({...}) function when combini...
What version of drizzle-orm are you using? 0.36.0 What version of drizzle-kit are you using? 0.27.0 Describe the Bug My use case is the following: I'm building a library dealing with a Neon dat...
2 Replies
Angelelz
Angelelz2mo ago
Due to the complexity of the types returned by drizzle, making an union like this is very complicated. I believe you can return a generic type to make it work, but I don't really remember the details of when I dealt with something like this I you put your code in drizzle.run and share it, I can help you make it work
Want results from more Discord servers?
Add your server