3 ms·
Postgres is pretty good at JSON. I think this would work for your example (assuming the implied schema), presumably in PgTyped too: SELECT user.*, json_agg(pos
by barnabee 6y ago
Postgres is pretty good at JSON. I think this would work for your example (assuming the implied schema), presumably in PgTyped too:
SELECT user.*, json_agg(post) as posts FROM user LEFT JOIN post ON user.id = post.id GROUP BY user.id
It's not as simple as you'd like (and you have to wrap the whole thing in a subquery + another json_agg if you really want Postgres to return a single JSON string) but it's not actually a lot more complicated, and avoids having to rely on magic that's likely brittle and hard to debug (or only really useful in a small subset of cases).