Kysely’s DatabaseIntrospector provides runtime access to database metadata. This is useful for building dynamic queries, generating documentation, or implementing database management tools.
Accessing the Introspector
The introspector is available on the Kysely instance:
Getting Schemas
Retrieve all schemas in the database (PostgreSQL, MSSQL):
Example output:
Getting Tables
Retrieve metadata for all tables and views:
Including Internal Tables
By default, Kysely’s internal migration tables are excluded. To include them:
Each table object contains:
Example:
Each column object provides detailed information:
Data Type Variations
The dataType field returns the database’s native type name, which varies across dialects even for the same migration.
Example: An integer column definition produces:
- PostgreSQL:
int4
- MySQL:
int
- SQLite:
INTEGER
Practical Examples
Generating Table Documentation
Validating Expected Schema
Finding Tables by Pattern
Detecting Schema Changes
Building a Dynamic Query Builder
Legacy Method
The getMetadata() method is deprecated. Use getTables() instead.
Use Cases
- Database Documentation - Auto-generate schema documentation
- Schema Validation - Verify database structure in tests
- Migration Safety - Check schema before applying migrations
- Dynamic UIs - Build admin interfaces that adapt to schema changes
- Code Generation - Generate TypeScript types from database schema
- Monitoring - Track schema changes over time
- Multi-tenancy - Introspect tenant-specific schemas
- Introspection queries can be slow on large databases
- Cache results when possible instead of repeatedly calling introspection methods
- Filter results early if you only need specific tables