jacksn
jacksn
Explore posts from servers
DTDrizzle Team
Created by sevenwestonroads on 4/20/2023 in #help
How to reproduce a Prisma `include` statement for arrays of related entities without SQL?
I think this would be best implemented by getting the modifierGroups in a subquery
21 replies
DTDrizzle Team
Created by sevenwestonroads on 4/20/2023 in #help
How to reproduce a Prisma `include` statement for arrays of related entities without SQL?
but i have no idea on how to make it work for typescript
21 replies
DTDrizzle Team
Created by sevenwestonroads on 4/20/2023 in #help
How to reproduce a Prisma `include` statement for arrays of related entities without SQL?
Yeah, it works
21 replies
DTDrizzle Team
Created by sevenwestonroads on 4/20/2023 in #help
How to reproduce a Prisma `include` statement for arrays of related entities without SQL?
@Raphaël Moreau
21 replies
DTDrizzle Team
Created by sevenwestonroads on 4/20/2023 in #help
How to reproduce a Prisma `include` statement for arrays of related entities without SQL?
do you have any suggestions on making this work with a nested relation?
// ⚠️ Potential for SQL injections, so you shouldn't allow user-specified key names
export function jsonBuildObject<T extends Record<string, AnyColumn>>(shape: T) {
const chunks: SQL[] = [];

Object.entries(shape).forEach(([key, value]) => {
if (chunks.length > 0) {
chunks.push(sql.raw(`,`));
}
chunks.push(sql.raw(`'${key}',`));
chunks.push(sql`${value}`);
});

return sql<InferColumnsDataTypes<T>[]>`json_build_object(${sql.fromList(
chunks,
)})`;
}

// ⚠️ Potential for SQL injections, so you shouldn't allow user-specified key names
export function jsonAggBuildObject<T extends Record<string, AnyColumn>>(
shape: T,
) {
return sql<InferColumnsDataTypes<T>[]>`coalesce(json_agg(${jsonBuildObject(
shape,
)}), '[]')`;
}
// ⚠️ Potential for SQL injections, so you shouldn't allow user-specified key names
export function jsonBuildObject<T extends Record<string, AnyColumn>>(shape: T) {
const chunks: SQL[] = [];

Object.entries(shape).forEach(([key, value]) => {
if (chunks.length > 0) {
chunks.push(sql.raw(`,`));
}
chunks.push(sql.raw(`'${key}',`));
chunks.push(sql`${value}`);
});

return sql<InferColumnsDataTypes<T>[]>`json_build_object(${sql.fromList(
chunks,
)})`;
}

// ⚠️ Potential for SQL injections, so you shouldn't allow user-specified key names
export function jsonAggBuildObject<T extends Record<string, AnyColumn>>(
shape: T,
) {
return sql<InferColumnsDataTypes<T>[]>`coalesce(json_agg(${jsonBuildObject(
shape,
)}), '[]')`;
}
Allowing for usage like this:
users: jsonAggBuildObject({
id: user.id,
name: user.name,
image: user.image,
tasks: jsonBuildObject({ id: task.id, title: task.title }),
}),
users: jsonAggBuildObject({
id: user.id,
name: user.name,
image: user.image,
tasks: jsonBuildObject({ id: task.id, title: task.title }),
}),
However, I cant really figure out how to stop typescript from complaining here and infer the correct type for tasks Some builtin feature to support such aggregation would be a great addition to the library, I really like what you guys built!
21 replies