drizzle best practices for create drizzle instance

From this example, is it a good practice to create the db every time or should I cache? I’m coming from a Java world where caching and connection pooling is the way to go. But I understand CF is cloud solution and this might be different. I could create that db in middleware and make it available via locals, but is that smart in terms of costs? Having an endpoint that does not access the d1 database will construct it then… should I make it lazy or something in locals?
import { drizzle } from 'drizzle-orm/d1';

export interface Env {
<BINDING_NAME>: D1Database;
}

export default {
async fetch(request: Request, env: Env) {
const db = drizzle(env.<BINDING_NAME>);
const result = await db.select().from(users).all()
return Response.json(result);
},
};
import { drizzle } from 'drizzle-orm/d1';

export interface Env {
<BINDING_NAME>: D1Database;
}

export default {
async fetch(request: Request, env: Env) {
const db = drizzle(env.<BINDING_NAME>);
const result = await db.select().from(users).all()
return Response.json(result);
},
};
1 Reply
singh.harshvikram
Was about to ask the same thing! Looking forward to the response...