Setup Admin.js with Nest.js and Mongoose

255 Views Asked by At

I'm in the process of configuring Admin.js with Nest.js and Mongoose. Initially, I followed the Admin.js documentation and installed version 7 of Admin.js in a new Nest.js project. I was able to integrate Admin.js into my project using dynamic imports as outlined in the documentation. However, I encountered difficulties when trying to register the Mongoose adapter, which resulted in persistent TypeScript errors.

I decided to downgrade the Admin.js packages to version 5. However, I now find myself encountering a new problem when attempting to add an entity as a resource:

app.module

import * as AdminJSMongoose from '@adminjs/mongoose';
import { AdminModule } from '@adminjs/nestjs';
import { MongooseModule } from '@nestjs/mongoose';
import AdminJS from 'adminjs';

AdminJS.registerAdapter({
  Resource: AdminJSMongoose.Resource,
  Database: AdminJSMongoose.Database,
});

MongooseModule.forRoot(process.env.MONGODB_URL),
AdminModule.createAdminAsync({
  imports: [MongooseModule],
  useFactory: async () => ({
    adminJsOptions: {
      rootPath: '/admin',
      resources: [Question],
    },
  }),
});

question.entity.ts

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

@Schema({ timestamps: true })
export class Question {
  @Prop({ type: String, required: false, unique: true })
  id: string;

  @Prop({ type: String, required: true })
  text: string;

  @Prop({ type: [{ text: String }], required: true })
  options: { text: string }[];

  @Prop({ type: String, required: true })
  correctOptionId: string;

  @Prop({ type: String })
  image: string;

  @Prop({ type: String, required: true })
  language: string;
}

export type QuestionDocument = Question & Document;

export const QuestionSchema = SchemaFactory.createForClass(Question);

The error message I'm encountering is: "NoResourceAdapterError: There are no adapters supporting one of the resource you provided."

I would appreciate guidance on resolving this issue and successfully configuring Admin.js with Mongoose in my Nest.js application.

1

There are 1 best solutions below

0
On

im not sure about mongoose, but in TypeORM, if im making an entity, it must extend the BaseEntity, otherwise i will get the same error.