Skip to main content
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:

Table Metadata

Each table object contains:
Example:

Column Metadata

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

  1. Database Documentation - Auto-generate schema documentation
  2. Schema Validation - Verify database structure in tests
  3. Migration Safety - Check schema before applying migrations
  4. Dynamic UIs - Build admin interfaces that adapt to schema changes
  5. Code Generation - Generate TypeScript types from database schema
  6. Monitoring - Track schema changes over time
  7. 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