justjavac/wechat-miniapp-radar · error · Error

DATABASE_URL is required for database operations.

Error message

DATABASE_URL is required for database operations.

What it means

Thrown by createDb() in db/client.ts when no DATABASE_URL is available. The function defaults its only parameter to process.env.DATABASE_URL and rejects any falsy value before connectDb() runs, because drizzle/postgres-js cannot build a client without a connection string. It is a hard precondition: the cache lookup and connection never execute, so the caller must guarantee the value. Optional callers such as persistEnrichment() in lib/enrichment.ts guard with `if (!process.env.DATABASE_URL) return false;` before calling createDb(), so this throw only reaches code that calls createDb() unconditionally.

Source

Thrown at db/client.ts:21

import * as schema from "@/db/schema";

function connectDb(databaseUrl: string) {
  const client = postgres(databaseUrl, {
    max: 1,
    prepare: false
  });

  return {
    client,
    db: drizzle(client, { schema })
  };
}

const cachedConnections = new Map<string, ReturnType<typeof connectDb>>();

export function createDb(databaseUrl = process.env.DATABASE_URL) {
  if (!databaseUrl) {
    throw new Error("DATABASE_URL is required for database operations.");
  }

  const cached = cachedConnections.get(databaseUrl);
  if (cached) return cached.db;

  const connection = connectDb(databaseUrl);
  cachedConnections.set(databaseUrl, connection);
  return connection.db;
}

export async function closeDb(databaseUrl?: string) {
  if (databaseUrl) {
    const cached = cachedConnections.get(databaseUrl);
    if (!cached) return;
    cachedConnections.delete(databaseUrl);
    await cached.client.end();
    return;
  }

View on GitHub (pinned to 02a010ecea)

Solutions

  1. Set DATABASE_URL to a valid postgres connection string in .env locally and in Vercel project environment for deployments.
  2. If you cannot provide a database, do not call createDb(): mirror persistEnrichment() and early-return when process.env.DATABASE_URL is falsy.
  3. Pass an explicit URL, e.g. createDb('postgres://user:pass@host/db'), in scripts/tests so the function does not depend on env.
  4. Verify the result with EXPECT_DATABASE=1 npm run db:verify once configured.

Example fix

// before
const db = createDb(); // throws when DATABASE_URL is unset

// after
if (!process.env.DATABASE_URL) {
  return { persisted: false };
}
const db = createDb();
Defensive patterns

Strategy: validation

Validate before calling

const hasDatabase = Boolean(process.env.DATABASE_URL?.trim());
if (!hasDatabase) {
  // skip DB-dependent work instead of calling createDb()
}

Prevention

When it happens

Trigger: Calling createDb() when process.env.DATABASE_URL is unset or empty and no explicit databaseUrl argument is supplied; importing a module that constructs the drizzle client at load time in a shell where the var was never injected (no `source .env`, a Vercel/CI job missing the secret, or a test runner that does not load .env).

Common situations: Forgetting to copy .env.example to .env locally; the var set in Vercel Preview but missing in Production; DATABASE_URL exported as an empty string by a wrapper; running db:import:test or db:verify without a configured DATABASE_URL (and without EXPECT_DATABASE=1).

Related errors


AI-assisted analysis of justjavac/wechat-miniapp-radar@02a010ecea (2026-08-12). Data as JSON: /api/errors/1a3e53884e46991d. Report an issue: GitHub.