- Created src/lib/auth.ts with NextAuth JWT strategy, 7-day rolling session, Credentials provider - Created src/app/api/auth/[...nextauth]/route.ts with GET/POST handlers and force-dynamic - Created middleware.ts at project root (not src/) protecting /agent/* routes - Fixed db/index.ts: lazy Proxy singleton prevents neon() crash during Next.js build - npm run build passes; /api/auth/[...nextauth] renders as Dynamic route
25 lines
702 B
TypeScript
25 lines
702 B
TypeScript
import { drizzle } from "drizzle-orm/neon-http";
|
|
import { neon } from "@neondatabase/serverless";
|
|
import * as schema from "./schema";
|
|
|
|
type DrizzleDb = ReturnType<typeof createDb>;
|
|
|
|
function createDb() {
|
|
const url = process.env.DATABASE_URL;
|
|
if (!url) {
|
|
throw new Error("DATABASE_URL environment variable is not set");
|
|
}
|
|
const sql = neon(url);
|
|
return drizzle({ client: sql, schema });
|
|
}
|
|
|
|
// Lazy singleton — created on first use, not at module load time
|
|
let _db: DrizzleDb | undefined;
|
|
|
|
export const db = new Proxy({} as DrizzleDb, {
|
|
get(_target, prop: string | symbol) {
|
|
if (!_db) _db = createDb();
|
|
return (_db as unknown as Record<string | symbol, unknown>)[prop];
|
|
},
|
|
});
|