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

Use sql parameter string typing instead of explicit generics in Typescript #495

Closed
geekuillaume opened this issue Feb 17, 2023 · 6 comments

Comments

@geekuillaume
Copy link

geekuillaume commented Feb 17, 2023

Is your feature request related to a problem? Please describe.

This is a potential improvement to remove the need to explicitly specify the generic <ISelectUserIdsQuery> when using the sql method. It's inspired by GraphQL codegenerator.

Describe the solution you'd like

During the codegen operation, in addition of generating the IOperationQuery, IOperationResult and IOperationParams, we can add an overload of the sql method that specify the exact string passed as parameter.

For example, when declaring the queries in typescript:

import { sql } from '@pgtyped/runtime';

const selectUser = sql(`SELECT * from users`);

it would generate a new file (called "queries.types.ts" for example):

import { sql as sourceSql } from '@pgtyped/runtime';

export type ISelectUserParams = void;
export interface ISelectUserQueryResult {
  id: string;
}
export interface ISelectUserQuery {
  params: ISelectUserParams;
  result: ISelectUserQueryResult;
}

export function sql(s: 'SELECT * FROM users;'): ReturnType<typeof sourceSql<ISelectUserQuery>>;
export function sql(s: string): unknown {
  return sourceSql([s] as any);
}

Then, in the original file, the import can be changed to import {sql} from './queries.types.ts' and then selectUser will be typed without any other change. This also links the query string to the typing, if the query is changed, the typing reverts to unknown which can then trigger other errors. This will prevent mistakes linked to changing the query but not regenerating the codegen.

Notes

This would require changing the calling method from using a template string to a normal string parameter:

sql`SELECT * from users;`

to

sql(`SELECT * from users;`)

This is necessary until this typescript issue is fixed: microsoft/TypeScript#33304

@adelsz
Copy link
Owner

adelsz commented Feb 17, 2023

Great suggestion @geekuillaume. That would be a significant DX boost.
I will try to work on this as part of the next release. Let me know if you would like to take a stab at it first.

@reverofevil
Copy link

When/if that TS issue is resolved, please, ping me. As might be obvious from the message there, I have a simple prototype for type-checked SQL literals.

@JesseVelden
Copy link
Contributor

JesseVelden commented May 2, 2023

Great suggestion @geekuillaume. That would be a significant DX boost. I will try to work on this as part of the next release. Let me know if you would like to take a stab at it first.

I've gave it a stab in #520 😉

@scottsidwell
Copy link

@adelsz anything we can do to help get this ticket / change some more attention? even if it's releasing #520 behind a beta / experimental flag? Happy to help however we can

@water-a
Copy link
Contributor

water-a commented Oct 5, 2023

@adelsz looks like #520 was merged in, wondering when we could get a release so I can start using it in my project!

@adelsz
Copy link
Owner

adelsz commented Oct 7, 2023

This has now been released as part of v2.3.0.
Many thanks to @JesseVelden for his PR 🙂

@adelsz adelsz closed this as completed Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants