Skip to content

feat(postgres): Enum Support  #32

Open
@sa2taka

Description

@sa2taka

First off, I want to express my gratitude for maintaining this fantastic tool.

Motivation

We would like to represent PostgreSQL enums as literal unions in TypeScript. This improvement would enhance type safety and effectiveness by properly mapping PostgreSQL enums to TypeScript literal union types

Go version of sqlc already has enum type support.

Example

A sample implementation was accidentally submitted as a Pull Request. Although it was closed, it serves as a starting point for discussion. You can view the PR here: #31.

-- schema.sql
CREATE TYPE authors_role AS ENUM ('admin', 'guest');

CREATE TABLE authors (
  id BIGSERIAL PRIMARY KEY,
  name text NOT NULL,
  bio text,
  role authors_role
);

-- query.sql
-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
export const getAuthorQuery = `-- name: GetAuthor :one
SELECT id, name, bio, role FROM authors
WHERE id = $1 LIMIT 1`;

export interface GetAuthorArgs {
    id: string;
    name: string;
    bio: string | null;
    role: "admin" | "guest" | null;
}

Improvement Points

  • The current implementation doesn't support mapping to TypeScript's enum type.
  • The current implementation doesn't create explicit type aliases.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions