Repository navigation
Description
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): SetfolderPathfrom the computedstoragePathvalue that's currently only written tometadata - Direct uploads (
mutations.ts→createDocumentFromUpload): SetfolderPathif provided in metadata - Internal mutations (
internal_mutations.ts→createDocument,updateDocument): Accept and persistfolderPath
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 metadataget_documents_cursor.ts→ sameget_document_by_path.ts→ use index query instead of.filter()onmetadata.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 + indexservices/platform/convex/documents/get_documents.ts— use indexed fieldservices/platform/convex/documents/get_documents_cursor.ts— use indexed fieldservices/platform/convex/documents/get_document_by_path.ts— use indexed fieldservices/platform/convex/documents/internal_mutations.ts— acceptfolderPathservices/platform/convex/documents/mutations.ts— setfolderPathon createservices/platform/convex/onedrive/import_files.ts— setfolderPathon syncservices/platform/convex/documents/transform_to_document_item.ts— read from field instead of metadataservices/platform/convex/documents/validators.ts— update validators if needed
Acceptance Criteria
-
folderPathis a top-level optional field on thedocumentsschema - Index
by_organizationId_and_folderPathexists - All document creation paths populate
folderPathwhen path info is available - All folder-based queries use the indexed field instead of
metadata.storagePath - Backfill migration copies existing
metadata.storagePathvalues tofolderPath - Existing folder browsing in UI continues to work correctly
- Unit tests cover indexed folder filtering
Blocked By
Nothing
Blocks
- feat(platform): Add document_list agent tool #658 (document_list agent tool — needs efficient folderPath filtering)
Activity
refactor(convex): add folderPath field, index, and thread through wri…
refactor(convex): migrate read paths to use folderPath index
refactor(convex): add backfill migration for folderPath
test(convex): add unit tests for folderPath indexed queries
fix(convex): use optional chaining in cursor test to satisfy linter
feat(convex): add folders table and replace folderPath with folderId
refactor(convex): update document read/write paths to use folderId
refactor(ui): update documents frontend for folder navigation
test(convex): add folder tests and backfill migration
feat(ui): add folder creation and display in documents table
fix(ui): pass folderId through upload flow so files land in current f…
feat(convex): add folders table and replace folderPath with folderId
refactor(convex): update document read/write paths to use folderId
refactor(ui): update documents frontend for folder navigation
test(convex): add folder tests and backfill migration
feat(ui): add folder creation and display in documents table
fix(ui): pass folderId through upload flow so files land in current f…
yannickmonney commentedon Apr 7, 2026
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.