Skip to main content

Schema Introspection (Experimental)

warning

This feature is in early development. The API may change in future releases. Currently only basic schema discovery is supported.

Overview

Schema introspection allows you to inspect database table structure programmatically at runtime. This is useful for building dynamic tools, migration systems, or adaptive ORM features that respond to the actual database schema.

sql.introspectSchema()

async introspectSchema(): Promise<IntrospectedSchema[]>

Queries information_schema.tables for base tables and returns schema objects with dialect and tables.

Returns: Promise<IntrospectedSchema[]>

Example:

const schemas = await sql.introspectSchema();
for (const schema of schemas) {
console.log(`Dialect: ${schema.dialect}`);
for (const table of schema.tables) {
console.log(` Table: ${table.name}`);
console.log(` Columns: ${table.columns.length}`);
}
}

Introspection Types

IntrospectedSchema

interface IntrospectedSchema {
dialect: string;
tables: IntrospectedTable[];
}
PropertyTypeDescription
dialectstringThe database dialect (e.g., "postgres", "mysql")
tablesIntrospectedTable[]Array of tables in the schema

IntrospectedTable

interface IntrospectedTable {
name: string;
columns: IntrospectedColumn[];
}
PropertyTypeDescription
namestringName of the table
columnsIntrospectedColumn[]Array of columns in the table

IntrospectedColumn

interface IntrospectedColumn {
name: string;
type: string;
nullable: boolean;
default?: string | null;
length?: number;
isPrimaryKey?: boolean;
isForeignKey?: boolean;
references?: { table: string; column: string };
}
PropertyTypeDescription
namestringColumn name
typestringData type
nullablebooleanWhether the column allows null
defaultstring | nullDefault value (optional)
lengthnumberLength/precision (optional)
isPrimaryKeybooleanWhether this is a primary key (optional)
isForeignKeybooleanWhether this is a foreign key (optional)
references{ table: string; column: string }Foreign key reference (optional)

Current Limitations

warning

The following limitations apply to the current implementation:

  • Only fetches table names, not full column metadata
  • columns arrays are currently empty
  • No support for views, stored procedures, or triggers
  • API may change significantly in future versions

For full column metadata, use sql.getTableSchema(table) instead.