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):Getting Tables
Retrieve metadata for all tables and views:Including Internal Tables
By default, Kysely’s internal migration tables are excluded. To include them:Table Metadata
Each table object contains:Column Metadata
Each column object provides detailed information:Data Type Variations
Example: Aninteger 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
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
Performance Considerations
- 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