Skip to content

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.

bash
npm install mondel mongodb zod
bash
pnpm add mondel mongodb zod
bash
yarn add mondel mongodb zod
bash
bun add mondel mongodb zod

Why Mondel?

Most MongoDB developers are stuck choosing between two extremes:

  1. Raw MongoDB Driver: Extremely flexible and fast, but completely untyped. You write db.collection('users') and hope you spelled it right, then cast the result as User and hope the database matches your interface.
  2. 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.

typescript
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;
}

Released under the MIT License.