- Somewhat off topic, as someone who hasn't used PostgreSQL and only has experience with mysql/MariaDB... I've never liked writing queries with numbered parameters from an array with placeholders like $1 in this example. I find them much easier to read and debug when I pass them with string keys, basically:
`UPDATE t SET x=:x WHERE 1`
`{x:42}`
I found that the original node-mysql didn't even allow this, so I wrote my own parser on top of it. But I don't see this style of binding used in examples very often. Is it frowned upon for some reason?
by barishnamazov
2 subcomments
- How is this different than kysely + kysely-codegen (or hand-made types)?
- If you're into Haskell prior art, there's postgresql-typed https://hackage.haskell.org/package/postgresql-typed-0.6.2.5... where you write queries, it safely(?) puts in your variables, and you automatically get back typed values.
- Is there a way to make this work with https://github.com/porsager/postgres ? It's very close to raw SQL but with safe substitutions. I use it with Zod, but it would be great to have the types derived automatically.
by kristiandupont
1 subcomments
- Very nice! I am the author of Kanel* which generates types for each table, view, etc. I tend to use it together with Knex which gives me strong type safety from simple selects and updates and sporadic type safety from complex queries with joins etc.
The advantage to your approach (I guess) is increased type safety for complex queries, trading off the loss of "fundamental" types with, say, branded ID types? I guess the two approaches are quite complementary, perhaps I should try that.
* https://github.com/kristiandupont/kanel
by blue_pants
0 subcomment
- How do you guys solve the problem of conforming DB schema to TS interfaces (or vice versa depending on what you like)?
Do you manually keep them in-sync (that's what I'm leaning into as the most practical solution)? Do you introspect the DB schema? Or maybe use something like Drizzle which autogenerates sql migration to keep the db schema in-sync
by owlstuffing
0 subcomment
- Something parallel to this and completely amazing: manifold-sql[1].
1. https://github.com/manifold-systems/manifold/blob/master/man...
- Seems very cool, if it works :) My suggestion here would be to provide a TS playground environment where I can inspect and poke around to see that the types really do work. I'd also love to see some more complex queries with joins, etc.
- Thank you. This looks like a nice improvement on pgtyped, and another good option.
I'm curious if there are any good patterns for dealing with dynamic query building or composing queries?
by semiquaver
1 subcomments
- How is this different from sqlc with sqlc-gen-typescript?
by MuffinFlavored
1 subcomments
- any reason why you did
const { rows } = client.query(
"select id, name, last_modified from tbl where id = $1",
[42],
);
instead of const { rows } = client.query(
"select id, name, last_modified from tbl where id = :id",
{ id: 42 },
);
- Just use Drizzle
by vivzkestrel
0 subcomment
- Doesnt pg-promise already do this?
by retropragma
0 subcomment
- The lack of composability makes this a hard no for me
- [dead]