Skip to content

Commit 2051b4b

Browse files
authored
feat(compass-collection): Add connection string to script screen CLOUDP-347558 (#7470)
* Add connection string to script screen
1 parent ab9e799 commit 2051b4b

File tree

10 files changed

+156
-4
lines changed

10 files changed

+156
-4
lines changed

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-collection/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"hadron-document": "^8.10.5",
6767
"mongodb": "^6.19.0",
6868
"mongodb-collection-model": "^5.35.5",
69+
"mongodb-connection-string-url": "^3.0.2",
6970
"mongodb-ns": "^3.0.1",
7071
"mongodb-schema": "^12.6.3",
7172
"react": "^17.0.2",

packages/compass-collection/src/components/collection-header-actions/collection-header-actions.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ describe('CollectionHeaderActions [Component]', function () {
214214
globalWrites: false,
215215
rollingIndexes: true,
216216
},
217+
userConnectionString: 'mongodb+srv://localhost:27017',
217218
},
218219
};
219220

packages/compass-collection/src/components/collection-header/collection-header.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ describe('CollectionHeader [Component]', function () {
372372
globalWrites: false,
373373
rollingIndexes: true,
374374
},
375+
userConnectionString: 'mongodb+srv://localhost:27017',
375376
},
376377
};
377378

packages/compass-collection/src/components/mock-data-generator-modal/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,5 @@ export const MONGO_TYPE_TO_FAKER_METHODS: Record<
118118
Symbol: ['lorem.word', 'string.symbol'],
119119
DBRef: ['database.mongodbObjectId'],
120120
};
121+
122+
export const DEFAULT_CONNECTION_STRING_FALLBACK = '<connection-string>';

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import { createStore, applyMiddleware } from 'redux';
1313
import thunk from 'redux-thunk';
1414
import MockDataGeneratorModal from './mock-data-generator-modal';
1515
import { MockDataGeneratorStep } from './types';
16-
import { StepButtonLabelMap } from './constants';
16+
import {
17+
DEFAULT_CONNECTION_STRING_FALLBACK,
18+
StepButtonLabelMap,
19+
} from './constants';
1720
import type { CollectionState } from '../../modules/collection-tab';
1821
import { default as collectionTabReducer } from '../../modules/collection-tab';
1922
import type { ConnectionInfo } from '@mongodb-js/connection-info';
@@ -38,6 +41,7 @@ const defaultSchemaAnalysisState: SchemaAnalysisState = {
3841
avgDocumentSize: undefined,
3942
},
4043
};
44+
const mockUserConnectionString = 'mockUserConnectionString';
4145

