AdminJS Fastify Integration with Mongoose - "NoResourceAdapterError"

89 Views Asked by At

I am trying to integrate AdminJS with Fastify and Mongoose in my Node.js application, but I am facing an issue with the "NoResourceAdapterError."

Here's my code:

app.ts

import AdminJSFastify from '@adminjs/fastify'
import AdminJS from 'adminjs'
import Fastify from 'fastify'
import mongoose from 'mongoose'
import * as AdminJSMongoose from '@adminjs/mongoose'
import { Category } from './model/category.entity'

const PORT = 3000

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

const start = async () => {
  await mongoose.connect(`MONGO_URI`)
  const app = Fastify()
  const admin = new AdminJS({
    databases: [],
    rootPath: '/admin'
  })

  const adminOptions = {
    // We pass Category to `resources`
    resources: [Category],
  }

  await AdminJSFastify.buildRouter(
    admin,
    app,
  )

  app.listen({ port: PORT }, (err, addr) => {
    if (err) {
      console.error(err)
    } else {
      console.log(`AdminJS started on http://localhost:${PORT}${admin.options.rootPath}`)
    }
  })
}

start()

category.entity.ts

import { model, Schema, Types } from 'mongoose'

export interface ICategory {
  title: string;
}

export const CategorySchema = new Schema<ICategory>(
  {
    title: { type: 'String', required: true },
  },
  { timestamps: true },
)

export const Category = model<ICategory>('Category', CategorySchema);

esbuild.mjs


import * as esbuild from "esbuild";

await esbuild.build({
  entryPoints: ["app.ts"],
  bundle: true,
  platform: "node",
  target: ["esnext"],
  outfile: "bundle/out.mjs",
  format: "esm",
  outExtension: {
    ".js": ".mjs",
  },
  banner: {
    js: "import { createRequire } from 'module';const require = createRequire(import.meta.url);",
  },
  minify: true,
  external: [
    "fsevents",
    "nock",
    "aws-sdk",
    "mock-aws-s3",
    "adminjs",
    "@adminjs/fastify",
    "bcrypt",
    "crypto",
    "@adminjs/mongoose",
  ],
});

package.json

{
  "name": "adminjs-test",
  "version": "1.0.0",
  "main": "index.js",
  "author": "kund4n",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "dev": "NODE_ENV=development tsx watch app.ts"
  },
  "dependencies": {
    "@adminjs/fastify": "^4.0.1",
    "@adminjs/mongoose": "^4.0.0",
    "@adminjs/typeorm": "^5.0.0",
    "adminjs": "^7.2.2",
    "mongoose": "^7.6.0",
    "tsx": "^3.13.0",
    "typeorm": "^0.3.17"
  }
}

When I run using yarn dev its working fine. but when I build it by using esbuild node esbuild.mjs && node bundle/out.mjs it thow an following error.

throw new NoResourceAdapterError(resourceObject);
              ^

NoResourceAdapterError: There are no adapters supporting one of the resource you provided

I tried some solution which are provided by adminjs github page and StackOverflow but both not working for me.

0

There are 0 best solutions below