@adminjs/sql

中文文档
2.2.6发布于 2024/07/10MITTypeScriptES Module近 30 天下载 1.4万

An official AdminJS adapter for SQL databases.

原始使用文档

@adminjs/sql

This is an official AdminJS adapter for SQL databases. It does not require you to use any ORMs, instead you just provide database connection and you model the data sources using AdminJS configuration.

Supported databases:

  • PostgreSQL
  • MySQL
  • more coming soon

This adapter is heavily inspired by wirekang's adminjs-sql which is an unofficial adapter for a MySQL database.

Installation

$ yarn add @adminjs/sql

Usage with Express

The example below shows usage with @adminjs/express. The usage of Adapter class is required to parse your database's schema.

import AdminJS from 'adminjs'
import express from 'express'
import Plugin from '@adminjs/express'
import Adapter, { Database, Resource } from '@adminjs/sql'

AdminJS.registerAdapter({
  Database,
  Resource,
})

const start = async () => {
  const app = express()

  const db = await new Adapter('postgresql', {
    connectionString: '<your database connection string>', //  postgresql://[user]:[password]@[netloc]:[port]/[dbname]
    database: '<your database name>',
  }).init();

  const admin = new AdminJS({
    resources: [
      {
        resource: db.table('users'),
        options: { /* any resource options, rbac, etc */ },
      },
    ],
    // databases: [db] <- you can also provide the DB connection to register all tables at once
  });

  admin.watch()

  const router = Plugin.buildRouter(admin)

  app.use(admin.options.rootPath, router)

  app.listen(8080, () => {
    console.log('app started')
  })
}

start()

Database Relations

Currently only many-to-one relation works out of the box if you specify foreign key constraints in your database. Other relations will require you to make UI/backend customizations. Please see our documentation to learn more.

Enums

As of version 1.0.0 database enums aren't automatically detected and loaded. You can assign them manually in your resource options:

// ...
  const admin = new AdminJS({
    resources: [{
      resource: db.table('users'),
      options: {
        properties: {
          role: {
            availableValues: [
              { label: 'Admin', value: 'ADMIN' },
              { label: 'Client', value: 'CLIENT' },
            ],
          },
        },
      },
    }],
  })
// ...

Timestamps

If your database tables have automatically default-set timestamps (created_at, updated_at, etc) they will be visible in create/edit forms by default. You can hide them in resource options:

// ...
  const admin = new AdminJS({
    resources: [{
      resource: db.table('users'),
      options: {
        properties: {
          created_at: { isVisible: false },
        },
      },
    }],
  })
// ...

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/sql 共发布 15 个版本,首次发布于 2023/01/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.6latest2024/07/102 年前
2.2.52024/06/172 年前
2.2.42024/06/142 年前
2.2.32024/04/112 年前
2.2.22024/03/302 年前
2.2.12023/07/313 年前

其他发布通道:beta → 2.0.0-beta.2。

关于 @adminjs/sql 的常见问题

@adminjs/sql 是什么?

An official AdminJS adapter for SQL databases.

如何安装 @adminjs/sql?

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

@adminjs/sql 支持 TypeScript 吗?

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

@adminjs/sql 支持 ESM 还是 CommonJS?

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

@adminjs/sql 可以商用吗?

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

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

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

@adminjs/sql 还在维护吗?

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