utility function for COALESCE

I have a CTE and I want an array to coalesce to [] and the count to coalesce to 0.
export function aggregateAsArray<Column extends AnyColumn>(column: Column) {
return sql<
GetColumnData<Column, 'raw'>[]
>`coalesce(array_agg(distinct ${sql`${column}`}) filter (where ${column} is not null), ARRAY[]::text[])`;
}
export function aggregateAsArray<Column extends AnyColumn>(column: Column) {
return sql<
GetColumnData<Column, 'raw'>[]
>`coalesce(array_agg(distinct ${sql`${column}`}) filter (where ${column} is not null), ARRAY[]::text[])`;
}
const tagData = db.$with('library_tag_data').as(
db
.select({
fooId: tableSurveysTagsLibrary.fooId,
tags: aggregateAsArray(tableSurveysTagsLibrary.tag).as('tags'),
tagCount: count(tableSurveysTagsLibrary.tag).as('tagCount')
})
.from(tableSurveysTagsLibrary)
.groupBy(tableSurveysTagsLibrary.fooId)
);
const tagData = db.$with('library_tag_data').as(
db
.select({
fooId: tableSurveysTagsLibrary.fooId,
tags: aggregateAsArray(tableSurveysTagsLibrary.tag).as('tags'),
tagCount: count(tableSurveysTagsLibrary.tag).as('tagCount')
})
.from(tableSurveysTagsLibrary)
.groupBy(tableSurveysTagsLibrary.fooId)
);
This feels too hard. What am I am missing?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?