Skip to content

Implement optimized label feature with batched queries#13

Draft
Copilot wants to merge 4 commits intomasterfrom
copilot/optimize-label-feature
Draft

Implement optimized label feature with batched queries#13
Copilot wants to merge 4 commits intomasterfrom
copilot/optimize-label-feature

Conversation

Copy link

Copilot AI commented Mar 25, 2026

Adds label functionality with N+1 query optimization built in. Previously would require N+1 queries (one per task) to fetch labels; now uses single JOIN query to fetch labels for all tasks.

Database Schema

  • labels table: id, name (unique), color, created_at
  • task_labels junction table: composite PK on (task_id, label_id), foreign keys with CASCADE
  • Indexes on both task_id and label_id for JOIN performance

Core Optimization

getTaskLabelsOptimized() batches label fetching:

// Single query fetches labels for all tasks at once
const getTaskLabelsOptimized = (taskIds, callback) => {
  const sql = `
    SELECT tl.task_id, l.id, l.name, l.color, l.created_at
    FROM task_labels tl
    INNER JOIN labels l ON tl.label_id = l.id
    WHERE tl.task_id IN (${placeholders})
  `;
  // Group by task_id in memory for O(1) lookup
};

enrichTasksWithLabels() helper applies this pattern to /api/tasks and / routes, reducing queries from N+1 to 2 total (1 for tasks, 1 for all labels).

API Endpoints

  • POST /api/labels - create label (validates hex color, enforces unique names)
  • GET /api/labels - list all labels
  • POST /api/tasks/:id/labels - assign label(s), supports {labelId: 1} or {labelIds: [1,2,3]}
  • DELETE /api/tasks/:id/labels/:labelId - remove label
  • POST /api/tasks/bulk/labels - bulk assign via {assignments: [{taskId, labelId}, ...]}
  • GET /api/tasks - now includes labels array per task

Implementation Notes

  • Bulk operations use callbackCalled flag to prevent race condition in async forEach loops
  • Express route ordering: /api/tasks/bulk/labels placed before /api/tasks/:id/* to avoid param matching
  • No explicit transactions (sqlite3 node API callback model makes nested transactions error-prone)
  • Validation accepts both numeric and string IDs for flexibility
Original prompt

This section details on the original issue you should resolve

<issue_title>new label feature optimization</issue_title>
<issue_description>## Overview\n\nThis issue tracks optimization opportunities for the label feature in the Task Manager app.\n\n## Areas to Optimize\n\n- enrichTasksWithLabels helper: Currently makes N individual getTaskLabels DB calls (one per task). This should be batched into a single query to reduce database round-trips.\n- Label assignment: Bulk assign/remove labels to tasks in a single transaction instead of individual calls.\n- Caching: Consider caching frequently accessed labels to avoid redundant DB reads.\n\n## Expected Outcome\n\n- Reduced database queries when loading tasks with labels\n- Improved response times for /api/tasks and / endpoints\n- Cleaner, more scalable label-fetching logic</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI changed the title [WIP] Optimize label feature in Task Manager app Implement optimized label feature with batched queries Mar 25, 2026
Copilot AI requested a review from kavyashri-as March 25, 2026 07:36
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.

new label feature optimization

2 participants