API Overview
Welcome to the Lesan API Reference. This section provides comprehensive documentation for every public API, module, and type in the Lesan framework.
What is Lesan?โ
Lesan is a blazing-fast, cross-platform web framework + ODM that works on Node.js, Bun, and Deno. It gives you GraphQL-like flexibility with unmatched performance, featuring:
- Client-driven projections โ fetch only the data you need
- Automatic bi-directional relationships โ define relations once, get both sides for free
- End-to-end TypeScript safety โ types generated from your schemas
- Extreme performance โ much faster than traditional ORMs or GraphQL
- Full MongoDB compatibility โ with a modern, type-safe developer experience
API Structureโ
The Lesan API is organized into the following core modules:
| Module | Description | Documentation |
|---|---|---|
| Core | Main entry point, server, context, and acts | Server API |
| Models & ODM | Schema definitions, relations, and database operations | Models & ODM |
| Queries & Projections | Find, aggregate, and client-driven projections | Queries & Projections |
| Type System | Generated types, inference, and utilities | Type System |
| Cross-Platform | Node.js, Bun, and Deno compatibility | Cross-Platform |
Quick Startโ
import { lesan, MongoClient, string, number, object, ObjectId } from "@hemedani/lesan";
// 1. Initialize Lesan
const coreApp = lesan();
// 2. Connect to MongoDB
const client = await new MongoClient("mongodb://localhost:27017/").connect();
const db = client.db("myapp");
coreApp.odm.setDb(db);
// 3. Define a schema
const country = coreApp.odm.newModel("country", {
name: string(),
population: number(),
}, {});
// 4. Define an action
const addCountry = async (body) => {
return await country.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 });
Architecture Overviewโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Lesan Core โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโค
โ Server โ Acts โ Schemas โ ODM โ
โ (HTTP) โ (Actions) โ (Models) โ (MongoDB) โ
โโโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโค
โ Platform Adapters (Node/Bun/Deno) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Server (runServer)โ
Handles HTTP requests, serves static files, provides the GraphQL-like playground, and routes actions to their handlers.
Acts (acts)โ
The action system where you define API endpoints. Each action has a validator (for input validation) and a function (for business logic).
Schemas (schemas)โ
Defines your data models with pure fields (scalar values) and relations (links to other models).
ODM (odm)โ
The Object Document Mapper that provides MongoDB operations like find, insertOne, addRelation, and aggregation with automatic projection support.
Context (contextFns)โ
A request-scoped container that carries values (like authenticated user, headers, request body) through your action functions.
Type Safetyโ
Lesan generates TypeScript types automatically from your schemas and actions. When you enable typeGeneration: true, Lesan creates:
- Schema types (e.g.,
countrySchema,provinceSchema) - Input types for relations (e.g.,
countryInp) - Request types for all your actions (e.g.,
ReqType) - A type-safe client (
lesanApi) for calling your API
See the Type System documentation for details.
Next Stepsโ
- Server API โ Learn how to configure and run the Lesan server
- Models & ODM โ Define schemas, relations, and perform database operations
- Queries & Projections โ Master client-driven projections and aggregation
- Type System โ Understand generated types and TypeScript utilities
- Cross-Platform โ Run Lesan on Node.js, Bun, or Deno