perf: make getRelations() pagination consistent and efficient

**Problem**: Pagination behavior was inconsistent across different query patterns:
- getRelations({ from: id, limit: 10 }) fetched ALL relationships then sliced
- getRelations({ limit: 10 }) paginated at storage layer
- storage.getVerbs() offset parameter wasn't being passed to adapters

**Root Cause**:
1. getRelations() used different code paths for from/to vs no-filter queries
2. storage.getVerbs() called getVerbsWithPagination without offset
3. Then tried to slice results, which failed for paginated queries

**Solution**:
- Unified getRelations() to ALWAYS use storage.getVerbs() with pagination
- Fixed storage.getVerbs() to convert offset to cursor for adapters
- All query patterns now paginate efficiently at storage layer
- Eliminated inefficient "fetch all then slice" pattern

**Performance Impact**:
- Before: getRelations({ from: entityId, limit: 10 }) on entity with 1000 relationships = 1000 fetched
- After: Only 10 fetched 
- All tests passing (14/14)

**Breaking**: None - fully backward compatible
This commit is contained in:
David Snelling 2025-10-21 13:28:38 -07:00
parent 8d217f3b84
commit 54d819cfcf
3 changed files with 39 additions and 46 deletions

View file

@ -752,14 +752,17 @@ export abstract class BaseStorage extends BaseStorageAdapter {
// Check if the adapter has a paginated method for getting verbs
if (typeof (this as any).getVerbsWithPagination === 'function') {
// Use the adapter's paginated method
// Convert offset to cursor if no cursor provided (adapters use cursor for offset)
const effectiveCursor = cursor || (offset > 0 ? offset.toString() : undefined)
const result = await (this as any).getVerbsWithPagination({
limit,
cursor,
cursor: effectiveCursor,
filter: options?.filter
})
// Apply offset if needed (some adapters might not support offset)
const items = result.items.slice(offset)
// Items are already offset by the adapter via cursor, no need to slice
const items = result.items
// CRITICAL SAFETY CHECK: Prevent infinite loops
// If we have no items but hasMore is true, force hasMore to false