Introduction
Welcome to Lesan โ a blazing-fast, cross-platform web framework and ODM for building modern APIs with MongoDB.
What is Lesan?โ
Lesan is a new way to build web servers and NoSQL data models. It combines the flexibility of GraphQL with the performance of direct MongoDB queries, all while maintaining complete end-to-end type safety.
Key Featuresโ
- Client-Driven Projections โ Fetch exactly the data you need, nothing more
- Automatic Bi-Directional Relations โ Define once, get both sides for free
- End-to-End TypeScript Safety โ Types generated from your actual code
- Cross-Platform โ Runs on Node.js, Bun, and Deno
- Extreme Performance โ Much faster than traditional ORMs or GraphQL
- MongoDB Native โ Full compatibility with MongoDB's powerful query language
Quick Startโ
Installationโ
# Node.js
npm install @hemedani/lesan mongodb superstruct
# Bun
bun add @hemedani/lesan mongodb superstruct
# Deno
import { lesan } from "jsr:@hemedani/lesan";
Create Your First APIโ
import { lesan, MongoClient, string, number, object, ObjectId } from "@hemedani/lesan";
// 1. Initialize
const coreApp = lesan();
// 2. Connect to MongoDB
const client = await new MongoClient("mongodb://localhost:27017/").connect();
coreApp.odm.setDb(client.db("myapp"));
// 3. Define a model
const countries = coreApp.odm.newModel("country", {
name: string(),
population: number(),
}, {});
// 4. Define an action
const addCountry = async (body) => {
return await countries.insertOne({
doc: body.details.set,
projection: body.details.get,
});
};
coreApp.acts.setAct({
schema: "country",
actName: "addCountry",
validator: object({
set: object({ name: string(), population: number() }),
get: object(),
}),
fn: addCountry,
});
// 5. Start the server
await coreApp.runServer({
port: 8000,
playground: true,
typeGeneration: true,
});
Test Your APIโ
curl -X POST http://localhost:8000/lesan \
-H "Content-Type: application/json" \
-d '{
"model": "country",
"act": "addCountry",
"details": {
"set": { "name": "Iran", "population": 85000000 },
"get": { "name": 1, "population": 1 }
}
}'
Or open http://localhost:8000/playground to use the interactive API explorer.
Documentation Structureโ
| Section | Description |
|---|---|
| API Reference | Complete API documentation |
| Server API | Server setup, actions, context |
| Models & ODM | Schemas, relations, CRUD operations |
| Queries & Projections | Client-driven projections and aggregation |
| Type System | TypeScript types and generated code |
| Cross-Platform | Node.js, Bun, and Deno support |
Why Lesan?โ
vs Traditional RESTโ
| Feature | REST | Lesan |
|---|---|---|
| Over-fetching | Common | Eliminated |
| Under-fetching | Common | Eliminated |
| Endpoint count | Many | One |
| Type safety | Manual | Automatic |
| Relations | Manual joins | Automatic |
vs GraphQLโ
| Feature | GraphQL | Lesan |
|---|---|---|
| Flexibility | High | High |
| Performance | Resolver overhead | Direct MongoDB |
| Complexity | High | Low |
| Type safety | Schema-first | Code-first |
| MongoDB | Needs wrapper | Native |
vs Traditional ORMsโ
| Feature | ORMs | Lesan |
|---|---|---|
| Relations | Foreign keys | Embedded (denormalized) |
| Queries | Abstracted | Native MongoDB |
| Projections | Limited | Client-driven |
| Performance | Good | Excellent |
Built with Lesanโ
Lesan is already powering real production applications. Here's a showcase of what can be built with it.
ZiWound โ War Crimes Documentation Platformโ
ziwound.com is a full-stack war crimes documentation system built entirely with Lesan. It's a serious, production-grade application that demonstrates Lesan's capabilities at scale. ZiWound is open source โ you can read the entire codebase on github.com/hemedani/ziwound and study the real models, acts, and utilities end-to-end.
Backend (Deno + Lesan + MongoDB):
- 15 data models โ
user,report,document,file,country,province,city,category,tag,blogPost,warCriminal,confirmation, and more - 98 actions (acts) โ the entire API is expressed as acts, with per-act validators handling input (
set) and client-driven projections (get) - Rich relation graph โ reports link to reporters, documents, tags, categories, hostile countries, and locations, with Lesan automatically embedding and syncing the reverse relations (e.g. each user keeps a sorted, limited list of their reports)
- GeoJSON support โ native MongoDB geospatial queries for interactive map-based exploration (MapLibre GL + Leaflet)
- JWT auth & RBAC โ role-based access control (Ghost, Manager, Editor, Ordinary) built on the request context +
preValidation/preActhooks - Production deployment โ Dockerized, with CORS config, static file serving, rate limiting, and generated TypeScript declarations (
typeGeneration: true)
Frontend (Next.js):
- Nine languages with full RTL/LTR layout flipping (Persian, English, Arabic, Chinese, Portuguese, Spanish, Dutch, Turkish, Russian)
- Secure multi-language report submission with file attachments, location, and metadata
- Advanced search and filtering of documented incidents, a blog section, and a comprehensive admin panel
- PWA support and server actions for type-safe backend communication
The whole stack โ data modeling, relations, validation, and the client-server contract โ is driven by Lesan's generated types, proving that Lesan scales from a quick prototype to a multi-language production platform.
Next Stepsโ
- Follow the Getting Started tutorial to build your first Lesan server from scratch
- Explore your running server in the Playground โ tabs, request history, E2E testing, and schema/act introspection
- Continue with the Tutorials โ relations, CRUD, and aggregation on a real country/city/user example
- Explore the API Reference to learn about all available APIs
- Check out the Server API for configuration options
- Learn about Models & Relations for data modeling
- Understand the philosophy behind Lesan's relationships โ how they compare to SQL, NoSQL, and real life
- See how Lesan communicates between server and client without a query language
- Understand Projections for flexible data fetching
- See Cross-Platform for deployment options
- Level up with the Advanced Guides โ deep projections, relations in depth, request lifecycle hooks, aggregation, filtering, pagination, and microservices
- See a real-world Lesan application โ runnable starter apps for Node.js, Bun, and Deno
- Study the run-in-repo procurement workflow app โ a complete 15-model hospital procurement & warehouse system with JWT auth, a configurable approval workflow, budget encumbrance, tendering, and inventory tracking
- Follow the step-by-step Procurement Workflow tutorials โ every model, act, and utility of that app explained one page at a time
- Study the ZiWound case study โ a real production platform (war crimes documentation, 15 models, 98 acts, 9 languages) built on Lesan with Deno and MongoDB
- Coming from Mongoose, GraphQL, or Express? See Migrating to Lesan for side-by-side before/after examples
- See how fast Lesan is โ Cross-Platform Benchmarks across Node.js, Bun, and Deno
Communityโ
- GitHub: github.com/MiaadTeam/lesan
- Issues: Report bugs and request features
- Contributions: Pull requests are welcome!