diff --git a/apps/api/package.json b/apps/api/package.json index 045cbdc6..09736fc5 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -43,6 +43,7 @@ "@fisch0920/drizzle-zod": "catalog:", "@hono/node-server": "catalog:", "@hono/zod-openapi": "catalog:", + "@neondatabase/serverless": "catalog:", "@paralleldrive/cuid2": "catalog:", "@sentry/node": "catalog:", "bcryptjs": "catalog:", diff --git a/apps/api/src/db/index.ts b/apps/api/src/db/index.ts index add9298c..fa808153 100644 --- a/apps/api/src/db/index.ts +++ b/apps/api/src/db/index.ts @@ -1,18 +1,33 @@ -import { drizzle } from '@fisch0920/drizzle-orm/postgres-js' +import { drizzle as neonPostgresDrizzle } from '@fisch0920/drizzle-orm/neon-http' +import { drizzle as postgresDrizzle } from '@fisch0920/drizzle-orm/postgres-js' +import { neon } from '@neondatabase/serverless' import postgres from 'postgres' import { env } from '@/lib/env' import * as schema from './schema' -type PostgresClient = ReturnType +type PostgresClient = ReturnType | ReturnType -// TODO: consider using [neon serverless driver](https://orm.drizzle.team/docs/connect-neon) for production +// TODO: consider using neon for both dev and prod; it would be simpler, but +// we'd lose the ability to connect to a 100% local postgres database which is +// nice for flights. let _postgresClient: PostgresClient | undefined const postgresClient = - _postgresClient ?? (_postgresClient = postgres(env.DATABASE_URL)) + _postgresClient ?? + (_postgresClient = env.isDev + ? postgres(env.DATABASE_URL) + : neon(env.DATABASE_URL)) -export const db = drizzle({ client: postgresClient, schema }) +export const db = env.isDev + ? postgresDrizzle({ + client: postgresClient as ReturnType, + schema + }) + : neonPostgresDrizzle({ + client: postgresClient as ReturnType, + schema + }) export * as schema from './schema' export { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9e891e79..58ab43f2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,6 +57,9 @@ catalogs: '@modelcontextprotocol/sdk': specifier: ^1.13.0 version: 1.13.0 + '@neondatabase/serverless': + specifier: ^1.0.1 + version: 1.0.1 '@number-flow/react': specifier: ^0.5.10 version: 0.5.10 @@ -506,6 +509,9 @@ importers: '@hono/zod-openapi': specifier: 'catalog:' version: 0.19.8(hono@4.8.1)(zod@3.25.67) + '@neondatabase/serverless': + specifier: 'catalog:' + version: 1.0.1 '@paralleldrive/cuid2': specifier: 'catalog:' version: 2.2.2 @@ -8852,7 +8858,6 @@ snapshots: dependencies: '@types/node': 22.15.33 '@types/pg': 8.15.4 - optional: true '@next/env@15.3.4': {} @@ -10296,7 +10301,6 @@ snapshots: '@types/node@22.15.33': dependencies: undici-types: 6.21.0 - optional: true '@types/node@24.0.4': dependencies: @@ -14581,8 +14585,7 @@ snapshots: jiti: 2.4.2 quansync: 0.2.10 - undici-types@6.21.0: - optional: true + undici-types@6.21.0: {} undici-types@7.8.0: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 4e8ac828..12e49657 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -26,6 +26,7 @@ catalog: "@hono/zod-openapi": ^0.19.8 "@hono/zod-validator": ^0.7.0 "@modelcontextprotocol/sdk": ^1.13.0 + "@neondatabase/serverless": ^1.0.1 "@number-flow/react": ^0.5.10 "@paralleldrive/cuid2": ^2.2.2 "@pmndrs/assets": ^1.7.0