@directus/schema package, offering a unified interface for inspecting and managing database schemas across all supported database systems.
Schema Inspector
The Schema Inspector provides a database-agnostic API for querying database structure information. It abstracts away vendor-specific SQL queries and presents a consistent interface regardless of the underlying database.Creating an Inspector
- PostgreSQL -
PostgresSchemaInspector - MySQL/MariaDB -
MySQLSchemaInspector - SQLite -
SqliteSchemaInspector - MS SQL Server -
MSSQLSchemaInspector - Oracle Database -
OracleDBSchemaInspector - CockroachDB -
CockroachDBSchemaInspector
Inspecting Tables
List All Tables
Get Table Information
Check if Table Exists
Inspecting Columns
List Columns in a Table
Get Column Information
Check if Column Exists
Column Metadata
The column information object includes comprehensive metadata:Primary Keys
Get the primary key column for a table:null if no primary key exists or if the table has a composite primary key.
Foreign Keys
Retrieve foreign key relationships:Schema Overview
Theoverview() method provides a complete snapshot of your database schema in a single call:
- Generating complete schema documentation
- Comparing schemas across environments
- Building data modeling tools
- Schema validation and testing
Database-Specific Features
PostgreSQL
Search Path Support
Set the schema search path:PostGIS Support
When PostGIS is installed, geometry columns are automatically detected:MySQL/MariaDB
Boolean Type Detection
MySQL’stinyint(1) is automatically mapped to boolean:
Engine and Collation Information
SQLite
Foreign Keys
SQLite foreign keys are automatically enabled:Oracle Database
Date Format Enforcement
Directus enforces ISO standard date/time formats:CockroachDB
Serial Normalization
CockroachDB automatically configures serial handling:Schema Builder
The@directus/schema-builder package provides a fluent API for building schema definitions programmatically:
Default Values
Default values are parsed and normalized across database vendors:PostgreSQL
MySQL
SQLite
Best Practices
Use the Inspector for Schema Queries
Instead of writing database-specific queries:Cache Schema Information
Schema information rarely changes. Cache the overview for better performance:Handle Database-Specific Types
Be aware of type variations:Validate Schema Changes
Before applying migrations, validate against current schema:Schema Validation
Directus validates several aspects of your database schema:Connection Validation
Ensures database connectivity on startup.Installation Check
Verifies Directus system tables exist:Extension Validation
Validates optional database extensions:Next Steps
- Learn about Database Migrations
- Explore Supported Databases