Skip to Content
ReferenceMemory.getThreadsByResourceIdPaginated()

getThreadsByResourceIdPaginated Reference

The getThreadsByResourceIdPaginated function retrieves threads associated with a specific resource ID with pagination support. This method addresses performance and memory usage concerns when dealing with large numbers of threads by returning results in manageable chunks with metadata for navigation.

Usage Example

import { Memory } from "@mastra/core/memory"; const memory = new Memory(config); // Basic usage with default parameters const result = await memory.getThreadsByResourceIdPaginated({ resourceId: "resource-123", page: 0, perPage: 100, }); console.log(result.threads); console.log(result.total); console.log(result.hasMore); // Custom pagination with sorting const customResult = await memory.getThreadsByResourceIdPaginated({ resourceId: "resource-123", page: 2, perPage: 50, orderBy: "updatedAt", sortDirection: "ASC", }); // Process paginated results let currentPage = 0; let hasMorePages = true; while (hasMorePages) { const pageResult = await memory.getThreadsByResourceIdPaginated({ resourceId: "user-456", page: currentPage, perPage: 25, orderBy: "createdAt", sortDirection: "DESC", }); // Process threads pageResult.threads.forEach(thread => { console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`); }); hasMorePages = pageResult.hasMore; currentPage++; }

Parameters

resourceId:

string
The ID of the resource whose threads are to be retrieved.

page:

number
Page number to retrieve. Must be a positive integer.

perPage:

number
Number of threads to return per page. Must be a positive integer.

orderBy?:

ThreadOrderBy
Field to sort threads by. Accepts 'createdAt' or 'updatedAt'. Default: 'createdAt'

sortDirection?:

ThreadSortDirection
Sort order direction. Accepts 'ASC' or 'DESC'. Default: 'DESC'

Type Definitions

type ThreadOrderBy = 'createdAt' | 'updatedAt'; type ThreadSortDirection = 'ASC' | 'DESC'; interface ThreadSortOptions { orderBy?: ThreadOrderBy; sortDirection?: ThreadSortDirection; } interface PaginationInfo { total: number; // Total number of threads across all pages page: number; // Current page number perPage: number; // Number of threads per page hasMore: boolean; // Whether additional pages exist }

Returns

threads:

StorageThreadType[]
Array of threads for the current page, sorted according to the specified criteria.

total:

number
Total number of threads associated with the resource ID across all pages.

page:

number
Current page number.

perPage:

number
Number of threads returned per page as specified in the request.

hasMore:

boolean
Indicates whether additional pages of results are available.

Technical Notes

Performance Considerations

This method executes database-level pagination using LIMIT/OFFSET operations (or equivalent), which provides better performance and memory usage compared to retrieving all threads and paginating in application code.

Default Values

  • orderBy: Defaults to "createdAt"
  • sortDirection: Defaults to "DESC" (newest first)

Relationship to getThreadsByResourceId

The paginated version (getThreadsByResourceIdPaginated) complements the existing getThreadsByResourceId method:

  • Use getThreadsByResourceId when you need all threads for a resource
  • Use getThreadsByResourceIdPaginated when working with potentially large thread collections or implementing UI pagination

Both methods support the same sorting options for consistency.

Error Handling

try { const result = await memory.getThreadsByResourceIdPaginated({ resourceId: "resource-123", page: 0, perPage: 100, }); if (result.threads.length === 0) { console.log("No threads found for this resource"); } } catch (error) { console.error("Failed to retrieve paginated threads:", error); }