4246
describe('MockDataGeneratorModal', () => {
4347
async function renderModal({
@@ -925,6 +929,7 @@ describe('MockDataGeneratorModal', () => {
925929
globalWrites: false,
926930
rollingIndexes: true,
927931
},
932+
userConnectionString: mockUserConnectionString,
928933
},
929934
};
930935

@@ -1069,6 +1074,94 @@ describe('MockDataGeneratorModal', () => {
10691074
expect(screen.getByText('firstName')).to.exist; // faker method
10701075
expect(screen.getByText('insertMany')).to.exist;
10711076
});
1077+
1078+
it('shows userConnectionString in the mongosh command when available', async () => {
1079+
const atlasConnectionInfo: ConnectionInfo = {
1080+
id: 'test-atlas-connection',
1081+
connectionOptions: { connectionString: 'mongodb://localhost:27017' },
1082+
atlasMetadata: {
1083+
orgId: 'test-org',
1084+
projectId: 'test-project-123',
1085+
clusterName: 'test-cluster',
1086+
clusterUniqueId: 'test-cluster-unique-id',
1087+
clusterType: 'REPLICASET' as const,
1088+
clusterState: 'IDLE' as const,
1089+
metricsId: 'test-metrics-id',
1090+
metricsType: 'replicaSet' as const,
1091+
regionalBaseUrl: null,
1092+
instanceSize: 'M10',
1093+
supports: {
1094+
globalWrites: false,
1095+
rollingIndexes: true,
1096+
},
1097+
userConnectionString: mockUserConnectionString,
1098+
},
1099+
};
1100+
1101+
await renderModal({
1102+
currentStep: MockDataGeneratorStep.GENERATE_DATA,
1103+
connectionInfo: atlasConnectionInfo,
1104+
fakerSchemaGeneration: {
1105+
status: 'completed',
1106+
originalLlmResponse: {
1107+
name: {
1108+
fakerMethod: 'person.firstName',
1109+
fakerArgs: [],
1110+
probability: 1.0,
1111+
mongoType: 'String',
1112+
},
1113+
},
1114+
editedFakerSchema: {
1115+
name: {
1116+
fakerMethod: 'person.firstName',
1117+
fakerArgs: [],
1118+
probability: 1.0,
1119+
mongoType: 'String',
1120+
},
1121+
},
1122+
requestId: 'test-request-id',
1123+
},
1124+
});
1125+
1126+
expect(screen.getByText(mockUserConnectionString, { exact: false })).to
1127+
.exist;
1128+
});
1129+
1130+
it('shows fallback connection string when there is no Atlas metadata', async () => {
1131+
const atlasConnectionInfoWithoutAtlasMetadata: ConnectionInfo = {
1132+
id: 'test-atlas-connection',
1133+
connectionOptions: { connectionString: 'mongodb://localhost:27017' },
1134+
};
1135+
1136+
await renderModal({
1137+
currentStep: MockDataGeneratorStep.GENERATE_DATA,
1138+
connectionInfo: atlasConnectionInfoWithoutAtlasMetadata,
1139+
fakerSchemaGeneration: {
1140+
status: 'completed',
1141+
originalLlmResponse: {
1142+
name: {
1143+
fakerMethod: 'person.firstName',
1144+
fakerArgs: [],
1145+
probability: 1.0,
1146+
mongoType: 'String',
1147+
},
1148+
},
1149+
editedFakerSchema: {
1150+
name: {
1151+
fakerMethod: 'person.firstName',
1152+
fakerArgs: [],
1153+
probability: 1.0,
1154+
mongoType: 'String',
1155+
},
1156+
},
1157+
requestId: 'test-request-id',
1158+
},
1159+
});
1160+
1161+
expect(
1162+
screen.getByText(DEFAULT_CONNECTION_STRING_FALLBACK, { exact: false })
1163+
).to.exist;
1164+
});
10721165
});
10731166

10741167
describe('when rendering the modal in a specific step', () => {

packages/compass-collection/src/components/mock-data-generator-modal/script-screen.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import type { FakerSchema } from './types';
2222
import type { ArrayLengthMap } from './script-generation-utils';
2323
import type { CollectionState } from '../../modules/collection-tab';
2424
import { SCHEMA_ANALYSIS_STATE_COMPLETE } from '../../schema-analysis-types';
25+
import { DEFAULT_CONNECTION_STRING_FALLBACK } from './constants';
26+
import { redactConnectionString } from 'mongodb-connection-string-url';
2527

26-
const RUN_SCRIPT_COMMAND = `
27-
mongosh "mongodb+srv://<your-cluster>.mongodb.net/<your-database>" \\
28+
const RUN_SCRIPT_COMMAND = (connectionString: string) => `
29+
mongosh "${redactConnectionString(connectionString)}" \\
2830
--username <your-username> \\
2931
--password "<your-password>" \\
3032
mockdatascript.js
@@ -92,6 +94,10 @@ const ScriptScreen = ({
9294
const isDarkMode = useDarkMode();
9395
const connectionInfo = useConnectionInfo();
9496

97+
const connectionString =
98+
connectionInfo?.atlasMetadata?.userConnectionString ??
99+
DEFAULT_CONNECTION_STRING_FALLBACK;
100+
95101
const { database, collection } = toNS(namespace);
96102

97103
// Generate the script using the faker schema
@@ -175,7 +181,9 @@ const ScriptScreen = ({
175181
reversible.
176182
</em>
177183
</Body>
178-
<Code language={Language.Bash}>{RUN_SCRIPT_COMMAND}</Code>
184+
<Code language={Language.Bash}>
185+
{RUN_SCRIPT_COMMAND(connectionString)}
186+
</Code>
179187
</section>
180188
<section
181189
className={cx(

packages/compass-collection/src/stores/collection-tab.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const mockAtlasConnectionInfo = {
7777
globalWrites: false,
7878
rollingIndexes: true,
7979
},
80+
userConnectionString: 'mongodb+srv://localhost:27017',
8081
},
8182
},
8283
};

packages/compass-generative-ai/src/atlas-ai-service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const mockConnectionInfo: ConnectionInfo = {
4141
globalWrites: false,
4242
rollingIndexes: false,
4343
},
44+
userConnectionString: 'mongodb+srv://localhost:27020',
4445
},
4546
};
4647

packages/connection-info/src/connection-info.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export interface AtlasClusterMetadata {
106106
*/
107107
rollingIndexes: boolean;
108108
};
109+
110+
/**
111+
* User-friendly connection string for the Atlas cluster, which uses the SRV address if available.
112+
* Otherwise, will default to <your-cluster> as a placeholder.
113+
*/
114+
userConnectionString: string;
109115
}
110116

111117
export interface ConnectionInfo {

0 commit comments

Comments
 (0)