Next.js Discord

Discord Forum

DB Connection at Build time

Unanswered
Théo Pomies posted this in #help-forum
Open in Discord
Hi! I'm trying to make a reusable template for myself to self-host projects using Next, Docker, Drizzle, Postgres and NextAuth

When docker compose tries to build it hits parts of the codebase that require a DB connection, for example in db/index.ts, following the drizzle docs, I have the following:
import * as schema from "./schema";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
import dotenv from "dotenv";

dotenv.config();

const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
  throw new Error("DATABASE_URL is not set (src/db/index.ts)");
}

const sql = postgres(connectionString, { max: 1 });
export const db = drizzle(sql, {
  schema,
});

await migrate(db, { migrationsFolder: "drizzle" });

This code is referenced by NextAuth/Auth.js with
  ...
export const {
  auth,
  handlers: { GET, POST },
} = NextAuth({
  ...authConfig,
  adapter: DrizzleAdapter(db),
});
  ...

Itself referenced by an api route to create a 'protected' route

During the build step, the 'db' service of my docker compose is not yet up, neither should it be, and so, the migration line and the api route raise errors, along with the fact that process.env.DATABASE_URL also doesn't exist, but these lines, I suppose, shouldn't be referenced anyways.

Could anyone assist me in understanding what I'm doing wrong/what to fix please.

2 Replies

It also happens in the route /api/auth/[...nextauth]/route.ts, during the 'Collecting page data...' step:
Collecting page data ...
11.14 Error: DATABASE_URL is not set (src/db/index.ts) <== Thrown by me
11.14     at 9498 (/app/.next/server/app/api/protected/route.js:1:2689)
11.14     at __webpack_require__ (/app/.next/server/webpack-runtime.js:1:145)
11.14     at __webpack_exec__ (/app/.next/server/app/api/protected/route.js:1:3641)
11.14     at /app/.next/server/app/api/protected/route.js:1:3680
11.14     at __webpack_require__.X (/app/.next/server/webpack-runtime.js:1:1623)
11.14     at /app/.next/server/app/api/protected/route.js:1:3654
11.14     at Object.<anonymous> (/app/.next/server/app/api/protected/route.js:1:3722)
11.14     at Module._compile (node:internal/modules/cjs/loader:1256:14)
11.14     at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
11.14     at Module.load (node:internal/modules/cjs/loader:1119:32)
11.14 
11.14 > Build error occurred
11.14 Error: Failed to collect page data for /api/protected
11.14     at /app/node_modules/.pnpm/next@14.0.0_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/utils.js:1211:15
11.14     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
11.14   type: 'Error'
11.14 }
11.17 error Command failed with exit code 1.
11.17 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Can anyone help please ?
I also opened an issue on the nextauth repo in case you want more info or want to see the repo I linked https://github.com/nextauthjs/next-auth/issues/8994