25 lines
646 B
TypeScript
25 lines
646 B
TypeScript
import { drizzle } from 'drizzle-orm/node-postgres'
|
|
import { Pool } from 'pg'
|
|
import * as schema from '@/db/schema.js'
|
|
|
|
let pgPoolInstance: Pool | null = null
|
|
let drizzleInstance: ReturnType<typeof drizzle> | null = null
|
|
|
|
export const useDrizzle = () => {
|
|
if (!pgPoolInstance || !drizzleInstance) {
|
|
pgPoolInstance = new Pool({
|
|
connectionString: process.env.DATABASE_CONNECTION_STRING,
|
|
})
|
|
drizzleInstance = drizzle({ client: pgPoolInstance, schema })
|
|
}
|
|
return drizzleInstance
|
|
}
|
|
|
|
export const closeDrizzle = () => {
|
|
if (pgPoolInstance) {
|
|
pgPoolInstance.end()
|
|
pgPoolInstance = null
|
|
drizzleInstance = null
|
|
}
|
|
}
|