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
TypeScriptMITCreated 2018-03-15mikro-orm.io
下载:v7.2.9v7.2.9 ↓7.2.92026-11-12
+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

 Uni

Top issues

Discussions

Latest release

v7.2.9 v7.2.9 2026-11-12
7.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

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON