Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mikro-orm

> 数据库
Open source

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/

9.1K stars0 likes3 views
WebsiteGitHub

About

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/

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:

bash
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:

…

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

Unit of Work maintains a list of objects (entities) affected by a business transaction and coordinates the writing out of changes. (Martin Fowler)

When you call em.flush(), all computed changes are queried inside a database transaction. This means you can control transaction boundaries simply by making changes to your entities and calling flush() when ready.

typescript
const author = await em.findOneOrFail(Author, 1, {
  populate: ['books'],
});
author.name = 'Jon Snow II';
author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
author.books.add(orm.em.create(Book, { title: 'New Book', author }));

// Flush computes change sets and executes them in a single transaction
await em.flush();

The above flush will execute:

sql
begin;
update "author" set "name" = 'Jon Snow II' where "id" = 1;
update "book"
  set "title" = case
    when ("id" = 1) then 'My Life on The Wall (2nd ed.)'
    when ("id" = 2) then 'Another Book (2nd ed.)'
    else "title" end
  where "id" in (1, 2);
insert into "book" ("title", "author_id") values ('New Book', 1);
commit;

Core Features

  • Clean and Simple Entity Definition — decorators, EntitySchema, or defineEntity
  • Identity Map and Unit of Work — automatic change tracking
  • Entity References and Collections
  • QueryBuilder and Kysely Integration
  • Transactions and Cascading
  • Populating Relations and Loading Strategies
  • Filters and Lifecycle Hooks
  • Schema Generator and Migrations
  • Entity Generator and Seeding
  • Embeddables, Custom Types, and Serialization
  • Composite and Foreign Keys as Primary Key
  • Entity Constructors and Property Validation
  • Modelling Relationships and Vanilla JS Support

Documentation

MikroORM documentation, included in this repo in the root directory, is built with Docusaurus and publicly hosted on GitHub Pages at https://mikro-orm.io.

There is also auto-generated CHANGELOG.md file based on commit messages (via semantic-release).

Example Integrations

You can find example integrations for some popular frameworks in the mikro-orm-examples repository:

TypeScript Examples

  • Express + MongoDB
  • Nest + MySQL
  • RealWorld example app (Nest + MySQL)
  • Koa + SQLite
  • GraphQL + PostgreSQL
  • Inversify + PostgreSQL
  • NextJS + MySQL
  • Accounts.js REST and GraphQL authentication + SQLite
  • Nest + Shopify + PostgreSQL + GraphQL
  • Elysia.js + libSQL + Bun
  • Electron.js + PostgreSQL

JavaScript Examples

  • Express + SQLite

Contributing

Contributions, issues and feature requests are welcome. Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

Authors

Martin Adámek

  • Twitter: @B4nan
  • Github: @b4nan

See also the list of contributors who participated in this project.

Show Your Support

Please star this repository if this project helped you!

If you'd like to support my open-source work, consider sponsoring me directly at github.com/sponsors/b4nan.

License

Copyright © 2018-present Martin Adámek.

This project is licensed under the MIT License - see the LICENSE file for details.

Issues· 19 open

View all issuesOpen on GitHub
  • #8350

    Declare sequence / identity start value and increment in entity metadata

    Updated Sep 29, 2026
  • #5971

    Virtual entity custom filter query

    enhancementUpdated Sep 15, 2026
  • #8267

    How to carry `forceUndefined: true` over to inferred types?

    Updated Sep 10, 2026
  • #7771

    First class Temporal types support

    enhancementUpdated Sep 6, 2026
  • #296

    Who uses MikroORM in production?

    discussionUpdated Jul 28, 2026
  • #8001

    Execute queries with bind parameters instead of inlining values into the SQL (v7 follow-up to #6829, with benchmarks)

    Updated Jul 22, 2026
  • #2372

    Throw exception if accessing primary key on unflushed entity

    enhancementUpdated Feb 27, 2026
  • #1173

    [Feature]: Embedded partial updates

    enhancementUpdated Jan 13, 2025
  • #5962

    Typesafe filters

    enhancementUpdated Aug 26, 2024
  • #2392

    Provide a `matches` utility or document EntityComparator

    enhancementUpdated Oct 25, 2023

> Tags

TypeScriptdatabasedatamapperentitiesentity

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库