getThreadsByResourceId Reference
The getThreadsByResourceId
function retrieves all threads associated with a specific resource ID from storage. Threads can be sorted by creation or modification time in ascending or descending order.
Usage Example
import { Memory } from "@mastra/core/memory";
const memory = new Memory(config);
// Basic usage - returns threads sorted by createdAt DESC (default)
const threads = await memory.getThreadsByResourceId({
resourceId: "resource-123",
});
// Custom sorting by updatedAt in ascending order
const threadsByUpdate = await memory.getThreadsByResourceId({
resourceId: "resource-123",
orderBy: "updatedAt",
sortDirection: "ASC",
});
Parameters
resourceId:
string
The ID of the resource whose threads are to be retrieved.
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;
}
Returns
StorageThreadType[]:
Promise
A promise that resolves to an array of threads associated with the given resource ID.
Related
- Memory Class Reference
- Getting Started with Memory (Covers threads/resources concept)
- createThread
- getThreadById