Pillow
Pillow
Explore posts from servers
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
anyway gj :D
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
ah, sorry, yeap.
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
Are u sure that if the translations are empty u won't get a null object?
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
No description
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
oh, sorry, I misunderstood the question. :D
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
No description
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
Here's an example
18 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
I wrote an utility function for json aggregation
import type { SQL, InferColumnsDataTypes } from "drizzle-orm";
import type { PgColumn } from "drizzle-orm/pg-core";
import { sql, DrizzleError } from "drizzle-orm";

export function jsonAgg<
T extends Record<string, PgColumn>,
>(select: T) {
const chunks: SQL[] = [];
const entries = Object.entries(select);

if (!entries.length) {
throw new DrizzleError('Cannot aggregate an empty object');
}

entries.forEach(([key, column], index) => {
if (index > 0) chunks.push(sql`,`);
chunks.push(sql.raw(`'${key}',`), sql`${column}`);
});

const filterChunks = entries.filter(([_, col]) => !col.notNull).map(([_, col]) => col);
const filterSql = filterChunks.length ? sql`FILTER (WHERE ${sql.join(filterChunks)} IS NOT NULL)` : "";

return sql<InferColumnsDataTypes<T>[]>`
COALESCE(json_agg(json_build_object(${sql.join(chunks)})) ${filterSql}, '[]')
`;
}
import type { SQL, InferColumnsDataTypes } from "drizzle-orm";
import type { PgColumn } from "drizzle-orm/pg-core";
import { sql, DrizzleError } from "drizzle-orm";

export function jsonAgg<
T extends Record<string, PgColumn>,
>(select: T) {
const chunks: SQL[] = [];
const entries = Object.entries(select);

if (!entries.length) {
throw new DrizzleError('Cannot aggregate an empty object');
}

entries.forEach(([key, column], index) => {
if (index > 0) chunks.push(sql`,`);
chunks.push(sql.raw(`'${key}',`), sql`${column}`);
});

const filterChunks = entries.filter(([_, col]) => !col.notNull).map(([_, col]) => col);
const filterSql = filterChunks.length ? sql`FILTER (WHERE ${sql.join(filterChunks)} IS NOT NULL)` : "";

return sql<InferColumnsDataTypes<T>[]>`
COALESCE(json_agg(json_build_object(${sql.join(chunks)})) ${filterSql}, '[]')
`;
}
It may look horrible, but it works :D
18 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
yeap, thx ❤️
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
https://orm.drizzle.team/docs/joins#aggregating-results Maybe I just need to aggregate the results within the js After prisma sql-like seems quite complicated, but more efficient.
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
COALESCE(
json_agg(json_build_object('id', g.id, 'name', g.name)),
'[]'::json
)
COALESCE(
json_agg(json_build_object('id', g.id, 'name', g.name)),
'[]'::json
)
I tried do smth like this, but..
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
No description
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
No description
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
Uh, i fixed it ;D
12 replies
DTDrizzle Team
Created by Pillow on 9/13/2023 in #help
Query in drizzle doesn't work
No description
12 replies
DTDrizzle Team
Created by Pillow on 8/7/2023 in #help
Relational queries: many-to-many
😭
13 replies
DTDrizzle Team
Created by Pillow on 8/7/2023 in #help
Relational queries: many-to-many
13 replies
DTDrizzle Team
Created by Pillow on 8/7/2023 in #help
Relational queries: many-to-many
I'd my results to look like
const res: {
id: number;
name: string;
verified: boolean;
invitedBy: number | null;
fullName: string;
group: {
id: number;
name: string;
description: string | null;
}[];
}[];
const res: {
id: number;
name: string;
verified: boolean;
invitedBy: number | null;
fullName: string;
group: {
id: number;
name: string;
description: string | null;
}[];
}[];
13 replies
DTDrizzle Team
Created by Pillow on 8/7/2023 in #help
Relational queries: many-to-many
13 replies
DTDrizzle Team
Created by Pillow on 8/7/2023 in #help
Relational queries: many-to-many
13 replies