-
Notifications
You must be signed in to change notification settings - Fork 30.5k
[@types/pg] Generify query methods #37892
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
[@types/pg] Generify query methods #37892
Conversation
It seems like |
@HoldYourWaffle Thank you for submitting this PR! 🔔 @pspeter3 @fluggo @bradleyayers @mateuszkrupa @aleung @mainnika @asmarques - please review this PR in the next few days. Be sure to explicitly select If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead. |
@HoldYourWaffle The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
👋 Hi there! I’ve run some quick performance metrics against master and your PR. This is still an experiment, so don’t panic if I say something crazy! I’m still learning how to interpret these metrics. Let’s review the numbers, shall we? Comparison details 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. If you have any questions or comments about me, you can ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this relate to #37689?
Seems like #37689 is a reduced implementation of this since it only types the return value. I'm not 100% sure what the additional Interesting to see that someone had this idea too. I've had this changes running in production for a while now, I honestly just forgot to send a PR out 😅 |
|
That makes a lot of sense, I'll incorporate something similar in this PR. |
I believe that for Edit On a second thought, I don't see the value on having the |
I'm not sure what the return type of
I'm not sure what you mean by this, could you clarify? What do you want to re-use?
In a way it's definitely like
To elaborate on my use-case, I have a setup where database type definitions are generated for me. So I'm writing my queries like this: await database.query<void, [UserFields.id]>(
'UPDATE "user" SET date_last_seen = now() WHERE id=$1',
[ user.id ]
); As you see I'm not literally reusing the same type twice. The |
I intended to refer to custom serializers, as opposed to parsers. Sorry for the confusion.
If you could somehow declare the types of the values once and have them checked against multiple sets of values, I thought that would somehow be a possible justification for declaring them, but it doesn't seem to be the case. To illustrate what I meant, see the following example which allows exactly that, although through an extra function: // Types declared once
function getSomething(values: [string, number]) {
pg.query(`select * from something where s = $1 and n = $2`, values)
}
// Type-checking performed multiple times
getSomething(['0', 0])
getSomething(['1', 1])
Thanks for the additional explanation. I still find it somewhat weird to specify the types of your own input, over which you have complete control. But I'm fine with the change. |
That's great to hear 😄 I think the bot wants you to push some approve button somewhere in that case. |
073d810
to
a91602a
Compare
Updated numbers for you here from 033bac5: Comparison details 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. |
@HoldYourWaffle The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
|
`R` will be the return type of the query, `I` a tuple for parameters
a91602a
to
c4a07b8
Compare
Whoops that's a pretty dumb mistake, no idea how I missed that. I think I've fixed it now, but it's too early for me to tell... |
Updated numbers for you here from e91f271: Comparison details 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. |
@HoldYourWaffle The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
c4a07b8
to
9ac29f8
Compare
@inad9300 I have addressed your concerns. I have also updated the minimum typescript version for @types/pg-copy-streams to avoid travis failing. |
@HoldYourWaffle The Travis CI build failed! Please review the logs for more information. Once you've pushed the fixes, the build will automatically re-run. Thanks! |
Updated numbers for you here from 8db1e4b: pg-copy-streams/v1Comparison details for pg-copy-streams/v1 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-ears/v1Comparison details for pg-ears/v1 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-large-object/v2Comparison details for pg-large-object/v2 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg/v7Comparison details for pg/v7 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. |
Updated numbers for you here from 479677f: pg-copy-streams/v1Comparison details for pg-copy-streams/v1 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-ears/v1Comparison details for pg-ears/v1 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-large-object/v2Comparison details for pg-large-object/v2 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-pool/v2Comparison details for pg-pool/v2 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg-query-stream/v1Comparison details for pg-query-stream/v1 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. pg/v7Comparison details for pg/v7 📊
It looks like nothing changed too much. I’m pretty lenient since I’m still an experiment, so take a look anyways and make sure nothing looks out of place. |
Finally got travis to pass. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in general, it's a good PR 👍🏻
pg-pool is ok about these changes.
A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped! |
Thanks for the contribution! |
I just published |
I just published |
I just published |
|
Please fill in this template.
npm test
.)npm run lint package-name
(ortsc
if notslint.json
is present).If changing an existing definition:
tslint.json
containing{ "extends": "dtslint/dt.json" }
. If for reason the any rule need to be disabled, disable it for that line using// tslint:disable-next-line [ruleName]
and not for whole package so that the need for disabling can be reviewed.I generified the
query
methods as well as the corresponding interfaces.R
will be the return type of the query,I
a tuple for the parameters.A usage example:
These additional type checks are aimed at users who have interfaces for their database types (for example auto-generated by schemats).
It is 100% opt-in and backwards compatible with the use of generic defaults added in TS 2.3.
The usefulness
R
parameter (for the return type) pretty much speaks for itself, it enables intellisense/typechecking without the use of annoying type assertions.The
I
parameter (the inputs) might be too repetitive/over-safe for some people, which is why it comes after the (more useful) returntype generic. For my use-case I'm automatically generating parts of these method calls, and an extra sanity check is very much welcome in that case. It's also a very welcome safeguard when refactoring database types, although that probably doesn't happen too often for most people.Because these generics are meant to check the parameters supplied directly the
no-unnecessary-generics
rule doesn't apply here. Thedtslint
page says:The emphasized part doesn't apply here, because the whole idea of these generics is to have users (optionally) manually provide an extra layer of security by specifying their own types.
any
is a perfectly valid type constraint here, the generics enable users to narrow it down to their own types if they so wish.