Connection management with Drizzle

I'm seeing lots of examples from AI assistants that implement complex connection management like this class-based example (attached image). However, in the Drizzle docs, the setup is much simpler:
import { drizzle } from 'drizzle-orm/bun-sqlite';
import { Database } from 'bun:sqlite';

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);
import { drizzle } from 'drizzle-orm/bun-sqlite';
import { Database } from 'bun:sqlite';

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);
Question: - Do we actually need to implement connection management (opening/closing) ourselves when using Drizzle, or does Drizzle handle this for us? Specifically: - Is the simple setup from the docs sufficient for production use? - Do we need to manually close connections when using Drizzle? - Should we implement connection pooling or is it unnecessary with SQLite + Drizzle?
No description
1 Reply
TOSL
TOSL3d ago
This is unnecessary. The drizzle() function creates a database instance that you can throughout your application(s). It is worth mentioning that if you want some control over the connections, it probably isn't too hard to do. Using postgres as a example, You might have something like this:
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from "postgres";

export const connection = postgres(process.env.DATABASE_URL, {
max: (process.env.DB_MIGRATING || env.DB_SEEDING) ? 1 : undefined,
});

export const db = drizzle(connection, {
schema,
logger: true,
});

export default db;
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from "postgres";

export const connection = postgres(process.env.DATABASE_URL, {
max: (process.env.DB_MIGRATING || env.DB_SEEDING) ? 1 : undefined,
});

export const db = drizzle(connection, {
schema,
logger: true,
});

export default db;

Did you find this page helpful?