Last active
August 26, 2025 02:31
-
-
Save nmquebb/59d84b2280ff4e26d72206631e284e3e to your computer and use it in GitHub Desktop.
json agg for drizzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function jsonAgg<T extends Record<string, AnyColumn>>(select: T) { | |
| const chunks: SQL[] = []; | |
| Object.entries(select).forEach(([key, column], index) => { | |
| if (index > 0) chunks.push(sql`,`); | |
| chunks.push(sql.raw(`'${key}',`), sql`${column}`); | |
| }); | |
| return sql<InferColumnsDataTypes<T>[]>` | |
| coalesce( | |
| json_agg(json_build_object(${sql.fromList(chunks)})), | |
| '[]' | |
| ) | |
| `; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I noticed that
sql.fromListis deprecated in newer drizzle versions, so we need to replace it withsql.joininstead, as described by the docs:Thank you for making this!