@mastra/mongodb 0.0.5-alpha.2 → 0.0.5-alpha.4
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +18 -0
- package/dist/_tsup-dts-rollup.d.cts +43 -0
- package/dist/_tsup-dts-rollup.d.ts +43 -0
- package/dist/index.cjs +79 -19
- package/dist/index.js +79 -19
- package/package.json +2 -2
- package/src/vector/index.test.ts +12 -5
- package/src/vector/index.ts +88 -22
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/mongodb@0.0.5-alpha.
|
|
2
|
+
> @mastra/mongodb@0.0.5-alpha.4 build /home/runner/work/mastra/mastra/stores/mongodb
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.4.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 6531ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.3
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 8779ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
20
|
+
[32mESM[39m [1mdist/index.js [22m[32m19.08 KB[39m
|
|
21
|
+
[32mESM[39m ⚡️ Build success in 672ms
|
|
22
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m19.13 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 860ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @mastra/mongodb
|
|
2
2
|
|
|
3
|
+
## 0.0.5-alpha.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [3e9c131]
|
|
8
|
+
- @mastra/core@0.9.4-alpha.4
|
|
9
|
+
|
|
10
|
+
## 0.0.5-alpha.3
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- c3bd795: [MASTRA-3358] Deprecate updateIndexById and deleteIndexById
|
|
15
|
+
- Updated dependencies [396be50]
|
|
16
|
+
- Updated dependencies [c3bd795]
|
|
17
|
+
- Updated dependencies [da082f8]
|
|
18
|
+
- Updated dependencies [a5810ce]
|
|
19
|
+
- @mastra/core@0.9.4-alpha.3
|
|
20
|
+
|
|
3
21
|
## 0.0.5-alpha.2
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -80,11 +80,54 @@ declare class MongoDBVector extends MastraVector {
|
|
|
80
80
|
listIndexes(): Promise<string[]>;
|
|
81
81
|
describeIndex(indexName: string): Promise<IndexStats>;
|
|
82
82
|
deleteIndex(indexName: string): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
85
|
+
*
|
|
86
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
87
|
+
* @param indexName - The name of the index containing the vector.
|
|
88
|
+
* @param id - The ID of the vector to update.
|
|
89
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
90
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
91
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
92
|
+
* @returns A promise that resolves when the update is complete.
|
|
93
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
94
|
+
*/
|
|
83
95
|
updateIndexById(indexName: string, id: string, update: {
|
|
84
96
|
vector?: number[];
|
|
85
97
|
metadata?: Record<string, any>;
|
|
86
98
|
}): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
101
|
+
* @param indexName - The name of the index containing the vector.
|
|
102
|
+
* @param id - The ID of the vector to update.
|
|
103
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
104
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
105
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
106
|
+
* @returns A promise that resolves when the update is complete.
|
|
107
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
108
|
+
*/
|
|
109
|
+
updateVector(indexName: string, id: string, update: {
|
|
110
|
+
vector?: number[];
|
|
111
|
+
metadata?: Record<string, any>;
|
|
112
|
+
}): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
115
|
+
*
|
|
116
|
+
* Deletes a vector by its ID.
|
|
117
|
+
* @param indexName - The name of the index containing the vector.
|
|
118
|
+
* @param id - The ID of the vector to delete.
|
|
119
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
120
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
121
|
+
*/
|
|
87
122
|
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Deletes a vector by its ID.
|
|
125
|
+
* @param indexName - The name of the index containing the vector.
|
|
126
|
+
* @param id - The ID of the vector to delete.
|
|
127
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
128
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
129
|
+
*/
|
|
130
|
+
deleteVector(indexName: string, id: string): Promise<void>;
|
|
88
131
|
private getCollection;
|
|
89
132
|
private validateVectorDimensions;
|
|
90
133
|
private setIndexDimension;
|
|
@@ -80,11 +80,54 @@ declare class MongoDBVector extends MastraVector {
|
|
|
80
80
|
listIndexes(): Promise<string[]>;
|
|
81
81
|
describeIndex(indexName: string): Promise<IndexStats>;
|
|
82
82
|
deleteIndex(indexName: string): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
85
|
+
*
|
|
86
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
87
|
+
* @param indexName - The name of the index containing the vector.
|
|
88
|
+
* @param id - The ID of the vector to update.
|
|
89
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
90
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
91
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
92
|
+
* @returns A promise that resolves when the update is complete.
|
|
93
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
94
|
+
*/
|
|
83
95
|
updateIndexById(indexName: string, id: string, update: {
|
|
84
96
|
vector?: number[];
|
|
85
97
|
metadata?: Record<string, any>;
|
|
86
98
|
}): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
101
|
+
* @param indexName - The name of the index containing the vector.
|
|
102
|
+
* @param id - The ID of the vector to update.
|
|
103
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
104
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
105
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
106
|
+
* @returns A promise that resolves when the update is complete.
|
|
107
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
108
|
+
*/
|
|
109
|
+
updateVector(indexName: string, id: string, update: {
|
|
110
|
+
vector?: number[];
|
|
111
|
+
metadata?: Record<string, any>;
|
|
112
|
+
}): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
115
|
+
*
|
|
116
|
+
* Deletes a vector by its ID.
|
|
117
|
+
* @param indexName - The name of the index containing the vector.
|
|
118
|
+
* @param id - The ID of the vector to delete.
|
|
119
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
120
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
121
|
+
*/
|
|
87
122
|
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Deletes a vector by its ID.
|
|
125
|
+
* @param indexName - The name of the index containing the vector.
|
|
126
|
+
* @param id - The ID of the vector to delete.
|
|
127
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
128
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
129
|
+
*/
|
|
130
|
+
deleteVector(indexName: string, id: string): Promise<void>;
|
|
88
131
|
private getCollection;
|
|
89
132
|
private validateVectorDimensions;
|
|
90
133
|
private setIndexDimension;
|
package/dist/index.cjs
CHANGED
|
@@ -290,33 +290,93 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
290
290
|
throw new Error(`Index (Collection) "${indexName}" does not exist`);
|
|
291
291
|
}
|
|
292
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
295
|
+
*
|
|
296
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
297
|
+
* @param indexName - The name of the index containing the vector.
|
|
298
|
+
* @param id - The ID of the vector to update.
|
|
299
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
300
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
301
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
302
|
+
* @returns A promise that resolves when the update is complete.
|
|
303
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
304
|
+
*/
|
|
293
305
|
async updateIndexById(indexName, id, update) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
306
|
+
this.logger.warn(
|
|
307
|
+
`Deprecation Warning: updateIndexById() is deprecated.
|
|
308
|
+
Please use updateVector() instead.
|
|
309
|
+
updateIndexById() will be removed on May 20th, 2025.`
|
|
310
|
+
);
|
|
311
|
+
await this.updateVector(indexName, id, update);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
315
|
+
* @param indexName - The name of the index containing the vector.
|
|
316
|
+
* @param id - The ID of the vector to update.
|
|
317
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
318
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
319
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
320
|
+
* @returns A promise that resolves when the update is complete.
|
|
321
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
322
|
+
*/
|
|
323
|
+
async updateVector(indexName, id, update) {
|
|
324
|
+
try {
|
|
325
|
+
if (!update.vector && !update.metadata) {
|
|
326
|
+
throw new Error("No updates provided");
|
|
327
|
+
}
|
|
328
|
+
const collection = await this.getCollection(indexName, true);
|
|
329
|
+
const updateDoc = {};
|
|
330
|
+
if (update.vector) {
|
|
331
|
+
const stats = await this.describeIndex(indexName);
|
|
332
|
+
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
333
|
+
updateDoc[this.embeddingFieldName] = update.vector;
|
|
334
|
+
}
|
|
335
|
+
if (update.metadata) {
|
|
336
|
+
const normalizedMeta = Object.keys(update.metadata).reduce(
|
|
337
|
+
(acc, key) => {
|
|
338
|
+
acc[key] = update.metadata[key] instanceof Date ? update.metadata[key].toISOString() : update.metadata[key];
|
|
339
|
+
return acc;
|
|
340
|
+
},
|
|
341
|
+
{}
|
|
342
|
+
);
|
|
343
|
+
updateDoc[this.metadataFieldName] = normalizedMeta;
|
|
344
|
+
}
|
|
345
|
+
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
346
|
+
} catch (error) {
|
|
347
|
+
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
311
348
|
}
|
|
312
|
-
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
313
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
352
|
+
*
|
|
353
|
+
* Deletes a vector by its ID.
|
|
354
|
+
* @param indexName - The name of the index containing the vector.
|
|
355
|
+
* @param id - The ID of the vector to delete.
|
|
356
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
357
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
358
|
+
*/
|
|
314
359
|
async deleteIndexById(indexName, id) {
|
|
360
|
+
this.logger.warn(
|
|
361
|
+
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
362
|
+
Please use deleteVector() instead.
|
|
363
|
+
deleteIndexById() will be removed on May 20th, 2025.`
|
|
364
|
+
);
|
|
365
|
+
await this.deleteVector(indexName, id);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Deletes a vector by its ID.
|
|
369
|
+
* @param indexName - The name of the index containing the vector.
|
|
370
|
+
* @param id - The ID of the vector to delete.
|
|
371
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
372
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
373
|
+
*/
|
|
374
|
+
async deleteVector(indexName, id) {
|
|
315
375
|
try {
|
|
316
376
|
const collection = await this.getCollection(indexName, true);
|
|
317
377
|
await collection.deleteOne({ _id: id });
|
|
318
378
|
} catch (error) {
|
|
319
|
-
throw new Error(`Failed to delete
|
|
379
|
+
throw new Error(`Failed to delete vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
320
380
|
}
|
|
321
381
|
}
|
|
322
382
|
// Private methods
|
package/dist/index.js
CHANGED
|
@@ -288,33 +288,93 @@ var MongoDBVector = class extends MastraVector {
|
|
|
288
288
|
throw new Error(`Index (Collection) "${indexName}" does not exist`);
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
293
|
+
*
|
|
294
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
295
|
+
* @param indexName - The name of the index containing the vector.
|
|
296
|
+
* @param id - The ID of the vector to update.
|
|
297
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
298
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
299
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
300
|
+
* @returns A promise that resolves when the update is complete.
|
|
301
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
302
|
+
*/
|
|
291
303
|
async updateIndexById(indexName, id, update) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
304
|
+
this.logger.warn(
|
|
305
|
+
`Deprecation Warning: updateIndexById() is deprecated.
|
|
306
|
+
Please use updateVector() instead.
|
|
307
|
+
updateIndexById() will be removed on May 20th, 2025.`
|
|
308
|
+
);
|
|
309
|
+
await this.updateVector(indexName, id, update);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
313
|
+
* @param indexName - The name of the index containing the vector.
|
|
314
|
+
* @param id - The ID of the vector to update.
|
|
315
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
316
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
317
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
318
|
+
* @returns A promise that resolves when the update is complete.
|
|
319
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
320
|
+
*/
|
|
321
|
+
async updateVector(indexName, id, update) {
|
|
322
|
+
try {
|
|
323
|
+
if (!update.vector && !update.metadata) {
|
|
324
|
+
throw new Error("No updates provided");
|
|
325
|
+
}
|
|
326
|
+
const collection = await this.getCollection(indexName, true);
|
|
327
|
+
const updateDoc = {};
|
|
328
|
+
if (update.vector) {
|
|
329
|
+
const stats = await this.describeIndex(indexName);
|
|
330
|
+
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
331
|
+
updateDoc[this.embeddingFieldName] = update.vector;
|
|
332
|
+
}
|
|
333
|
+
if (update.metadata) {
|
|
334
|
+
const normalizedMeta = Object.keys(update.metadata).reduce(
|
|
335
|
+
(acc, key) => {
|
|
336
|
+
acc[key] = update.metadata[key] instanceof Date ? update.metadata[key].toISOString() : update.metadata[key];
|
|
337
|
+
return acc;
|
|
338
|
+
},
|
|
339
|
+
{}
|
|
340
|
+
);
|
|
341
|
+
updateDoc[this.metadataFieldName] = normalizedMeta;
|
|
342
|
+
}
|
|
343
|
+
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
344
|
+
} catch (error) {
|
|
345
|
+
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
309
346
|
}
|
|
310
|
-
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
311
347
|
}
|
|
348
|
+
/**
|
|
349
|
+
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
350
|
+
*
|
|
351
|
+
* Deletes a vector by its ID.
|
|
352
|
+
* @param indexName - The name of the index containing the vector.
|
|
353
|
+
* @param id - The ID of the vector to delete.
|
|
354
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
355
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
356
|
+
*/
|
|
312
357
|
async deleteIndexById(indexName, id) {
|
|
358
|
+
this.logger.warn(
|
|
359
|
+
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
360
|
+
Please use deleteVector() instead.
|
|
361
|
+
deleteIndexById() will be removed on May 20th, 2025.`
|
|
362
|
+
);
|
|
363
|
+
await this.deleteVector(indexName, id);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Deletes a vector by its ID.
|
|
367
|
+
* @param indexName - The name of the index containing the vector.
|
|
368
|
+
* @param id - The ID of the vector to delete.
|
|
369
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
370
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
371
|
+
*/
|
|
372
|
+
async deleteVector(indexName, id) {
|
|
313
373
|
try {
|
|
314
374
|
const collection = await this.getCollection(indexName, true);
|
|
315
375
|
await collection.deleteOne({ _id: id });
|
|
316
376
|
} catch (error) {
|
|
317
|
-
throw new Error(`Failed to delete
|
|
377
|
+
throw new Error(`Failed to delete vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
318
378
|
}
|
|
319
379
|
}
|
|
320
380
|
// Private methods
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/mongodb",
|
|
3
|
-
"version": "0.0.5-alpha.
|
|
3
|
+
"version": "0.0.5-alpha.4",
|
|
4
4
|
"description": "MongoDB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"cloudflare": "^4.1.0",
|
|
24
24
|
"mongodb": "^6.15.0",
|
|
25
25
|
"uuid": "^11.1.0",
|
|
26
|
-
"@mastra/core": "^0.9.4-alpha.
|
|
26
|
+
"@mastra/core": "^0.9.4-alpha.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@microsoft/api-extractor": "^7.52.5",
|
package/src/vector/index.test.ts
CHANGED
|
@@ -379,7 +379,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
379
379
|
const idToBeUpdated = ids[0];
|
|
380
380
|
const newVector = [1, 2, 3, 4];
|
|
381
381
|
const newMetaData = { test: 'updates' };
|
|
382
|
-
await vectorDB.
|
|
382
|
+
await vectorDB.updateVector(indexName, idToBeUpdated, { vector: newVector, metadata: newMetaData });
|
|
383
383
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
384
384
|
const results = await vectorDB.query({
|
|
385
385
|
indexName,
|
|
@@ -399,7 +399,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
399
399
|
expect(ids).toHaveLength(4);
|
|
400
400
|
const idToBeUpdated = ids[0];
|
|
401
401
|
const newMetaData = { test: 'metadata only update' };
|
|
402
|
-
await vectorDB.
|
|
402
|
+
await vectorDB.updateVector(indexName, idToBeUpdated, { metadata: newMetaData });
|
|
403
403
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
404
404
|
const results = await vectorDB.query({
|
|
405
405
|
indexName,
|
|
@@ -419,7 +419,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
419
419
|
expect(ids).toHaveLength(4);
|
|
420
420
|
const idToBeUpdated = ids[0];
|
|
421
421
|
const newVector = [1, 2, 3, 4];
|
|
422
|
-
await vectorDB.
|
|
422
|
+
await vectorDB.updateVector(indexName, idToBeUpdated, { vector: newVector });
|
|
423
423
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
424
424
|
const results = await vectorDB.query({
|
|
425
425
|
indexName,
|
|
@@ -434,15 +434,22 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
434
434
|
expect(updatedResult?.vector).toEqual(newVector);
|
|
435
435
|
});
|
|
436
436
|
it('should throw exception when no updates are given', async () => {
|
|
437
|
-
await expect(vectorDB.
|
|
437
|
+
await expect(vectorDB.updateVector(indexName, 'nonexistent-id', {})).rejects.toThrow('No updates provided');
|
|
438
438
|
});
|
|
439
439
|
it('should delete the vector by id', async () => {
|
|
440
440
|
const ids = await vectorDB.upsert({ indexName, vectors: testVectors });
|
|
441
441
|
expect(ids).toHaveLength(4);
|
|
442
442
|
const idToBeDeleted = ids[0];
|
|
443
|
-
|
|
443
|
+
|
|
444
|
+
const initialStats = await vectorDB.describeIndex(indexName);
|
|
445
|
+
|
|
446
|
+
await vectorDB.deleteVector(indexName, idToBeDeleted);
|
|
444
447
|
const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0, 0], topK: 2 });
|
|
445
448
|
expect(results.map(res => res.id)).not.toContain(idToBeDeleted);
|
|
449
|
+
|
|
450
|
+
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for count to update
|
|
451
|
+
const finalStats = await vectorDB.describeIndex(indexName);
|
|
452
|
+
expect(finalStats.count).toBe(initialStats.count - 1);
|
|
446
453
|
});
|
|
447
454
|
});
|
|
448
455
|
|
package/src/vector/index.ts
CHANGED
|
@@ -284,45 +284,111 @@ export class MongoDBVector extends MastraVector {
|
|
|
284
284
|
}
|
|
285
285
|
}
|
|
286
286
|
|
|
287
|
+
/**
|
|
288
|
+
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
289
|
+
*
|
|
290
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
291
|
+
* @param indexName - The name of the index containing the vector.
|
|
292
|
+
* @param id - The ID of the vector to update.
|
|
293
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
294
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
295
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
296
|
+
* @returns A promise that resolves when the update is complete.
|
|
297
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
298
|
+
*/
|
|
287
299
|
async updateIndexById(
|
|
288
300
|
indexName: string,
|
|
289
301
|
id: string,
|
|
290
302
|
update: { vector?: number[]; metadata?: Record<string, any> },
|
|
291
303
|
): Promise<void> {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
304
|
+
this.logger.warn(
|
|
305
|
+
`Deprecation Warning: updateIndexById() is deprecated.
|
|
306
|
+
Please use updateVector() instead.
|
|
307
|
+
updateIndexById() will be removed on May 20th, 2025.`,
|
|
308
|
+
);
|
|
309
|
+
await this.updateVector(indexName, id, update);
|
|
310
|
+
}
|
|
295
311
|
|
|
296
|
-
|
|
297
|
-
|
|
312
|
+
/**
|
|
313
|
+
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
314
|
+
* @param indexName - The name of the index containing the vector.
|
|
315
|
+
* @param id - The ID of the vector to update.
|
|
316
|
+
* @param update - An object containing the vector and/or metadata to update.
|
|
317
|
+
* @param update.vector - An optional array of numbers representing the new vector.
|
|
318
|
+
* @param update.metadata - An optional record containing the new metadata.
|
|
319
|
+
* @returns A promise that resolves when the update is complete.
|
|
320
|
+
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
321
|
+
*/
|
|
322
|
+
async updateVector(
|
|
323
|
+
indexName: string,
|
|
324
|
+
id: string,
|
|
325
|
+
update: { vector?: number[]; metadata?: Record<string, any> },
|
|
326
|
+
): Promise<void> {
|
|
327
|
+
try {
|
|
328
|
+
if (!update.vector && !update.metadata) {
|
|
329
|
+
throw new Error('No updates provided');
|
|
330
|
+
}
|
|
298
331
|
|
|
299
|
-
|
|
300
|
-
updateDoc
|
|
301
|
-
}
|
|
332
|
+
const collection = await this.getCollection(indexName, true);
|
|
333
|
+
const updateDoc: Record<string, any> = {};
|
|
302
334
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
update.metadata![key] instanceof Date ? update.metadata![key].toISOString() : update.metadata![key];
|
|
309
|
-
return acc;
|
|
310
|
-
},
|
|
311
|
-
{} as Record<string, any>,
|
|
312
|
-
);
|
|
335
|
+
if (update.vector) {
|
|
336
|
+
const stats = await this.describeIndex(indexName);
|
|
337
|
+
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
338
|
+
updateDoc[this.embeddingFieldName] = update.vector;
|
|
339
|
+
}
|
|
313
340
|
|
|
314
|
-
|
|
315
|
-
|
|
341
|
+
if (update.metadata) {
|
|
342
|
+
// Normalize metadata in updates too
|
|
343
|
+
const normalizedMeta = Object.keys(update.metadata).reduce(
|
|
344
|
+
(acc, key) => {
|
|
345
|
+
acc[key] =
|
|
346
|
+
update.metadata![key] instanceof Date ? update.metadata![key].toISOString() : update.metadata![key];
|
|
347
|
+
return acc;
|
|
348
|
+
},
|
|
349
|
+
{} as Record<string, any>,
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
updateDoc[this.metadataFieldName] = normalizedMeta;
|
|
353
|
+
}
|
|
316
354
|
|
|
317
|
-
|
|
355
|
+
await collection.findOneAndUpdate({ _id: id }, { $set: updateDoc });
|
|
356
|
+
} catch (error: any) {
|
|
357
|
+
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
358
|
+
}
|
|
318
359
|
}
|
|
319
360
|
|
|
361
|
+
/**
|
|
362
|
+
* @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
|
|
363
|
+
*
|
|
364
|
+
* Deletes a vector by its ID.
|
|
365
|
+
* @param indexName - The name of the index containing the vector.
|
|
366
|
+
* @param id - The ID of the vector to delete.
|
|
367
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
368
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
369
|
+
*/
|
|
320
370
|
async deleteIndexById(indexName: string, id: string): Promise<void> {
|
|
371
|
+
this.logger.warn(
|
|
372
|
+
`Deprecation Warning: deleteIndexById() is deprecated.
|
|
373
|
+
Please use deleteVector() instead.
|
|
374
|
+
deleteIndexById() will be removed on May 20th, 2025.`,
|
|
375
|
+
);
|
|
376
|
+
await this.deleteVector(indexName, id);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Deletes a vector by its ID.
|
|
381
|
+
* @param indexName - The name of the index containing the vector.
|
|
382
|
+
* @param id - The ID of the vector to delete.
|
|
383
|
+
* @returns A promise that resolves when the deletion is complete.
|
|
384
|
+
* @throws Will throw an error if the deletion operation fails.
|
|
385
|
+
*/
|
|
386
|
+
async deleteVector(indexName: string, id: string): Promise<void> {
|
|
321
387
|
try {
|
|
322
388
|
const collection = await this.getCollection(indexName, true);
|
|
323
389
|
await collection.deleteOne({ _id: id });
|
|
324
390
|
} catch (error: any) {
|
|
325
|
-
throw new Error(`Failed to delete
|
|
391
|
+
throw new Error(`Failed to delete vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
326
392
|
}
|
|
327
393
|
}
|
|
328
394
|
|