@ 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'