Aryan
Aryan
Explore posts from servers
DTDrizzle Team
Created by dBranded1 on 8/29/2023 in #help
Too many clients already (!?)
For create-t3-app users, the schema will be in another file - not in your main Drizzle client file by default. Just add this generic type to PostgresJsDatabase to fix this issue:
type PostgresJsDatabase<typeof schema>;
type PostgresJsDatabase<typeof schema>;
So the final file will look like:
// index.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import { env } from "~/env.js";
import * as schema from "./schema";

import { type PostgresJsDatabase } from "drizzle-orm/postgres-js";

// Fix for "sorry, too many clients already"
declare global {
// eslint-disable-next-line no-var -- only var works here
var db: PostgresJsDatabase<typeof schema> | undefined;
}

let db: PostgresJsDatabase<typeof schema>;

if (env.NODE_ENV === "production") {
db = drizzle(postgres(env.DATABASE_URL), { schema });
} else {
if (!global.db) global.db = drizzle(postgres(env.DATABASE_URL), { schema });

db = global.db;
}

export { db };
// index.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import { env } from "~/env.js";
import * as schema from "./schema";

import { type PostgresJsDatabase } from "drizzle-orm/postgres-js";

// Fix for "sorry, too many clients already"
declare global {
// eslint-disable-next-line no-var -- only var works here
var db: PostgresJsDatabase<typeof schema> | undefined;
}

let db: PostgresJsDatabase<typeof schema>;

if (env.NODE_ENV === "production") {
db = drizzle(postgres(env.DATABASE_URL), { schema });
} else {
if (!global.db) global.db = drizzle(postgres(env.DATABASE_URL), { schema });

db = global.db;
}

export { db };
11 replies