Cloudflare d1 on cloudflare pages functions

In the drizzle docs, the following code is mentioned to work with cloudflare workers but how do I make it work for cloudflare pages functions
import { drizzle } from 'drizzle-orm/d1';

export interface Env {
DB: D1Database;
}

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

export interface Env {
DB: D1Database;
}

export default {
async fetch(request: Request, env: Env) {
const db = drizzle(env.DB);
const result = await db.select().from(users).all()
return Response.json(result);
},
};
I also need to export a singleton db instance from it to pass it to my better-auth instance, how do i make it work?
1 Reply
gebna
gebna4mo ago
I'm trying to do what you're doing. I think you export a function that returns an instance instead of a singleton. And create the singleton on the fly in hooks/middleware

Did you find this page helpful?