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

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,109 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 1.0.0-beta.15
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed duplicate spans migration issue across all storage backends. When upgrading from older versions, existing duplicate (traceId, spanId) combinations in the spans table could prevent the unique constraint from being created. The migration deduplicates spans before adding the constraint. ([#12073](https://github.com/mastra-ai/mastra/pull/12073))
8
+
9
+ **Deduplication rules (in priority order):**
10
+ 1. Keep completed spans (those with `endedAt` set) over incomplete spans
11
+ 2. Among spans with the same completion status, keep the one with the newest `updatedAt`
12
+ 3. Use `createdAt` as the final tiebreaker
13
+
14
+ **What changed:**
15
+ - Added `migrateSpans()` method to observability stores for manual migration
16
+ - Added `checkSpansMigrationStatus()` method to check if migration is needed
17
+ - All stores use optimized single-query deduplication to avoid memory issues on large tables
18
+
19
+ **Usage example:**
20
+
21
+ ```typescript
22
+ const observability = await storage.getStore('observability');
23
+ const status = await observability.checkSpansMigrationStatus();
24
+ if (status.needsMigration) {
25
+ const result = await observability.migrateSpans();
26
+ console.log(`Migration complete: ${result.duplicatesRemoved} duplicates removed`);
27
+ }
28
+ ```
29
+
30
+ Fixes #11840
31
+
32
+ - Renamed MastraStorage to MastraCompositeStore for better clarity. The old MastraStorage name remains available as a deprecated alias for backward compatibility, but will be removed in a future version. ([#12093](https://github.com/mastra-ai/mastra/pull/12093))
33
+
34
+ **Migration:**
35
+
36
+ Update your imports and usage:
37
+
38
+ ```typescript
39
+ // Before
40
+ import { MastraStorage } from '@mastra/core/storage';
41
+
42
+ const storage = new MastraStorage({
43
+ id: 'composite',
44
+ domains: { ... }
45
+ });
46
+
47
+ // After
48
+ import { MastraCompositeStore } from '@mastra/core/storage';
49
+
50
+ const storage = new MastraCompositeStore({
51
+ id: 'composite',
52
+ domains: { ... }
53
+ });
54
+ ```
55
+
56
+ The new name better reflects that this is a composite storage implementation that routes different domains (workflows, traces, messages) to different underlying stores, avoiding confusion with the general "Mastra Storage" concept.
57
+
58
+ - Updated dependencies [[`026b848`](https://github.com/mastra-ai/mastra/commit/026b8483fbf5b6d977be8f7e6aac8d15c75558ac), [`ffa553a`](https://github.com/mastra-ai/mastra/commit/ffa553a3edc1bd17d73669fba66d6b6f4ac10897)]:
59
+ - @mastra/core@1.0.0-beta.26
60
+
61
+ ## 1.0.0-beta.14
62
+
63
+ ### Patch Changes
64
+
65
+ - Added new `listThreads` method for flexible thread filtering across all storage adapters. ([#11832](https://github.com/mastra-ai/mastra/pull/11832))
66
+
67
+ **New Features**
68
+ - Filter threads by `resourceId`, `metadata`, or both (with AND logic for metadata key-value pairs)
69
+ - All filter parameters are optional, allowing you to list all threads or filter as needed
70
+ - Full pagination and sorting support
71
+
72
+ **Example Usage**
73
+
74
+ ```typescript
75
+ // List all threads
76
+ const allThreads = await memory.listThreads({});
77
+
78
+ // Filter by resourceId only
79
+ const userThreads = await memory.listThreads({
80
+ filter: { resourceId: 'user-123' },
81
+ });
82
+
83
+ // Filter by metadata only
84
+ const supportThreads = await memory.listThreads({
85
+ filter: { metadata: { category: 'support' } },
86
+ });
87
+
88
+ // Filter by both with pagination
89
+ const filteredThreads = await memory.listThreads({
90
+ filter: {
91
+ resourceId: 'user-123',
92
+ metadata: { priority: 'high', status: 'open' },
93
+ },
94
+ orderBy: { field: 'updatedAt', direction: 'DESC' },
95
+ page: 0,
96
+ perPage: 20,
97
+ });
98
+ ```
99
+
100
+ **Security Improvements**
101
+ - Added validation to prevent SQL injection via malicious metadata keys
102
+ - Added pagination parameter validation to prevent integer overflow attacks
103
+
104
+ - 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)]:
105
+ - @mastra/core@1.0.0-beta.25
106
+
3
107
  ## 1.0.0-beta.13
4
108
 
5
109
  ### 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.15
@@ -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.15
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.15",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -63,17 +63,17 @@ This is useful when all primitives share the same storage backend and have simil
63
63
 
64
64
  #### Composite storage
65
65
 
66
- Add storage to your Mastra instance using `MastraStorage` and configure individual storage domains to use different storage providers.
66
+ Add storage to your Mastra instance using `MastraCompositeStore` and configure individual storage domains to use different storage providers.
67
67
 
68
68
  ```typescript title="src/mastra/index.ts"
69
69
  import { Mastra } from "@mastra/core";
70
- import { MastraStorage } from "@mastra/core/storage";
70
+ import { MastraCompositeStore } from "@mastra/core/storage";
71
71
  import { MemoryLibSQL } from "@mastra/libsql";
72
72
  import { WorkflowsPG } from "@mastra/pg";
73
73
  import { ObservabilityStorageClickhouse } from "@mastra/clickhouse";
74
74
 
75
75
  export const mastra = new Mastra({
76
- storage: new MastraStorage({
76
+ storage: new MastraCompositeStore({
77
77
  id: "composite",
78
78
  domains: {
79
79
  memory: new MemoryLibSQL({ url: "file:./memory.db" }),
@@ -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/).
@@ -9,11 +9,11 @@
9
9
 
10
10
  > Documentation for combining multiple storage backends in Mastra.
11
11
 
12
- `MastraStorage` can compose storage domains from different providers. Use it when you need different databases for different purposes. For example, use LibSQL for memory and PostgreSQL for workflows.
12
+ `MastraCompositeStore` can compose storage domains from different providers. Use it when you need different databases for different purposes. For example, use LibSQL for memory and PostgreSQL for workflows.
13
13
 
14
14
  ## Installation
15
15
 
16
- `MastraStorage` is included in `@mastra/core`:
16
+ `MastraCompositeStore` is included in `@mastra/core`:
17
17
 
18
18
  ```bash
19
19
  npm install @mastra/core@beta
@@ -44,13 +44,13 @@ Mastra organizes storage into five specialized domains, each handling a specific
44
44
  Import domain classes directly from each store package and compose them:
45
45
 
46
46
  ```typescript title="src/mastra/index.ts"
47
- import { MastraStorage } from "@mastra/core/storage";
47
+ import { MastraCompositeStore } from "@mastra/core/storage";
48
48
  import { WorkflowsPG, ScoresPG } from "@mastra/pg";
49
49
  import { MemoryLibSQL } from "@mastra/libsql";
50
50
  import { Mastra } from "@mastra/core";
51
51
 
52
52
  export const mastra = new Mastra({
53
- storage: new MastraStorage({
53
+ storage: new MastraCompositeStore({
54
54
  id: "composite",
55
55
  domains: {
56
56
  memory: new MemoryLibSQL({ url: "file:./local.db" }),
@@ -66,7 +66,7 @@ export const mastra = new Mastra({
66
66
  Use `default` to specify a fallback storage, then override specific domains:
67
67
 
68
68
  ```typescript title="src/mastra/index.ts"
69
- import { MastraStorage } from "@mastra/core/storage";
69
+ import { MastraCompositeStore } from "@mastra/core/storage";
70
70
  import { PostgresStore } from "@mastra/pg";
71
71
  import { MemoryLibSQL } from "@mastra/libsql";
72
72
  import { Mastra } from "@mastra/core";
@@ -77,7 +77,7 @@ const pgStore = new PostgresStore({
77
77
  });
78
78
 
79
79
  export const mastra = new Mastra({
80
- storage: new MastraStorage({
80
+ storage: new MastraCompositeStore({
81
81
  id: "composite",
82
82
  default: pgStore,
83
83
  domains: {
@@ -91,14 +91,14 @@ export const mastra = new Mastra({
91
91
 
92
92
  ## Initialization
93
93
 
94
- `MastraStorage` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
94
+ `MastraCompositeStore` initializes each configured domain independently. When passed to the Mastra class, `init()` is called automatically:
95
95
 
96
96
  ```typescript title="src/mastra/index.ts"
97
- import { MastraStorage } from "@mastra/core/storage";
97
+ import { MastraCompositeStore } from "@mastra/core/storage";
98
98
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
99
99
  import { Mastra } from "@mastra/core";
100
100
 
101
- const storage = new MastraStorage({
101
+ const storage = new MastraCompositeStore({
102
102
  id: "composite",
103
103
  domains: {
104
104
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
@@ -115,10 +115,10 @@ export const mastra = new Mastra({
115
115
  If using storage directly, call `init()` explicitly:
116
116
 
117
117
  ```typescript
118
- import { MastraStorage } from "@mastra/core/storage";
118
+ import { MastraCompositeStore } from "@mastra/core/storage";
119
119
  import { MemoryPG } from "@mastra/pg";
120
120
 
121
- const storage = new MastraStorage({
121
+ const storage = new MastraCompositeStore({
122
122
  id: "composite",
123
123
  domains: {
124
124
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),
@@ -139,11 +139,11 @@ const thread = await memoryStore?.getThreadById({ threadId: "..." });
139
139
  Use a local database for development while keeping production data in a managed service:
140
140
 
141
141
  ```typescript
142
- import { MastraStorage } from "@mastra/core/storage";
142
+ import { MastraCompositeStore } from "@mastra/core/storage";
143
143
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
144
144
  import { MemoryLibSQL } from "@mastra/libsql";
145
145
 
146
- const storage = new MastraStorage({
146
+ const storage = new MastraCompositeStore({
147
147
  id: "composite",
148
148
  domains: {
149
149
  // Use local SQLite for development, PostgreSQL for production
@@ -162,11 +162,11 @@ const storage = new MastraStorage({
162
162
  Use a time-series database for traces while keeping other data in PostgreSQL:
163
163
 
164
164
  ```typescript
165
- import { MastraStorage } from "@mastra/core/storage";
165
+ import { MastraCompositeStore } from "@mastra/core/storage";
166
166
  import { MemoryPG, WorkflowsPG, ScoresPG } from "@mastra/pg";
167
167
  import { ObservabilityStorageClickhouse } from "@mastra/clickhouse";
168
168
 
169
- const storage = new MastraStorage({
169
+ const storage = new MastraCompositeStore({
170
170
  id: "composite",
171
171
  domains: {
172
172
  memory: new MemoryPG({ connectionString: process.env.DATABASE_URL }),