@directus/schema

中文文档
14.0.2发布于 2026/08/19SEE LICENSE IN licenseTypeScriptES Module构建来源已验证近 30 天下载 29.5万

用于提取关于现有 DB 方案的信息的工具

中文使用文档
文档目录8 节
  1. 使用量
  2. 实例
  3. API 密码
  4. 表格
  5. 列数
  6. 杂项.
  7. 捐款
  8. 测试

@ directus/schema (英语)

用于提取数据库计划信息的工具a

使用量

软件包的初始化方式是,通过一个Knex的例子:

import knex from 'knex';
import schema from '@directus/schema';

const database = knex({
    client: 'mysql',
    connection: {
        host: '127.0.0.1',
        user: 'your_database_user',
        password: 'your_database_password',
        database: 'myapp_test',
        charset: 'utf8',
    },
});

const inspector = schema(database);

export default inspector;

实例

import inspector from './inspector';

async function logTables() {
    const tables = await inspector.tables();
    console.log(tables);
}

API 密码

注意: MySQL 不支持 schema 参数,因为计划和数据库在MySQL中含糊不清.

注2:一些数据库类型可能比其他类型稍多一些。 查看特定文件的类型 概述从司机到司机的预期.

注3: MSSQL 不支持对表格或列的评论

表格

tables(): Promise<string[]>

检索当前数据库中的所有表格.

await inspector.tables();
// => ['articles', 'images', 'reviews']

tableInfo(table?: string): Promise<Table | Table[]>

为给定表格获取表格信息, 如果没有指定表格, 则获取所有表格

await inspector.tableInfo('articles');
// => {
//   name: 'articles',
//   schema: 'project',
//   comment: 'Informational blog posts'
// }

await inspector.tableInfo();
// => [
//   {
//     name: 'articles',
//     schema: 'project',
//     comment: 'Informational blog posts'
//   },
//   { ... },
//   { ... }
// ]

hasTable(table: string): Promise<boolean>

检查当前数据库中是否存在表格 .

await inspector.hasTable('articles');
// => true

列数

columns(table?: string): Promise<{ table: string, column: string }[]>

调取某个表格中的所有列, 如果没有指定表格则调取所有列

await inspector.columns();
// => [
//   {
//     "table": "articles",
//     "column": "id"
//   },
//   {
//     "table": "articles",
//     "column": "title"
//   },
//   {
//     "table": "images",
//     "column": "id"
//   }
// ]

await inspector.columns('articles');
// => [
//   {
//     "table": "articles",
//     "column": "id"
//   },
//   {
//     "table": "articles",
//     "column": "title"
//   }
// ]

columnInfo(table?: string, column?: string): Promise<Column[] | Column>

从给定的表格中获取所有列。 返回所有列,如果 table 参数未定义.

await inspector.columnInfo('articles');
// => [
//   {
//     name: "id",
//     table: "articles",
//     type: "VARCHAR",
//     defaultValue: null,
//     maxLength: null,
//     isNullable: false,
//     isPrimaryKey: true,
//     hasAutoIncrement: true,
//     foreignKeyColumn: null,
//     foreignKeyTable: null,
//     comment: "Primary key for the articles collection"
//   },
//   { ... },
//   { ... }
// ]

await inspector.columnInfo('articles', 'id');
// => {
//   name: "id",
//   table: "articles",
//   type: "VARCHAR",
//   defaultValue: null,
//   maxLength: null,
//   isNullable: false,
//   isPrimaryKey: true,
//   hasAutoIncrement: true,
//   foreignKeyColumn: null,
//   foreignKeyTable: null,
//   comment: "Primary key for the articles collection"
// }

primary(table: string): Promise<string>

获取给定表格的主键列

await inspector.primary('articles');
// => "id"

杂项.

withSchema(schema: string): void

在 MySQL 不支持

设定要使用的策略 。 注:这是由检查员实例决定的,只需一次:

inspector.withSchema('my-schema');

捐款

欢迎拉动请求。 对于重大变动,请先开出一个问题来讨论你想要改变什么.

请确保酌情更新测试.

测试

首个开始插头容器 :

$ docker compose up

然后运行测试:

$ npm test

标准摩卡过滤器(grep)可以使用:

$ npm test -- -g '.tableInfo'

版本与发布记录

@directus/schema 共发布 182 个版本,首次发布于 2020/11/10;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
14.0.2latest2026/08/192 个月前
14.0.12026/07/303 个月前
14.0.02026/06/115 个月前
14.0.0-rc.0canary2026/05/295 个月前
13.0.82026/04/166 个月前
13.0.72026/03/317 个月前

关于 @directus/schema 的常见问题

@directus/schema 是什么?

用于提取关于现有 DB 方案的信息的工具

如何安装 @directus/schema?

在项目目录运行 npm install @directus/schema,或使用 pnpm add / yarn add / bun add。

@directus/schema 支持 TypeScript 吗?

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

@directus/schema 支持 ESM 还是 CommonJS?

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

@directus/schema 可以商用吗?

@directus/schema 使用 SEE LICENSE IN license 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@directus/schema 的下载量有多少?

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

@directus/schema 还在维护吗?

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