AdminJS是一个自动管理界面,可以插入到您的应用程序中。作为开发人员,您提供数据库模型(例如帖子、评论、商店、产品或您的应用程序使用的任何其他内容),AdminJS 生成允许您(或其他受信任的用户)管理内容的 UI。

admin-bro : https://www.npmjs.com/package/admin-bro

暂停使用

由于AdminJs从版本7开始,仅支持 ESM,并且不再使用 CommonJS 语法。但是因为 NestJS 自 2023/04 起不支持 ESM,故暂时先不用了,后面换回express的项目在更新。

安装


npm i adminjs @adminjs/nestjs

npm i @adminjs/express express-session express-formidable

配置

临时方法

main.ts

import { NestFactory, HttpAdapterHost } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const AdminJS = await import("adminjs");
  const AdminJSExpress = await import("@adminjs/express");
  const expressApp = app.get(HttpAdapterHost).httpAdapter;
  const admin = new AdminJS.default({});
  expressApp.use(
    admin.options.rootPath,
    AdminJSExpress.default.buildRouter(admin)
  );
  await app.listen(3000);
}
bootstrap();

或者

app.module.ts

import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
const DEFAULT_ADMIN = {
  email: "admin@example.com",
  password: "password",
};
const authenticate = async (email: string, password: string) => {
  if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) {
    return Promise.resolve(DEFAULT_ADMIN);
  }
  return null;
};
// 动态引入AdminJs
@Module({
  imports: [
    // AdminJS version 7 is ESM-only. In order to import it, you have to use dynamic imports.
    import("@adminjs/nestjs").then(({ AdminModule }) =>
      AdminModule.createAdminAsync({
        useFactory: () => ({
          adminJsOptions: {
            rootPath: "/admin",
            resources: [],
          },
          auth: {
            authenticate,
            cookieName: "adminjs",
            cookiePassword: "secret",
          },
          sessionOptions: {
            resave: true,
            saveUninitialized: true,
            secret: "secret",
          },
        }),
      })
    ),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

效果

官方演示站点:https://adminjs-demo.herokuapp.com/app/login

Email: admin@example.com
Password: password