Back to Mikro Orm

@mikro-orm/core

packages/core/README.md

7.0.143.7 KB
Original Source

@mikro-orm/core

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

This is the core package of MikroORM, providing the EntityManager, metadata system, unit of work, identity map, and entity lifecycle management.

Installation

Install @mikro-orm/core together with a driver package for your database:

sh
npm install @mikro-orm/core @mikro-orm/postgresql    # PostgreSQL
npm install @mikro-orm/core @mikro-orm/mysql          # MySQL
npm install @mikro-orm/core @mikro-orm/mariadb        # MariaDB
npm install @mikro-orm/core @mikro-orm/sqlite         # SQLite
npm install @mikro-orm/core @mikro-orm/libsql         # libSQL / Turso
npm install @mikro-orm/core @mikro-orm/mongodb        # MongoDB
npm install @mikro-orm/core @mikro-orm/mssql          # MS SQL Server
npm install @mikro-orm/core @mikro-orm/oracledb       # Oracle

Quick Start

Define entities using defineEntity (recommended), decorators, or EntitySchema:

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(),
    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);

// Initialize the ORM
const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: 'my-db',
});

// Create and persist entities
const author = orm.em.create(Author, { name: 'Jon Snow', email: '[email protected]' });
orm.em.create(Book, { title: 'My Life on The Wall', author });
await orm.em.flush();

// Find entities with relations
const authors = await orm.em.find(
  Author,
  { name: { $like: '%Jon%' } },
  {
    populate: ['books'],
  },
);

Key Features

Documentation

See the official MikroORM documentation and the quick start guide.

License

Copyright © 2018-present Martin Adámek. Licensed under the MIT License.