@mastra/mongodb 0.0.5 → 0.1.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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +35 -0
- package/dist/_tsup-dts-rollup.d.cts +30 -48
- package/dist/_tsup-dts-rollup.d.ts +30 -48
- package/dist/index.cjs +35 -50
- package/dist/index.d.cts +1 -3
- package/dist/index.d.ts +1 -3
- package/dist/index.js +35 -50
- package/package.json +7 -4
- package/src/vector/index.test.ts +21 -15
- package/src/vector/index.ts +45 -70
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/mongodb@0.0
|
|
2
|
+
> @mastra/mongodb@0.1.0-alpha.1 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 7075ms
|
|
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 8116ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[32mESM[39m [1mdist/index.js [22m[
|
|
21
|
-
[32mESM[39m ⚡️ Build success in
|
|
22
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
23
|
-
[32mCJS[39m ⚡️ Build success in
|
|
20
|
+
[32mESM[39m [1mdist/index.js [22m[32m18.05 KB[39m
|
|
21
|
+
[32mESM[39m ⚡️ Build success in 634ms
|
|
22
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m18.11 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 629ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @mastra/mongodb
|
|
2
2
|
|
|
3
|
+
## 0.1.0-alpha.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 83da932: Move @mastra/core to peerdeps
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- a7292b0: BREAKING(@mastra/core, all vector stores): Vector store breaking changes (remove deprecated functions and positional arguments)
|
|
12
|
+
- Updated dependencies [b3a3d63]
|
|
13
|
+
- Updated dependencies [344f453]
|
|
14
|
+
- Updated dependencies [0a3ae6d]
|
|
15
|
+
- Updated dependencies [95911be]
|
|
16
|
+
- Updated dependencies [5eb5a99]
|
|
17
|
+
- Updated dependencies [7e632c5]
|
|
18
|
+
- Updated dependencies [1e9fbfa]
|
|
19
|
+
- Updated dependencies [b2ae5aa]
|
|
20
|
+
- Updated dependencies [a7292b0]
|
|
21
|
+
- Updated dependencies [0dcb9f0]
|
|
22
|
+
- @mastra/core@0.10.0-alpha.1
|
|
23
|
+
|
|
24
|
+
## 0.0.6-alpha.0
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- d0ee3c6: Change all public functions and constructors in vector stores to use named args and prepare to phase out positional args
|
|
29
|
+
- Updated dependencies [f53a6ac]
|
|
30
|
+
- Updated dependencies [eabdcd9]
|
|
31
|
+
- Updated dependencies [90be034]
|
|
32
|
+
- Updated dependencies [99f050a]
|
|
33
|
+
- Updated dependencies [d0ee3c6]
|
|
34
|
+
- Updated dependencies [23f258c]
|
|
35
|
+
- Updated dependencies [2672a05]
|
|
36
|
+
- @mastra/core@0.9.5-alpha.0
|
|
37
|
+
|
|
3
38
|
## 0.0.5
|
|
4
39
|
|
|
5
40
|
### Patch Changes
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
2
|
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
4
|
+
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
5
|
+
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
3
6
|
import type { IndexStats } from '@mastra/core/vector';
|
|
4
7
|
import { MastraVector } from '@mastra/core/vector';
|
|
5
8
|
import type { MongoClientOptions } from 'mongodb';
|
|
6
9
|
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
7
|
-
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
8
10
|
import type { QueryResult } from '@mastra/core/vector';
|
|
9
|
-
import type { QueryVectorArgs } from '@mastra/core/vector';
|
|
10
11
|
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
-
import type {
|
|
12
|
+
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
12
13
|
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
13
14
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
14
15
|
|
|
@@ -33,9 +34,13 @@ export declare class MongoDBFilterTranslator extends BaseFilterTranslator {
|
|
|
33
34
|
isEmpty(filter: any): boolean;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
declare
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
declare interface MongoDBIndexReadyParams {
|
|
38
|
+
indexName: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
checkIntervalMs?: number;
|
|
41
|
+
}
|
|
42
|
+
export { MongoDBIndexReadyParams }
|
|
43
|
+
export { MongoDBIndexReadyParams as MongoDBIndexReadyParams_alias_1 }
|
|
39
44
|
|
|
40
45
|
declare interface MongoDBQueryVectorParams extends QueryVectorParams {
|
|
41
46
|
documentFilter?: VectorFilter;
|
|
@@ -43,14 +48,6 @@ declare interface MongoDBQueryVectorParams extends QueryVectorParams {
|
|
|
43
48
|
export { MongoDBQueryVectorParams }
|
|
44
49
|
export { MongoDBQueryVectorParams as MongoDBQueryVectorParams_alias_1 }
|
|
45
50
|
|
|
46
|
-
declare type MongoDBUpsertArgs = [...UpsertVectorArgs, string[]?];
|
|
47
|
-
export { MongoDBUpsertArgs }
|
|
48
|
-
export { MongoDBUpsertArgs as MongoDBUpsertArgs_alias_1 }
|
|
49
|
-
|
|
50
|
-
declare type MongoDBUpsertParams = ParamsToArgs<MongoDBUpsertArgs>;
|
|
51
|
-
export { MongoDBUpsertParams }
|
|
52
|
-
export { MongoDBUpsertParams as MongoDBUpsertParams_alias_1 }
|
|
53
|
-
|
|
54
51
|
declare interface MongoDBUpsertVectorParams extends UpsertVectorParams {
|
|
55
52
|
documents?: string[];
|
|
56
53
|
}
|
|
@@ -73,29 +70,27 @@ declare class MongoDBVector extends MastraVector {
|
|
|
73
70
|
});
|
|
74
71
|
connect(): Promise<void>;
|
|
75
72
|
disconnect(): Promise<void>;
|
|
76
|
-
createIndex(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Waits for the index to be ready.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} indexName - The name of the index to wait for
|
|
78
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
79
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
80
|
+
* @returns A promise that resolves when the index is ready
|
|
81
|
+
*/
|
|
82
|
+
waitForIndexReady({ indexName, timeoutMs, checkIntervalMs, }: MongoDBIndexReadyParams): Promise<void>;
|
|
83
|
+
upsert({ indexName, vectors, metadata, ids, documents }: MongoDBUpsertVectorParams): Promise<string[]>;
|
|
84
|
+
query({ indexName, queryVector, topK, filter, includeVector, documentFilter, }: MongoDBQueryVectorParams): Promise<QueryResult[]>;
|
|
80
85
|
listIndexes(): Promise<string[]>;
|
|
81
|
-
describeIndex(indexName: string): Promise<IndexStats>;
|
|
82
|
-
deleteIndex(indexName: string): Promise<void>;
|
|
83
86
|
/**
|
|
84
|
-
*
|
|
87
|
+
* Retrieves statistics about a vector index.
|
|
85
88
|
*
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
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.
|
|
89
|
+
* @param {string} indexName - The name of the index to describe
|
|
90
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
94
91
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
metadata?: Record<string, any>;
|
|
98
|
-
}): Promise<void>;
|
|
92
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
93
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
99
94
|
/**
|
|
100
95
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
101
96
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -106,20 +101,7 @@ declare class MongoDBVector extends MastraVector {
|
|
|
106
101
|
* @returns A promise that resolves when the update is complete.
|
|
107
102
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
108
103
|
*/
|
|
109
|
-
updateVector(indexName
|
|
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
|
-
*/
|
|
122
|
-
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
104
|
+
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
123
105
|
/**
|
|
124
106
|
* Deletes a vector by its ID.
|
|
125
107
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -127,7 +109,7 @@ declare class MongoDBVector extends MastraVector {
|
|
|
127
109
|
* @returns A promise that resolves when the deletion is complete.
|
|
128
110
|
* @throws Will throw an error if the deletion operation fails.
|
|
129
111
|
*/
|
|
130
|
-
deleteVector(indexName
|
|
112
|
+
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
131
113
|
private getCollection;
|
|
132
114
|
private validateVectorDimensions;
|
|
133
115
|
private setIndexDimension;
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
2
|
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
3
|
+
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
4
|
+
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
5
|
+
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
3
6
|
import type { IndexStats } from '@mastra/core/vector';
|
|
4
7
|
import { MastraVector } from '@mastra/core/vector';
|
|
5
8
|
import type { MongoClientOptions } from 'mongodb';
|
|
6
9
|
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
7
|
-
import type { ParamsToArgs } from '@mastra/core/vector';
|
|
8
10
|
import type { QueryResult } from '@mastra/core/vector';
|
|
9
|
-
import type { QueryVectorArgs } from '@mastra/core/vector';
|
|
10
11
|
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
11
|
-
import type {
|
|
12
|
+
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
12
13
|
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
13
14
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
14
15
|
|
|
@@ -33,9 +34,13 @@ export declare class MongoDBFilterTranslator extends BaseFilterTranslator {
|
|
|
33
34
|
isEmpty(filter: any): boolean;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
declare
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
declare interface MongoDBIndexReadyParams {
|
|
38
|
+
indexName: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
checkIntervalMs?: number;
|
|
41
|
+
}
|
|
42
|
+
export { MongoDBIndexReadyParams }
|
|
43
|
+
export { MongoDBIndexReadyParams as MongoDBIndexReadyParams_alias_1 }
|
|
39
44
|
|
|
40
45
|
declare interface MongoDBQueryVectorParams extends QueryVectorParams {
|
|
41
46
|
documentFilter?: VectorFilter;
|
|
@@ -43,14 +48,6 @@ declare interface MongoDBQueryVectorParams extends QueryVectorParams {
|
|
|
43
48
|
export { MongoDBQueryVectorParams }
|
|
44
49
|
export { MongoDBQueryVectorParams as MongoDBQueryVectorParams_alias_1 }
|
|
45
50
|
|
|
46
|
-
declare type MongoDBUpsertArgs = [...UpsertVectorArgs, string[]?];
|
|
47
|
-
export { MongoDBUpsertArgs }
|
|
48
|
-
export { MongoDBUpsertArgs as MongoDBUpsertArgs_alias_1 }
|
|
49
|
-
|
|
50
|
-
declare type MongoDBUpsertParams = ParamsToArgs<MongoDBUpsertArgs>;
|
|
51
|
-
export { MongoDBUpsertParams }
|
|
52
|
-
export { MongoDBUpsertParams as MongoDBUpsertParams_alias_1 }
|
|
53
|
-
|
|
54
51
|
declare interface MongoDBUpsertVectorParams extends UpsertVectorParams {
|
|
55
52
|
documents?: string[];
|
|
56
53
|
}
|
|
@@ -73,29 +70,27 @@ declare class MongoDBVector extends MastraVector {
|
|
|
73
70
|
});
|
|
74
71
|
connect(): Promise<void>;
|
|
75
72
|
disconnect(): Promise<void>;
|
|
76
|
-
createIndex(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Waits for the index to be ready.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} indexName - The name of the index to wait for
|
|
78
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
79
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
80
|
+
* @returns A promise that resolves when the index is ready
|
|
81
|
+
*/
|
|
82
|
+
waitForIndexReady({ indexName, timeoutMs, checkIntervalMs, }: MongoDBIndexReadyParams): Promise<void>;
|
|
83
|
+
upsert({ indexName, vectors, metadata, ids, documents }: MongoDBUpsertVectorParams): Promise<string[]>;
|
|
84
|
+
query({ indexName, queryVector, topK, filter, includeVector, documentFilter, }: MongoDBQueryVectorParams): Promise<QueryResult[]>;
|
|
80
85
|
listIndexes(): Promise<string[]>;
|
|
81
|
-
describeIndex(indexName: string): Promise<IndexStats>;
|
|
82
|
-
deleteIndex(indexName: string): Promise<void>;
|
|
83
86
|
/**
|
|
84
|
-
*
|
|
87
|
+
* Retrieves statistics about a vector index.
|
|
85
88
|
*
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
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.
|
|
89
|
+
* @param {string} indexName - The name of the index to describe
|
|
90
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
94
91
|
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
metadata?: Record<string, any>;
|
|
98
|
-
}): Promise<void>;
|
|
92
|
+
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
93
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
99
94
|
/**
|
|
100
95
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
101
96
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -106,20 +101,7 @@ declare class MongoDBVector extends MastraVector {
|
|
|
106
101
|
* @returns A promise that resolves when the update is complete.
|
|
107
102
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
108
103
|
*/
|
|
109
|
-
updateVector(indexName
|
|
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
|
-
*/
|
|
122
|
-
deleteIndexById(indexName: string, id: string): Promise<void>;
|
|
104
|
+
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
123
105
|
/**
|
|
124
106
|
* Deletes a vector by its ID.
|
|
125
107
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -127,7 +109,7 @@ declare class MongoDBVector extends MastraVector {
|
|
|
127
109
|
* @returns A promise that resolves when the deletion is complete.
|
|
128
110
|
* @throws Will throw an error if the deletion operation fails.
|
|
129
111
|
*/
|
|
130
|
-
deleteVector(indexName
|
|
112
|
+
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
131
113
|
private getCollection;
|
|
132
114
|
private validateVectorDimensions;
|
|
133
115
|
private setIndexDimension;
|
package/dist/index.cjs
CHANGED
|
@@ -121,8 +121,7 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
121
121
|
async disconnect() {
|
|
122
122
|
await this.client.close();
|
|
123
123
|
}
|
|
124
|
-
async createIndex(
|
|
125
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
124
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
126
125
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
127
126
|
throw new Error("Dimension must be a positive integer");
|
|
128
127
|
}
|
|
@@ -160,7 +159,19 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
160
159
|
}
|
|
161
160
|
await collection.updateOne({ _id: "__index_metadata__" }, { $set: { dimension, metric } }, { upsert: true });
|
|
162
161
|
}
|
|
163
|
-
|
|
162
|
+
/**
|
|
163
|
+
* Waits for the index to be ready.
|
|
164
|
+
*
|
|
165
|
+
* @param {string} indexName - The name of the index to wait for
|
|
166
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
167
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
168
|
+
* @returns A promise that resolves when the index is ready
|
|
169
|
+
*/
|
|
170
|
+
async waitForIndexReady({
|
|
171
|
+
indexName,
|
|
172
|
+
timeoutMs = 6e4,
|
|
173
|
+
checkIntervalMs = 2e3
|
|
174
|
+
}) {
|
|
164
175
|
const collection = await this.getCollection(indexName, true);
|
|
165
176
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
166
177
|
const startTime = Date.now();
|
|
@@ -175,11 +186,10 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
175
186
|
}
|
|
176
187
|
throw new Error(`Index "${indexNameInternal}" did not become ready within timeout`);
|
|
177
188
|
}
|
|
178
|
-
async upsert(
|
|
179
|
-
const { indexName, vectors, metadata, ids, documents } = params;
|
|
189
|
+
async upsert({ indexName, vectors, metadata, ids, documents }) {
|
|
180
190
|
const collection = await this.getCollection(indexName);
|
|
181
191
|
this.collectionForValidation = collection;
|
|
182
|
-
const stats = await this.describeIndex(indexName);
|
|
192
|
+
const stats = await this.describeIndex({ indexName });
|
|
183
193
|
await this.validateVectorDimensions(vectors, stats.dimension);
|
|
184
194
|
const generatedIds = ids || vectors.map(() => uuid.v4());
|
|
185
195
|
const operations = vectors.map((vector, idx) => {
|
|
@@ -212,8 +222,14 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
212
222
|
await collection.bulkWrite(operations);
|
|
213
223
|
return generatedIds;
|
|
214
224
|
}
|
|
215
|
-
async query(
|
|
216
|
-
|
|
225
|
+
async query({
|
|
226
|
+
indexName,
|
|
227
|
+
queryVector,
|
|
228
|
+
topK = 10,
|
|
229
|
+
filter,
|
|
230
|
+
includeVector = false,
|
|
231
|
+
documentFilter
|
|
232
|
+
}) {
|
|
217
233
|
const collection = await this.getCollection(indexName, true);
|
|
218
234
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
219
235
|
const mongoFilter = this.transformFilter(filter);
|
|
@@ -269,7 +285,13 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
269
285
|
const collections = await this.db.listCollections().toArray();
|
|
270
286
|
return collections.map((col) => col.name);
|
|
271
287
|
}
|
|
272
|
-
|
|
288
|
+
/**
|
|
289
|
+
* Retrieves statistics about a vector index.
|
|
290
|
+
*
|
|
291
|
+
* @param {string} indexName - The name of the index to describe
|
|
292
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
293
|
+
*/
|
|
294
|
+
async describeIndex({ indexName }) {
|
|
273
295
|
const collection = await this.getCollection(indexName, true);
|
|
274
296
|
const count = await collection.countDocuments({ _id: { $ne: "__index_metadata__" } });
|
|
275
297
|
const metadataDoc = await collection.findOne({ _id: "__index_metadata__" });
|
|
@@ -281,7 +303,7 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
281
303
|
metric
|
|
282
304
|
};
|
|
283
305
|
}
|
|
284
|
-
async deleteIndex(indexName) {
|
|
306
|
+
async deleteIndex({ indexName }) {
|
|
285
307
|
const collection = await this.getCollection(indexName, false);
|
|
286
308
|
if (collection) {
|
|
287
309
|
await collection.drop();
|
|
@@ -291,8 +313,6 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
291
313
|
}
|
|
292
314
|
}
|
|
293
315
|
/**
|
|
294
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
295
|
-
*
|
|
296
316
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
297
317
|
* @param indexName - The name of the index containing the vector.
|
|
298
318
|
* @param id - The ID of the vector to update.
|
|
@@ -302,25 +322,7 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
302
322
|
* @returns A promise that resolves when the update is complete.
|
|
303
323
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
304
324
|
*/
|
|
305
|
-
async
|
|
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) {
|
|
325
|
+
async updateVector({ indexName, id, update }) {
|
|
324
326
|
try {
|
|
325
327
|
if (!update.vector && !update.metadata) {
|
|
326
328
|
throw new Error("No updates provided");
|
|
@@ -328,7 +330,7 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
328
330
|
const collection = await this.getCollection(indexName, true);
|
|
329
331
|
const updateDoc = {};
|
|
330
332
|
if (update.vector) {
|
|
331
|
-
const stats = await this.describeIndex(indexName);
|
|
333
|
+
const stats = await this.describeIndex({ indexName });
|
|
332
334
|
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
333
335
|
updateDoc[this.embeddingFieldName] = update.vector;
|
|
334
336
|
}
|
|
@@ -347,23 +349,6 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
347
349
|
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
348
350
|
}
|
|
349
351
|
}
|
|
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
|
-
*/
|
|
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
352
|
/**
|
|
368
353
|
* Deletes a vector by its ID.
|
|
369
354
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -371,7 +356,7 @@ var MongoDBVector = class extends vector.MastraVector {
|
|
|
371
356
|
* @returns A promise that resolves when the deletion is complete.
|
|
372
357
|
* @throws Will throw an error if the deletion operation fails.
|
|
373
358
|
*/
|
|
374
|
-
async deleteVector(indexName, id) {
|
|
359
|
+
async deleteVector({ indexName, id }) {
|
|
375
360
|
try {
|
|
376
361
|
const collection = await this.getCollection(indexName, true);
|
|
377
362
|
await collection.deleteOne({ _id: id });
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { MONGODB_PROMPT } from './_tsup-dts-rollup.cjs';
|
|
2
|
-
export { MongoDBUpsertArgs } from './_tsup-dts-rollup.cjs';
|
|
3
|
-
export { MongoDBQueryArgs } from './_tsup-dts-rollup.cjs';
|
|
4
|
-
export { MongoDBUpsertParams } from './_tsup-dts-rollup.cjs';
|
|
5
2
|
export { MongoDBUpsertVectorParams } from './_tsup-dts-rollup.cjs';
|
|
6
3
|
export { MongoDBQueryVectorParams } from './_tsup-dts-rollup.cjs';
|
|
4
|
+
export { MongoDBIndexReadyParams } from './_tsup-dts-rollup.cjs';
|
|
7
5
|
export { MongoDBVector } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export { MONGODB_PROMPT } from './_tsup-dts-rollup.js';
|
|
2
|
-
export { MongoDBUpsertArgs } from './_tsup-dts-rollup.js';
|
|
3
|
-
export { MongoDBQueryArgs } from './_tsup-dts-rollup.js';
|
|
4
|
-
export { MongoDBUpsertParams } from './_tsup-dts-rollup.js';
|
|
5
2
|
export { MongoDBUpsertVectorParams } from './_tsup-dts-rollup.js';
|
|
6
3
|
export { MongoDBQueryVectorParams } from './_tsup-dts-rollup.js';
|
|
4
|
+
export { MongoDBIndexReadyParams } from './_tsup-dts-rollup.js';
|
|
7
5
|
export { MongoDBVector } from './_tsup-dts-rollup.js';
|
package/dist/index.js
CHANGED
|
@@ -119,8 +119,7 @@ var MongoDBVector = class extends MastraVector {
|
|
|
119
119
|
async disconnect() {
|
|
120
120
|
await this.client.close();
|
|
121
121
|
}
|
|
122
|
-
async createIndex(
|
|
123
|
-
const { indexName, dimension, metric = "cosine" } = params;
|
|
122
|
+
async createIndex({ indexName, dimension, metric = "cosine" }) {
|
|
124
123
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
125
124
|
throw new Error("Dimension must be a positive integer");
|
|
126
125
|
}
|
|
@@ -158,7 +157,19 @@ var MongoDBVector = class extends MastraVector {
|
|
|
158
157
|
}
|
|
159
158
|
await collection.updateOne({ _id: "__index_metadata__" }, { $set: { dimension, metric } }, { upsert: true });
|
|
160
159
|
}
|
|
161
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Waits for the index to be ready.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} indexName - The name of the index to wait for
|
|
164
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
165
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
166
|
+
* @returns A promise that resolves when the index is ready
|
|
167
|
+
*/
|
|
168
|
+
async waitForIndexReady({
|
|
169
|
+
indexName,
|
|
170
|
+
timeoutMs = 6e4,
|
|
171
|
+
checkIntervalMs = 2e3
|
|
172
|
+
}) {
|
|
162
173
|
const collection = await this.getCollection(indexName, true);
|
|
163
174
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
164
175
|
const startTime = Date.now();
|
|
@@ -173,11 +184,10 @@ var MongoDBVector = class extends MastraVector {
|
|
|
173
184
|
}
|
|
174
185
|
throw new Error(`Index "${indexNameInternal}" did not become ready within timeout`);
|
|
175
186
|
}
|
|
176
|
-
async upsert(
|
|
177
|
-
const { indexName, vectors, metadata, ids, documents } = params;
|
|
187
|
+
async upsert({ indexName, vectors, metadata, ids, documents }) {
|
|
178
188
|
const collection = await this.getCollection(indexName);
|
|
179
189
|
this.collectionForValidation = collection;
|
|
180
|
-
const stats = await this.describeIndex(indexName);
|
|
190
|
+
const stats = await this.describeIndex({ indexName });
|
|
181
191
|
await this.validateVectorDimensions(vectors, stats.dimension);
|
|
182
192
|
const generatedIds = ids || vectors.map(() => v4());
|
|
183
193
|
const operations = vectors.map((vector, idx) => {
|
|
@@ -210,8 +220,14 @@ var MongoDBVector = class extends MastraVector {
|
|
|
210
220
|
await collection.bulkWrite(operations);
|
|
211
221
|
return generatedIds;
|
|
212
222
|
}
|
|
213
|
-
async query(
|
|
214
|
-
|
|
223
|
+
async query({
|
|
224
|
+
indexName,
|
|
225
|
+
queryVector,
|
|
226
|
+
topK = 10,
|
|
227
|
+
filter,
|
|
228
|
+
includeVector = false,
|
|
229
|
+
documentFilter
|
|
230
|
+
}) {
|
|
215
231
|
const collection = await this.getCollection(indexName, true);
|
|
216
232
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
217
233
|
const mongoFilter = this.transformFilter(filter);
|
|
@@ -267,7 +283,13 @@ var MongoDBVector = class extends MastraVector {
|
|
|
267
283
|
const collections = await this.db.listCollections().toArray();
|
|
268
284
|
return collections.map((col) => col.name);
|
|
269
285
|
}
|
|
270
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Retrieves statistics about a vector index.
|
|
288
|
+
*
|
|
289
|
+
* @param {string} indexName - The name of the index to describe
|
|
290
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
291
|
+
*/
|
|
292
|
+
async describeIndex({ indexName }) {
|
|
271
293
|
const collection = await this.getCollection(indexName, true);
|
|
272
294
|
const count = await collection.countDocuments({ _id: { $ne: "__index_metadata__" } });
|
|
273
295
|
const metadataDoc = await collection.findOne({ _id: "__index_metadata__" });
|
|
@@ -279,7 +301,7 @@ var MongoDBVector = class extends MastraVector {
|
|
|
279
301
|
metric
|
|
280
302
|
};
|
|
281
303
|
}
|
|
282
|
-
async deleteIndex(indexName) {
|
|
304
|
+
async deleteIndex({ indexName }) {
|
|
283
305
|
const collection = await this.getCollection(indexName, false);
|
|
284
306
|
if (collection) {
|
|
285
307
|
await collection.drop();
|
|
@@ -289,8 +311,6 @@ var MongoDBVector = class extends MastraVector {
|
|
|
289
311
|
}
|
|
290
312
|
}
|
|
291
313
|
/**
|
|
292
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
293
|
-
*
|
|
294
314
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
295
315
|
* @param indexName - The name of the index containing the vector.
|
|
296
316
|
* @param id - The ID of the vector to update.
|
|
@@ -300,25 +320,7 @@ var MongoDBVector = class extends MastraVector {
|
|
|
300
320
|
* @returns A promise that resolves when the update is complete.
|
|
301
321
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
302
322
|
*/
|
|
303
|
-
async
|
|
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) {
|
|
323
|
+
async updateVector({ indexName, id, update }) {
|
|
322
324
|
try {
|
|
323
325
|
if (!update.vector && !update.metadata) {
|
|
324
326
|
throw new Error("No updates provided");
|
|
@@ -326,7 +328,7 @@ var MongoDBVector = class extends MastraVector {
|
|
|
326
328
|
const collection = await this.getCollection(indexName, true);
|
|
327
329
|
const updateDoc = {};
|
|
328
330
|
if (update.vector) {
|
|
329
|
-
const stats = await this.describeIndex(indexName);
|
|
331
|
+
const stats = await this.describeIndex({ indexName });
|
|
330
332
|
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
331
333
|
updateDoc[this.embeddingFieldName] = update.vector;
|
|
332
334
|
}
|
|
@@ -345,23 +347,6 @@ var MongoDBVector = class extends MastraVector {
|
|
|
345
347
|
throw new Error(`Failed to update vector by id: ${id} for index name: ${indexName}: ${error.message}`);
|
|
346
348
|
}
|
|
347
349
|
}
|
|
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
|
-
*/
|
|
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
350
|
/**
|
|
366
351
|
* Deletes a vector by its ID.
|
|
367
352
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -369,7 +354,7 @@ var MongoDBVector = class extends MastraVector {
|
|
|
369
354
|
* @returns A promise that resolves when the deletion is complete.
|
|
370
355
|
* @throws Will throw an error if the deletion operation fails.
|
|
371
356
|
*/
|
|
372
|
-
async deleteVector(indexName, id) {
|
|
357
|
+
async deleteVector({ indexName, id }) {
|
|
373
358
|
try {
|
|
374
359
|
const collection = await this.getCollection(indexName, true);
|
|
375
360
|
await collection.deleteOne({ _id: id });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/mongodb",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
4
|
"description": "MongoDB provider for Mastra - includes vector store capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -22,8 +22,7 @@
|
|
|
22
22
|
"@types/mongodb": "^4.0.7",
|
|
23
23
|
"cloudflare": "^4.1.0",
|
|
24
24
|
"mongodb": "^6.15.0",
|
|
25
|
-
"uuid": "^11.1.0"
|
|
26
|
-
"@mastra/core": "^0.9.4"
|
|
25
|
+
"uuid": "^11.1.0"
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
28
|
"@microsoft/api-extractor": "^7.52.5",
|
|
@@ -32,7 +31,11 @@
|
|
|
32
31
|
"tsup": "^8.4.0",
|
|
33
32
|
"typescript": "^5.8.2",
|
|
34
33
|
"vitest": "^3.1.2",
|
|
35
|
-
"@internal/lint": "0.0.5"
|
|
34
|
+
"@internal/lint": "0.0.5",
|
|
35
|
+
"@mastra/core": "0.10.0-alpha.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@mastra/core": "^0.9.4"
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
41
|
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
package/src/vector/index.test.ts
CHANGED
|
@@ -60,7 +60,7 @@ async function createIndexAndWait(
|
|
|
60
60
|
metric: 'cosine' | 'euclidean' | 'dotproduct',
|
|
61
61
|
) {
|
|
62
62
|
await vectorDB.createIndex({ indexName, dimension, metric });
|
|
63
|
-
await vectorDB.waitForIndexReady(indexName);
|
|
63
|
+
await vectorDB.waitForIndexReady({ indexName });
|
|
64
64
|
const created = await waitForCondition(
|
|
65
65
|
async () => {
|
|
66
66
|
const cols = await vectorDB.listIndexes();
|
|
@@ -75,7 +75,7 @@ async function createIndexAndWait(
|
|
|
75
75
|
// Delete index (collection) and wait until it is removed
|
|
76
76
|
async function deleteIndexAndWait(vectorDB: MongoDBVector, indexName: string) {
|
|
77
77
|
try {
|
|
78
|
-
await vectorDB.deleteIndex(indexName);
|
|
78
|
+
await vectorDB.deleteIndex({ indexName });
|
|
79
79
|
const deleted = await waitForCondition(
|
|
80
80
|
async () => {
|
|
81
81
|
const cols = await vectorDB.listIndexes();
|
|
@@ -105,7 +105,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
105
105
|
// Cleanup any existing collections
|
|
106
106
|
try {
|
|
107
107
|
const cols = await vectorDB.listIndexes();
|
|
108
|
-
await Promise.all(cols.map(c => vectorDB.deleteIndex(c)));
|
|
108
|
+
await Promise.all(cols.map(c => vectorDB.deleteIndex({ indexName: c })));
|
|
109
109
|
const deleted = await waitForCondition(async () => (await vectorDB.listIndexes()).length === 0, 30000, 2000);
|
|
110
110
|
if (!deleted) throw new Error('Timed out waiting for collections to be deleted');
|
|
111
111
|
} catch (error) {
|
|
@@ -119,12 +119,12 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
119
119
|
|
|
120
120
|
afterAll(async () => {
|
|
121
121
|
try {
|
|
122
|
-
await vectorDB.deleteIndex(testIndexName);
|
|
122
|
+
await vectorDB.deleteIndex({ indexName: testIndexName });
|
|
123
123
|
} catch (error) {
|
|
124
124
|
console.error('Failed to delete test collection:', error);
|
|
125
125
|
}
|
|
126
126
|
try {
|
|
127
|
-
await vectorDB.deleteIndex(testIndexName2);
|
|
127
|
+
await vectorDB.deleteIndex({ indexName: testIndexName2 });
|
|
128
128
|
} catch (error) {
|
|
129
129
|
console.error('Failed to delete test collection:', error);
|
|
130
130
|
}
|
|
@@ -137,7 +137,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
137
137
|
expect(cols).toContain(testIndexName);
|
|
138
138
|
|
|
139
139
|
// Check stats (should be zero docs initially)
|
|
140
|
-
const initialStats = await vectorDB.describeIndex(testIndexName);
|
|
140
|
+
const initialStats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
141
141
|
expect(initialStats).toEqual({ dimension: 4, metric: 'cosine', count: 0 });
|
|
142
142
|
|
|
143
143
|
// Upsert 4 vectors with metadata
|
|
@@ -153,7 +153,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
153
153
|
|
|
154
154
|
// Wait for the document count to update (increased delay to 5000ms)
|
|
155
155
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
156
|
-
const updatedStats = await vectorDB.describeIndex(testIndexName);
|
|
156
|
+
const updatedStats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
157
157
|
expect(updatedStats.count).toEqual(4);
|
|
158
158
|
|
|
159
159
|
// Query for similar vectors (delay again to allow for index update)
|
|
@@ -175,7 +175,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
175
175
|
expect(filteredResults[0]?.metadata).toEqual({ label: 'vector2' });
|
|
176
176
|
|
|
177
177
|
// Final stats should show > 0 documents
|
|
178
|
-
const finalStats = await vectorDB.describeIndex(testIndexName);
|
|
178
|
+
const finalStats = await vectorDB.describeIndex({ indexName: testIndexName });
|
|
179
179
|
expect(finalStats.count).toBeGreaterThan(0);
|
|
180
180
|
});
|
|
181
181
|
|
|
@@ -379,7 +379,11 @@ 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.updateVector(
|
|
382
|
+
await vectorDB.updateVector({
|
|
383
|
+
indexName,
|
|
384
|
+
id: idToBeUpdated,
|
|
385
|
+
update: { vector: newVector, metadata: newMetaData },
|
|
386
|
+
});
|
|
383
387
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
384
388
|
const results = await vectorDB.query({
|
|
385
389
|
indexName,
|
|
@@ -399,7 +403,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
399
403
|
expect(ids).toHaveLength(4);
|
|
400
404
|
const idToBeUpdated = ids[0];
|
|
401
405
|
const newMetaData = { test: 'metadata only update' };
|
|
402
|
-
await vectorDB.updateVector(indexName, idToBeUpdated, { metadata: newMetaData });
|
|
406
|
+
await vectorDB.updateVector({ indexName, id: idToBeUpdated, update: { metadata: newMetaData } });
|
|
403
407
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
404
408
|
const results = await vectorDB.query({
|
|
405
409
|
indexName,
|
|
@@ -419,7 +423,7 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
419
423
|
expect(ids).toHaveLength(4);
|
|
420
424
|
const idToBeUpdated = ids[0];
|
|
421
425
|
const newVector = [1, 2, 3, 4];
|
|
422
|
-
await vectorDB.updateVector(indexName, idToBeUpdated, { vector: newVector });
|
|
426
|
+
await vectorDB.updateVector({ indexName, id: idToBeUpdated, update: { vector: newVector } });
|
|
423
427
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
424
428
|
const results = await vectorDB.query({
|
|
425
429
|
indexName,
|
|
@@ -434,21 +438,23 @@ describe('MongoDBVector Integration Tests', () => {
|
|
|
434
438
|
expect(updatedResult?.vector).toEqual(newVector);
|
|
435
439
|
});
|
|
436
440
|
it('should throw exception when no updates are given', async () => {
|
|
437
|
-
await expect(vectorDB.updateVector(indexName, 'nonexistent-id', {})).rejects.toThrow(
|
|
441
|
+
await expect(vectorDB.updateVector({ indexName, id: 'nonexistent-id', update: {} })).rejects.toThrow(
|
|
442
|
+
'No updates provided',
|
|
443
|
+
);
|
|
438
444
|
});
|
|
439
445
|
it('should delete the vector by id', async () => {
|
|
440
446
|
const ids = await vectorDB.upsert({ indexName, vectors: testVectors });
|
|
441
447
|
expect(ids).toHaveLength(4);
|
|
442
448
|
const idToBeDeleted = ids[0];
|
|
443
449
|
|
|
444
|
-
const initialStats = await vectorDB.describeIndex(indexName);
|
|
450
|
+
const initialStats = await vectorDB.describeIndex({ indexName });
|
|
445
451
|
|
|
446
|
-
await vectorDB.deleteVector(indexName, idToBeDeleted);
|
|
452
|
+
await vectorDB.deleteVector({ indexName, id: idToBeDeleted });
|
|
447
453
|
const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0, 0], topK: 2 });
|
|
448
454
|
expect(results.map(res => res.id)).not.toContain(idToBeDeleted);
|
|
449
455
|
|
|
450
456
|
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait for count to update
|
|
451
|
-
const finalStats = await vectorDB.describeIndex(indexName);
|
|
457
|
+
const finalStats = await vectorDB.describeIndex({ indexName });
|
|
452
458
|
expect(finalStats.count).toBe(initialStats.count - 1);
|
|
453
459
|
});
|
|
454
460
|
});
|
package/src/vector/index.ts
CHANGED
|
@@ -5,9 +5,10 @@ import type {
|
|
|
5
5
|
CreateIndexParams,
|
|
6
6
|
UpsertVectorParams,
|
|
7
7
|
QueryVectorParams,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
DescribeIndexParams,
|
|
9
|
+
DeleteIndexParams,
|
|
10
|
+
DeleteVectorParams,
|
|
11
|
+
UpdateVectorParams,
|
|
11
12
|
} from '@mastra/core/vector';
|
|
12
13
|
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
13
14
|
import { MongoClient } from 'mongodb';
|
|
@@ -17,10 +18,6 @@ import { v4 as uuidv4 } from 'uuid';
|
|
|
17
18
|
import { MongoDBFilterTranslator } from './filter';
|
|
18
19
|
|
|
19
20
|
// Define necessary types and interfaces
|
|
20
|
-
export type MongoDBUpsertArgs = [...UpsertVectorArgs, string[]?];
|
|
21
|
-
export type MongoDBQueryArgs = [...QueryVectorArgs, string?];
|
|
22
|
-
export type MongoDBUpsertParams = ParamsToArgs<MongoDBUpsertArgs>;
|
|
23
|
-
|
|
24
21
|
export interface MongoDBUpsertVectorParams extends UpsertVectorParams {
|
|
25
22
|
documents?: string[];
|
|
26
23
|
}
|
|
@@ -29,6 +26,12 @@ export interface MongoDBQueryVectorParams extends QueryVectorParams {
|
|
|
29
26
|
documentFilter?: VectorFilter;
|
|
30
27
|
}
|
|
31
28
|
|
|
29
|
+
export interface MongoDBIndexReadyParams {
|
|
30
|
+
indexName: string;
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
checkIntervalMs?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
32
35
|
// Define the document interface
|
|
33
36
|
interface MongoDBDocument extends Document {
|
|
34
37
|
_id: string; // Explicitly declare '_id' as string
|
|
@@ -68,9 +71,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
68
71
|
await this.client.close();
|
|
69
72
|
}
|
|
70
73
|
|
|
71
|
-
async createIndex(
|
|
72
|
-
const { indexName, dimension, metric = 'cosine' } = params;
|
|
73
|
-
|
|
74
|
+
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
74
75
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
75
76
|
throw new Error('Dimension must be a positive integer');
|
|
76
77
|
}
|
|
@@ -118,7 +119,19 @@ export class MongoDBVector extends MastraVector {
|
|
|
118
119
|
await collection.updateOne({ _id: '__index_metadata__' }, { $set: { dimension, metric } }, { upsert: true });
|
|
119
120
|
}
|
|
120
121
|
|
|
121
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Waits for the index to be ready.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} indexName - The name of the index to wait for
|
|
126
|
+
* @param {number} timeoutMs - The maximum time in milliseconds to wait for the index to be ready (default: 60000)
|
|
127
|
+
* @param {number} checkIntervalMs - The interval in milliseconds at which to check if the index is ready (default: 2000)
|
|
128
|
+
* @returns A promise that resolves when the index is ready
|
|
129
|
+
*/
|
|
130
|
+
async waitForIndexReady({
|
|
131
|
+
indexName,
|
|
132
|
+
timeoutMs = 60000,
|
|
133
|
+
checkIntervalMs = 2000,
|
|
134
|
+
}: MongoDBIndexReadyParams): Promise<void> {
|
|
122
135
|
const collection = await this.getCollection(indexName, true);
|
|
123
136
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
124
137
|
|
|
@@ -135,15 +148,13 @@ export class MongoDBVector extends MastraVector {
|
|
|
135
148
|
throw new Error(`Index "${indexNameInternal}" did not become ready within timeout`);
|
|
136
149
|
}
|
|
137
150
|
|
|
138
|
-
async upsert(
|
|
139
|
-
const { indexName, vectors, metadata, ids, documents } = params;
|
|
140
|
-
|
|
151
|
+
async upsert({ indexName, vectors, metadata, ids, documents }: MongoDBUpsertVectorParams): Promise<string[]> {
|
|
141
152
|
const collection = await this.getCollection(indexName);
|
|
142
153
|
|
|
143
154
|
this.collectionForValidation = collection;
|
|
144
155
|
|
|
145
156
|
// Get index stats to check dimension
|
|
146
|
-
const stats = await this.describeIndex(indexName);
|
|
157
|
+
const stats = await this.describeIndex({ indexName });
|
|
147
158
|
|
|
148
159
|
// Validate vector dimensions
|
|
149
160
|
await this.validateVectorDimensions(vectors, stats.dimension);
|
|
@@ -187,9 +198,14 @@ export class MongoDBVector extends MastraVector {
|
|
|
187
198
|
return generatedIds;
|
|
188
199
|
}
|
|
189
200
|
|
|
190
|
-
async query(
|
|
191
|
-
|
|
192
|
-
|
|
201
|
+
async query({
|
|
202
|
+
indexName,
|
|
203
|
+
queryVector,
|
|
204
|
+
topK = 10,
|
|
205
|
+
filter,
|
|
206
|
+
includeVector = false,
|
|
207
|
+
documentFilter,
|
|
208
|
+
}: MongoDBQueryVectorParams): Promise<QueryResult[]> {
|
|
193
209
|
const collection = await this.getCollection(indexName, true);
|
|
194
210
|
const indexNameInternal = `${indexName}_vector_index`;
|
|
195
211
|
|
|
@@ -255,7 +271,13 @@ export class MongoDBVector extends MastraVector {
|
|
|
255
271
|
return collections.map(col => col.name);
|
|
256
272
|
}
|
|
257
273
|
|
|
258
|
-
|
|
274
|
+
/**
|
|
275
|
+
* Retrieves statistics about a vector index.
|
|
276
|
+
*
|
|
277
|
+
* @param {string} indexName - The name of the index to describe
|
|
278
|
+
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
279
|
+
*/
|
|
280
|
+
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
259
281
|
const collection = await this.getCollection(indexName, true);
|
|
260
282
|
|
|
261
283
|
// Get the count of documents, excluding the metadata document
|
|
@@ -273,7 +295,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
273
295
|
};
|
|
274
296
|
}
|
|
275
297
|
|
|
276
|
-
async deleteIndex(indexName:
|
|
298
|
+
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
277
299
|
const collection = await this.getCollection(indexName, false); // Do not throw error if collection doesn't exist
|
|
278
300
|
if (collection) {
|
|
279
301
|
await collection.drop();
|
|
@@ -285,8 +307,6 @@ export class MongoDBVector extends MastraVector {
|
|
|
285
307
|
}
|
|
286
308
|
|
|
287
309
|
/**
|
|
288
|
-
* @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
|
|
289
|
-
*
|
|
290
310
|
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
291
311
|
* @param indexName - The name of the index containing the vector.
|
|
292
312
|
* @param id - The ID of the vector to update.
|
|
@@ -296,34 +316,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
296
316
|
* @returns A promise that resolves when the update is complete.
|
|
297
317
|
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
298
318
|
*/
|
|
299
|
-
async
|
|
300
|
-
indexName: string,
|
|
301
|
-
id: string,
|
|
302
|
-
update: { vector?: number[]; metadata?: Record<string, any> },
|
|
303
|
-
): Promise<void> {
|
|
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
|
-
/**
|
|
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> {
|
|
319
|
+
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
327
320
|
try {
|
|
328
321
|
if (!update.vector && !update.metadata) {
|
|
329
322
|
throw new Error('No updates provided');
|
|
@@ -333,7 +326,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
333
326
|
const updateDoc: Record<string, any> = {};
|
|
334
327
|
|
|
335
328
|
if (update.vector) {
|
|
336
|
-
const stats = await this.describeIndex(indexName);
|
|
329
|
+
const stats = await this.describeIndex({ indexName });
|
|
337
330
|
await this.validateVectorDimensions([update.vector], stats.dimension);
|
|
338
331
|
updateDoc[this.embeddingFieldName] = update.vector;
|
|
339
332
|
}
|
|
@@ -358,24 +351,6 @@ export class MongoDBVector extends MastraVector {
|
|
|
358
351
|
}
|
|
359
352
|
}
|
|
360
353
|
|
|
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
|
-
*/
|
|
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
354
|
/**
|
|
380
355
|
* Deletes a vector by its ID.
|
|
381
356
|
* @param indexName - The name of the index containing the vector.
|
|
@@ -383,7 +358,7 @@ export class MongoDBVector extends MastraVector {
|
|
|
383
358
|
* @returns A promise that resolves when the deletion is complete.
|
|
384
359
|
* @throws Will throw an error if the deletion operation fails.
|
|
385
360
|
*/
|
|
386
|
-
async deleteVector(indexName
|
|
361
|
+
async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
|
|
387
362
|
try {
|
|
388
363
|
const collection = await this.getCollection(indexName, true);
|
|
389
364
|
await collection.deleteOne({ _id: id });
|