mikro-orm /mikro-orm
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, MS SQL Server, PostgreSQL and SQLite/libSQL databases.
★ 9,273⑂ 709👁 47⑃ 3,464● 3,381▤ 255
+0
日增长
9,273
stars
709
forks
47
watchers
3,464
prs
3,381
issues
255
releases
README
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL (including CockroachDB and PGlite), SQLite (including libSQL), MSSQL and Oracle databases.
Heavily inspired by Doctrine and Hibernate.
Quick Start
Install a driver package for your database:
sh
npm install @mikro-orm/postgresql PostgreSQL
npm install @mikro-orm/pglite PGlite (embedded PostgreSQL in WASM)
npm install @mikro-orm/mysql MySQL
npm install @mikro-orm/mariadb MariaDB
npm install @mikro-orm/sqlite SQLite
npm install @mikro-orm/libsql libSQL / Turso
npm install @mikro-orm/sql-js sql.js (in-memory SQLite in WASM)
npm install @mikro-orm/mongodb MongoDB
npm install @mikro-orm/mssql MS SQL Server
npm install @mikro-orm/oracledb Oracle
If you use additional packages like @mikro-orm/cli, @mikro-orm/migrations, or @mikro-orm/entity-generator, install @mikro-orm/core explicitly as well. See the quick start guide for details.
Define Entities
The recommended way to define entities is using defineEntity with setClass:
typescript
import { defineEntity, p, MikroORM } from '@mikro-orm/postgresql';
const AuthorSchema = defineEntity({
name: 'Author',
properties: {
id: p.integer().primary(),
name: p.string(),
email: p.string(),
born: p.datetime().nullable(),
books: () = p.oneToMany(Book).mappedBy('author'),
},
});
export class Author extends AuthorSchema.class {}
AuthorSchema.setClass(Author);
const BookSchema = defineEntity({
name: 'Book',
properties: {
id: p.integer().primary(),
title: p.string(),
author: () = p.manyToOne(Author).inversedBy('books'),
},
});
export class Book extends BookSchema.class {}
BookSchema.setClass(Book);
You can also define entities using decorators or EntitySchema. See the defining entities guide for all options.
Initialize and Use
typescript
import { MikroORM, RequestContext } from '@mikro-orm/postgresql';
const orm = await MikroORM.init({
entities: [Author, Book],
dbName: 'my-db',
});
// Create new entities
const author = orm.em.create(Author, {
name: 'Jon Snow',
email: '[email protected]',
});
const book = orm.em.create(Book, {
title: 'My Life on The Wall',
author,
});
// Flush persists all tracked changes in a single transaction
await orm.em.flush();
Querying
typescript
// Find with relations
const authors = await orm.em.findAll(Author, {
populate: ['books'],
orderBy: { name: 'asc' },
});
// Type-safe QueryBuilder
const qb = orm.em.createQueryBuilder(Author);
const result = await qb
.select('')
.where({ books: { title: { $like: '%Wall%' } } })
.getResult();
Request Context
In web applications, use RequestContext to isolate the identity map per request:
typescript
const app = express();
app.use((req, res, next) = {
RequestContext.create(orm.em, next);
});
More info about RequestContext is described here.
Unit of Work
UniTop issues
#296Who uses MikroORM in production?66 comments · 2020-01-08#2372Throw exception if accessing primary key on unflushed entity13 comments · 2021-11-08#1173[Feature]: Embedded partial updates13 comments · 2020-12-07#2380Read-only EntityManager forking6 comments · 2021-11-08#7771First class Temporal types support5 comments · 2026-05-21#3397Allow [uninitialized] Collections as values for filter queries4 comments · 2022-08-15#2392Provide a `matches` utility or document EntityComparator4 comments · 2021-11-10#2397Provide an identity-map friendly update/remove method3 comments · 2021-11-10
Discussions
#8660docs/upgrading-v6-to-v70 comments · 2026-11-09#8605docs: Add Performance Benchmarks Against Other Popular TypeScript ORMs2 comments · 2026-11-05#8522Question: How to get TypeScript suggestions for the property names to work when chaining `.check()` in `defineEntity`3 comments · 2026-10-27#8388TDK CLI integration: run `mikro-orm migration:up` as a one-shot job in a TDK landscape2 comments · 2026-10-01#8372Query builder at migration time3 comments · 2026-09-30#8352Proposal: Laravel-inspired schema dumps for migrations1 comments · 2026-09-29#8220docs/entity-generator2 comments · 2026-08-28#8174docs/usage-with-nestjs1 comments · 2026-08-18
Latest release
v7.2.9 v7.2.9 2026-11-127.2.9 (2026-11-12) Bug Fixes cli: resolve TS loaders from the project root (8669) (1b655f9) core: accept FindOptions typed options in generic find() wrappers (8662) (932227e), closes 8655 core: clean up the serialization context when toObject() or serialize() throws (8636) (70ce74e) core: detect in-place mutations of Date properties on loaded entities (8675) (2a02c9f), closes 4391 core: do not propagate STI sibling property checks to other subtypes (8688) (b0b2303), closes 8687 core: don't re-query empty inverse 1:1 relations on repeated populate (8627) (b074921), closes 8624