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: Option to use undefined instead of null for optional arguments / nullable columns #33

Open
mecab opened this issue Jun 26, 2024 · 0 comments

Comments

@mecab
Copy link

mecab commented Jun 26, 2024

Hi, I find sqlc and sqlc-gen-typescirpt are amazing!

Currently, the null type is used to indicate optional parameters, so the following query

-- name: CreateBook :one
INSERT INTO book (title, description)
VALUES (@title, COALESCE(@description, '':text))
RETURNING *;

generates the following argument type

export interface CreateBookArgs {
    title: string;
    description: string | null;
}
export async function createBook(client: Client, args: CreateBookArgs): Promise<CreateBookRow | null> {

which does not allow us to omit the empty fields so you have to always specify null.

createBook(client, { title: "a book" }); // error

createBook(client, { title: "a book", description: null }); 

I think it would be more convenient if there's an option to use undefined instead.

export interface CreateBookArgs {
    title: string;
    description: string | undefined; // or description?: string
}

With this type, you can write

createBook(client, { title: "a book" });

The option should also be useful for the users who use such request parser that coalesces empty inputs as undefined.

The same applies to the returning type (potentially empty row and potentially empty fields).

For implementation, I find pg handles undefined as null so it is purely a type issue, but I have no idea about other engines for now.

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