Skip to content

Feature Request: Support Column Comments in PostgresIntrospector #49

@MasaGon

Description

@MasaGon

Feature Request: Support Column Comments in PostgresIntrospector

Summary

The PostgresIntrospector in kysely-data-api does not retrieve column comments, while the official Kysely PostgresIntrospector (v0.27+) does support this feature using col_description().

Current Behavior

The getTables() method in kysely-data-api/src/postgres-introspector.ts does not include column comments in the returned ColumnMetadata.

Current implementation:

.select([
  "a.attname as column",
  "a.attnotnull as not_null",
  "a.atthasdef as has_default",
  "c.relname as table",
  // ... other fields
])

Expected Behavior

Column comments should be retrieved, matching the behavior of Kysely's official PostgresIntrospector.

Kysely's implementation (v0.27+):

.select([
  // ... other fields
  sql<string | null>`col_description(a.attrelid, a.attnum)`.as('column_description'),
])

Then in #parseTableMetadata:

comment: it.column_description || undefined,

Use Case

This is essential for code generation tools (like kysely-codegen) that generate TypeScript interfaces with JSDoc comments from database schema comments.

Example workflow:

  1. Add comment in migration: COMMENT ON COLUMN users.email IS 'User email address'
  2. Run introspection
  3. Generate types with comments for better IDE autocomplete

Proposed Solution

Add col_description() to the SELECT clause in postgres-introspector.ts:

.select([
  "a.attname as column",
  "a.attnotnull as not_null",
  "a.atthasdef as has_default",
  "c.relname as table",
  sql<string>`case when c.relkind = 'v' then true else false end`.as("is_view"),
  "ns.nspname as schema",
  "typ.typname as type",
  "dtns.nspname as type_schema",
  sql<string | null>`col_description(a.attrelid, a.attnum)`.as('column_description'),  // ← Add this
  
  // auto incrementing detection
  this.#db
    .selectFrom("pg_class")
    .select(sql`true`.as("auto_incrementing"))
    .whereRef("relnamespace", "=", "c.relnamespace")
    .where("relkind", "=", "S")
    .where("relname", "=", sql`c.relname || '_' || a.attname || '_seq'`)
    .as("auto_incrementing"),
])

And update #parseTableMetadata:

table.columns.push(
  Object.freeze({
    name: it.column,
    dataType: it.type,
    dataTypeSchema: it.type_schema,
    isNullable: !it.not_null,
    isAutoIncrementing: !!it.auto_incrementing,
    hasDefaultValue: it.has_default,
    comment: it.column_description || undefined,  // ← Add this
  })
);

References

Additional Context

I'm currently working around this limitation by manually querying pg_description, but native support would be cleaner and more consistent with Kysely's official dialects.

Would you accept a PR for this feature? I'm happy to contribute if this aligns with the project's direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions