# @directus/schema 中文文档

规范地址：https://npmcn.com/package/%40directus/schema/

版本：14\.0\.2
npm 元数据更新时间：2026-08-18T17:09:17.179Z
中文译文更新时间：2026-11-12T10:50:11.912Z
软件包许可：SEE LICENSE IN license

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40directus/schema
原文对照：https://npmcn.com/package/%40directus/schema/?lang=original
项目主页：https://directus.com/
源码仓库：https://github.com/directus/directus

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：79.1 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2020-11-10T14:41:52.019Z
- 最新版本发布：2026-08-18T17:09:16.811Z
- 近 30 天下载量：294921（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：182（近 12 个月 8 个）

## 中文简介

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

## 安装

```sh
npm install @directus/schema
```

也可以使用：`pnpm add @directus/schema`、`yarn add @directus/schema`、`bun add @directus/schema`

## README

# @ directus/schema (英语)

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

## 使用量

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

```ts
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;
```

## 实例

```ts
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[]>`

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

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

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

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

```ts
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>`

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

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

### 列数

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

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

```ts
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` 参数未定义.

```ts
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>`

获取给定表格的主键列

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

### 杂项.

#### `withSchema(schema: string): void`

_在 MySQL 不支持_

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

```ts
inspector.withSchema('my-schema');
```

## 捐款

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

请确保酌情更新测试.

### 测试

首个开始插头容器 :

```shell
$ docker compose up
```

然后运行测试:

```shell
$ npm test
```

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

```shell
$ npm test -- -g '.tableInfo'
```

## 常见问题

### @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 和提交记录判断。

