question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] insert/update params type nullability

See original GitHub issue

currently, input types for insert and update queries (the ones in SET column_name = :param) are always defined as param: Type | null | void.

ideally, pgtyped should infer nullability. but you said in #118 that can’t be done easily.

you could implement a new syntax for explicitly specifying nullability and generate nullable types accordingly, e.g:

  • :param -> current behaviour (no breaking changes), emit warning if ambiguous (possibly behind a flag)
  • :?param or :param? -> force nullable type
  • :!param or :param! -> force non nullable type

also, when you generate properties as name: Type | null | void, parameters are required by TS (have to explicitly set the to undefined), it would be better to generate them as name?: Type | null (undefined is still accepted by TS, but you can also omit that property).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
JohnnyCrazycommented, May 15, 2021

I was quite intrigued by the idea and think this would be a great expansion for pgtyped. Since I wanted to dive into pgtyped anyway, I went ahead and created a small prototype (https://github.com/JohnnyCrazy/pgtyped/tree/param-nullability) with the following syntax:

Scalar

/* @name GetAllComments */
SELECT * FROM book_comments WHERE id = :id! OR user_id = :id;

Since :id is required at least once, it will not be nullable:

export interface IGetAllCommentsParams {
  id: number;
}

Array

/*
  @name GetAllCommentsByIds
  @param ids -> (...)
*/
SELECT * FROM book_comments WHERE id in :ids!;

We can also handle array spread syntax in the query itself

export interface IGetAllCommentsByIdsParams {
  ids: readonly (number)[];
}

Pick

/*
  @name InsertSingleComment
  @param comment -> (userId!, commentBody)
*/
INSERT INTO book_comments (user_id, body) VALUES :comment;

In the pick syntax we have to declare required keys in the param declaration, since the object itself is required anyway

export interface IInsertSingleCommentParams {
  comment: {
    userId: number,
    commentBody: string | null | void
  };
}

Array Pick

/*
  @name InsertBooks
  @param books -> ((rank!, name!, authorId)...)
*/
INSERT INTO books (rank, name, author_id)
VALUES :books RETURNING id as book_id;

In the pick syntax we have to declare required keys in the param declaration, since the array itself is required anyway

export interface IInsertBooksParams {
  books: readonly ({
    rank: number,
    name: string,
    authorId: number | null | void
  })[];
}

The TS version is pretty much the same principle, $userId! $$users! $user(id!, body). I didn’t find any mayor deal breakers and it should be non-breaking (I don’t think ! behind a variable is any valid postgres query). So in the end depends on @adelsz if he would like to see this in the project, I could clean up the branch and PR it.

4reactions
adelszcommented, May 31, 2021

Thanks @JohnnyCrazy, that is a great proposal. I like the suffix modificator approach and will be happy to review & merge if you are willing to put together a draft PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

not-null columns in insert queries · Issue #118 · adelsz/pgtyped
We have table items with field name, which is not nullable. We also have query: INSERT INTO ... [feature request] insert/update params type...
Read more >
Parameter defined as nullable, but no default value provided
The rule checks T-SQL code for procedures and functions parameters, which are defined as null-able, but have no default value provided. Even defined...
Read more >
Insert, update, and delete data using data manipulation ...
This page describes how to insert, update, and delete Spanner data using Data Manipulation Language (DML) statements. You can run DML statements using...
Read more >
query in sqlx - Rust - Docs.rs
Bind parameters in the SQL string are specific to the database backend: ... Option::None will be bound as NULL , so if binding...
Read more >
15: 34.3. Command Execution Functions - PostgreSQL
PQexecParams is like PQexec , but offers additional functionality: parameter values can be ... It is ignored for null parameters and text-format parameters....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found