Skip to content

Commit 864d021

Browse files
authored
Connect to DB in URI initially (#225)
* Connect to URI-set DB * revert compass-shell package.json
1 parent 37049d8 commit 864d021

25 files changed

+161
-43
lines changed

packages/cli-repl/src/arg-mapper.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import mapCliToDriver from './arg-mapper';
2-
import CliOptions from './cli-options';
2+
import { CliOptions } from '@mongosh/service-provider-server';
33
import { expect } from 'chai';
44

55
describe('arg-mapper.mapCliToDriver', () => {

packages/cli-repl/src/arg-mapper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import CliOptions from './cli-options';
2-
import { NodeOptions } from '@mongosh/service-provider-server';
1+
import { NodeOptions, CliOptions } from '@mongosh/service-provider-server';
32
import setValue from 'lodash.set';
43

54
/**

packages/cli-repl/src/arg-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import CliOptions from './cli-options';
1+
import { CliOptions } from '@mongosh/service-provider-server';
22
import { USAGE } from './constants';
33
import i18n from '@mongosh/i18n';
44
import minimist from 'minimist';

packages/cli-repl/src/cli-repl.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint no-console: 0, no-sync: 0*/
22

3-
import { CliServiceProvider, NodeOptions } from '@mongosh/service-provider-server';
3+
import { CliServiceProvider, NodeOptions, CliOptions } from '@mongosh/service-provider-server';
44
import { ShellInternalState } from '@mongosh/shell-api';
55
import ShellEvaluator from '@mongosh/shell-evaluator';
66
import formatOutput, { formatError } from './format-output';
@@ -11,7 +11,6 @@ import { MongoshWarning } from '@mongosh/errors';
1111
import { changeHistory, retractPassword } from '@mongosh/history';
1212
import { REPLServer, Recoverable } from 'repl';
1313
import jsonParse from 'fast-json-parse';
14-
import CliOptions from './cli-options';
1514
import completer from './completer';
1615
import i18n from '@mongosh/i18n';
1716
import { ObjectId } from 'bson';

packages/cli-repl/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import CliRepl from './cli-repl';
22
import parseCliArgs from './arg-parser';
33
import mapCliToDriver from './arg-mapper';
4-
import generateUri from './uri-generator';
54
import completer from './completer';
65
import clr from './clr';
76
import { USAGE, TELEMETRY, MONGOSH_WIKI } from './constants';
@@ -15,7 +14,6 @@ export {
1514
MONGOSH_WIKI,
1615
CliRepl,
1716
completer,
18-
generateUri,
1917
parseCliArgs,
2018
mapCliToDriver
2119
};

packages/cli-repl/src/run.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { CliRepl, parseCliArgs, mapCliToDriver, generateUri, USAGE } from './index';
1+
import { CliRepl, parseCliArgs, mapCliToDriver, USAGE } from './index';
2+
import { generateUri } from '@mongosh/service-provider-server';
23

34
try {
45
const options = parseCliArgs(process.argv);
56
const { version } = require('../package.json');
67

78
if (options.help) {
9+
// eslint-disable-next-line no-console
810
console.log(USAGE);
911
} else if (options.version) {
12+
// eslint-disable-next-line no-console
1013
console.log(version);
1114
} else {
1215
process.title = 'mongosh';
@@ -17,5 +20,6 @@ try {
1720
new CliRepl(driverUri, { appname, ...driverOptions }, options);
1821
}
1922
} catch (e) {
23+
// eslint-disable-next-line no-console
2024
console.log(e.message);
2125
}

packages/cli-repl/test/e2e.spec.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { MongoClient } from 'mongodb';
22
import { eventually } from './helpers';
33
import { TestShell } from './test-shell';
4-
import { startTestServer } from '../../../testing/integration-testing-hooks';
4+
import {
5+
startTestServer,
6+
LOCAL_INSTANCE_HOST,
7+
LOCAL_INSTANCE_PORT
8+
} from '../../../testing/integration-testing-hooks';
59

610
describe('e2e', function() {
711
const connectionString = startTestServer();
@@ -42,6 +46,57 @@ describe('e2e', function() {
4246
});
4347
});
4448

49+
describe('set db', () => {
50+
describe('via host:port/test', () => {
51+
let shell;
52+
beforeEach(async() => {
53+
shell = TestShell.start({ args: [`${LOCAL_INSTANCE_HOST}:${LOCAL_INSTANCE_PORT}/testdb1`] });
54+
await shell.waitForPrompt();
55+
shell.assertNoErrors();
56+
});
57+
it('db set correctly', async() => {
58+
await shell.executeLine('db');
59+
shell.assertNoErrors();
60+
61+
await eventually(() => {
62+
shell.assertContainsOutput('testdb1');
63+
});
64+
});
65+
});
66+
describe('via mongodb://uri', () => {
67+
let shell;
68+
beforeEach(async() => {
69+
shell = TestShell.start({ args: [`mongodb://${LOCAL_INSTANCE_HOST}:${LOCAL_INSTANCE_PORT}/testdb2`] });
70+
await shell.waitForPrompt();
71+
shell.assertNoErrors();
72+
});
73+
it('db set correctly', async() => {
74+
await shell.executeLine('db');
75+
shell.assertNoErrors();
76+
77+
await eventually(() => {
78+
shell.assertContainsOutput('testdb2');
79+
});
80+
});
81+
});
82+
describe('legacy db only', () => {
83+
let shell;
84+
beforeEach(async() => {
85+
shell = TestShell.start({ args: ['testdb3', `--port=${LOCAL_INSTANCE_PORT}`] });
86+
await shell.waitForPrompt();
87+
shell.assertNoErrors();
88+
});
89+
it('db set correctly', async() => {
90+
await shell.executeLine('db');
91+
shell.assertNoErrors();
92+
93+
await eventually(() => {
94+
shell.assertContainsOutput('testdb3');
95+
});
96+
});
97+
});
98+
});
99+
45100
describe('with connection string', () => {
46101
let db;
47102
let client;
@@ -109,6 +164,15 @@ describe('e2e', function() {
109164
shell.assertNoErrors();
110165
});
111166

167+
it('db set correctly', async() => {
168+
await shell.executeLine('db');
169+
shell.assertNoErrors();
170+
171+
await eventually(() => {
172+
shell.assertContainsOutput('test');
173+
});
174+
});
175+
112176
it('allows to find documents', async() => {
113177
await shell.writeInputLine(`use ${dbName}`);
114178

packages/service-provider-browser/src/stitch-service-provider-browser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
CommandOptions,
99
WriteConcern,
1010
getConnectInfo,
11-
ReplPlatform
11+
ReplPlatform,
12+
DEFAULT_DB
1213
} from '@mongosh/service-provider-core';
1314

1415
import StitchTransport from './stitch-transport';
@@ -40,6 +41,7 @@ const ATLAS = 'mongodb-atlas';
4041
class StitchServiceProviderBrowser implements ServiceProvider {
4142
readonly stitchTransport: StitchTransport<StitchAppClient, RemoteMongoClient>;
4243
public readonly platform: ReplPlatform;
44+
public readonly initialDb: string;
4345
/**
4446
* Create a StitchBrowserTransport from a Stitch app id.
4547
*
@@ -150,6 +152,7 @@ class StitchServiceProviderBrowser implements ServiceProvider {
150152
this.stitchTransport =
151153
new StitchTransport<StitchAppClient, RemoteMongoClient>(stitchClient, mongoClient);
152154
this.platform = ReplPlatform.Browser;
155+
this.initialDb = DEFAULT_DB;
153156
}
154157

155158
aggregateDb(database: string, pipeline: Document[], options?: Document, dbOptions?: DatabaseOptions): Cursor {

packages/service-provider-core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"node": "^12.4.0"
2424
},
2525
"dependencies": {
26-
"mongodb-build-info": "^1.0.0"
26+
"mongodb-build-info": "^1.0.0",
27+
"@mongosh/i18n": "^0.0.5"
2728
},
2829
"dependency-check": {
2930
"entries": [

packages/service-provider-core/src/admin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ export default interface Admin {
4242
* What platform (Compass/CLI/Browser)
4343
*/
4444
platform: ReplPlatform;
45+
46+
/**
47+
* The initial database
48+
*/
49+
initialDb: string;
4550
}

0 commit comments

Comments
 (0)