* Bump knex-schema-inspector * Fix cli role name attr * Update relation type * Restructure relations * Restructure relations table * Update api type for relation record * Fetch relations in new structure * Update schema-inspector * Use new relations schema structure in api * Update relations GETters * Add default value to one deselect * Add create relationship on existing field * Add updating existing relationship * Add delete relations * Add relations query resolver * Add graphql mutations for relations * Fix reading from wrong name * Fix wrong method name * No idea why this flip flops every install * Update relation type * Accept null in use-collection composable * Use new relations structure in translations * Use new relations structure in new-collection * Start updating field detail store * Renames for new relations structure * Silently ignore passed collection/field in relation update * Fix setting pk field in m2o relational setup * Small tweaks in o2m setup * Fix m2m setup * Tweak m2o setup * Fix m2a setup * Allow null for related collection (m2a) * Fix languages code name * Fix migration default value * Fix relational cleanup in collections/fields * Fix transaction problem in field delete * Fix inserting relational o2m items * Don't execute updateByQuery on empty item set Fixes #5710, fixes #5070 * Show referential action input on m2o * Finish language for m2o * Show triggers config on o2m * Delete items on one_deselect_item delete * Fix naming, show relational trigger config on m2m * Tweak language, add setup to m2a * Fix linter warnings * Add trigger setup for translations * fix Edit non-schema relationship issue * Sync existing on_delete triggers in o2m setup * Add migration to setup foreign key constraints * Update illegal FK values before setting constraint * Fix MySQL unsigned vs not-unsigned in FK creation * Use pretty names for labels in relational triggers * Prefix auto-junction when system table Fixes #5493 * Add system foreign key triggers Fixes #5749 * Update docs
@directus/schema
Utility for extracting information about the database schema
Usage
The package is initialized by passing it an instance of 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;
Examples
import inspector from './inspector';
async function logTables() {
const tables = await inspector.tables();
console.log(tables);
}
API
Note: MySQL doesn't support the schema parameter, as schema and database are ambiguous in MySQL.
Note 2: Some database types might return slightly more information than others. See the type files for a specific overview what to expect from driver to driver.
Note 3: MSSQL doesn't support comment for either tables or columns
Tables
tables(): Promise<string[]>
Retrieve all tables in the current database.
await inspector.tables();
// => ['articles', 'images', 'reviews']
tableInfo(table?: string): Promise<Table | Table[]>
Retrieve the table info for the given table, or all tables if no table is specified
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>
Check if a table exists in the current database.
await inspector.hasTable('articles');
// => true
Columns
columns(table?: string): Promise<{ table: string, column: string }[]>
Retrieve all columns in a given table, or all columns if no table is specified
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>
Retrieve all columns from a given table. Returns all columns if table parameter is undefined.
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>
Retrieve the primary key column for a given table
await inspector.primary('articles');
// => "id"
Misc.
withSchema(schema: string): void
Not supported in MySQL
Set the schema to use. Note: this is set on the inspector instance and only has to be done once:
inspector.withSchema('my-schema');
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Tests
First start docker containers:
$ docker-compose up -d
Then run tests:
$ npm test
Standard mocha filter (grep) can be used:
$ npm test -- -g '.tableInfo'