many-to-many selection as array

const data = await db
    .select({
      id: posts.id,
      content: posts.content,
      imageUrl: posts.imageUrl,
      placeholder: posts.placeholder,
      allowComments: posts.allowComments,
      createdAt: posts.createdAt,
      author: {
        id: users.id,
        username: users.username,
        firstName: users.firstName,
        lastName: users.lastName,
        imageUrl: users.imageUrl,
      },
      likeCount: count(likes.postId),
      likedByMe: countDistinct(likes.userId),
    })
    .from(posts)
    .leftJoin(users, eq(posts.authorId, users.id))
    .leftJoin(hashtagsToPosts, eq(hashtagsToPosts.postId, posts.id))
    .leftJoin(hashtags, eq(hashtags.id, hashtagsToPosts.hashtagId))
    .leftJoin(likes, eq(posts.id, likes.postId))

    .where(cursor ? lte(posts.createdAt, new Date(cursor)) : undefined)
    .limit(limit + 1)
    .orderBy(desc(posts.createdAt))
    .groupBy(posts.id)

How to join hashtagsToPosts with hashtags to data selection
Was this page helpful?