You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
import { drizzle } from 'drizzle-orm/postgres-js'
|
|
import postgres from 'postgres'
|
|
import * as schema from '../database/schema'
|
|
|
|
/**
|
|
* Database connection utility
|
|
*
|
|
* Creates a singleton Drizzle ORM instance with the complete schema.
|
|
* This allows for type-safe database queries throughout the application.
|
|
*
|
|
* Usage:
|
|
* ```typescript
|
|
* import { db } from '~/server/utils/db'
|
|
*
|
|
* // Query with relations
|
|
* const users = await db.query.users.findMany({
|
|
* with: { orders: true }
|
|
* })
|
|
*
|
|
* // Raw SQL with type safety
|
|
* import { users } from '~/server/database/schema'
|
|
* import { eq } from 'drizzle-orm'
|
|
*
|
|
* const user = await db.select().from(users).where(eq(users.id, userId))
|
|
* ```
|
|
*/
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
// Create postgres.js client
|
|
const client = postgres(config.databaseUrl)
|
|
|
|
// Create Drizzle ORM instance with schema
|
|
export const db = drizzle(client, { schema })
|
|
|
|
/**
|
|
* Helper function to get database instance
|
|
* Used in event handlers for consistency with Nuxt patterns
|
|
*
|
|
* @returns Drizzle database instance
|
|
*/
|
|
export function useDatabase() {
|
|
return db
|
|
}
|
|
|