Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/compass-collection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"hadron-document": "^8.10.5",
"mongodb": "^6.19.0",
"mongodb-collection-model": "^5.35.5",
"mongodb-connection-string-url": "^3.0.2",
"mongodb-ns": "^3.0.1",
"mongodb-schema": "^12.6.3",
"react": "^17.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('CollectionHeaderActions [Component]', function () {
globalWrites: false,
rollingIndexes: true,
},
userConnectionString: 'mongodb+srv://localhost:27017',
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ describe('CollectionHeader [Component]', function () {
globalWrites: false,
rollingIndexes: true,
},
userConnectionString: 'mongodb+srv://localhost:27017',
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ export const MONGO_TYPE_TO_FAKER_METHODS: Record<
Symbol: ['lorem.word', 'string.symbol'],
DBRef: ['database.mongodbObjectId'],
};

export const DEFAULT_CONNECTION_STRING_FALLBACK = '<connection-string>';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import MockDataGeneratorModal from './mock-data-generator-modal';
import { MockDataGeneratorStep } from './types';
import { StepButtonLabelMap } from './constants';
import {
DEFAULT_CONNECTION_STRING_FALLBACK,
StepButtonLabelMap,
} from './constants';
import type { CollectionState } from '../../modules/collection-tab';
import { default as collectionTabReducer } from '../../modules/collection-tab';
import type { ConnectionInfo } from '@mongodb-js/connection-info';
Expand All @@ -38,6 +41,7 @@ const defaultSchemaAnalysisState: SchemaAnalysisState = {
avgDocumentSize: undefined,
},
};
const mockUserConnectionString = 'mockUserConnectionString';

describe('MockDataGeneratorModal', () => {
async function renderModal({
Expand Down Expand Up @@ -925,6 +929,7 @@ describe('MockDataGeneratorModal', () => {
globalWrites: false,
rollingIndexes: true,
},
userConnectionString: mockUserConnectionString,
},
};

Expand Down Expand Up @@ -1069,6 +1074,94 @@ describe('MockDataGeneratorModal', () => {
expect(screen.getByText('firstName')).to.exist; // faker method
expect(screen.getByText('insertMany')).to.exist;
});

it('shows userConnectionString in the mongosh command when available', async () => {
const atlasConnectionInfo: ConnectionInfo = {
id: 'test-atlas-connection',
connectionOptions: { connectionString: 'mongodb://localhost:27017' },
atlasMetadata: {
orgId: 'test-org',
projectId: 'test-project-123',
clusterName: 'test-cluster',
clusterUniqueId: 'test-cluster-unique-id',
clusterType: 'REPLICASET' as const,
clusterState: 'IDLE' as const,
metricsId: 'test-metrics-id',
metricsType: 'replicaSet' as const,
regionalBaseUrl: null,
instanceSize: 'M10',
supports: {
globalWrites: false,
rollingIndexes: true,
},
userConnectionString: mockUserConnectionString,
},
};

await renderModal({
currentStep: MockDataGeneratorStep.GENERATE_DATA,
connectionInfo: atlasConnectionInfo,
fakerSchemaGeneration: {
status: 'completed',
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
requestId: 'test-request-id',
},
});

expect(screen.getByText(mockUserConnectionString, { exact: false })).to
.exist;
});

it('shows fallback connection string when there is no Atlas metadata', async () => {
const atlasConnectionInfoWithoutAtlasMetadata: ConnectionInfo = {
id: 'test-atlas-connection',
connectionOptions: { connectionString: 'mongodb://localhost:27017' },
};

await renderModal({
currentStep: MockDataGeneratorStep.GENERATE_DATA,
connectionInfo: atlasConnectionInfoWithoutAtlasMetadata,
fakerSchemaGeneration: {
status: 'completed',
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
requestId: 'test-request-id',
},
});

expect(
screen.getByText(DEFAULT_CONNECTION_STRING_FALLBACK, { exact: false })
).to.exist;
});
});

describe('when rendering the modal in a specific step', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import type { FakerSchema } from './types';
import type { ArrayLengthMap } from './script-generation-utils';
import type { CollectionState } from '../../modules/collection-tab';
import { SCHEMA_ANALYSIS_STATE_COMPLETE } from '../../schema-analysis-types';
import { DEFAULT_CONNECTION_STRING_FALLBACK } from './constants';
import { redactConnectionString } from 'mongodb-connection-string-url';

const RUN_SCRIPT_COMMAND = `
mongosh "mongodb+srv://<your-cluster>.mongodb.net/<your-database>" \\
const RUN_SCRIPT_COMMAND = (connectionString: string) => `
mongosh "${redactConnectionString(connectionString)}" \\
--username <your-username> \\
--password "<your-password>" \\
mockdatascript.js
Expand Down Expand Up @@ -92,6 +94,10 @@ const ScriptScreen = ({
const isDarkMode = useDarkMode();
const connectionInfo = useConnectionInfo();

const connectionString =
connectionInfo?.atlasMetadata?.userConnectionString ??
DEFAULT_CONNECTION_STRING_FALLBACK;

const { database, collection } = toNS(namespace);

// Generate the script using the faker schema
Expand Down Expand Up @@ -175,7 +181,9 @@ const ScriptScreen = ({
reversible.
</em>
</Body>
<Code language={Language.Bash}>{RUN_SCRIPT_COMMAND}</Code>
<Code language={Language.Bash}>
{RUN_SCRIPT_COMMAND(connectionString)}
</Code>
</section>
<section
className={cx(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const mockAtlasConnectionInfo = {
globalWrites: false,
rollingIndexes: true,
},
userConnectionString: 'mongodb+srv://localhost:27017',
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const mockConnectionInfo: ConnectionInfo = {
globalWrites: false,
rollingIndexes: false,
},
userConnectionString: 'mongodb+srv://localhost:27020',
},
};

Expand Down
6 changes: 6 additions & 0 deletions packages/connection-info/src/connection-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export interface AtlasClusterMetadata {
*/
rollingIndexes: boolean;
};

/**
* User-friendly connection string for the Atlas cluster, which uses the SRV address if available.
* Otherwise, will default to <your-cluster> as a placeholder.
Comment on lines +111 to +112
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc comment says the fallback defaults to , but the actual fallback used elsewhere is the full SRV form mongodb+srv://.mongodb.net/. Update the comment to reflect the exact expected format and clarify whether userConnectionString should always be a full connection string (including protocol and optional database) or just a host placeholder.

Suggested change
* User-friendly connection string for the Atlas cluster, which uses the SRV address if available.
* Otherwise, will default to <your-cluster> as a placeholder.
* User-friendly connection string for the Atlas cluster, typically in the SRV format:
* mongodb+srv://<your-cluster>.mongodb.net/<your-database>
* If the SRV address is not available, the fallback is the full SRV form with placeholders:
* mongodb+srv://<your-cluster>.mongodb.net/<your-database>
* This property should always be a full connection string, including protocol and optional database.

Copilot uses AI. Check for mistakes.
*/
userConnectionString: string;
}

export interface ConnectionInfo {
Expand Down
Loading