@mastra/mongodb 1.12.1-alpha.0 → 1.13.0-alpha.0
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 +37 -0
- package/dist/docs/SKILL.md +4 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-semantic-recall.md +401 -0
- package/dist/docs/references/docs-memory-working-memory.md +3 -3
- package/dist/docs/references/docs-rag-vector-databases.md +7 -2
- package/dist/docs/references/docs-storage-overview.md +214 -0
- package/dist/docs/references/reference-storage-composite.md +337 -0
- package/dist/docs/references/reference-storage-mongodb.md +46 -46
- package/dist/docs/references/reference-vectors-mongodb.md +93 -4
- package/dist/index.cjs +68 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +69 -67
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/package.json +10 -10
|
@@ -32,7 +32,7 @@ bun add @mastra/mongodb@latest
|
|
|
32
32
|
|
|
33
33
|
## Usage
|
|
34
34
|
|
|
35
|
-
Ensure you have a MongoDB Atlas Local (via Docker) or MongoDB Atlas Cloud instance with Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
35
|
+
Ensure you have a [MongoDB Atlas Local (via Docker)](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/) or [MongoDB Atlas Cloud](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-getting-started/) instance with Atlas Search enabled. MongoDB 7.0+ is recommended.
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
38
|
import { MongoDBStore } from '@mastra/mongodb'
|
|
@@ -40,7 +40,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
40
40
|
const storage = new MongoDBStore({
|
|
41
41
|
id: 'mongodb-storage',
|
|
42
42
|
uri: process.env.MONGODB_URI,
|
|
43
|
-
dbName: process.env.
|
|
43
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
44
44
|
})
|
|
45
45
|
```
|
|
46
46
|
|
|
@@ -54,7 +54,15 @@ const storage = new MongoDBStore({
|
|
|
54
54
|
|
|
55
55
|
**dbName** (`string`): The name of the database you want the storage to use.
|
|
56
56
|
|
|
57
|
-
**options** (`MongoClientOptions`): MongoDB client options for advanced configuration (SSL, connection pooling, etc.).
|
|
57
|
+
**options** (`MongoClientOptions`): MongoDB client options for advanced configuration (SSL, connection pooling, etc.). See Connection Options
|
|
58
|
+
|
|
59
|
+
**disableInit** (`boolean`): When true, automatic initialization (collection creation) is disabled. Useful for CI/CD pipelines where you want to run migrations explicitly. You must call storage.init() manually when this is true.
|
|
60
|
+
|
|
61
|
+
**skipDefaultIndexes** (`boolean`): When true, default indexes will not be created during initialization. Useful when managing indexes separately or using only custom indexes.
|
|
62
|
+
|
|
63
|
+
**indexes** (`MongoDBIndexConfig[]`): Custom indexes to create during initialization. Each index must specify a collection, keys, and optional index options. See Indexes
|
|
64
|
+
|
|
65
|
+
**connectorHandler** (`ConnectorHandler`): Custom connection handler for advanced connection management. Alternative to providing uri/dbName directly.
|
|
58
66
|
|
|
59
67
|
> **Deprecation Notice:** The `url` parameter is deprecated but still supported for backward compatibility. Please use `uri` instead in all new code.
|
|
60
68
|
|
|
@@ -84,6 +92,26 @@ const store2 = new MongoDBStore({
|
|
|
84
92
|
socketTimeoutMS: 45000,
|
|
85
93
|
},
|
|
86
94
|
})
|
|
95
|
+
|
|
96
|
+
// With custom indexes
|
|
97
|
+
const store3 = new MongoDBStore({
|
|
98
|
+
id: 'mongodb-storage-03',
|
|
99
|
+
uri: 'mongodb+srv://user:password@cluster.mongodb.net',
|
|
100
|
+
dbName: 'mastra_storage',
|
|
101
|
+
indexes: [
|
|
102
|
+
{ collection: 'mastra_threads', keys: { 'metadata.type': 1 } },
|
|
103
|
+
{ collection: 'mastra_messages', keys: { 'metadata.status': 1 }, options: { sparse: true } },
|
|
104
|
+
],
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// For CI/CD with explicit initialization
|
|
108
|
+
const store4 = new MongoDBStore({
|
|
109
|
+
id: 'mongodb-storage-04',
|
|
110
|
+
uri: 'mongodb+srv://user:password@cluster.mongodb.net',
|
|
111
|
+
dbName: 'mastra_storage',
|
|
112
|
+
disableInit: true, // Disable auto-init
|
|
113
|
+
})
|
|
114
|
+
await store4.init() // Call init explicitly
|
|
87
115
|
```
|
|
88
116
|
|
|
89
117
|
## Additional notes
|
|
@@ -114,7 +142,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
114
142
|
const storage = new MongoDBStore({
|
|
115
143
|
id: 'mongodb-storage',
|
|
116
144
|
uri: process.env.MONGODB_URI,
|
|
117
|
-
dbName: process.env.
|
|
145
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
118
146
|
})
|
|
119
147
|
|
|
120
148
|
const mastra = new Mastra({
|
|
@@ -130,7 +158,7 @@ import { MongoDBStore } from '@mastra/mongodb'
|
|
|
130
158
|
const storage = new MongoDBStore({
|
|
131
159
|
id: 'mongodb-storage',
|
|
132
160
|
uri: process.env.MONGODB_URI,
|
|
133
|
-
dbName: process.env.
|
|
161
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
134
162
|
})
|
|
135
163
|
|
|
136
164
|
// Required when using storage directly
|
|
@@ -143,57 +171,28 @@ const thread = await memoryStore?.getThreadById({ threadId: '...' })
|
|
|
143
171
|
|
|
144
172
|
> **Warning:** If `init()` isn't called, collections won't be created and storage operations will fail silently or throw errors.
|
|
145
173
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
MongoDB storage includes built-in vector search capabilities for AI applications:
|
|
174
|
+
### Connection Management
|
|
149
175
|
|
|
150
|
-
|
|
176
|
+
The `close()` method closes the MongoDB client connection. Call this when shutting down your application:
|
|
151
177
|
|
|
152
178
|
```typescript
|
|
153
|
-
import {
|
|
179
|
+
import { MongoDBStore } from '@mastra/mongodb'
|
|
154
180
|
|
|
155
|
-
const
|
|
156
|
-
id: 'mongodb-
|
|
181
|
+
const storage = new MongoDBStore({
|
|
182
|
+
id: 'mongodb-storage',
|
|
157
183
|
uri: process.env.MONGODB_URI,
|
|
158
|
-
dbName: process.env.
|
|
184
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
159
185
|
})
|
|
160
186
|
|
|
161
|
-
//
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
})
|
|
187
|
+
// Use storage...
|
|
188
|
+
|
|
189
|
+
// Clean up on shutdown
|
|
190
|
+
await storage.close()
|
|
166
191
|
```
|
|
167
192
|
|
|
168
|
-
|
|
193
|
+
## Vector search capabilities
|
|
169
194
|
|
|
170
|
-
|
|
171
|
-
// Store vectors with metadata
|
|
172
|
-
await vectorStore.upsert({
|
|
173
|
-
indexName: "document_embeddings",
|
|
174
|
-
vectors: [
|
|
175
|
-
{
|
|
176
|
-
id: "doc-1",
|
|
177
|
-
values: [0.1, 0.2, 0.3, ...], // 1536-dimensional vector
|
|
178
|
-
metadata: {
|
|
179
|
-
title: "Document Title",
|
|
180
|
-
category: "technical",
|
|
181
|
-
source: "api-docs",
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
// Similarity search
|
|
188
|
-
const results = await vectorStore.query({
|
|
189
|
-
indexName: "document_embeddings",
|
|
190
|
-
vector: queryEmbedding,
|
|
191
|
-
topK: 5,
|
|
192
|
-
filter: {
|
|
193
|
-
category: "technical",
|
|
194
|
-
},
|
|
195
|
-
});
|
|
196
|
-
```
|
|
195
|
+
MongoDB storage includes built-in vector search capabilities for AI applications. For detailed vector operations including index creation, upserting embeddings, similarity search, and metadata filtering, see the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb).
|
|
197
196
|
|
|
198
197
|
## Usage example
|
|
199
198
|
|
|
@@ -214,6 +213,7 @@ export const mongodbAgent = new Agent({
|
|
|
214
213
|
model: 'openai/gpt-5.5',
|
|
215
214
|
memory: new Memory({
|
|
216
215
|
storage: new MongoDBStore({
|
|
216
|
+
id: 'mongodb-storage',
|
|
217
217
|
uri: process.env.MONGODB_URI!,
|
|
218
218
|
dbName: process.env.MONGODB_DB_NAME!,
|
|
219
219
|
}),
|
|
@@ -38,7 +38,7 @@ import { MongoDBVector } from '@mastra/mongodb'
|
|
|
38
38
|
const store = new MongoDBVector({
|
|
39
39
|
id: 'mongodb-vector',
|
|
40
40
|
uri: process.env.MONGODB_URI,
|
|
41
|
-
dbName: process.env.
|
|
41
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
42
42
|
})
|
|
43
43
|
```
|
|
44
44
|
|
|
@@ -52,7 +52,7 @@ import { MongoDBVector } from '@mastra/mongodb'
|
|
|
52
52
|
const store = new MongoDBVector({
|
|
53
53
|
id: 'mongodb-vector',
|
|
54
54
|
uri: process.env.MONGODB_URI,
|
|
55
|
-
dbName: process.env.
|
|
55
|
+
dbName: process.env.MONGODB_DB_NAME,
|
|
56
56
|
embeddingFieldPath: 'text.contentEmbedding', // Store embeddings at text.contentEmbedding
|
|
57
57
|
})
|
|
58
58
|
```
|
|
@@ -71,6 +71,14 @@ const store = new MongoDBVector({
|
|
|
71
71
|
|
|
72
72
|
## Methods
|
|
73
73
|
|
|
74
|
+
### `connect()`
|
|
75
|
+
|
|
76
|
+
Establishes connection to the MongoDB server. This is called automatically on first use, but can be called explicitly if needed.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
await store.connect()
|
|
80
|
+
```
|
|
81
|
+
|
|
74
82
|
### `createIndex()`
|
|
75
83
|
|
|
76
84
|
Creates a new vector index (collection) in MongoDB.
|
|
@@ -81,6 +89,16 @@ Creates a new vector index (collection) in MongoDB.
|
|
|
81
89
|
|
|
82
90
|
**metric** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
83
91
|
|
|
92
|
+
### `waitForIndexReady()`
|
|
93
|
+
|
|
94
|
+
Waits for an index to become ready after creation. Useful when you need to ensure an index is ready before performing operations.
|
|
95
|
+
|
|
96
|
+
**indexName** (`string`): Name of the index to wait for
|
|
97
|
+
|
|
98
|
+
**timeoutMs** (`number`): Maximum time to wait in milliseconds (Default: `60000`)
|
|
99
|
+
|
|
100
|
+
**checkIntervalMs** (`number`): Interval between status checks in milliseconds (Default: `2000`)
|
|
101
|
+
|
|
84
102
|
### `upsert()`
|
|
85
103
|
|
|
86
104
|
Adds or updates vectors and their metadata in the collection.
|
|
@@ -93,6 +111,8 @@ Adds or updates vectors and their metadata in the collection.
|
|
|
93
111
|
|
|
94
112
|
**ids** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
95
113
|
|
|
114
|
+
**documents** (`string[]`): Optional document text content to store alongside vectors
|
|
115
|
+
|
|
96
116
|
### `query()`
|
|
97
117
|
|
|
98
118
|
Searches for similar vectors with optional metadata filtering.
|
|
@@ -109,6 +129,8 @@ Searches for similar vectors with optional metadata filtering.
|
|
|
109
129
|
|
|
110
130
|
**includeVector** (`boolean`): Whether to include vector data in results (Default: `false`)
|
|
111
131
|
|
|
132
|
+
**numCandidates** (`number`): Number of candidates the HNSW graph considers before selecting top-K results. Higher values improve recall at the cost of latency. See: https\://www\.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/ (Default: `20 * topK (capped at 10000)`)
|
|
133
|
+
|
|
112
134
|
### `describeIndex()`
|
|
113
135
|
|
|
114
136
|
Returns information about the index (collection).
|
|
@@ -224,7 +246,7 @@ try {
|
|
|
224
246
|
|
|
225
247
|
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
|
|
226
248
|
|
|
227
|
-
> **Note:**
|
|
249
|
+
> **Note:** MongoDB Atlas Vector Search is recommended for production use. For self-hosted deployments, Vector Search is available with [local Atlas deployments via the Atlas CLI](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/).
|
|
228
250
|
|
|
229
251
|
This setup uses FastEmbed, a local embedding model, to generate vector embeddings. To use this, install `@mastra/fastembed`:
|
|
230
252
|
|
|
@@ -290,6 +312,73 @@ export const mongodbAgent = new Agent({
|
|
|
290
312
|
})
|
|
291
313
|
```
|
|
292
314
|
|
|
315
|
+
### Vector embeddings with VoyageAI
|
|
316
|
+
|
|
317
|
+
VoyageAI provides specialized embedding models optimized for retrieval tasks. VoyageAI is also integrated with MongoDB Atlas for multimodal embeddings.
|
|
318
|
+
|
|
319
|
+
**npm**:
|
|
320
|
+
|
|
321
|
+
```bash
|
|
322
|
+
npm install @mastra/voyageai@latest
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
**pnpm**:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
pnpm add @mastra/voyageai@latest
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
**Yarn**:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
yarn add @mastra/voyageai@latest
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
**Bun**:
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
bun add @mastra/voyageai@latest
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
Basic usage example:
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
import { Memory } from '@mastra/memory'
|
|
347
|
+
import { Agent } from '@mastra/core/agent'
|
|
348
|
+
import { MongoDBStore, MongoDBVector } from '@mastra/mongodb'
|
|
349
|
+
import { voyage } from '@mastra/voyageai'
|
|
350
|
+
|
|
351
|
+
export const mongodbVoyageAgent = new Agent({
|
|
352
|
+
id: 'mongodb-voyage-agent',
|
|
353
|
+
name: 'MongoDB VoyageAI Agent',
|
|
354
|
+
instructions: 'You are an AI agent with semantic recall powered by VoyageAI and MongoDB.',
|
|
355
|
+
model: 'openai/gpt-5.5',
|
|
356
|
+
memory: new Memory({
|
|
357
|
+
storage: new MongoDBStore({
|
|
358
|
+
id: 'mongodb-storage',
|
|
359
|
+
uri: process.env.MONGODB_URI!,
|
|
360
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
361
|
+
}),
|
|
362
|
+
vector: new MongoDBVector({
|
|
363
|
+
id: 'mongodb-vector',
|
|
364
|
+
uri: process.env.MONGODB_URI!,
|
|
365
|
+
dbName: process.env.MONGODB_DB_NAME!,
|
|
366
|
+
}),
|
|
367
|
+
embedder: voyage, // VoyageAI's default model (voyage-3.5, 1024 dimensions)
|
|
368
|
+
options: {
|
|
369
|
+
lastMessages: 10,
|
|
370
|
+
semanticRecall: {
|
|
371
|
+
topK: 5,
|
|
372
|
+
messageRange: 2,
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
}),
|
|
376
|
+
})
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
For comprehensive VoyageAI embedding examples including specialized models, multimodal embeddings, and retrieval optimization, see the [VoyageAI embeddings documentation](https://mastra.ai/models/embeddings).
|
|
380
|
+
|
|
293
381
|
## Related
|
|
294
382
|
|
|
295
|
-
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|
|
383
|
+
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|
|
384
|
+
- [VoyageAI Embeddings Documentation](https://mastra.ai/models/embeddings)
|
package/dist/index.cjs
CHANGED
|
@@ -15,7 +15,7 @@ var skills = require('@mastra/core/storage/domains/skills');
|
|
|
15
15
|
|
|
16
16
|
// package.json
|
|
17
17
|
var package_default = {
|
|
18
|
-
version: "1.
|
|
18
|
+
version: "1.13.0-alpha.0"};
|
|
19
19
|
var MongoDBFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
20
20
|
getSupportedOperators() {
|
|
21
21
|
return {
|
|
@@ -2218,6 +2218,11 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2218
2218
|
keys: { datasetId: 1, datasetVersion: 1 },
|
|
2219
2219
|
options: { name: "idx_dataset_items_datasetid_version" }
|
|
2220
2220
|
},
|
|
2221
|
+
{
|
|
2222
|
+
collection: storage.TABLE_DATASET_ITEMS,
|
|
2223
|
+
keys: { datasetId: 1, externalId: 1, datasetVersion: 1 },
|
|
2224
|
+
options: { name: "idx_dataset_items_datasetid_externalid_version", sparse: true }
|
|
2225
|
+
},
|
|
2221
2226
|
{
|
|
2222
2227
|
collection: storage.TABLE_DATASET_ITEMS,
|
|
2223
2228
|
keys: { datasetId: 1, validTo: 1, isDeleted: 1, createdAt: -1, id: 1 },
|
|
@@ -2288,6 +2293,7 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2288
2293
|
id: row.id,
|
|
2289
2294
|
datasetId: row.datasetId,
|
|
2290
2295
|
datasetVersion: row.datasetVersion,
|
|
2296
|
+
externalId: row.externalId ?? null,
|
|
2291
2297
|
organizationId: row.organizationId ?? null,
|
|
2292
2298
|
projectId: row.projectId ?? null,
|
|
2293
2299
|
input: typeof row.input === "string" ? storage.safelyParseJSON(row.input) : row.input,
|
|
@@ -2319,7 +2325,8 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2319
2325
|
// --- Dataset CRUD ---
|
|
2320
2326
|
async createDataset(input) {
|
|
2321
2327
|
try {
|
|
2322
|
-
const id = crypto$1.randomUUID();
|
|
2328
|
+
const id = input.id ?? crypto$1.randomUUID();
|
|
2329
|
+
if (input.id !== void 0) this.validateCallerDefinedDatasetId(input.id);
|
|
2323
2330
|
const now = /* @__PURE__ */ new Date();
|
|
2324
2331
|
const collection = await this.getCollection(storage.TABLE_DATASETS);
|
|
2325
2332
|
const record = {
|
|
@@ -2344,6 +2351,10 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2344
2351
|
await collection.insertOne(record);
|
|
2345
2352
|
return this.transformDatasetRow(record);
|
|
2346
2353
|
} catch (error$1) {
|
|
2354
|
+
if (input.id !== void 0 && storage.hasErrorCode(error$1, /* @__PURE__ */ new Set([11e3]))) {
|
|
2355
|
+
const existing = await this.getDatasetById({ id: input.id });
|
|
2356
|
+
if (existing) return this.resolveExistingDataset(existing, { ...input, id: input.id });
|
|
2357
|
+
}
|
|
2347
2358
|
if (error$1 instanceof error.MastraError) throw error$1;
|
|
2348
2359
|
throw new error.MastraError(
|
|
2349
2360
|
{
|
|
@@ -2657,6 +2668,7 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2657
2668
|
id: args.id,
|
|
2658
2669
|
datasetId: args.datasetId,
|
|
2659
2670
|
datasetVersion: newVersion,
|
|
2671
|
+
externalId: existing.externalId ?? null,
|
|
2660
2672
|
organizationId: parentOrganizationId,
|
|
2661
2673
|
projectId: parentProjectId,
|
|
2662
2674
|
validTo: null,
|
|
@@ -2753,6 +2765,7 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2753
2765
|
id,
|
|
2754
2766
|
datasetId,
|
|
2755
2767
|
datasetVersion: newVersion,
|
|
2768
|
+
externalId: existing.externalId ?? null,
|
|
2756
2769
|
organizationId: parentOrganizationId,
|
|
2757
2770
|
projectId: parentProjectId,
|
|
2758
2771
|
validTo: null,
|
|
@@ -2794,37 +2807,27 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2794
2807
|
// --- Batch operations ---
|
|
2795
2808
|
async _doBatchInsertItems(input) {
|
|
2796
2809
|
try {
|
|
2797
|
-
|
|
2798
|
-
|
|
2810
|
+
if (input.items.length === 0) return [];
|
|
2811
|
+
const externalIds = [...new Set(input.items.flatMap((item) => item.externalId ? [item.externalId] : []))];
|
|
2812
|
+
if (externalIds.length > 0 && !await this.#connector.supportsTransactions()) {
|
|
2799
2813
|
throw new error.MastraError({
|
|
2800
|
-
id:
|
|
2814
|
+
id: "DATASET_ITEM_IDENTITY_REQUIRES_TRANSACTIONS",
|
|
2801
2815
|
domain: error.ErrorDomain.STORAGE,
|
|
2802
2816
|
category: error.ErrorCategory.USER,
|
|
2803
2817
|
details: { datasetId: input.datasetId }
|
|
2804
2818
|
});
|
|
2805
2819
|
}
|
|
2806
|
-
if (!input.items || input.items.length === 0) {
|
|
2807
|
-
return [];
|
|
2808
|
-
}
|
|
2809
|
-
const now = /* @__PURE__ */ new Date();
|
|
2810
|
-
const versionId = crypto$1.randomUUID();
|
|
2811
|
-
const itemsWithIds = input.items.map((itemInput) => ({
|
|
2812
|
-
generatedId: crypto$1.randomUUID(),
|
|
2813
|
-
itemInput
|
|
2814
|
-
}));
|
|
2815
2820
|
const datasetsCollection = await this.getCollection(storage.TABLE_DATASETS);
|
|
2816
2821
|
const itemsCollection = await this.getCollection(storage.TABLE_DATASET_ITEMS);
|
|
2817
2822
|
const versionsCollection = await this.getCollection(storage.TABLE_DATASET_VERSIONS);
|
|
2818
|
-
let
|
|
2819
|
-
let organizationId = null;
|
|
2820
|
-
let projectId = null;
|
|
2823
|
+
let resolvedItems = [];
|
|
2821
2824
|
await this.#connector.withTransaction(async (session) => {
|
|
2822
|
-
const
|
|
2825
|
+
const dataset = await datasetsCollection.findOneAndUpdate(
|
|
2823
2826
|
{ id: input.datasetId },
|
|
2824
|
-
{ $inc: {
|
|
2827
|
+
{ $inc: { identityRevision: 1 } },
|
|
2825
2828
|
{ session, returnDocument: "after" }
|
|
2826
2829
|
);
|
|
2827
|
-
if (!
|
|
2830
|
+
if (!dataset) {
|
|
2828
2831
|
throw new error.MastraError({
|
|
2829
2832
|
id: storage.createStorageErrorId("MONGODB", "BULK_ADD_ITEMS", "DATASET_NOT_FOUND"),
|
|
2830
2833
|
domain: error.ErrorDomain.STORAGE,
|
|
@@ -2832,54 +2835,52 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2832
2835
|
details: { datasetId: input.datasetId }
|
|
2833
2836
|
});
|
|
2834
2837
|
}
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
projectId,
|
|
2844
|
-
validTo: null,
|
|
2845
|
-
isDeleted: false,
|
|
2846
|
-
input: itemInput.input,
|
|
2847
|
-
groundTruth: itemInput.groundTruth ?? null,
|
|
2848
|
-
expectedTrajectory: itemInput.expectedTrajectory ?? null,
|
|
2849
|
-
toolMocks: itemInput.toolMocks ?? null,
|
|
2850
|
-
requestContext: itemInput.requestContext ?? null,
|
|
2851
|
-
metadata: itemInput.metadata ?? null,
|
|
2852
|
-
source: itemInput.source ?? null,
|
|
2853
|
-
createdAt: now,
|
|
2854
|
-
updatedAt: now
|
|
2855
|
-
}));
|
|
2856
|
-
await itemsCollection.insertMany(docs, { session });
|
|
2857
|
-
await versionsCollection.insertOne(
|
|
2858
|
-
{
|
|
2859
|
-
id: versionId,
|
|
2860
|
-
datasetId: input.datasetId,
|
|
2861
|
-
version: newVersion,
|
|
2862
|
-
createdAt: now
|
|
2863
|
-
},
|
|
2864
|
-
{ session }
|
|
2838
|
+
const historyRows = externalIds.length === 0 ? [] : await itemsCollection.find({ datasetId: input.datasetId, externalId: { $in: externalIds } }, { session }).sort({ datasetVersion: 1 }).toArray();
|
|
2839
|
+
const plan = this.planDatasetItemBatch(
|
|
2840
|
+
input.items,
|
|
2841
|
+
historyRows.map((row) => this.transformItemRowFull(row)),
|
|
2842
|
+
crypto$1.randomUUID
|
|
2843
|
+
);
|
|
2844
|
+
const resolved = new Map(
|
|
2845
|
+
[...plan.existingCurrentItems].map(([id, row]) => [id, this.datasetItemFromRow(row)])
|
|
2865
2846
|
);
|
|
2847
|
+
if (plan.inserts.length > 0) {
|
|
2848
|
+
const now = /* @__PURE__ */ new Date();
|
|
2849
|
+
const newVersion = Number(dataset.version) + 1;
|
|
2850
|
+
await datasetsCollection.updateOne(
|
|
2851
|
+
{ id: input.datasetId },
|
|
2852
|
+
{ $set: { version: newVersion, updatedAt: now } },
|
|
2853
|
+
{ session }
|
|
2854
|
+
);
|
|
2855
|
+
for (const insert of plan.inserts) {
|
|
2856
|
+
const item = {
|
|
2857
|
+
id: insert.id,
|
|
2858
|
+
datasetId: input.datasetId,
|
|
2859
|
+
datasetVersion: newVersion,
|
|
2860
|
+
externalId: insert.item.externalId ?? null,
|
|
2861
|
+
organizationId: dataset.organizationId ?? null,
|
|
2862
|
+
projectId: dataset.projectId ?? null,
|
|
2863
|
+
input: insert.item.input,
|
|
2864
|
+
groundTruth: insert.item.groundTruth,
|
|
2865
|
+
expectedTrajectory: insert.item.expectedTrajectory,
|
|
2866
|
+
toolMocks: insert.item.toolMocks,
|
|
2867
|
+
requestContext: insert.item.requestContext,
|
|
2868
|
+
metadata: insert.item.metadata,
|
|
2869
|
+
source: insert.item.source,
|
|
2870
|
+
createdAt: now,
|
|
2871
|
+
updatedAt: now
|
|
2872
|
+
};
|
|
2873
|
+
await itemsCollection.insertOne({ ...item, validTo: null, isDeleted: false }, { session });
|
|
2874
|
+
resolved.set(item.id, item);
|
|
2875
|
+
}
|
|
2876
|
+
await versionsCollection.insertOne(
|
|
2877
|
+
{ id: crypto$1.randomUUID(), datasetId: input.datasetId, version: newVersion, createdAt: now },
|
|
2878
|
+
{ session }
|
|
2879
|
+
);
|
|
2880
|
+
}
|
|
2881
|
+
resolvedItems = plan.resolvedIds.map((id) => resolved.get(id));
|
|
2866
2882
|
});
|
|
2867
|
-
return
|
|
2868
|
-
id: generatedId,
|
|
2869
|
-
datasetId: input.datasetId,
|
|
2870
|
-
datasetVersion: newVersion,
|
|
2871
|
-
organizationId,
|
|
2872
|
-
projectId,
|
|
2873
|
-
input: itemInput.input,
|
|
2874
|
-
groundTruth: itemInput.groundTruth,
|
|
2875
|
-
expectedTrajectory: itemInput.expectedTrajectory,
|
|
2876
|
-
toolMocks: itemInput.toolMocks,
|
|
2877
|
-
requestContext: itemInput.requestContext,
|
|
2878
|
-
metadata: itemInput.metadata,
|
|
2879
|
-
source: itemInput.source,
|
|
2880
|
-
createdAt: now,
|
|
2881
|
-
updatedAt: now
|
|
2882
|
-
}));
|
|
2883
|
+
return resolvedItems;
|
|
2883
2884
|
} catch (error$1) {
|
|
2884
2885
|
if (error$1 instanceof error.MastraError) throw error$1;
|
|
2885
2886
|
throw new error.MastraError(
|
|
@@ -2938,6 +2939,7 @@ var MongoDBDatasetsStorage = class extends storage.DatasetsStorage {
|
|
|
2938
2939
|
id: item.id,
|
|
2939
2940
|
datasetId: input.datasetId,
|
|
2940
2941
|
datasetVersion: newVersion,
|
|
2942
|
+
externalId: item.externalId ?? null,
|
|
2941
2943
|
organizationId: parentOrganizationId,
|
|
2942
2944
|
projectId: parentProjectId,
|
|
2943
2945
|
validTo: null,
|