how to group Related Items
// index.ts
const coursesList = await db
.select({ title: courses.title, tags: tags.tag })
.from(courseTags)
.leftJoin(courses, eq(courses.id, courseTags.courseId))
.leftJoin(tags, eq(courseTags.tagId, tags.id));
// index.ts
const coursesList = await db
.select({ title: courses.title, tags: tags.tag })
.from(courseTags)
.leftJoin(courses, eq(courses.id, courseTags.courseId))
.leftJoin(tags, eq(courseTags.tagId, tags.id));
# actual output
courses: [
{
title: "Course 1",
tags: "Tag 1"
}, {
title: "Course 2",
tags: "Tag 2"
}, {
title: "Course 3",
tags: "Tag 3"
}, {
title: "Course 1",
tags: "tag 4"
}, {
title: "Course 1",
tags: "tag 5"
}
]
# actual output
courses: [
{
title: "Course 1",
tags: "Tag 1"
}, {
title: "Course 2",
tags: "Tag 2"
}, {
title: "Course 3",
tags: "Tag 3"
}, {
title: "Course 1",
tags: "tag 4"
}, {
title: "Course 1",
tags: "tag 5"
}
]
# desired output
courses: [
{
title: "Course 1",
tags: ["Tag 1", "tag 4", "tag 5"]
}, {
title: "Course 2",
tags: ["Tag 2"]
}, {
title: "Course 3",
tags: ["Tag 3"]
}
]
# desired output
courses: [
{
title: "Course 1",
tags: ["Tag 1", "tag 4", "tag 5"]
}, {
title: "Course 2",
tags: ["Tag 2"]
}, {
title: "Course 3",
tags: ["Tag 3"]
}
]
0 Replies