@adminjs/typeorm

中文文档
5.0.1发布于 2024/02/14MITTypeScriptES Module零依赖近 30 天下载 5.7万

TypeORM adapter for AdminJS

原始使用文档
文档目录9 节
  1. adminjs-typeorm
  2. Usage
  3. Example
  4. ManyToOne
  5. Contribution
  6. Running the example app
  7. Pull request
  8. License
  9. About rst.software

adminjs-typeorm

This is an official AdminJS adapter which integrates TypeORM into AdminJS. (originally forked from Arteha/admin-bro-typeorm)

Installation: yarn add @adminjs/typeorm

Usage

The plugin can be registered using standard AdminJS.registerAdapter method.

import { Database, Resource } from '@adminjs/typeorm'
import AdminJS from 'adminjs'

AdminJS.registerAdapter({ Database, Resource });

// Optional: if you use class-validator you have to inject this to resource.
import { validate } from 'class-validator'
Resource.validate = validate

Example

import {
  BaseEntity,
  Entity, PrimaryGeneratedColumn, Column,
  ManyToOne,
  RelationId
} from 'typeorm'
import { MyDataSource } from './my-data-source'
import * as express from 'express'
import { Database, Resource } from '@adminjs/typeorm'
import { validate } from 'class-validator'

import AdminJS from 'adminjs'
import * as AdminJSExpress from '@adminjs/express'

Resource.validate = validate
AdminJS.registerAdapter({ Database, Resource })

@Entity()
export class Person extends BaseEntity
{
  @PrimaryGeneratedColumn()
  public id: number;

  @Column({type: 'varchar'})
  public firstName: string;

  @Column({type: 'varchar'})
  public lastName: string;

  @ManyToOne(type => CarDealer, carDealer => carDealer.cars)
  organization: Organization;

  // in order be able to fetch resources in adminjs - we have to have id available
  @RelationId((person: Person) => person.organization)
  organizationId: number;

  // For fancy clickable relation links:
  public toString(): string
  {
    return `${firstName} ${lastName}`;
  }
}

( async () =>
{
  await MyDataSource.initialize();

  const adminJs = new AdminJS({
    // databases: [MyDataSource],
    resources: [
      { resource: Person, options: { parent: { name: 'foobar' } } }
      ],
    rootPath: '/admin',
  })

  const app = express()
  const router = AdminJSExpress.buildRouter(adminJs)
  app.use(adminJs.options.rootPath, router)
  app.listen(3000)
})()

ManyToOne

Admin supports ManyToOne relationship but you also have to define @RealationId as stated in the example above.

Contribution

Running the example app

If you want to set this up locally this is the suggested process:

  1. fork the repo
  2. Install dependencies
yarn install
  1. register this package as a (linked package)[https://classic.yarnpkg.com/en/docs/cli/link/]
yarn link
  1. Setup example app

Install all dependencies and use previously linked version of @adminjs/typeorm.

cd example-app
yarn install
yarn link @adminjs/typeorm

Optionally you might want to link your local version of adminjs package

  1. Make sure you have all the envs set (which are defined in example-app/ormconfig.js)

  2. Build the package in watch mode

(in the root folder)

yarn dev
  1. run the app in the dev mode
cd example-app
yarn dev

Pull request

Before you make a PR make sure all tests pass and your code wont causes linter errors. You can do this by running:

yarn lint
yarn test

or with proper envs: POSTGRES_USER=yourtestuser POSTGRES_DATABASE="database_test" yarn test

License

AdminJS is copyrighted © 2023 rst.software. It is a free software, and may be redistributed under the terms specified in the LICENSE file.

About rst.software

We’re an open, friendly team that helps clients from all over the world to transform their businesses and create astonishing products.

  • We are available for hire.
  • If you want to work for us - check out the career page.

版本与发布记录

@adminjs/typeorm 共发布 15 个版本,首次发布于 2021/06/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.1latest2024/02/142 年前
5.0.02023/04/183 年前
5.0.0-beta.1beta2023/03/203 年前
4.0.02022/07/134 年前
3.0.22022/06/244 年前
3.0.12022/06/234 年前

关于 @adminjs/typeorm 的常见问题

@adminjs/typeorm 是什么?

TypeORM adapter for AdminJS

如何安装 @adminjs/typeorm?

在项目目录运行 npm install @adminjs/typeorm,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:adminjs(^7.0.0)、typeorm(~0.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@adminjs/typeorm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adminjs/typeorm 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adminjs/typeorm 可以商用吗?

@adminjs/typeorm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adminjs/typeorm 的下载量有多少?

根据 npm 官方下载统计,@adminjs/typeorm 在 2026/10/05 – 2026/11/03 这 30 天内被下载 56,727 次,日均约 1,891 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adminjs/typeorm 还在维护吗?

最新版本 5.0.1 发布于 2024/02/14(2 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。