Implementing D1 batching (getting typed queries as input and returning typed InferResult)
Hi. I've wanted to get D1 batching to work, so I've tried doing as below. Functionally the code works, however I would like to get the proper type safety for this. Essentially I extend Kysely to add the
executeBatch
method and I want to infer the results for each of the individual queries. Is this possible?6 Replies
https://pastecord.com/fazekabave.ts
the executeBatch method gets the compiled queries and the intention it returns an array of queryresponses
like is this possible
like the easiest way from a type perspective is probably getting compiledquery inputs and not returning anything (not ideal tho)
per se i think i'm really asking for a way to represent an array of queries with type safety, since i can get the result type with
InferResult<Query>
Hey 👋
There's an existing issue for this. How many queries do you run per batch btw?
like 10-20 on average maybe? i have instances where i also run say ~3k queries at once. defo could be optimized into say inserting more values (one case) but then sqlite limit of 1k variables throttles it quite a bit, and then update queries can't really be grouped (besides adding a CTE or a temp table or something)
That's too much for the compiler.
iirc, we aimed at 5-6 type-safe, and beyond not as type-safe.
Honestly I'd go for a "staging table" and do big upserts to "main table".
iirc, we aimed at 5-6 type-safe, and beyond not as type-safe.but if it's all inserts/updates/deletes without
returning
, getting correct return types is pointless at this point.hmm so i can probably just have a type that accepts any query and executes it with no result
because no return types would work for those three
Yeah, something like that.