Skip to main content

Memory.getThreadsByResourceIdPaginated()

The .getThreadsByResourceIdPaginated() method retrieves threads associated with a specific resource ID with pagination support.

Usage Example

await memory.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: 0,
perPage: 10,
});

Parameters

resourceId:

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

page:

number
Page number to retrieve

perPage:

number
Number of threads to return per page

orderBy?:

'createdAt' | 'updatedAt'
Field to sort threads by

sortDirection?:

'ASC' | 'DESC'
Sort order direction

Returns

result:

Promise<PaginationInfo & { threads: StorageThreadType[] }>
A promise that resolves to paginated thread results with metadata

Extended usage example

import { mastra } from "./mastra";

const agent = mastra.getAgent("agent");
const memory = await agent.getMemory();

let currentPage = 0;
let hasMorePages = true;

while (hasMorePages) {
const threads = await memory?.getThreadsByResourceIdPaginated({
resourceId: "user-123",
page: currentPage,
perPage: 25,
orderBy: "createdAt",
sortDirection: "ASC",
});

if (!threads) {
console.log("No threads");
break;
}

threads.threads.forEach((thread) => {
console.log(`Thread: ${thread.id}, Created: ${thread.createdAt}`);
});

hasMorePages = threads.hasMore;
currentPage++;
}