MichaelRapp
MichaelRapp
DTDrizzle Team
Created by MichaelRapp on 10/4/2023 in #help
Type support for porting Kysely jsonArrayFrom to Drizzle
I am trying to port this functionality from kysley to Drizzle, but I am struggling getting the typing to work. Kysely function:
export function jsonArrayFrom<O>(
expr: Expression<O>
): RawBuilder<Simplify<O>[]> {
return sql`(select coalesce(json_agg(agg), '[]') from ${expr} as agg)`
}
export function jsonArrayFrom<O>(
expr: Expression<O>
): RawBuilder<Simplify<O>[]> {
return sql`(select coalesce(json_agg(agg), '[]') from ${expr} as agg)`
}
Drizzle function:
export function jsonArrayFrom(subquery) {
return sql`(select coalesce(json_agg(agg), '[]') from ${subquery} as agg)`;
}
export function jsonArrayFrom(subquery) {
return sql`(select coalesce(json_agg(agg), '[]') from ${subquery} as agg)`;
}
Usage example:
ctx.db.select({
id: plan,
displayName: plan.displayName,
deductibles: jsonArrayFrom(ctx.db.select().from(deductible)),
}).from(plan)
ctx.db.select({
id: plan,
displayName: plan.displayName,
deductibles: jsonArrayFrom(ctx.db.select().from(deductible)),
}).from(plan)
1 replies