@mastra/mongodb 0.12.2 → 0.12.3-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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/mongodb@0.12.1 build /home/runner/work/mastra/mastra/stores/mongodb
2
+ > @mastra/mongodb@0.12.3-alpha.0 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
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.5.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 10902ms
9
+ TSC ⚡️ Build success in 11153ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/stores/mongodb/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 13195ms
16
+ DTS ⚡️ Build success in 14339ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- ESM dist/index.js 73.11 KB
21
- ESM ⚡️ Build success in 1823ms
22
- CJS dist/index.cjs 74.40 KB
23
- CJS ⚡️ Build success in 1832ms
20
+ CJS dist/index.cjs 74.49 KB
21
+ CJS ⚡️ Build success in 1991ms
22
+ ESM dist/index.js 73.15 KB
23
+ ESM ⚡️ Build success in 1991ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @mastra/mongodb
2
2
 
3
+ ## 0.12.3-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 25f2987: fix: filter.\_id.\$in cannot be empty
8
+ - b0f1e2f: fix: mongodb save scores as an object
9
+ - Updated dependencies [510e2c8]
10
+ - Updated dependencies [2f72fb2]
11
+ - Updated dependencies [3f89307]
12
+ - Updated dependencies [9eda7d4]
13
+ - Updated dependencies [9d49408]
14
+ - Updated dependencies [2ecf658]
15
+ - Updated dependencies [7a7754f]
16
+ - Updated dependencies [fc92d80]
17
+ - Updated dependencies [23a6a7c]
18
+ - Updated dependencies [09bca64]
19
+ - @mastra/core@0.12.0-alpha.0
20
+
3
21
  ## 0.12.2
4
22
 
5
23
  ### Patch Changes
@@ -48,15 +48,12 @@ import type { WorkflowRuns } from '@mastra/core/storage';
48
48
  import type { WorkflowRunState } from '@mastra/core/workflows';
49
49
  import { WorkflowsStorage } from '@mastra/core/storage';
50
50
 
