Skip to content

Conversation

lojsk
Copy link

@lojsk lojsk commented Aug 5, 2025

On Contact search, we get rid of additional queries for triggers and emails, and is now done with joins. Also, we return already greatest createdAt and sort by it on the query level.

For Avtivity Feed, we do limits and offset already on the DB level, that way queries become way faster because they don't need to get all the data from the DB to the backend. To get the same format, we used to_jsonb and jsonb_build_object. For bringing triggers and emails to the same table we used UNION, for that reason, all can be done in the DB level, even pagination.

Extra small fix is filtered.length to contacts.count, where I think that the correct value that should be used.

lojsk added 2 commits August 5, 2025 12:24
(cherry picked from commit 861b334)
@driaug driaug requested a review from Copilot August 21, 2025 09:11
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request optimizes database queries for the Activity Feed and Contact search functionality by moving sorting and pagination logic to the database level instead of handling it in the application layer.

  • Replaces multiple separate queries with optimized JOIN and UNION operations
  • Implements database-level pagination and sorting using raw SQL queries
  • Updates frontend to use simplified contact data structure without separate triggers/emails arrays

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/dashboard/src/pages/contacts/index.tsx Removes client-side sorting logic and simplifies last activity calculation
packages/dashboard/src/lib/hooks/contacts.ts Updates TypeScript types to reflect simplified Contact structure
packages/api/src/services/ProjectService.ts Replaces separate trigger/email queries with optimized UNION query using raw SQL
packages/api/src/controllers/Projects.ts Optimizes contact search with JOIN operations and GREATEST function for timestamps

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

FROM
contacts c
LEFT JOIN triggers t on c.id = t."contactId"
LEFT JOIN emails e on e.id = e."contactId"
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

The JOIN condition is incorrect. It should be e."contactId" = c.id instead of e.id = e."contactId". The current condition joins emails table on itself rather than joining it with contacts.

Suggested change
LEFT JOIN emails e on e.id = e."contactId"
LEFT JOIN emails e on e."contactId" = c.id

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant