Page cover

MikroORM

@adminjs/mikroorm

Before reading this article, make sure you have set up an AdminJS instance using one of the supported Plugins. Additionally, you should have installed @adminjs/mikroorm as described in Getting started section.

This guide will assume you have set up MikroORM using it's documentation or Nest.js documentation.

There are small differences in how you connect MikroORM to Nest.js vs other plugins, so the guide will be split into two sections accordingly.

Example model:

owner.entity.ts
import { v4 } from 'uuid'
import { BaseEntity, Entity, PrimaryKey, Property } from '@mikro-orm/core'

export interface IOwner {
  firstName: string;
  lastName: string;
  age: number;
}

@Entity({ tableName: 'owners' })
export class Owner extends BaseEntity<Owner, 'id'> implements IOwner {
  @PrimaryKey({ columnType: 'uuid' })
  id = v4()

  @Property({ fieldName: 'first_name', columnType: 'text' })
  firstName: string

  @Property({ fieldName: 'last_name', columnType: 'text' })
  lastName: string

  @Property({ fieldName: 'age', columnType: 'integer' })
  age: number

  @Property({ fieldName: 'created_at', columnType: 'timestamptz' })
  createdAt: Date = new Date()

  @Property({
    fieldName: 'updated_at',
    columnType: 'timestamptz',
    onUpdate: () => new Date(),
  })
  updatedAt: Date = new Date()
}

Standard

Make sure you have followed the tutorial for the framework you are using in the Plugins section.

The configuration for non-Nest.js plugins is basically the same for each one of them:

  • You must initialize MikroORM before creating AdminJS instance

  • You must import AdminJSMikroORM adapter and register it

  • You must import the entities you want to use and pass them to AdminJS resources options

Nest.js

Make sure you have set up your app.module.ts according to Nest.js documentation and you have followed Nest.js plugin tutorial as well.

Your app.module.ts should have imports option which contains:

  • MikroOrmModule.forRoot(...) to set up MikroORM:

  • AdminModule.createAdminAsync({ ... }

In your app.module.ts add these imports at the top of the file:

Following this, register AdminJSMikroORM adapter somewhere after your imports:

This will allow you to pass MikroORM models for AdminJS to load. If we use the Owner entity that we used as en example earlier, you should import it into app.module.ts and pass it into resources in your adminJsOptions:

Last updated