Finally found a MongoDB ORM that works in Cloudflare Workers

Been struggling with MongoDB ORMs in serverless environments for months. Everything was either too heavy, had terrible TypeScript support, or just didn’t work with Cloudflare Workers’ connection model.

Just wanted to share what I ended up building - Mondel. It’s a 27KB TypeScript ORM that actually solves the serverless MongoDB problems:

const userSchema = defineSchema("users", {
  timestamps: true,
  fields: {
    email: s.string().required().unique(),
    role: s.enum(["ADMIN", "USER"]).default("USER"),
  },
});
 
// Factory pattern for serverless (no persistent connections)
const connect = createClient({ serverless: true, schemas });
 
// Just works in Workers
const db = await connect(env.MONGODB_URI);
const user = await db.users.create({ email: "test@example.com" });

What finally worked for me:

  • Bundle size under 30KB (was hitting 100KB+ with other ORMs)

  • Full TypeScript autocomplete (no more runtime type errors)

  • Lazy connections (crucial for Workers pricing model)

  • Built-in Zod validation

  • Transactions, aggregations - all the MongoDB features I need

Been running this in production for 6 months now. If anyone else is dealing with the “MongoDB + serverless” headache, this might save you some pain.

Docs: https://mondel-orm.pages.dev
NPM: https://www.npmjs.com/package/mondel

Bonus: Works everywhere else too

  • Node.js (traditional servers) - persistent connections

  • Bun - super fast runtime support

  • Deno - edge runtime compatible

  • Vercel Edge, AWS Lambda - all serverless platforms

  // Node.js mode - connects immediately 
const db = await createClient({ uri: process.env.MONGODB_URI, schemas: [userSchema], });

Full disclosure: I’ve been using this in production and just published it to NPM today as v0.2.0. It’s been battle-tested in my apps but would love feedback from others dealing with the same MongoDB + serverless headaches.

What solutions have you guys found for MongoDB in serverless?

3 Likes

Thanks for sharing @Edjo_Brandao. That’s an impressive size reduction, how did you manage to achieve it?

1 Like

Please confirm that MONGODB_URI supports srv version, or we will have to use legacy URI with multiple replica sets, and lastly, is it production-ready? How much latency do you experience with Cloudflare Workers?

This is really interesting, @Edjo_Brandao. Thanks for sharing :fire:

I recently wrote about using a reusable MongoDB client pattern with TanStack Start, and it’s cool to see similar problems being solved in edge runtimes. Curious how you’re handling connection reuse and cold starts in Workers?

Hey @Edjo_Brandao , based on mondel/src/client/client.ts at 4f375d4436dd971bb4dbacb278bb7cd323168edc · edjo/mondel · GitHub it seems you’re caching the MongoClient instance as a way to optimize for reuse, but if the worker environment is cycled wouldn’t this still result in a new client instance being created and proxied?

I worked around this by maintaining state using durable objects, but I’d be curious to get a sense of how effective you’ve found your approach. Have you benchmarked this to validate it’s behaving the way you expect?