Skip to main content
Version: Next

MikroORM <Driver, EM, Entities>

The main class used to configure and bootstrap the ORM.

@example
// import from driver package
import { MikroORM, defineEntity, p } from '@mikro-orm/sqlite';

const User = defineEntity({
name: 'User',
properties: {
id: p.integer().primary(),
name: p.string(),
},
});

const orm = new MikroORM({
entities: [User],
dbName: 'my.db',
});
await orm.schema.update();

const em = orm.em.fork();
const u1 = em.create(User, { name: 'John' });
const u2 = em.create(User, { name: 'Ben' });
await em.flush();

Hierarchy

Index

Constructors

constructor

  • new MikroORM<Driver, EM, Entities>(options): MikroORM<Driver, EM, Entities>
  • Synchronous variant of the init method with some limitations:

    • folder-based discovery not supported
    • ORM extensions are not autoloaded
    • when metadata cache is enabled, FileCacheAdapter needs to be explicitly set in the config

    Parameters

    • options: Options<Driver, EM, Entities>

    Returns MikroORM<Driver, EM, Entities>

Properties

readonlyconfig

config: Configuration<Driver, Driver[typeof EntityManagerType] & EntityManager<Driver>>

readonlydriver

driver: Driver

em

em: EM & { ~entities?: Entities }

The global EntityManager instance. If you are using RequestContext helper, it will automatically pick the request specific context under the hood

Accessors

entityGenerator

migrator

schema

  • get schema(): ReturnType<ReturnType<Driver[getPlatform]>[getSchemaGenerator]>
  • Gets the SchemaGenerator.


    Returns ReturnType<ReturnType<Driver[getPlatform]>[getSchemaGenerator]>

seeder

Methods

checkConnection

  • checkConnection(): Promise<{ ok: true } | { error?: Error; ok: false; reason: string }>
  • Checks whether the database connection is active, returns the reason if not.


    Returns Promise<{ ok: true } | { error?: Error; ok: false; reason: string }>

close

  • close(force): Promise<void>
  • Closes the database connection.


    Parameters

    • force: boolean = false

    Returns Promise<void>

connect

  • connect(): Promise<Driver>
  • Connects to the database.


    Returns Promise<Driver>

discoverEntity

  • discoverEntity<T>(entities, reset): void
  • Allows dynamically discovering new entity by reference, handy for testing schema diffing.


    Parameters

    Returns void

getMetadata

isConnected

  • isConnected(): Promise<boolean>
  • Checks whether the database connection is active.


    Returns Promise<boolean>

reconnect

  • reconnect(options): Promise<void>
  • Reconnects, possibly to a different database.


    Parameters

    • options: Partial<Options<Driver, EM, Entities>> = {}

    Returns Promise<void>

staticinit

  • init<D, EM, Entities>(options): Promise<MikroORM<D, EM, Entities>>
  • Initialize the ORM, load entity metadata, create EntityManager and connect to the database. If you omit the options parameter, your CLI config will be used.


    Parameters

    Returns Promise<MikroORM<D, EM, Entities>>