Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added all selected columns to the groupBy function #1054

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion server/api/router/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,17 @@ export const postRouter = createTRPCRouter({
cursor ? paginationMapping[sort].cursor : undefined,
),
)
.groupBy(post.id, bookmarked.id, user.id)
.groupBy(
post.id,
post.slug,
post.title,
post.excerpt,
post.published,
post.readTimeMins,
post.likes,
bookmarked.id,
user.id,
)
Comment on lines +354 to +364
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Avoid grouping by all selected columns; consider query refactoring

Including all selected columns in the groupBy clause may lead to performance issues and isn't always necessary. This approach can cause inefficient queries and might not return the desired results. Since the joins with post_tag and tag tables may introduce duplicate posts due to posts having multiple tags, consider using DISTINCT to retrieve unique posts or refactoring the query to eliminate duplicates without grouping.

Apply this diff to use distinct instead of groupBy:

             .where(
               and(
                 isNotNull(post.published),
                 lte(post.published, new Date().toISOString()),
                 tagFilter ? eq(tag.title, tagFilter.toUpperCase()) : undefined,
                 cursor ? paginationMapping[sort].cursor : undefined,
               ),
             )
+            .distinctOn([post.id])
-            .groupBy(
-              post.id,
-              post.slug,
-              post.title,
-              post.excerpt,
-              post.published,
-              post.readTimeMins,
-              post.likes,
-              bookmarked.id,
-              user.id,
-            )
             .limit(limit + 1)
             .orderBy(paginationMapping[sort].orderBy);

Alternatively, you can restructure the query to fetch tags separately or use aggregate functions if necessary.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.groupBy(
post.id,
post.slug,
post.title,
post.excerpt,
post.published,
post.readTimeMins,
post.likes,
bookmarked.id,
user.id,
)
.where(
and(
isNotNull(post.published),
lte(post.published, new Date().toISOString()),
tagFilter ? eq(tag.title, tagFilter.toUpperCase()) : undefined,
cursor ? paginationMapping[sort].cursor : undefined,
),
)
.distinctOn([post.id])
.limit(limit + 1)
.orderBy(paginationMapping[sort].orderBy);

.limit(limit + 1)
.orderBy(paginationMapping[sort].orderBy);

Expand Down