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/prisma as described in Getting started section.
Your app.module.ts should have imports option which contains:
AdminModule.createAdminAsync({ ... }
In your app.module.ts add these imports at the top of the file:
app.module.ts
import { Database, Resource, getModelByName } from '@adminjs/prisma'
import AdminJS from 'adminjs'
import { PrismaService } from './prisma.service.js' // PrismaService from Nest.js documentation
Following this, register AdminJSPrisma adapter somewhere after your imports:
app.module.ts
AdminJS.registerAdapter({ Database, Resource })
This will allow you to pass Prisma models for AdminJS to load. If we use the Publisher 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 { Category } from './category.entity.js'
// ... other code
AdminModule.createAdminAsync({
useFactory: () => {
// Note: Feel free to contribute to this documentation if you find a Nest-way of
// injecting PrismaService into AdminJS module
const prisma = new PrismaService()
return {
adminJsOptions: {
rootPath: '/admin',
resources: [{
resource: { model: getModelByName('Post'), client: prisma },
options: {},
}],
},
}
}
}),
// ... other code
Custom client module
In case your generated client is not under the default path, you can pass clientModule to each resource's configuration:
// other imports
// your custom prisma module
import PrismaModule from '../prisma/client-prisma/index.js';
// ...
const prisma = new PrismaModule.PrismaClient();
// ...
// Notice `clientModule` per resource
const admin = new AdminJS({
resources: [{
resource: {
model: getModelByName('Post', PrismaModule),
client: prisma,
clientModule: PrismaModule,
},
}],
});