Bertrand
Bertrand
DTDrizzle Team
Created by Bertrand on 11/1/2024 in #help
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!
5 replies