scriptbytes
DTDrizzle Team
•Created by scriptbytes on 1/26/2024 in #help
Partial Select - Child array
After some tinkering I got my sub-query count to work, but I had to hard-code the name of the table and the column name in the where that I'm querying from, otherwise it uses the wrong name:
extras: {
likeCount:
sql
(select cast(count(*) as int) from recipe_like where recipe_like.recipe_id = ${recipe.id})
.as(
'lowered_name'
),
},5 replies
DTDrizzle Team
•Created by scriptbytes on 1/26/2024 in #help
Partial Select - Child array
Yea I saw I was able to do that, but my next issue is it seems there's things I can with the select syntax that I can't do in the query syntax.
For example, let's say in my query I want to also select a field that is the Count of likes:
const test = await db
.select({
...getTableColumns(recipe),
comments: getTableColumns(comment), // This should be an array
likeCount:
sql<number>
(select cast(count(*) as int) from ${recipeLike} where ${recipeLike.recipeId} = ${recipe.id})
.as(
'like_count'
)
})
.from(recipe)
.leftJoin(comment, eq(recipe.id, comment.recipeId));5 replies