Open mscandal opened 3 years ago
Another use-case for records:
/*
@name getEvent
@param keys -> ((
time,
place
)...)
*/
SELECT * from "Events"
WHERE ("time", "place") in :keys;
I was trying to do the same thing as @mattkrick and solved it by instead combining two arrays into a temporary table and then using it with in
. This works and pgtyped infers the correct types as well
/* @name sqlTransactionOutput */
WITH pointers AS (
SELECT tx_hash, output_index
FROM
unnest(
(:tx_hash)::bytea[],
(:output_index)::int[]
) x(tx_hash,output_index)
)
SELECT "TransactionOutput".payload
FROM
"Transaction"
INNER JOIN "TransactionOutput" ON "Transaction".id = "TransactionOutput".tx_id
WHERE ("Transaction".hash, "TransactionOutput".output_index) in (SELECT tx_hash, output_index FROM pointers);
For custom named types, I quickly hacked a script to obtain the type definition of a record type from postgres's schema. It's obviously not trivial to incorporate.
The entry point is here: https://github.com/adelsz/pgtyped/blob/b00aa813ce308c61f9bf54bbe5c5de7fd64df09b/packages/cli/src/generator.ts#L149-L151
You'd have to plug the logic there to look up a "custom" type in the DB and generate its type definition. It's not that hard though. I guess the simplest thing to do would be to add custom types to TypeAllocator's mapping. Maybe a separate data structure is needed. Again, the architecture is not currently perfectly suited to do this lazy adding of types to the TypeMapper but it's not rocket science.
Here's my dumb script, it's obviously just a proof of concept and the only really relevant part is the SQL query from it.
Came across this today:
Example to produce error:
It would be super useful to be able to query by composite keys