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
31 changes: 31 additions & 0 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,5 +643,36 @@ describe('MongoshNodeRepl', () => {
expect(output).to.contain('> ');
expect(output).to.not.contain('error');
});

it('changes the prompt when db is reassigned', async() => {
const connectionInfo = {
extraInfo: {
uri: 'mongodb://localhost:27017/test',
is_localhost: true
},
buildInfo: {
version: '4.4.1',
modules: ['enterprise']
}
};

sp.getConnectionInfo.resolves(connectionInfo);
sp.getNewConnection.callsFake(async() => {
Object.assign(connectionInfo.extraInfo, {
is_localhost: true,
is_data_lake: true
});
return sp;
});
sp.platform = 2; // ReplPlatform.CLI ... let's maybe stop using an enum for this
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can make it a string enum with some human readable values 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah … although I’d personally lean towards using something'cli' | 'java-shell' | 'compass' | 'browser' – I think this is one of those things @rose-m might have meant yesterday?


const initialized = await mongoshRepl.initialize(serviceProvider);
await mongoshRepl.startRepl(initialized);
expect(output).to.contain('Enterprise > ');

input.write('db = Mongo("foo").getDB("bar")\n');
await waitEval(bus);
expect(output).to.contain('Atlas Data Lake > ');
});
});
});
2 changes: 1 addition & 1 deletion packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class Mongo extends ShellApiClass {
// if used too early.
get _serviceProvider(): ServiceProvider {
if (this.__serviceProvider === null) {
throw new MongoshInternalError('No ServiceProvider available for this mongo');
throw new MongoshInternalError('No ServiceProvider available for this mongo', ShellApiErrors.NotConnected);
}
return this.__serviceProvider;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/shell-api/src/shell-internal-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@ describe('ShellInternalState', () => {
});
});

describe('topology Sharded but it’s Atlas', () => {
it('shows atlas proxy identifier', async() => {
serviceProvider.getTopology.returns({
description: {
type: 'Sharded'
}
});
serviceProvider.getConnectionInfo.resolves({
extraInfo: {
uri: 'mongodb://localhost/',
is_atlas: true
}
});

await internalState.fetchConnectionInfo();
const prompt = await internalState.getDefaultPrompt();
expect(prompt).to.equal('[atlas proxy]> ');
});
});

describe('topology Unknown', () => {
it('just shows the default prompt', async() => {
const servers = new Map();
Expand Down
18 changes: 14 additions & 4 deletions packages/shell-api/src/shell-internal-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ export default class ShellInternalState {
);
}

get currentServiceProvider(): ServiceProvider {
try {
return this.currentDb._mongo._serviceProvider;
} catch (err) {
if (err.code === ShellApiErrors.NotConnected) {
return this.initialServiceProvider;
}
throw err;
}
}

public emitApiCall(event: ApiEvent): void {
this.messageBus.emit('mongosh:api-call', event);
}
Expand All @@ -255,7 +266,7 @@ export default class ShellInternalState {
// eslint-disable-next-line complexity
topology: () => {
let topology: Topologies;
const topologyDescription = this.initialServiceProvider.getTopology()?.description as TopologyDescription;
const topologyDescription = this.currentServiceProvider.getTopology()?.description as TopologyDescription;
const topologyType: TopologyTypeId | undefined = topologyDescription?.type;
switch (topologyType) {
case 'ReplicaSetNoPrimary':
Expand Down Expand Up @@ -326,12 +337,11 @@ export default class ShellInternalState {
}

private getTopologySpecificPrompt(): string {
const description = this.initialServiceProvider.getTopology()?.description;
const description = this.currentServiceProvider.getTopology()?.description;
if (!description) {
return '';
}


let replicaSet = description.setName;
let serverTypePrompt = '';
// TODO: replace with proper TopologyType constants - NODE-2973
Expand All @@ -348,7 +358,7 @@ export default class ShellInternalState {
serverTypePrompt = '[primary]';
break;
case 'Sharded':
serverTypePrompt = '[mongos]';
serverTypePrompt = this.connectionInfo?.extraInfo?.is_atlas ? '[atlas proxy]' : '[mongos]';
break;
default:
return '';
Expand Down