Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 3, 2026·3 min de lecture

MikroORM — TypeScript ORM with Data Mapper and Unit of Work

MikroORM is a TypeScript ORM for Node.js built on the Data Mapper pattern with Unit of Work and Identity Map. It supports PostgreSQL, MySQL, MariaDB, SQLite, and MongoDB with full type safety.

Introduction

MikroORM is a TypeScript-first ORM that implements the Data Mapper pattern with automatic Unit of Work tracking. Unlike Active Record ORMs, it keeps your domain models clean of persistence logic while providing automatic change detection and transaction batching.

What MikroORM Does

  • Maps TypeScript/JavaScript classes to database tables using decorators or EntitySchema
  • Tracks entity changes via Unit of Work and flushes them in optimized batches
  • Supports multiple databases: PostgreSQL, MySQL, MariaDB, SQLite, MongoDB
  • Provides a QueryBuilder for complex queries alongside the EntityManager API
  • Handles relations, eager/lazy loading, cascading, and population strategies

Architecture Overview

MikroORM uses an Identity Map to ensure each database row is represented by exactly one entity instance per request. The Unit of Work collects all changes (inserts, updates, deletes) and computes a changeset at flush time, ordering operations to respect foreign key constraints. The metadata discovery system reads decorators or schema definitions at boot to build a complete mapping model.

Self-Hosting & Configuration

  • Install core package plus your database driver (@mikro-orm/postgresql, etc.)
  • Configure via mikro-orm.config.ts or pass options to MikroORM.init()
  • Use the CLI for migrations: npx mikro-orm migration:create and migration:up
  • Set discovery.warnWhenNoEntities: false in production if entities are compiled
  • Integrate with NestJS, Express, or Fastify via official packages

Key Features

  • Full TypeScript type safety with strict relation typing and FindOptions
  • Automatic Unit of Work removes manual save/update calls
  • Embeddables and polymorphic relations for complex domain models
  • Built-in seeder and schema generator for rapid prototyping
  • First-class MongoDB support alongside SQL databases

Comparison with Similar Tools

  • TypeORM — Active Record style with less strict typing; MikroORM is stricter Data Mapper
  • Prisma — Schema-first with generated client; MikroORM uses code-first decorators
  • Drizzle ORM — Lightweight SQL builder; MikroORM adds Unit of Work and Identity Map
  • Sequelize — Older JS ORM without native TypeScript; MikroORM is TS-native
  • Objection.js — Knex-based with models; MikroORM has richer change tracking

FAQ

Q: How does MikroORM differ from TypeORM? A: MikroORM enforces Data Mapper pattern with proper Unit of Work, while TypeORM mixes Active Record and Data Mapper without strict separation.

Q: Can I use MikroORM with NestJS? A: Yes, the official @mikro-orm/nestjs package provides module integration with request-scoped EntityManager.

Q: Does it support database migrations? A: Yes, it auto-generates migration files by diffing your entities against the current schema.

Q: Is MongoDB support production-ready? A: Yes, MikroORM treats MongoDB as a first-class driver with the same EntityManager API and Unit of Work semantics.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires