Skip to main content
Version: Next

MikroORM <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

Properties

readonlyinheritedconfig

readonlyinheriteddriver

driver: LibSqlDriver

inheritedem

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

inheritedentityGenerator

  • Gets the EntityGenerator.


    Returns IEntityGenerator

inheritedmigrator

  • Gets the Migrator.


    Returns IMigrator

inheritedschema

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


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

inheritedseeder

  • Gets the SeedManager


    Returns ISeedManager

Methods

inheritedcheckConnection

  • 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 }>

inheritedclose

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


    Parameters

    • force: boolean = false

    Returns Promise<void>

inheritedconnect

  • Connects to the database.


    Returns Promise<LibSqlDriver>

inheriteddiscoverEntity

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


    Parameters

    Returns void

inheritedgetMetadata

  • Gets the MetadataStorage.


    Returns MetadataStorage

inheritedisConnected

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


    Returns Promise<boolean>

inheritedreconnect

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


    Parameters

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