@directus/schema
Version:
Utility for extracting information about existing DB schema
97 lines (96 loc) • 2.78 kB
TypeScript
import { Knex } from "knex";
//#region src/types/table.d.ts
interface Table {
name: string;
comment?: string | null;
schema?: string;
collation?: string;
engine?: string;
owner?: string;
sql?: string;
catalog?: string;
}
//#endregion
//#region src/types/column.d.ts
interface Column {
name: string;
table: string;
data_type: string;
default_value: string | number | boolean | null;
max_length: number | null;
numeric_precision: number | null;
numeric_scale: number | null;
is_nullable: boolean;
is_unique: boolean;
is_indexed: boolean;
is_primary_key: boolean;
is_generated: boolean;
generation_expression?: string | null;
has_auto_increment: boolean;
foreign_key_table: string | null;
foreign_key_column: string | null;
comment?: string | null;
schema?: string;
foreign_key_schema?: string | null;
}
//#endregion
//#region src/types/foreign-key.d.ts
type ForeignKey = {
table: string;
column: string;
foreign_key_table: string;
foreign_key_column: string;
foreign_key_schema?: string;
constraint_name: null | string;
on_update: null | 'NO ACTION' | 'RESTRICT' | 'CASCADE' | 'SET NULL' | 'SET DEFAULT';
on_delete: null | 'NO ACTION' | 'RESTRICT' | 'CASCADE' | 'SET NULL' | 'SET DEFAULT';
};
//#endregion
//#region src/types/overview.d.ts
type SchemaOverview = {
[table: string]: {
primary: string;
columns: {
[column: string]: {
table_name: string;
column_name: string;
default_value: string | null;
is_nullable: boolean;
is_generated: boolean;
data_type: string;
numeric_precision?: number | null;
numeric_scale?: number | null;
max_length: number | null;
};
};
};
};
//#endregion
//#region src/types/schema-inspector.d.ts
interface SchemaInspector {
knex: Knex;
overview: () => Promise<SchemaOverview>;
tables(): Promise<string[]>;
tableInfo(): Promise<Table[]>;
tableInfo(table: string): Promise<Table>;
hasTable(table: string): Promise<boolean>;
columns(table?: string): Promise<{
table: string;
column: string;
}[]>;
columnInfo(): Promise<Column[]>;
columnInfo(table?: string): Promise<Column[]>;
columnInfo(table: string, column: string): Promise<Column>;
hasColumn(table: string, column: string): Promise<boolean>;
primary(table: string): Promise<string | null>;
foreignKeys(table?: string): Promise<ForeignKey[]>;
withSchema?(schema: string): void;
}
interface SchemaInspectorConstructor {
new (knex: Knex): SchemaInspector;
}
//#endregion
//#region src/index.d.ts
declare const createInspector: (knex: Knex) => SchemaInspector;
//#endregion
export { Column, ForeignKey, SchemaInspector, SchemaInspectorConstructor, SchemaOverview, Table, createInspector };