Skip to content

refactor(platform): Promote folderPath to top-level documents schema field #703

Closed

Description

@larryro

Summary

Promote folderPath from the metadata JSON field to a top-level column on the documents table, enabling index-based filtering instead of full-table scans.

Motivation

Currently, folder path information is stored inside the metadata JSON field as storagePath. Since Convex cannot index JSON fields, filtering documents by folder requires scanning all documents in the organization and checking metadata.storagePath in memory (see get_documents.ts:46-50 and get_documents_cursor.ts:65-67).

This is a prerequisite for #658 (document_list agent tool), which needs efficient folderPath filtering. Without an indexed field, the tool's limit parameter cannot work correctly (must scan everything before truncating), and performance degrades with document volume.

Proposed Changes

1. Schema Change

Add folderPath as a top-level optional string field on the documents table:

folderPath: v.optional(v.string()),  // e.g., "orgId/contracts/2024"

2. New Index

.index('by_organizationId_and_folderPath', ['organizationId', 'folderPath'])

3. Write Path Updates

Ensure folderPath is populated on document creation/update:

  • OneDrive/SharePoint sync (onedrive/import_files.ts): Set folderPath from the computed storagePath value that's currently only written to metadata
  • Direct uploads (mutations.ts → createDocumentFromUpload): Set folderPath if provided in metadata
  • Internal mutations (internal_mutations.ts → createDocument, updateDocument): Accept and persist folderPath

4. Read Path Migration

Update all folderPath filtering to use the indexed field instead of metadata.storagePath:

  • get_documents.ts → use index query instead of .filter() on metadata
  • get_documents_cursor.ts → same
  • get_document_by_path.ts → use index query instead of .filter() on metadata.storagePath

5. Data Backfill

Write a migration that copies metadata.storagePath → folderPath for all existing documents that have this value set.

Files to Modify

  • services/platform/convex/documents/schema.ts — add field + index
  • services/platform/convex/documents/get_documents.ts — use indexed field
  • services/platform/convex/documents/get_documents_cursor.ts — use indexed field
  • services/platform/convex/documents/get_document_by_path.ts — use indexed field
  • services/platform/convex/documents/internal_mutations.ts — accept folderPath
  • services/platform/convex/documents/mutations.ts — set folderPath on create
  • services/platform/convex/onedrive/import_files.ts — set folderPath on sync
  • services/platform/convex/documents/transform_to_document_item.ts — read from field instead of metadata
  • services/platform/convex/documents/validators.ts — update validators if needed

Acceptance Criteria

  • folderPath is a top-level optional field on the documents schema
  • Index by_organizationId_and_folderPath exists
  • All document creation paths populate folderPath when path info is available
  • All folder-based queries use the indexed field instead of metadata.storagePath
  • Backfill migration copies existing metadata.storagePath values to folderPath
  • Existing folder browsing in UI continues to work correctly
  • Unit tests cover indexed folder filtering

Blocked By

Nothing

Blocks

Activity

  1. added
    priority: high
    improvement
    service: platform
    on Mar 6, 2026
  2. yannickmonney commented on Apr 7, 2026

    @yannickmonney
    Contributor

    This schema migration needs careful planning — promoting folderPath to a top-level field requires a data migration for existing documents. Should be coordinated with a release that includes the migration script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementpriority: highservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions