@mastra/mongodb 1.17.0-alpha.0 → 1.17.0-alpha.1

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,14 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 1.17.0-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed resource-scoped message includes across storage adapters so included context cannot cross resource boundaries. ([#20984](https://github.com/mastra-ai/mastra/pull/20984))
8
+
9
+ - Updated dependencies [[`6445eba`](https://github.com/mastra-ai/mastra/commit/6445eba6020abac681aba1cc9289f446cb400cbe), [`df31eb0`](https://github.com/mastra-ai/mastra/commit/df31eb0c7087d782a0d9346e467f9a4af4b0eef6), [`fcd0667`](https://github.com/mastra-ai/mastra/commit/fcd0667a4e378be35c9a1b1eb19cce78fbfd7282), [`bab06b1`](https://github.com/mastra-ai/mastra/commit/bab06b18923873a584bdfc71a6b4ec7fb4727fb7)]:
10
+ - @mastra/core@1.58.0-alpha.5
11
+
3
12
  ## 1.17.0-alpha.0
4
13
 
5
14
  ### Minor Changes
@@ -3,7 +3,7 @@ name: mastra-mongodb
3
3
  description: Documentation for @mastra/mongodb. Use when working with @mastra/mongodb APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/mongodb"
6
- version: "1.17.0-alpha.0"
6
+ version: "1.17.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.17.0-alpha.0",
2
+ "version": "1.17.0-alpha.1",
3
3
  "package": "@mastra/mongodb",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -251,6 +251,64 @@ const memoryStore = await storage.getStore('memory')
251
251
  const thread = await memoryStore?.getThreadById({ threadId: '...' })
252
252
  ```
253
253
 
254
+ ## Closing connections
255
+
256
+ `close()` releases the connections of the stores a composite was built from: the `default` and `editor` stores, plus any domain that owns its own client. Each store is closed once, even when it backs several domains. When passed to the Mastra class, `close()` is called by `shutdown()`:
257
+
258
+ ```typescript
259
+ import { MastraCompositeStore } from '@mastra/core/storage'
260
+ import { PostgresStore } from '@mastra/pg'
261
+ import { Mastra } from '@mastra/core'
262
+
263
+ const pgStore = new PostgresStore({
264
+ id: 'pg-storage',
265
+ connectionString: process.env.DATABASE_URL,
266
+ })
267
+
268
+ export const mastra = new Mastra({
269
+ storage: new MastraCompositeStore({ id: 'composite', default: pgStore }),
270
+ })
271
+
272
+ process.on('SIGTERM', async () => {
273
+ // Releases the Postgres pool, so the process can exit
274
+ await mastra.shutdown()
275
+ })
276
+ ```
277
+
278
+ A store you construct only to supply a domain isn't reachable through the composite. Keep a reference to it and close it yourself:
279
+
280
+ ```typescript
281
+ import { MastraCompositeStore } from '@mastra/core/storage'
282
+ import { ClickhouseStore } from '@mastra/clickhouse'
283
+ import { PostgresStore } from '@mastra/pg'
284
+ import { Mastra } from '@mastra/core'
285
+
286
+ const pgStore = new PostgresStore({
287
+ id: 'pg-storage',
288
+ connectionString: process.env.DATABASE_URL,
289
+ })
290
+
291
+ const clickhouseStore = new ClickhouseStore({
292
+ id: 'clickhouse-storage',
293
+ url: process.env.CLICKHOUSE_URL,
294
+ username: process.env.CLICKHOUSE_USERNAME,
295
+ password: process.env.CLICKHOUSE_PASSWORD,
296
+ })
297
+
298
+ export const mastra = new Mastra({
299
+ storage: new MastraCompositeStore({
300
+ id: 'composite',
301
+ default: pgStore,
302
+ domains: { observability: clickhouseStore.stores?.observability },
303
+ }),
304
+ })
305
+
306
+ process.on('SIGTERM', async () => {
307
+ await mastra.shutdown()
308
+ await clickhouseStore.close()
309
+ })
310
+ ```
311
+
254
312
  ## Use cases
255
313
 
256
314
  ### Separate databases for different workloads
package/dist/index.cjs CHANGED
@@ -10,7 +10,7 @@ let _mastra_core_agent = require("@mastra/core/agent");
10
10
  let _mastra_core_evals = require("@mastra/core/evals");
11
11
  let _mastra_core_storage_domains_skills = require("@mastra/core/storage/domains/skills");
12
12
  //#region package.json
13
- var version = "1.17.0-alpha.0";
13
+ var version = "1.17.0-alpha.1";
14
14
  //#endregion
15
15
  //#region src/vector/filter.ts
16
16
  /**
@@ -5405,12 +5405,23 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5405
5405
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
5406
5406
  });
5407
5407
  }
5408
- async _getIncludedMessages({ include }) {
5408
+ /**
5409
+ * Fetches the messages named by `include` together with their surrounding context.
5410
+ *
5411
+ * @param include - Message ids to pin, each with an optional before/after window.
5412
+ * @param resourceId - When set, restricts both the pinned messages and their context
5413
+ * to that resource so an id from another resource returns nothing.
5414
+ */
5415
+ async _getIncludedMessages({ include, resourceId }) {
5409
5416
  if (!include || include.length === 0) return null;
5410
5417
  const collection = await this.getCollection(_mastra_core_storage.TABLE_MESSAGES);
5418
+ const resourceFilter = resourceId ? { resourceId } : {};
5411
5419
  const targetIds = include.map((inc) => inc.id).filter(Boolean);
5412
5420
  if (targetIds.length === 0) return null;
5413
- const targetDocs = await collection.find({ id: { $in: targetIds } }, { projection: {
5421
+ const targetDocs = await collection.find({
5422
+ id: { $in: targetIds },
5423
+ ...resourceFilter
5424
+ }, { projection: {
5414
5425
  id: 1,
5415
5426
  thread_id: 1,
5416
5427
  createdAt: 1
@@ -5427,7 +5438,8 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5427
5438
  if (!target) continue;
5428
5439
  const prevMessages = await collection.find({
5429
5440
  thread_id: target.threadId,
5430
- createdAt: { $lte: target.createdAt }
5441
+ createdAt: { $lte: target.createdAt },
5442
+ ...resourceFilter
5431
5443
  }).sort({
5432
5444
  createdAt: -1,
5433
5445
  id: -1
@@ -5436,7 +5448,8 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5436
5448
  if (withNextMessages > 0) {
5437
5449
  const nextMessages = await collection.find({
5438
5450
  thread_id: target.threadId,
5439
- createdAt: { $gt: target.createdAt }
5451
+ createdAt: { $gt: target.createdAt },
5452
+ ...resourceFilter
5440
5453
  }).sort({
5441
5454
  createdAt: 1,
5442
5455
  id: 1
@@ -5511,7 +5524,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5511
5524
  hasMore: false
5512
5525
  };
5513
5526
  if (perPage === 0 && include && include.length > 0) {
5514
- const includeMessages = await this._getIncludedMessages({ include });
5527
+ const includeMessages = await this._getIncludedMessages({
5528
+ include,
5529
+ resourceId
5530
+ });
5515
5531
  const list = new _mastra_core_agent.MessageList().add(includeMessages ?? [], "memory");
5516
5532
  return {
5517
5533
  messages: this._sortMessages(list.get.all.db(), field, direction),
@@ -5547,7 +5563,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5547
5563
  };
5548
5564
  const messageIds = new Set(messages.map((m) => m.id));
5549
5565
  if (include && include.length > 0) {
5550
- const includeMessages = await this._getIncludedMessages({ include });
5566
+ const includeMessages = await this._getIncludedMessages({
5567
+ include,
5568
+ resourceId
5569
+ });
5551
5570
  if (includeMessages) {
5552
5571
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
5553
5572
  messages.push(includeMsg);
@@ -5628,7 +5647,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5628
5647
  hasMore: false
5629
5648
  };
5630
5649
  if (perPage === 0 && include && include.length > 0) {
5631
- const includeMessages = await this._getIncludedMessages({ include });
5650
+ const includeMessages = await this._getIncludedMessages({
5651
+ include,
5652
+ resourceId
5653
+ });
5632
5654
  if (!includeMessages || includeMessages.length === 0) return {
5633
5655
  messages: [],
5634
5656
  total: 0,
@@ -5671,7 +5693,10 @@ var MemoryStorageMongoDB = class MemoryStorageMongoDB extends _mastra_core_stora
5671
5693
  };
5672
5694
  const messageIds = new Set(messages.map((m) => m.id));
5673
5695
  if (include && include.length > 0) {
5674
- const includeMessages = await this._getIncludedMessages({ include });
5696
+ const includeMessages = await this._getIncludedMessages({
5697
+ include,
5698
+ resourceId
5699
+ });
5675
5700
  if (includeMessages) {
5676
5701
  for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
5677
5702
  messages.push(includeMsg);