LogoLogo
Join our community
  • AdminJS
  • Contribute
  • Demo
  • Addons Marketplace
  • Installation
    • Getting started
    • Plugins
      • Adonis
      • Express
      • Nest
      • Fastify
      • Hapi
      • Koa
      • Community Plugins
        • FeathersJS
        • AdonisJS
      • Matrix
    • Adapters
      • TypeORM
      • Sequelize
      • Prisma
      • MikroORM
      • Objection
      • SQL
      • Mongoose
      • Community Adapters
        • AdonisJS
    • What's new in v7?
    • Migration Guide v7
  • Basics
    • Resource
    • Action
    • Property
    • Features
      • Relations
      • Upload
      • Logger
      • Import & Export
      • Password
      • Leaflet Maps
      • Writing your own features
    • API
      • List
      • New
      • Search
      • Show
      • Edit
      • Delete
      • Bulk Delete
    • Themes
    • Authentication
      • FirebaseAuthProvider
      • MatrixAuthProvider
  • How to write an addon?
  • UI Customization
    • Writing your own Components
    • Overwriting CSS styles
    • Dashboard customization
    • Changing the form view
    • Storybook
  • Tutorials
    • Role-Based Access Control
    • Internationalization (i18n)
    • Content Management System
    • Custom components library
    • Custom component internationalization
  • FAQ
    • PDF Generator
    • Charts
    • Forgot Password
  • ⚠️Legacy documentation
Powered by GitBook
On this page
  • Standard
  • Nest.js
  1. Installation
  2. Adapters

TypeORM

@adminjs/typeorm

PreviousAdaptersNextSequelize

Last updated 2 years ago

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

This guide will assume you have set up TypeORM using it's or .

@adminjs/typeorm currently only supports entities that extend BaseEntity as shown on the example below:

organization.entity.ts
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm'

@Entity({ name: 'organizations' })
export class Organization extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id: number;

  @Column()
  public name: string;
}

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

Standard

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

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

  • You must import the data source and initialize it

  • You must import AdminJSTypeorm adapter and register it

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

app.ts
// ... other imports
import * as AdminJSTypeorm from '@adminjs/typeorm'

import dataSource from './path/to/your/datasource.js'
import { Organization } from './organization.entity.js'

AdminJS.registerAdapter({
  Resource: AdminJSTypeorm.Resource,
  Database: AdminJSTypeorm.Database,
})

// ... other code
const start = async () => {
  // Make sure you initialize the data source before you create your AdminJS instance
  await dataSource.initialize()
  const adminOptions = {
    // We pass Organization to `resources`
    resources: [Organization],
  }
  // 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"
  const admin = new AdminJS(adminOptions)
  // ... other code
}

start()

Nest.js

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

  • TypeOrmModule.forRoot(params) to set up TypeORM

  • AdminModule.createAdminAsync({ ... }

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

app.module.ts
import * as AdminJSTypeorm from '@adminjs/typeorm'
import AdminJS from 'adminjs'

Following this, register AdminJSTypeorm adapter somewhere after your imports:

app.module.ts
AdminJS.registerAdapter({
  Resource: AdminJSTypeorm.Resource,
  Database: AdminJSTypeorm.Database,
})

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

app.module.ts
// ... other imports
import { Organization } from './organization.entity.js'
// ... other code
AdminModule.createAdminAsync({
  useFactory: () => ({
    adminJsOptions: {
      rootPath: '/admin',
      resources: [Organization],
    },
  }),
}),
// ... other code

Make sure you have set up your app.module.ts according to and you have followed as well.

Plugins
Getting started
documentation
Nest.js documentation
Plugins
Nest.js documentation
Nest.js plugin tutorial
Page cover image