Skip to content
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases.
TypeScript TSQL JavaScript
Branch: master
Clone or download
Pull request Compare This branch is 3 commits behind mikro-orm:master.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE chore: add issue templates Sep 15, 2019
docs docs(serializing): fix typo (mikro-orm#179) Sep 29, 2019
lib fix(validation): allow undefined in nullable entity properties Sep 19, 2019
tests fix(validation): allow undefined in nullable entity properties Sep 19, 2019
.codeclimate.yml fix(sql): support `IS NULL` queries in `QueryBuilder` Jun 14, 2019
.gitignore chore: ignore sqlite db file and update TODO Mar 31, 2019
.travis.yml fix(deps): update dependencies to support node 12 (mikro-orm#52) May 13, 2019 chore(release): 2.7.8 Sep 19, 2019 docs: add contributing guideline Apr 3, 2019
LICENSE Initial commit Mar 15, 2018 chore: move website to custom domain and fix repo links after transfer Sep 2, 2019 docs: improve readme and nestjs guide (link to nestjs-mikro-orm module) Jun 16, 2019
docker-compose.yml chore(deps): update postgres docker tag to v11.5 (mikro-orm#90) Aug 9, 2019
package.json fix(deps): update dependency ts-morph to v4 (mikro-orm#162) Sep 24, 2019
tsconfig.json chore: make linter more strict (mikro-orm#86) Aug 7, 2019
tslint.json chore: enforce empty lines via linter (mikro-orm#89) Aug 7, 2019
yarn.lock fix(deps): update dependency ts-morph to v4 (mikro-orm#162) Sep 24, 2019


Simple TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases.

Heavily inspired by Doctrine and Nextras Orm.

NPM version Chat on slack Downloads Dependency Status Build Status Coverage Status Maintainability

📖 Documentation

MikroORM's documentation, included in this repo in the root directory, is built with Jekyll and publicly hosted on GitHub Pages at

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

Core features

📦 Example integrations

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

TypeScript examples

JavaScript examples

🚀 Quick start

First install the module via yarn or npm and do not forget to install the database driver as well:

$ yarn add mikro-orm mongodb # for mongo
$ yarn add mikro-orm mysql2  # for mysql
$ yarn add mikro-orm pg      # for postgresql
$ yarn add mikro-orm sqlite  # for sqlite


$ npm i -s mikro-orm mongodb # for mongo
$ npm i -s mikro-orm mysql2  # for mysql
$ npm i -s mikro-orm pg      # for postgresql
$ npm i -s mikro-orm sqlite  # for sqlite

Next you will need to enable support for decorators in tsconfig.json via:

"experimentalDecorators": true

Then call MikroORM.init as part of bootstrapping your app:

const orm = await MikroORM.init({
  entitiesDirs: ['./dist/entities'], // path to your JS entities (dist), relative to `baseDir`
  dbName: 'my-db-name',
  clientUrl: '...', // defaults to 'mongodb://localhost:27017' for mongodb driver
  autoFlush: false, // read more here:
console.log(orm.em); // access EntityManager via `em` property

There are more ways to configure your entities, take a look at installation page.

Then you will need to fork entity manager for each request so their identity maps will not collide. To do so, use the RequestContext helper:

const app = express();

app.use((req, res, next) => {
  RequestContext.create(orm.em, next);

You should register this middleware as the last one just before request handlers and before any of your custom middleware that is using the ORM. There might be issues when you register it before request processing middleware like queryParser or bodyParser, so definitely register the context after them.

More info about RequestContext is described here.

Now you can start defining your entities (in one of the entitiesDirs folders):


export class Book {

  _id: ObjectID;

  title: string;

  author: Author;

  @ManyToMany({ entity: () => BookTag, inversedBy: 'books' })
  tags = new Collection<BookTag>(this);

  constructor(title: string, author: Author) {
    this.title = title; = author;


export interface Book extends IEntity { }

More information can be found in defining entities section in docs.

When you have your entities defined, you can start using ORM either via EntityManager or via EntityRepositorys.

To save entity state to database, you need to persist it. Persist takes care or deciding whether to use insert or update and computes appropriate change-set. Entity references that are not persisted yet (does not have identifier) will be cascade persisted automatically.

// use constructors in your entities for required parameters
const author = new Author('Jon Snow', '');
author.born = new Date();

const publisher = new Publisher('7K publisher');

const book1 = new Book('My Life on The Wall, part 1', author);
book1.publisher = publisher;
const book2 = new Book('My Life on The Wall, part 2', author);
book2.publisher = publisher;
const book3 = new Book('My Life on The Wall, part 3', author);
book3.publisher = publisher;

// just persist books, author and publisher will be automatically cascade persisted
await orm.em.persistAndFlush([book1, book2, book3]);

To fetch entities from database you can use find() and findOne() of EntityManager:

const authors = orm.em.find(Author, {});

for (const author of authors) {
  console.log(author); // instance of Author entity
  console.log(; // Jon Snow

  for (const book of author.books) { // iterating books collection
    console.log(book); // instance of Book entity
    console.log(book.title); // My Life on The Wall, part 1/2/3

More convenient way of fetching entities from database is by using EntityRepository, that carries the entity name so you do not have to pass it to every find and findOne calls:

const booksRepository = orm.em.getRepository(Book);

// with sorting, limit and offset parameters, populating author references
const books = await booksRepository.find({ author: '...' }, ['author'], { title: QueryOrder.DESC }, 2, 1);

// or with options object
const books = await booksRepository.find({ author: '...' }, { 
  populate: ['author'],
  limit: 1,
  offset: 2,
  sort: { title: QueryOrder.DESC },

console.log(books); // Book[]

Take a look at docs about working with EntityManager or using EntityRepository instead.

🤝 Contributing

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


👤 Martin Adámek

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

Show your support

Please ⭐️ this repository if this project helped you!

📝 License

Copyright © 2018 Martin Adámek.

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

You can’t perform that action at this time.