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.
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
app.ts
// ... other importsimport { MikroORM } from'@mikro-orm/core'import*as AdminJSMikroORM from'@adminjs/mikroorm'import { Owner } from'./owner.entity.js'AdminJS.registerAdapter({ Resource:AdminJSMikroORM.Resource, Database:AdminJSMikroORM.Database,})// Note: `config` is your MikroORM configuration as described in it's docsconstconfig= { entities: [Owner], dbName:'adminjs', type:'postgresql', clientUrl:'postgres://adminjs:adminjs@localhost:5435/adminjs',}// ... other codeconststart=async () => {constorm=awaitMikroORM.init(config)constadminOptions= {// We pass Owner to `resources` resources: [{ resource: { model: Owner, orm }, options: {} }], }// Please note that some plugins don't need you to create AdminJS instance manually,// instead you would just pass `adminOptions` into the plugin directly,// an example would be "@adminjs/hapi"constadmin=newAdminJS(adminOptions)// ... other code}start()
Your app.module.ts should have imports option which contains:
MikroOrmModule.forRoot(...) to set up MikroORM:
// Note: this is a default configuration from Nest.js documentationMikroOrmModule.forRoot({ entities: ['./dist/entities'], entitiesTs: ['./src/entities'], dbName:'my-db-name.sqlite3', type:'sqlite',})
AdminModule.createAdminAsync({ ... }
In your app.module.ts add these imports at the top of the file:
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: