@mastra/pg 1.0.0-beta.13 → 1.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,51 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 1.0.0-beta.14
4
+
5
+ ### Patch Changes
6
+
7
+ - Added new `listThreads` method for flexible thread filtering across all storage adapters. ([#11832](https://github.com/mastra-ai/mastra/pull/11832))
8
+
9
+ **New Features**
10
+ - Filter threads by `resourceId`, `metadata`, or both (with AND logic for metadata key-value pairs)
11
+ - All filter parameters are optional, allowing you to list all threads or filter as needed
12
+ - Full pagination and sorting support
13
+
14
+ **Example Usage**
15
+
16
+ ```typescript
17
+ // List all threads
18
+ const allThreads = await memory.listThreads({});
19
+
20
+ // Filter by resourceId only
21
+ const userThreads = await memory.listThreads({
22
+ filter: { resourceId: 'user-123' },
23
+ });
24
+
25
+ // Filter by metadata only
26
+ const supportThreads = await memory.listThreads({
27
+ filter: { metadata: { category: 'support' } },
28
+ });
29
+
30
+ // Filter by both with pagination
31
+ const filteredThreads = await memory.listThreads({
32
+ filter: {
33
+ resourceId: 'user-123',
34
+ metadata: { priority: 'high', status: 'open' },
35
+ },
36
+ orderBy: { field: 'updatedAt', direction: 'DESC' },
37
+ page: 0,
38
+ perPage: 20,
39
+ });
40
+ ```
41
+
42
+ **Security Improvements**
43
+ - Added validation to prevent SQL injection via malicious metadata keys
44
+ - Added pagination parameter validation to prevent integer overflow attacks
45
+
46
+ - Updated dependencies [[`ed3e3dd`](https://github.com/mastra-ai/mastra/commit/ed3e3ddec69d564fe2b125e083437f76331f1283), [`6833c69`](https://github.com/mastra-ai/mastra/commit/6833c69607418d257750bbcdd84638993d343539), [`47b1c16`](https://github.com/mastra-ai/mastra/commit/47b1c16a01c7ffb6765fe1e499b49092f8b7eba3), [`3a76a80`](https://github.com/mastra-ai/mastra/commit/3a76a80284cb71a0faa975abb3d4b2a9631e60cd), [`8538a0d`](https://github.com/mastra-ai/mastra/commit/8538a0d232619bf55dad7ddc2a8b0ca77c679a87), [`9312dcd`](https://github.com/mastra-ai/mastra/commit/9312dcd1c6f5b321929e7d382e763d95fdc030f5)]:
47
+ - @mastra/core@1.0.0-beta.25
48
+
3
49
  ## 1.0.0-beta.13
4
50
 
5
51
  ### Minor Changes
@@ -33,4 +33,4 @@ docs/
33
33
  ## Version
34
34
 
35
35
  Package: @mastra/pg
36
- Version: 1.0.0-beta.13
36
+ Version: 1.0.0-beta.14
@@ -5,7 +5,7 @@ description: Documentation for @mastra/pg. Includes links to type definitions an
5
5
 
6
6
  # @mastra/pg Documentation
7
7
 
8
- > **Version**: 1.0.0-beta.13
8
+ > **Version**: 1.0.0-beta.14
9
9
  > **Package**: @mastra/pg
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.13",
2
+ "version": "1.0.0-beta.14",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -115,6 +115,9 @@ export const agent = new Agent({
115
115
 
116
116
  This is useful when different agents need to store data in separate databases for security, compliance, or organizational reasons.
117
117
 
118
+ > **Mastra Cloud Store limitation**
119
+ Agent-level storage is not supported when using [Mastra Cloud Store](https://mastra.ai/docs/v1/mastra-cloud/deployment#using-mastra-cloud-store). If you use Mastra Cloud Store, configure storage on the Mastra instance instead. This limitation does not apply if you bring your own database.
120
+
118
121
  ## Threads and resources
119
122
 
120
123
  Mastra organizes memory into threads using two identifiers:
@@ -136,6 +139,21 @@ const stream = await agent.stream("message for agent", {
136
139
  > **Note:**
137
140
  [Studio](https://mastra.ai/docs/v1/getting-started/studio) automatically generates a thread and resource ID for you. Remember to to pass these explicitly when calling `stream` or `generate` yourself.
138
141
 
142
+ ### Thread and resource relationship
143
+
144
+ Each thread has an owner (its `resourceId`) that is set when the thread is created and cannot be changed. When you query a thread, you must use the correct owner's resource ID. Attempting to query a thread with a different resource ID will result in an error:
145
+
146
+ ```text
147
+ Thread with id <thread_id> is for resource with id <resource_a>
148
+ but resource <resource_b> was queried
149
+ ```
150
+
151
+ Note that while each thread has one owner, messages within that thread can have different `resourceId` values. This is used for message attribution and filtering (e.g., distinguishing between different agents in a multi-agent system, or filtering messages for analytics).
152
+
153
+ **Security:** Memory is a storage layer, not an authorization layer. Your application must implement access control before calling memory APIs. The `resourceId` parameter controls both validation and filtering - provide it to validate ownership and filter messages, or omit it for server-side access without validation.
154
+
155
+ To avoid accidentally reusing thread IDs across different owners, use UUIDs: `crypto.randomUUID()`
156
+
139
157
  ### Thread title generation
140
158
 
141
159
  Mastra can automatically generate descriptive thread titles based on the user's first message.
@@ -127,7 +127,7 @@ export const agent = new Agent({
127
127
  - [createThread](https://mastra.ai/reference/v1/memory/createThread)
128
128
  - [recall](https://mastra.ai/reference/v1/memory/recall)
129
129
  - [getThreadById](https://mastra.ai/reference/v1/memory/getThreadById)
130
- - [listThreadsByResourceId](https://mastra.ai/reference/v1/memory/listThreadsByResourceId)
130
+ - [listThreads](https://mastra.ai/reference/v1/memory/listThreads)
131
131
  - [deleteMessages](https://mastra.ai/reference/v1/memory/deleteMessages)
132
132
  - [cloneThread](https://mastra.ai/reference/v1/memory/cloneThread)
133
133
  - [Clone Utility Methods](https://mastra.ai/reference/v1/memory/clone-utilities)
@@ -27,7 +27,7 @@ await store.upsert({
27
27
  });
28
28
  ```
29
29
 
30
- ### Using MongoDB Atlas Vector search
30
+ <h3>Using MongoDB Atlas Vector search</h3>
31
31
 
32
32
  For detailed setup instructions and best practices, see the [official MongoDB Atlas Vector Search documentation](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/?utm_campaign=devrel&utm_source=third-party-content&utm_medium=cta&utm_content=mastra-docs).
33
33
 
@@ -55,7 +55,7 @@ await store.upsert({
55
55
  });
56
56
  ```
57
57
 
58
- ### Using PostgreSQL with pgvector
58
+ <h3>Using PostgreSQL with pgvector</h3>
59
59
 
60
60
  PostgreSQL with the pgvector extension is a good solution for teams already using PostgreSQL who want to minimize infrastructure complexity.
61
61
  For detailed setup instructions and best practices, see the [official pgvector repository](https://github.com/pgvector/pgvector).
@@ -323,7 +323,7 @@ await store.upsert({
323
323
  });
324
324
  ```
325
325
 
326
- ### Using LanceDB
326
+ <h3>Using LanceDB</h3>
327
327
 
328
328
  LanceDB is an embedded vector database built on the Lance columnar format, suitable for local development or cloud deployment.
329
329
  For detailed setup instructions and best practices, see the [official LanceDB documentation](https://lancedb.github.io/lancedb/).
package/dist/index.cjs CHANGED
@@ -3219,27 +3219,52 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
3219
3219
  );
3220
3220
  }
3221
3221
  }
3222
- async listThreadsByResourceId(args) {
3223
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
3224
- if (page < 0) {
3222
+ async listThreads(args) {
3223
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
3224
+ try {
3225
+ this.validatePaginationInput(page, perPageInput ?? 100);
3226
+ } catch (error$1) {
3225
3227
  throw new error.MastraError({
3226
- id: storage.createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
3228
+ id: storage.createStorageErrorId("PG", "LIST_THREADS", "INVALID_PAGE"),
3227
3229
  domain: error.ErrorDomain.STORAGE,
3228
3230
  category: error.ErrorCategory.USER,
3229
- text: "Page number must be non-negative",
3230
- details: {
3231
- resourceId,
3232
- page
3233
- }
3231
+ text: error$1 instanceof Error ? error$1.message : "Invalid pagination parameters",
3232
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
3234
3233
  });
3235
3234
  }
3236
- const { field, direction } = this.parseOrderBy(orderBy);
3237
3235
  const perPage = storage.normalizePerPage(perPageInput, 100);
3236
+ try {
3237
+ this.validateMetadataKeys(filter?.metadata);
3238
+ } catch (error$1) {
3239
+ throw new error.MastraError({
3240
+ id: storage.createStorageErrorId("PG", "LIST_THREADS", "INVALID_METADATA_KEY"),
3241
+ domain: error.ErrorDomain.STORAGE,
3242
+ category: error.ErrorCategory.USER,
3243
+ text: error$1 instanceof Error ? error$1.message : "Invalid metadata key",
3244
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
3245
+ });
3246
+ }
3247
+ const { field, direction } = this.parseOrderBy(orderBy);
3238
3248
  const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
3239
3249
  try {
3240
3250
  const tableName = getTableName3({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName3(this.#schema) });
3241
- const baseQuery = `FROM ${tableName} WHERE "resourceId" = $1`;
3242
- const queryParams = [resourceId];
3251
+ const whereClauses = [];
3252
+ const queryParams = [];
3253
+ let paramIndex = 1;
3254
+ if (filter?.resourceId) {
3255
+ whereClauses.push(`"resourceId" = $${paramIndex}`);
3256
+ queryParams.push(filter.resourceId);
3257
+ paramIndex++;
3258
+ }
3259
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
3260
+ for (const [key, value] of Object.entries(filter.metadata)) {
3261
+ whereClauses.push(`metadata::jsonb @> $${paramIndex}::jsonb`);
3262
+ queryParams.push(JSON.stringify({ [key]: value }));
3263
+ paramIndex++;
3264
+ }
3265
+ }
3266
+ const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
3267
+ const baseQuery = `FROM ${tableName} ${whereClause}`;
3243
3268
  const countQuery = `SELECT COUNT(*) ${baseQuery}`;
3244
3269
  const countResult = await this.#db.client.one(countQuery, queryParams);
3245
3270
  const total = parseInt(countResult.count, 10);
@@ -3253,7 +3278,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
3253
3278
  };
3254
3279
  }
3255
3280
  const limitValue = perPageInput === false ? total : perPage;
3256
- const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`;
3281
+ const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $${paramIndex} OFFSET $${paramIndex + 1}`;
3257
3282
  const rows = await this.#db.client.manyOrNone(
3258
3283
  dataQuery,
3259
3284
  [...queryParams, limitValue, offset]
@@ -3277,11 +3302,12 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
3277
3302
  } catch (error$1) {
3278
3303
  const mastraError = new error.MastraError(
3279
3304
  {
3280
- id: storage.createStorageErrorId("PG", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
3305
+ id: storage.createStorageErrorId("PG", "LIST_THREADS", "FAILED"),
3281
3306
  domain: error.ErrorDomain.STORAGE,
3282
3307
  category: error.ErrorCategory.THIRD_PARTY,
3283
3308
  details: {
3284
- resourceId,
3309
+ ...filter?.resourceId && { resourceId: filter.resourceId },
3310
+ hasMetadataFilter: !!filter?.metadata,
3285
3311
  page
3286
3312
  }
3287
3313
  },