Getting Started
Mondel is a lightweight, type-safe ORM for MongoDB designed specifically for modern TypeScript environments, including serverless and edge functions.
Installation
Install the core package along with potential peer dependencies.
npm install mondel mongodb zodpnpm add mondel mongodb zodyarn add mondel mongodb zodbun add mondel mongodb zodWhy Mondel?
The Missing Link
Most MongoDB developers are stuck choosing between two extremes:
- Raw MongoDB Driver: Extremely flexible and fast, but completely untyped. You write
db.collection('users')and hope you spelled it right, then cast the resultas Userand hope the database matches your interface. - Mongoose: A heavy, feature-rich ODM built for long-running Node.js servers. It carries 10+ years of legacy code, global state, and complex "magic" (hooks, virtuals, hydration) that makes it unsuitable for serverless environments where cold-start time is critical.
Mondel sits perfectly in the middle. It gives you the raw performance and flexibility of the native driver, but with the type safety and schema validation you expect from a modern tool.
Serverless First
Traditional ORMs often rely on a single, persistent database connection established at application startup. In serverless functions (AWS Lambda, Vercel Functions, Cloudflare Workers), this pattern causes issues:
- Cold Starts: Establishing a heavy connection on every request adds latency.
- Connection Limits: Thousands of concurrent lambda invocations can quickly exhaust your database's connection pool.
- Global State: Global connection variables don't work reliably across requests in some edge runtimes.
Mondel is designed to be stateless. Its createClient function is a lightweight factory that returns a connection handler. It allows the underlying MongoDB driver to manage pooling efficiently where possible, or handle rapid connections/disconnections gracefully in stateless environments.
Zero Magic
- No Class Decorators: We use simple functions to define schemas.
- No Active Record Pattern: We don't wrap your data in heavy model classes. What you get from the database is a plain JavaScript object.
- Explicit > Implicit: We avoid "magic" behavior like automatic timestamps unless explicitly requested, or hidden hooks that fire without you knowing.
Quick Start
Create a schema and a client to start querying your database.
import { defineSchema, s, createClient } from "mondel";
// 1. Define your schema
const users = defineSchema("users", {
timestamps: true,
fields: {
// _id is implicit - auto-generated by MongoDB and typed as ObjectId
email: s.string().required(),
name: s.string(),
},
});
// 2. Create a client factory
const connect = createClient({
serverless: true,
schemas: [users],
});
// 3. Use it in your application
async function handler() {
const db = await connect(process.env.MONGO_URI);
const result = await db.users.create({
email: "hello@mondel.dev",
name: "Mondel User",
});
// result.insertedId is the auto-generated _id
return result.insertedId;
}