Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(postgres): Enum Support #32

Open
sa2taka opened this issue Jun 23, 2024 · 0 comments
Open

feat(postgres): Enum Support #32

sa2taka opened this issue Jun 23, 2024 · 0 comments

Comments

@sa2taka
Copy link

sa2taka commented Jun 23, 2024

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.

@sa2taka sa2taka changed the title Feature Request: PostgreSQL Enum Support feat(postgres): Enum Support Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant