@mastra/mongodb 0.13.5 → 0.13.6-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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 0.13.6-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#7343](https://github.com/mastra-ai/mastra/pull/7343) [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e) Thanks [@LekoArts](https://github.com/LekoArts)! - Update the `package.json` file to include additional fields like `repository`, `homepage` or `files`.
8
+
9
+ - Updated dependencies [[`85ef90b`](https://github.com/mastra-ai/mastra/commit/85ef90bb2cd4ae4df855c7ac175f7d392c55c1bf), [`de3cbc6`](https://github.com/mastra-ai/mastra/commit/de3cbc61079211431bd30487982ea3653517278e)]:
10
+ - @mastra/core@0.15.3-alpha.5
11
+
3
12
  ## 0.13.5
4
13
 
5
14
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/mongodb",
3
- "version": "0.13.5",
3
+ "version": "0.13.6-alpha.0",
4
4
  "description": "MongoDB provider for Mastra - includes vector store capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,13 +32,26 @@
32
32
  "typescript": "^5.8.3",
33
33
  "vitest": "^3.2.4",
34
34
  "@internal/lint": "0.0.34",
35
- "@mastra/core": "0.15.2",
36
- "@internal/types-builder": "0.0.9",
37
- "@internal/storage-test-utils": "0.0.30"
35
+ "@internal/storage-test-utils": "0.0.30",
36
+ "@mastra/core": "0.15.3-alpha.5",
37
+ "@internal/types-builder": "0.0.9"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@mastra/core": ">=0.13.0-0 <0.16.0-0"
41
41
  },
42
+ "files": [
43
+ "dist",
44
+ "CHANGELOG.md"
45
+ ],
46
+ "homepage": "https://mastra.ai",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/mastra-ai/mastra.git",
50
+ "directory": "stores/mongodb"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/mastra-ai/mastra/issues"
54
+ },
42
55
  "scripts": {
43
56
  "build": "tsup --silent --config tsup.config.ts",
44
57
  "build:watch": "tsup --watch --silent --config tsup.config.ts",
@@ -1,4 +0,0 @@
1
-
2
- > @mastra/mongodb@0.13.4 build /home/runner/work/mastra/mastra/stores/mongodb
3
- > tsup --silent --config tsup.config.ts
4
-
@@ -1,30 +0,0 @@
1
- services:
2
- mongodb-storage:
3
- image: mongo:8.0.12
4
- container_name: 'mongodb-storage-test-db'
5
- ports:
6
- - '27017:27017'
7
- volumes:
8
- - mongodbdata:/data/db
9
- healthcheck:
10
- test: ['CMD', 'mongosh', '--eval', "db.adminCommand('ping')"]
11
- interval: 2s
12
- timeout: 2s
13
- retries: 15
14
- start_period: 3s
15
- mongodb-vector:
16
- image: mongodb/mongodb-atlas-local
17
- container_name: 'mongodb-vector-test-db'
18
- environment:
19
- MONGODB_INITDB_ROOT_USERNAME: mongodb
20
- MONGODB_INITDB_ROOT_PASSWORD: mongodb
21
- ports:
22
- - 27018:27017
23
- healthcheck:
24
- test: ['CMD', 'mongosh', '-u', 'mongodb', '-p', 'mongodb', '--eval', "db.adminCommand('ping')"]
25
- interval: 2s
26
- timeout: 2s
27
- retries: 15
28
- start_period: 3s
29
- volumes:
30
- mongodbdata:
package/eslint.config.js DELETED
@@ -1,6 +0,0 @@
1
- import { createConfig } from '@internal/lint/eslint';
2
-
3
- const config = await createConfig();
4
-
5
- /** @type {import("eslint").Linter.Config[]} */
6
- export default [...config];
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './vector';
2
- export * from './storage';
3
- export * from './storage/connectors/base';
4
- export { MONGODB_PROMPT } from './vector/prompt';
@@ -1,93 +0,0 @@
1
- import { MongoClient } from 'mongodb';
2
- import type { Db } from 'mongodb';
3
- import type { ConnectorHandler } from './connectors/base';
4
- import type { DatabaseConfig } from './types';
5
-
6
- type MongoDBConnectorOptions =
7
- | {
8
- client: MongoClient;
9
- dbName: string;
10
- handler: undefined;
11
- }
12
- | {
13
- client: undefined;
14
- dbName: undefined;
15
- handler: ConnectorHandler;
16
- };
17
-
18
- export class MongoDBConnector {
19
- readonly #client?: MongoClient;
20
- readonly #dbName?: string;
21
- readonly #handler?: ConnectorHandler;
22
- #isConnected: boolean;
23
- #db?: Db;
24
-
25
- constructor(options: MongoDBConnectorOptions) {
26
- this.#client = options.client;
27
- this.#dbName = options.dbName;
28
- this.#handler = options.handler;
29
- this.#isConnected = false;
30
- }
31
-
32
- static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector {
33
- if (!config.url?.trim().length) {
34
- throw new Error(
35
- 'MongoDBStore: url must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults.',
36
- );
37
- }
38
-
39
- if (!config.dbName?.trim().length) {
40
- throw new Error(
41
- 'MongoDBStore: dbName must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults.',
42
- );
43
- }
44
-
45
- return new MongoDBConnector({
46
- client: new MongoClient(config.url, config.options),
47
- dbName: config.dbName,
48
- handler: undefined,
49
- });
50
- }
51
-
52
- static fromConnectionHandler(handler: ConnectorHandler): MongoDBConnector {
53
- return new MongoDBConnector({
54
- client: undefined,
55
- dbName: undefined,
56
- handler,
57
- });
58
- }
59
-
60
- private async getConnection(): Promise<Db> {
61
- if (this.#client) {
62
- if (this.#isConnected && this.#db) {
63
- return this.#db;
64
- }
65
- await this.#client.connect();
66
- this.#db = this.#client.db(this.#dbName);
67
- this.#isConnected = true;
68
- return this.#db;
69
- }
70
-
71
- throw new Error('MongoDBStore: client cannot be empty. Check your MongoDBConnector configuration.');
72
- }
73
-
74
- async getCollection(collectionName: string) {
75
- if (this.#handler) {
76
- return this.#handler.getCollection(collectionName);
77
- }
78
- const db = await this.getConnection();
79
- return db.collection(collectionName);
80
- }
81
-
82
- async close() {
83
- if (this.#client) {
84
- await this.#client.close();
85
- this.#isConnected = false;
86
- return;
87
- }
88
-
89
- if (this.#handler) {
90
- await this.#handler.close();
91
- }
92
- }
93
- }
@@ -1,93 +0,0 @@
1
- import { MongoClient } from 'mongodb';
2
- import type { Db } from 'mongodb';
3
- import type { DatabaseConfig } from '../types';
4
- import type { ConnectorHandler } from './base';
5
-
6
- type MongoDBConnectorOptions =
7
- | {
8
- client: MongoClient;
9
- dbName: string;
10
- handler: undefined;
11
- }
12
- | {
13
- client: undefined;
14
- dbName: undefined;
15
- handler: ConnectorHandler;
16
- };
17
-
18
- export class MongoDBConnector {
19
- readonly #client?: MongoClient;
20
- readonly #dbName?: string;
21
- readonly #handler?: ConnectorHandler;
22
- #isConnected: boolean;
23
- #db?: Db;
24
-
25
- constructor(options: MongoDBConnectorOptions) {
26
- this.#client = options.client;
27
- this.#dbName = options.dbName;
28
- this.#handler = options.handler;
29
- this.#isConnected = false;
30
- }
31
-
32
- static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector {
33
- if (!config.url?.trim().length) {
34
- throw new Error(
35
- 'MongoDBStore: url must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults.',
36
- );
37
- }
38
-
39
- if (!config.dbName?.trim().length) {
40
- throw new Error(
41
- 'MongoDBStore: dbName must be provided and cannot be empty. Passing an empty string may cause fallback to local MongoDB defaults.',
42
- );
43
- }
44
-
45
- return new MongoDBConnector({
46
- client: new MongoClient(config.url, config.options),
47
- dbName: config.dbName,
48
- handler: undefined,
49
- });
50
- }
51
-
52
- static fromConnectionHandler(handler: ConnectorHandler): MongoDBConnector {
53
- return new MongoDBConnector({
54
- client: undefined,
55
- dbName: undefined,
56
- handler,
57
- });
58
- }
59
-
60
- private async getConnection(): Promise<Db> {
61
- if (this.#client) {
62
- if (this.#isConnected && this.#db) {
63
- return this.#db;
64
- }
65
- await this.#client.connect();
66
- this.#db = this.#client.db(this.#dbName);
67
- this.#isConnected = true;
68
- return this.#db;
69
- }
70
-
71
- throw new Error('MongoDBStore: client cannot be empty. Check your MongoDBConnector configuration.');
72
- }
73
-
74
- async getCollection(collectionName: string) {
75
- if (this.#handler) {
76
- return this.#handler.getCollection(collectionName);
77
- }
78
- const db = await this.getConnection();
79
- return db.collection(collectionName);
80
- }
81
-
82
- async close() {
83
- if (this.#client) {
84
- await this.#client.close();
85
- this.#isConnected = false;
86
- return;
87
- }
88
-
89
- if (this.#handler) {
90
- await this.#handler.close();
91
- }
92
- }
93
- }
@@ -1,7 +0,0 @@
1
- import type { Collection } from 'mongodb';
2
-
3
- export interface ConnectorHandler {
4
- getCollection(collectionName: string): Promise<Collection>;
5
-
6
- close(): Promise<void>;
7
- }
@@ -1,193 +0,0 @@
1
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
2
- import type { MetricResult } from '@mastra/core/eval';
3
- import { LegacyEvalsStorage, TABLE_EVALS, safelyParseJSON } from '@mastra/core/storage';
4
- import type { PaginationArgs, PaginationInfo, EvalRow } from '@mastra/core/storage';
5
- import type { StoreOperationsMongoDB } from '../operations';
6
-
7
- function transformEvalRow(row: Record<string, any>): EvalRow {
8
- let testInfoValue = null;
9
- if (row.test_info) {
10
- try {
11
- testInfoValue = typeof row.test_info === 'string' ? safelyParseJSON(row.test_info) : row.test_info;
12
- } catch (e) {
13
- console.warn('Failed to parse test_info:', e);
14
- }
15
- }
16
-
17
- let resultValue: MetricResult;
18
- try {
19
- resultValue = typeof row.result === 'string' ? safelyParseJSON(row.result) : row.result;
20
- } catch (e) {
21
- console.warn('Failed to parse result:', e);
22
- throw new Error('Invalid result format');
23
- }
24
-
25
- return {
26
- agentName: row.agent_name as string,
27
- input: row.input as string,
28
- output: row.output as string,
29
- result: resultValue,
30
- metricName: row.metric_name as string,
31
- instructions: row.instructions as string,
32
- testInfo: testInfoValue,
33
- globalRunId: row.global_run_id as string,
34
- runId: row.run_id as string,
35
- createdAt: row.createdAt as string,
36
- };
37
- }
38
-
39
- export class LegacyEvalsMongoDB extends LegacyEvalsStorage {
40
- private operations: StoreOperationsMongoDB;
41
-
42
- constructor({ operations }: { operations: StoreOperationsMongoDB }) {
43
- super();
44
- this.operations = operations;
45
- }
46
-
47
- /** @deprecated use getEvals instead */
48
- async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
49
- try {
50
- const query: any = {
51
- agent_name: agentName,
52
- };
53
-
54
- if (type === 'test') {
55
- query['test_info'] = { $ne: null };
56
- // is not possible to filter by test_info.testPath because it is not a json field
57
- // query['test_info.testPath'] = { $ne: null };
58
- }
59
-
60
- if (type === 'live') {
61
- // is not possible to filter by test_info.testPath because it is not a json field
62
- query['test_info'] = null;
63
- }
64
-
65
- const collection = await this.operations.getCollection(TABLE_EVALS);
66
- const documents = await collection.find(query).sort({ created_at: 'desc' }).toArray();
67
- const result = documents.map((row: any) => transformEvalRow(row));
68
- // Post filter to remove if test_info.testPath is null
69
- return result.filter((row: any) => {
70
- if (type === 'live') {
71
- return !Boolean(row.testInfo?.testPath);
72
- }
73
-
74
- if (type === 'test') {
75
- return row.testInfo?.testPath !== null;
76
- }
77
- return true;
78
- });
79
- } catch (error) {
80
- // Handle case where table doesn't exist yet
81
- if (error instanceof Error && error.message.includes('no such table')) {
82
- return [];
83
- }
84
- throw new MastraError(
85
- {
86
- id: 'STORAGE_MONGODB_STORE_GET_EVALS_BY_AGENT_NAME_FAILED',
87
- domain: ErrorDomain.STORAGE,
88
- category: ErrorCategory.THIRD_PARTY,
89
- details: { agentName },
90
- },
91
- error,
92
- );
93
- }
94
- }
95
-
96
- async getEvals(
97
- options: {
98
- agentName?: string;
99
- type?: 'test' | 'live';
100
- } & PaginationArgs = {},
101
- ): Promise<PaginationInfo & { evals: EvalRow[] }> {
102
- const { agentName, type, page = 0, perPage = 100, dateRange } = options;
103
- const fromDate = dateRange?.start;
104
- const toDate = dateRange?.end;
105
- const currentOffset = page * perPage;
106
-
107
- const query: any = {};
108
- if (agentName) {
109
- query['agent_name'] = agentName;
110
- }
111
-
112
- if (type === 'test') {
113
- query['test_info'] = { $ne: null };
114
- } else if (type === 'live') {
115
- query['test_info'] = null;
116
- }
117
-
118
- if (fromDate || toDate) {
119
- query['createdAt'] = {};
120
- if (fromDate) {
121
- query['createdAt']['$gte'] = fromDate;
122
- }
123
- if (toDate) {
124
- query['createdAt']['$lte'] = toDate;
125
- }
126
- }
127
-
128
- try {
129
- const collection = await this.operations.getCollection(TABLE_EVALS);
130
- let total = 0;
131
- // Only get total count when using pagination
132
- if (page === 0 || perPage < 1000) {
133
- total = await collection.countDocuments(query);
134
- }
135
-
136
- if (total === 0) {
137
- return {
138
- evals: [],
139
- total: 0,
140
- page,
141
- perPage,
142
- hasMore: false,
143
- };
144
- }
145
-
146
- const documents = await collection
147
- .find(query)
148
- .sort({ created_at: 'desc' })
149
- .skip(currentOffset)
150
- .limit(perPage)
151
- .toArray();
152
-
153
- const evals = documents.map((row: any) => transformEvalRow(row));
154
-
155
- // Post filter to remove if test_info.testPath is null
156
- const filteredEvals = evals.filter((row: any) => {
157
- if (type === 'live') {
158
- return !Boolean(row.testInfo?.testPath);
159
- }
160
-
161
- if (type === 'test') {
162
- return row.testInfo?.testPath !== null;
163
- }
164
- return true;
165
- });
166
-
167
- const hasMore = currentOffset + filteredEvals.length < total;
168
-
169
- return {
170
- evals: filteredEvals,
171
- total,
172
- page,
173
- perPage,
174
- hasMore,
175
- };
176
- } catch (error) {
177
- throw new MastraError(
178
- {
179
- id: 'STORAGE_MONGODB_STORE_GET_EVALS_FAILED',
180
- domain: ErrorDomain.STORAGE,
181
- category: ErrorCategory.THIRD_PARTY,
182
- details: {
183
- agentName: agentName || 'all',
184
- type: type || 'all',
185
- page,
186
- perPage,
187
- },
188
- },
189
- error,
190
- );
191
- }
192
- }
193
- }