51
- export declare interface ConnectorHandler {
52
- getCollection(collectionName: string): Promise<Collection>;
53
- close(): Promise<void>;
54
- }
55
-
56
- export declare interface ConnectorHandler_alias_1 {
51
+ declare interface ConnectorHandler {
57
52
  getCollection(collectionName: string): Promise<Collection>;
58
53
  close(): Promise<void>;
59
54
  }
55
+ export { ConnectorHandler }
56
+ export { ConnectorHandler as ConnectorHandler_alias_1 }
60
57
 
61
58
  export declare function createExecuteOperationWithRetry({ logger, maxRetries, initialBackoffMs, }: {
62
59
  logger: IMastraLogger;
@@ -173,7 +170,7 @@ export { MONGODB_PROMPT as MONGODB_PROMPT_alias_1 }
173
170
  declare type MongoDBBlacklisted = BlacklistedRootOperators | '$size';
174
171
 
175
172
  export declare type MongoDBConfig = DatabaseConfig | {
176
- connectorHandler: ConnectorHandler_alias_1;
173
+ connectorHandler: ConnectorHandler;
177
174
  };
178
175
 
179
176
  export declare class MongoDBConnector {
@@ -190,7 +187,7 @@ export declare class MongoDBConnector_alias_1 {
190
187
  #private;
191
188
  constructor(options: MongoDBConnectorOptions_2);
192
189
  static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector_alias_1;
193
- static fromConnectionHandler(handler: ConnectorHandler_alias_1): MongoDBConnector_alias_1;
190
+ static fromConnectionHandler(handler: ConnectorHandler): MongoDBConnector_alias_1;
194
191
  private getConnection;
195
192
  getCollection(collectionName: string): Promise<Collection<Document>>;
196
193
  close(): Promise<void>;
@@ -213,7 +210,7 @@ declare type MongoDBConnectorOptions_2 = {
213
210
  } | {
214
211
  client: undefined;
215
212
  dbName: undefined;
216
- handler: ConnectorHandler_alias_1;
213
+ handler: ConnectorHandler;
217
214
  };
218
215
 
219
216
  /**
@@ -238,7 +235,7 @@ export { MongoDBIndexReadyParams }
238
235
  export { MongoDBIndexReadyParams as MongoDBIndexReadyParams_alias_1 }
239
236
 
240
237
  export declare interface MongoDBOperationsConfig {
241
- connector: ConnectorHandler_alias_1;
238
+ connector: ConnectorHandler;
242
239
  }
243
240
 
244
241
  declare type MongoDBOperatorValueMap = Omit<OperatorValueMap, '$options'> & {
@@ -48,15 +48,12 @@ import type { WorkflowRuns } from '@mastra/core/storage';
48
48
  import type { WorkflowRunState } from '@mastra/core/workflows';
49
49
  import { WorkflowsStorage } from '@mastra/core/storage';
50
50
 
51
- export declare interface ConnectorHandler {
52
- getCollection(collectionName: string): Promise<Collection>;
53
- close(): Promise<void>;
54
- }
55
-
56
- export declare interface ConnectorHandler_alias_1 {
51
+ declare interface ConnectorHandler {
57
52
  getCollection(collectionName: string): Promise<Collection>;
58
53
  close(): Promise<void>;
59
54
  }
55
+ export { ConnectorHandler }
56
+ export { ConnectorHandler as ConnectorHandler_alias_1 }
60
57
 
61
58
  export declare function createExecuteOperationWithRetry({ logger, maxRetries, initialBackoffMs, }: {
62
59
  logger: IMastraLogger;
@@ -173,7 +170,7 @@ export { MONGODB_PROMPT as MONGODB_PROMPT_alias_1 }
173
170
  declare type MongoDBBlacklisted = BlacklistedRootOperators | '$size';
174
171
 
175
172
  export declare type MongoDBConfig = DatabaseConfig | {
176
- connectorHandler: ConnectorHandler_alias_1;
173
+ connectorHandler: ConnectorHandler;
177
174
  };
178
175
 
179
176
  export declare class MongoDBConnector {
@@ -190,7 +187,7 @@ export declare class MongoDBConnector_alias_1 {
190
187
  #private;
191
188
  constructor(options: MongoDBConnectorOptions_2);
192
189
  static fromDatabaseConfig(config: DatabaseConfig): MongoDBConnector_alias_1;
193
- static fromConnectionHandler(handler: ConnectorHandler_alias_1): MongoDBConnector_alias_1;
190
+ static fromConnectionHandler(handler: ConnectorHandler): MongoDBConnector_alias_1;
194
191
  private getConnection;
195
192
  getCollection(collectionName: string): Promise<Collection<Document>>;
196
193
  close(): Promise<void>;
@@ -213,7 +210,7 @@ declare type MongoDBConnectorOptions_2 = {
213
210
  } | {
214
211
  client: undefined;
215
212
  dbName: undefined;
216
- handler: ConnectorHandler_alias_1;
213
+ handler: ConnectorHandler;
217
214
  };
218
215
 
219
216
  /**
@@ -238,7 +235,7 @@ export { MongoDBIndexReadyParams }
238
235
  export { MongoDBIndexReadyParams as MongoDBIndexReadyParams_alias_1 }
239
236
 
240
237
  export declare interface MongoDBOperationsConfig {
241
- connector: ConnectorHandler_alias_1;
238
+ connector: ConnectorHandler;
242
239
  }
243
240
 
244
241
  declare type MongoDBOperatorValueMap = Omit<OperatorValueMap, '$options'> & {
package/dist/index.cjs CHANGED
@@ -343,7 +343,9 @@ var MongoDBVector = class extends vector.MastraVector {
343
343
  };
344
344
  if (Object.keys(combinedFilter).length > 0) {
345
345
  const candidateIds = await collection.aggregate([{ $match: combinedFilter }, { $project: { _id: 1 } }]).map((doc) => doc._id).toArray();
346
- vectorSearch.filter = { _id: { $in: candidateIds } };
346
+ if (candidateIds.length > 0) {
347
+ vectorSearch.filter = { _id: { $in: candidateIds } };
348
+ }
347
349
  }
348
350
  const pipeline = [
349
351
  {
@@ -1598,19 +1600,19 @@ var ScoresStorageMongoDB = class extends storage.ScoresStorage {
1598
1600
  scorerId: score.scorerId,
1599
1601
  traceId: score.traceId || "",
1600
1602
  runId: score.runId,
1601
- scorer: typeof score.scorer === "string" ? score.scorer : JSON.stringify(score.scorer),
1602
- extractStepResult: typeof score.extractStepResult === "string" ? score.extractStepResult : JSON.stringify(score.extractStepResult),
1603
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? score.analyzeStepResult : JSON.stringify(score.analyzeStepResult),
1603
+ scorer: typeof score.scorer === "string" ? storage.safelyParseJSON(score.scorer) : score.scorer,
1604
+ extractStepResult: typeof score.extractStepResult === "string" ? storage.safelyParseJSON(score.extractStepResult) : score.extractStepResult,
1605
+ analyzeStepResult: typeof score.analyzeStepResult === "string" ? storage.safelyParseJSON(score.analyzeStepResult) : score.analyzeStepResult,
1604
1606
  score: score.score,
1605
1607
  reason: score.reason,
1606
1608
  extractPrompt: score.extractPrompt,
1607
1609
  analyzePrompt: score.analyzePrompt,
1608
1610
  reasonPrompt: score.reasonPrompt,
1609
- input: typeof score.input === "string" ? score.input : JSON.stringify(score.input),
1610
- output: typeof score.output === "string" ? score.output : JSON.stringify(score.output),
1611
+ input: typeof score.input === "string" ? storage.safelyParseJSON(score.input) : score.input,
1612
+ output: typeof score.output === "string" ? storage.safelyParseJSON(score.output) : score.output,
1611
1613
  additionalContext: score.additionalContext,
1612
- runtimeContext: typeof score.runtimeContext === "string" ? score.runtimeContext : JSON.stringify(score.runtimeContext),
1613
- entity: typeof score.entity === "string" ? score.entity : JSON.stringify(score.entity),
1614
+ runtimeContext: typeof score.runtimeContext === "string" ? storage.safelyParseJSON(score.runtimeContext) : score.runtimeContext,
1615
+ entity: typeof score.entity === "string" ? storage.safelyParseJSON(score.entity) : score.entity,
1614
1616
  source: score.source,
1615
1617
  resourceId: score.resourceId || "",
1616
1618
  threadId: score.threadId || "",
@@ -2054,6 +2056,40 @@ var WorkflowsStorageMongoDB = class extends storage.WorkflowsStorage {
2054
2056
  };
2055
2057
 
2056
2058
  // src/storage/index.ts
2059
+ var loadConnector = (config) => {
2060
+ try {
2061
+ if ("connectorHandler" in config) {
2062
+ return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
2063
+ }
2064
+ } catch (error$1) {
2065
+ throw new error.MastraError(
2066
+ {
2067
+ id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2068
+ domain: error.ErrorDomain.STORAGE,
2069
+ category: error.ErrorCategory.USER,
2070
+ details: { connectionHandler: true }
2071
+ },
2072
+ error$1
2073
+ );
2074
+ }
2075
+ try {
2076
+ return MongoDBConnector.fromDatabaseConfig({
2077
+ options: config.options,
2078
+ url: config.url,
2079
+ dbName: config.dbName
2080
+ });
2081
+ } catch (error$1) {
2082
+ throw new error.MastraError(
2083
+ {
2084
+ id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2085
+ domain: error.ErrorDomain.STORAGE,
2086
+ category: error.ErrorCategory.USER,
2087
+ details: { url: config?.url, dbName: config?.dbName }
2088
+ },
2089
+ error$1
2090
+ );
2091
+ }
2092
+ };
2057
2093
  var MongoDBStore = class extends storage.MastraStorage {
2058
2094
  #connector;
2059
2095
  stores;
@@ -2068,39 +2104,7 @@ var MongoDBStore = class extends storage.MastraStorage {
2068
2104
  constructor(config) {
2069
2105
  super({ name: "MongoDBStore" });
2070
2106
  this.stores = {};
2071
- try {
2072
- if ("connectorHandler" in config) {
2073
- this.#connector = MongoDBConnector.fromConnectionHandler(config.connectorHandler);
2074
- return;
2075
- }
2076
- } catch (error$1) {
2077
- throw new error.MastraError(
2078
- {
2079
- id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2080
- domain: error.ErrorDomain.STORAGE,
2081
- category: error.ErrorCategory.USER,
2082
- details: { connectionHandler: true }
2083
- },
2084
- error$1
2085
- );
2086
- }
2087
- try {
2088
- this.#connector = MongoDBConnector.fromDatabaseConfig({
2089
- options: config.options,
2090
- url: config.url,
2091
- dbName: config.dbName
2092
- });
2093
- } catch (error$1) {
2094
- throw new error.MastraError(
2095
- {
2096
- id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2097
- domain: error.ErrorDomain.STORAGE,
2098
- category: error.ErrorCategory.USER,
2099
- details: { url: config?.url, dbName: config?.dbName }
2100
- },
2101
- error$1
2102
- );
2103
- }
2107
+ this.#connector = loadConnector(config);
2104
2108
  const operations = new StoreOperationsMongoDB({
2105
2109
  connector: this.#connector
2106
2110
  });
package/dist/index.d.cts CHANGED
@@ -3,3 +3,4 @@ export { MongoDBUpsertVectorParams } from './_tsup-dts-rollup.cjs';
3
3
  export { MongoDBIndexReadyParams } from './_tsup-dts-rollup.cjs';
4
4
  export { MongoDBVector } from './_tsup-dts-rollup.cjs';
5
5
  export { MongoDBStore } from './_tsup-dts-rollup.cjs';
6
+ export { ConnectorHandler } from './_tsup-dts-rollup.cjs';
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export { MongoDBUpsertVectorParams } from './_tsup-dts-rollup.js';
3
3
  export { MongoDBIndexReadyParams } from './_tsup-dts-rollup.js';
4
4
  export { MongoDBVector } from './_tsup-dts-rollup.js';
5
5
  export { MongoDBStore } from './_tsup-dts-rollup.js';
6
+ export { ConnectorHandler } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -341,7 +341,9 @@ var MongoDBVector = class extends MastraVector {
341
341
  };
342
342
  if (Object.keys(combinedFilter).length > 0) {
343
343
  const candidateIds = await collection.aggregate([{ $match: combinedFilter }, { $project: { _id: 1 } }]).map((doc) => doc._id).toArray();
344
- vectorSearch.filter = { _id: { $in: candidateIds } };
344
+ if (candidateIds.length > 0) {
345
+ vectorSearch.filter = { _id: { $in: candidateIds } };
346
+ }
345
347
  }
346
348
  const pipeline = [
347
349
  {
@@ -1596,19 +1598,19 @@ var ScoresStorageMongoDB = class extends ScoresStorage {
1596
1598
  scorerId: score.scorerId,
1597
1599
  traceId: score.traceId || "",
1598
1600
  runId: score.runId,
1599
- scorer: typeof score.scorer === "string" ? score.scorer : JSON.stringify(score.scorer),
1600
- extractStepResult: typeof score.extractStepResult === "string" ? score.extractStepResult : JSON.stringify(score.extractStepResult),
1601
- analyzeStepResult: typeof score.analyzeStepResult === "string" ? score.analyzeStepResult : JSON.stringify(score.analyzeStepResult),
1601
+ scorer: typeof score.scorer === "string" ? safelyParseJSON(score.scorer) : score.scorer,
1602
+ extractStepResult: typeof score.extractStepResult === "string" ? safelyParseJSON(score.extractStepResult) : score.extractStepResult,
1603
+ analyzeStepResult: typeof score.analyzeStepResult === "string" ? safelyParseJSON(score.analyzeStepResult) : score.analyzeStepResult,
1602
1604
  score: score.score,
1603
1605
  reason: score.reason,
1604
1606
  extractPrompt: score.extractPrompt,
1605
1607
  analyzePrompt: score.analyzePrompt,
1606
1608
  reasonPrompt: score.reasonPrompt,
1607
- input: typeof score.input === "string" ? score.input : JSON.stringify(score.input),
1608
- output: typeof score.output === "string" ? score.output : JSON.stringify(score.output),
1609
+ input: typeof score.input === "string" ? safelyParseJSON(score.input) : score.input,
1610
+ output: typeof score.output === "string" ? safelyParseJSON(score.output) : score.output,
1609
1611
  additionalContext: score.additionalContext,
1610
- runtimeContext: typeof score.runtimeContext === "string" ? score.runtimeContext : JSON.stringify(score.runtimeContext),
1611
- entity: typeof score.entity === "string" ? score.entity : JSON.stringify(score.entity),
1612
+ runtimeContext: typeof score.runtimeContext === "string" ? safelyParseJSON(score.runtimeContext) : score.runtimeContext,
1613
+ entity: typeof score.entity === "string" ? safelyParseJSON(score.entity) : score.entity,
1612
1614
  source: score.source,
1613
1615
  resourceId: score.resourceId || "",
1614
1616
  threadId: score.threadId || "",
@@ -2052,6 +2054,40 @@ var WorkflowsStorageMongoDB = class extends WorkflowsStorage {
2052
2054
  };
2053
2055
 
2054
2056
  // src/storage/index.ts
2057
+ var loadConnector = (config) => {
2058
+ try {
2059
+ if ("connectorHandler" in config) {
2060
+ return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
2061
+ }
2062
+ } catch (error) {
2063
+ throw new MastraError(
2064
+ {
2065
+ id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2066
+ domain: ErrorDomain.STORAGE,
2067
+ category: ErrorCategory.USER,
2068
+ details: { connectionHandler: true }
2069
+ },
2070
+ error
2071
+ );
2072
+ }
2073
+ try {
2074
+ return MongoDBConnector.fromDatabaseConfig({
2075
+ options: config.options,
2076
+ url: config.url,
2077
+ dbName: config.dbName
2078
+ });
2079
+ } catch (error) {
2080
+ throw new MastraError(
2081
+ {
2082
+ id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2083
+ domain: ErrorDomain.STORAGE,
2084
+ category: ErrorCategory.USER,
2085
+ details: { url: config?.url, dbName: config?.dbName }
2086
+ },
2087
+ error
2088
+ );
2089
+ }
2090
+ };
2055
2091
  var MongoDBStore = class extends MastraStorage {
2056
2092
  #connector;
2057
2093
  stores;
@@ -2066,39 +2102,7 @@ var MongoDBStore = class extends MastraStorage {
2066
2102
  constructor(config) {
2067
2103
  super({ name: "MongoDBStore" });
2068
2104
  this.stores = {};
2069
- try {
2070
- if ("connectorHandler" in config) {
2071
- this.#connector = MongoDBConnector.fromConnectionHandler(config.connectorHandler);
2072
- return;
2073
- }
2074
- } catch (error) {
2075
- throw new MastraError(
2076
- {
2077
- id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2078
- domain: ErrorDomain.STORAGE,
2079
- category: ErrorCategory.USER,
2080
- details: { connectionHandler: true }
2081
- },
2082
- error
2083
- );
2084
- }
2085
- try {
2086
- this.#connector = MongoDBConnector.fromDatabaseConfig({
2087
- options: config.options,
2088
- url: config.url,
2089
- dbName: config.dbName
2090
- });
2091
- } catch (error) {
2092
- throw new MastraError(
2093
- {
2094
- id: "STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED",
2095
- domain: ErrorDomain.STORAGE,
2096
- category: ErrorCategory.USER,
2097
- details: { url: config?.url, dbName: config?.dbName }
2098
- },
2099
- error
2100
- );
2101
- }
2105
+ this.#connector = loadConnector(config);
2102
2106
  const operations = new StoreOperationsMongoDB({
2103
2107
  connector: this.#connector
2104
2108
  });
@@ -1,6 +1,6 @@
1
1
  services:
2
2
  mongodb-storage:
3
- image: mongo:8.0.11
3
+ image: mongo:8.0.12
4
4
  container_name: 'mongodb-storage-test-db'
5
5
  ports:
6
6
  - '27017:27017'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/mongodb",
3
- "version": "0.12.2",
3
+ "version": "0.12.3-alpha.0",
4
4
  "description": "MongoDB provider for Mastra - includes vector store capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -31,9 +31,9 @@
31
31
  "tsup": "^8.5.0",
32
32
  "typescript": "^5.8.3",
33
33
  "vitest": "^3.2.4",
34
- "@internal/lint": "0.0.22",
35
- "@internal/storage-test-utils": "0.0.18",
36
- "@mastra/core": "0.11.1"
34
+ "@internal/lint": "0.0.23",
35
+ "@internal/storage-test-utils": "0.0.19",
36
+ "@mastra/core": "0.12.0-alpha.0"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@mastra/core": ">=0.10.7-0 <0.12.0-0"
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './vector';
2
2
  export * from './storage';
3
+ export * from './storage/connectors/base';
3
4
  export { MONGODB_PROMPT } from './vector/prompt';
@@ -1,6 +1,6 @@
1
1
  import { MongoClient } from 'mongodb';
2
2
  import type { Db } from 'mongodb';
3
- import type { ConnectorHandler } from './ConnectorHandler';
3
+ import type { ConnectorHandler } from './connectors/base';
4
4
  import type { DatabaseConfig } from './types';
5
5
 
6
6
  type MongoDBConnectorOptions =
@@ -142,26 +142,26 @@ export class ScoresStorageMongoDB extends ScoresStorage {
142
142
  scorerId: score.scorerId,
143
143
  traceId: score.traceId || '',
144
144
  runId: score.runId,
145
- scorer: typeof score.scorer === 'string' ? score.scorer : JSON.stringify(score.scorer),
145
+ scorer: typeof score.scorer === 'string' ? safelyParseJSON(score.scorer) : score.scorer,
146
146
  extractStepResult:
147
147
  typeof score.extractStepResult === 'string'
148
- ? score.extractStepResult
149
- : JSON.stringify(score.extractStepResult),
148
+ ? safelyParseJSON(score.extractStepResult)
149
+ : score.extractStepResult,
150
150
  analyzeStepResult:
151
151
  typeof score.analyzeStepResult === 'string'
152
- ? score.analyzeStepResult
153
- : JSON.stringify(score.analyzeStepResult),
152
+ ? safelyParseJSON(score.analyzeStepResult)
153
+ : score.analyzeStepResult,
154
154
  score: score.score,
155
155
  reason: score.reason,
156
156
  extractPrompt: score.extractPrompt,
157
157
  analyzePrompt: score.analyzePrompt,
158
158
  reasonPrompt: score.reasonPrompt,
159
- input: typeof score.input === 'string' ? score.input : JSON.stringify(score.input),
160
- output: typeof score.output === 'string' ? score.output : JSON.stringify(score.output),
159
+ input: typeof score.input === 'string' ? safelyParseJSON(score.input) : score.input,
160
+ output: typeof score.output === 'string' ? safelyParseJSON(score.output) : score.output,
161
161
  additionalContext: score.additionalContext,
162
162
  runtimeContext:
163
- typeof score.runtimeContext === 'string' ? score.runtimeContext : JSON.stringify(score.runtimeContext),
164
- entity: typeof score.entity === 'string' ? score.entity : JSON.stringify(score.entity),
163
+ typeof score.runtimeContext === 'string' ? safelyParseJSON(score.runtimeContext) : score.runtimeContext,
164
+ entity: typeof score.entity === 'string' ? safelyParseJSON(score.entity) : score.entity,
165
165
  source: score.source,
166
166
  resourceId: score.resourceId || '',
167
167
  threadId: score.threadId || '',
@@ -51,6 +51,11 @@ describe('Validation', () => {
51
51
  it('does not throw on valid config', () => {
52
52
  expect(() => new MongoDBStore(validWithConnectionHandlerConfig)).not.toThrow();
53
53
  });
54
+
55
+ it('should initialize the stores correctly', () => {
56
+ const store = new MongoDBStore(validWithConnectionHandlerConfig);
57
+ expect(Object.keys(store.stores)).not.toHaveLength(0);
58
+ });
54
59
  });
55
60
  });
56
61
 
@@ -1,5 +1,5 @@
1
1
  import type { MastraMessageContentV2 } from '@mastra/core/agent';
2
- import { ErrorDomain, ErrorCategory, MastraError } from '@mastra/core/error';
2
+ import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
3
3
  import type { MastraMessageV1, MastraMessageV2, StorageThreadType } from '@mastra/core/memory';
4
4
  import type { ScoreRowData } from '@mastra/core/scores';
5
5
  import type {
@@ -7,15 +7,15 @@ import type {
7
7
  PaginationArgs,
8
8
  PaginationInfo,
9
9
  StorageColumn,
10
+ StorageDomains,
10
11
  StorageGetMessagesArg,
11
12
  StorageGetTracesArg,
13
+ StorageGetTracesPaginatedArg,
14
+ StoragePagination,
15
+ StorageResourceType,
12
16
  TABLE_NAMES,
13
17
  WorkflowRun,
14
18
  WorkflowRuns,
15
- StorageResourceType,
16
- StorageDomains,
17
- StoragePagination,
18
- StorageGetTracesPaginatedArg,
19
19
  } from '@mastra/core/storage';
20
20
  import { MastraStorage } from '@mastra/core/storage';
21
21
  import type { Trace } from '@mastra/core/telemetry';
@@ -29,6 +29,42 @@ import { TracesStorageMongoDB } from './domains/traces';
29
29
  import { WorkflowsStorageMongoDB } from './domains/workflows';
30
30
  import type { MongoDBConfig } from './types';
31
31
 
32
+ const loadConnector = (config: MongoDBConfig): MongoDBConnector => {
33
+ try {
34
+ if ('connectorHandler' in config) {
35
+ return MongoDBConnector.fromConnectionHandler(config.connectorHandler);
36
+ }
37
+ } catch (error) {
38
+ throw new MastraError(
39
+ {
40
+ id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
41
+ domain: ErrorDomain.STORAGE,
42
+ category: ErrorCategory.USER,
43
+ details: { connectionHandler: true },
44
+ },
45
+ error,
46
+ );
47
+ }
48
+
49
+ try {
50
+ return MongoDBConnector.fromDatabaseConfig({
51
+ options: config.options,
52
+ url: config.url,
53
+ dbName: config.dbName,
54
+ });
55
+ } catch (error) {
56
+ throw new MastraError(
57
+ {
58
+ id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
59
+ domain: ErrorDomain.STORAGE,
60
+ category: ErrorCategory.USER,
61
+ details: { url: config?.url, dbName: config?.dbName },
62
+ },
63
+ error,
64
+ );
65
+ }
66
+ };
67
+
32
68
  export class MongoDBStore extends MastraStorage {
33
69
  #connector: MongoDBConnector;
34
70
 
@@ -53,40 +89,7 @@ export class MongoDBStore extends MastraStorage {
53
89
 
54
90
  this.stores = {} as StorageDomains;
55
91
 
56
- try {
57
- if ('connectorHandler' in config) {
58
- this.#connector = MongoDBConnector.fromConnectionHandler(config.connectorHandler);
59
- return;
60
- }
61
- } catch (error) {
62
- throw new MastraError(
63
- {
64
- id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
65
- domain: ErrorDomain.STORAGE,
66
- category: ErrorCategory.USER,
67
- details: { connectionHandler: true },
68
- },
69
- error,
70
- );
71
- }
72
-
73
- try {
74
- this.#connector = MongoDBConnector.fromDatabaseConfig({
75
- options: config.options,
76
- url: config.url,
77
- dbName: config.dbName,
78
- });
79
- } catch (error) {
80
- throw new MastraError(
81
- {
82
- id: 'STORAGE_MONGODB_STORE_CONSTRUCTOR_FAILED',
83
- domain: ErrorDomain.STORAGE,
84
- category: ErrorCategory.USER,
85
- details: { url: config?.url, dbName: config?.dbName },
86
- },
87
- error,
88
- );
89
- }
92
+ this.#connector = loadConnector(config);
90
93
 
91
94
  const operations = new StoreOperationsMongoDB({
92
95
  connector: this.#connector,
@@ -327,7 +327,9 @@ export class MongoDBVector extends MastraVector<MongoDBVectorFilter> {
327
327
  .map(doc => doc._id)
328
328
  .toArray();
329
329
 
330
- vectorSearch.filter = { _id: { $in: candidateIds } };
330
+ if (candidateIds.length > 0) {
331
+ vectorSearch.filter = { _id: { $in: candidateIds } };
332
+ }
331
333
  }
332
334
 
333
335
  // Build the aggregation pipeline
@@ -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
- }