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
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"atropos": "^2.0.2",
"clsx": "^2.1.1",
"copy-to-clipboard": "^3.3.3",
"drizzle-orm": "^0.31.2",
"drizzle-orm": "^0.33.0",
"fathom-client": "^3.7.2",
"framer-motion": "^10.18.0",
"highlight.js": "^11.10.0",
Expand Down
12 changes: 11 additions & 1 deletion server/api/router/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,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