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
2 changes: 1 addition & 1 deletion packages/cli-repl/src/arg-mapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mapCliToDriver from './arg-mapper';
import CliOptions from './cli-options';
import { CliOptions } from '@mongosh/service-provider-server';
import { expect } from 'chai';

describe('arg-mapper.mapCliToDriver', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-repl/src/arg-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import CliOptions from './cli-options';
import { NodeOptions } from '@mongosh/service-provider-server';
import { NodeOptions, CliOptions } from '@mongosh/service-provider-server';
import setValue from 'lodash.set';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/src/arg-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CliOptions from './cli-options';
import { CliOptions } from '@mongosh/service-provider-server';
import { USAGE } from './constants';
import i18n from '@mongosh/i18n';
import minimist from 'minimist';
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint no-console: 0, no-sync: 0*/

import { CliServiceProvider, NodeOptions } from '@mongosh/service-provider-server';
import { CliServiceProvider, NodeOptions, CliOptions } from '@mongosh/service-provider-server';
import { ShellInternalState } from '@mongosh/shell-api';
import ShellEvaluator from '@mongosh/shell-evaluator';
import formatOutput, { formatError } from './format-output';
Expand All @@ -11,7 +11,6 @@ import { MongoshWarning } from '@mongosh/errors';
import { changeHistory, retractPassword } from '@mongosh/history';
import { REPLServer, Recoverable } from 'repl';
import jsonParse from 'fast-json-parse';
import CliOptions from './cli-options';
import completer from './completer';
import i18n from '@mongosh/i18n';
import { ObjectId } from 'bson';
Expand Down
2 changes: 0 additions & 2 deletions packages/cli-repl/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CliRepl from './cli-repl';
import parseCliArgs from './arg-parser';
import mapCliToDriver from './arg-mapper';
import generateUri from './uri-generator';
import completer from './completer';
import clr from './clr';
import { USAGE, TELEMETRY, MONGOSH_WIKI } from './constants';
Expand All @@ -15,7 +14,6 @@ export {
MONGOSH_WIKI,
CliRepl,
completer,
generateUri,
parseCliArgs,
mapCliToDriver
};
6 changes: 5 additions & 1 deletion packages/cli-repl/src/run.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { CliRepl, parseCliArgs, mapCliToDriver, generateUri, USAGE } from './index';
import { CliRepl, parseCliArgs, mapCliToDriver, USAGE } from './index';
import { generateUri } from '@mongosh/service-provider-server';

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

if (options.help) {
// eslint-disable-next-line no-console
console.log(USAGE);
} else if (options.version) {
// eslint-disable-next-line no-console
console.log(version);
} else {
process.title = 'mongosh';
Expand All @@ -17,5 +20,6 @@ try {
new CliRepl(driverUri, { appname, ...driverOptions }, options);
}
} catch (e) {
// eslint-disable-next-line no-console
console.log(e.message);
}
66 changes: 65 additions & 1 deletion packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { MongoClient } from 'mongodb';
import { eventually } from './helpers';
import { TestShell } from './test-shell';
import { startTestServer } from '../../../testing/integration-testing-hooks';
import {
startTestServer,
LOCAL_INSTANCE_HOST,
LOCAL_INSTANCE_PORT
} from '../../../testing/integration-testing-hooks';

describe('e2e', function() {
const connectionString = startTestServer();
Expand Down Expand Up @@ -42,6 +46,57 @@ describe('e2e', function() {
});
});

describe('set db', () => {
describe('via host:port/test', () => {
let shell;
beforeEach(async() => {
shell = TestShell.start({ args: [`${LOCAL_INSTANCE_HOST}:${LOCAL_INSTANCE_PORT}/testdb1`] });
await shell.waitForPrompt();
shell.assertNoErrors();
});
it('db set correctly', async() => {
await shell.executeLine('db');
shell.assertNoErrors();

await eventually(() => {
shell.assertContainsOutput('testdb1');
});
});
});
describe('via mongodb://uri', () => {
let shell;
beforeEach(async() => {
shell = TestShell.start({ args: [`mongodb://${LOCAL_INSTANCE_HOST}:${LOCAL_INSTANCE_PORT}/testdb2`] });
await shell.waitForPrompt();
shell.assertNoErrors();
});
it('db set correctly', async() => {
await shell.executeLine('db');
shell.assertNoErrors();

await eventually(() => {
shell.assertContainsOutput('testdb2');
});
});
});
describe('legacy db only', () => {
let shell;
beforeEach(async() => {
shell = TestShell.start({ args: ['testdb3', `--port=${LOCAL_INSTANCE_PORT}`] });
await shell.waitForPrompt();
shell.assertNoErrors();
});
it('db set correctly', async() => {
await shell.executeLine('db');
shell.assertNoErrors();

await eventually(() => {
shell.assertContainsOutput('testdb3');
});
});
});
});

describe('with connection string', () => {
let db;
let client;
Expand Down Expand Up @@ -109,6 +164,15 @@ describe('e2e', function() {
shell.assertNoErrors();
});

it('db set correctly', async() => {
await shell.executeLine('db');
shell.assertNoErrors();

await eventually(() => {
shell.assertContainsOutput('test');
});
});

it('allows to find documents', async() => {
await shell.writeInputLine(`use ${dbName}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
CommandOptions,
WriteConcern,
getConnectInfo,
ReplPlatform
ReplPlatform,
DEFAULT_DB
} from '@mongosh/service-provider-core';

import StitchTransport from './stitch-transport';
Expand Down Expand Up @@ -40,6 +41,7 @@ const ATLAS = 'mongodb-atlas';
class StitchServiceProviderBrowser implements ServiceProvider {
readonly stitchTransport: StitchTransport<StitchAppClient, RemoteMongoClient>;
public readonly platform: ReplPlatform;
public readonly initialDb: string;
/**
* Create a StitchBrowserTransport from a Stitch app id.
*
Expand Down Expand Up @@ -150,6 +152,7 @@ class StitchServiceProviderBrowser implements ServiceProvider {
this.stitchTransport =
new StitchTransport<StitchAppClient, RemoteMongoClient>(stitchClient, mongoClient);
this.platform = ReplPlatform.Browser;
this.initialDb = DEFAULT_DB;
}

aggregateDb(database: string, pipeline: Document[], options?: Document, dbOptions?: DatabaseOptions): Cursor {
Expand Down
3 changes: 2 additions & 1 deletion packages/service-provider-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"node": "^12.4.0"
},
"dependencies": {
"mongodb-build-info": "^1.0.0"
"mongodb-build-info": "^1.0.0",
"@mongosh/i18n": "^0.0.5"
},
"dependency-check": {
"entries": [
Expand Down
5 changes: 5 additions & 0 deletions packages/service-provider-core/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ export default interface Admin {
* What platform (Compass/CLI/Browser)
*/
platform: ReplPlatform;

/**
* The initial database
*/
initialDb: string;
}
9 changes: 8 additions & 1 deletion packages/service-provider-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import CommandOptions from './command-options';
import DatabaseOptions from './database-options';
import getConnectInfo from './connect-info';
import { ReplPlatform } from './platform';
import CliOptions from './cli-options';
import generateUri, { Scheme } from './uri-generator';
const DEFAULT_DB = 'test';

export {
ServiceProvider,
Expand All @@ -21,5 +24,9 @@ export {
CommandOptions,
DatabaseOptions,
getConnectInfo,
ReplPlatform
ReplPlatform,
CliOptions,
generateUri,
Scheme,
DEFAULT_DB
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import i18n from '@mongosh/i18n';
import CliOptions from './cli-options';
import { DEFAULT_DB } from './index';

/**
* URI schemes.
Expand Down Expand Up @@ -38,11 +39,6 @@ const DEFAULT_PORT = '27017';
*/
const CONFLICT = 'cli-repl.uri-generator.no-host-port';

/**
* The default db name.
*/
const TEST = 'test';

/**
* Validate conflicts in the options.
*
Expand Down Expand Up @@ -128,7 +124,7 @@ function generateUri(options: CliOptions): string {
validateConflicts(options);
}

return `${Scheme.Mongo}${host || generateHost(options)}:${port || generatePort(options)}/${db || TEST}`;
return `${Scheme.Mongo}${host || generateHost(options)}:${port || generatePort(options)}/${db || DEFAULT_DB}`;
}

export default generateUri;
Expand Down
9 changes: 8 additions & 1 deletion packages/service-provider-server/src/cli-service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
WriteConcern,
CommandOptions,
getConnectInfo,
ReplPlatform
ReplPlatform,
DEFAULT_DB
} from '@mongosh/service-provider-core';

import NodeOptions from './node/node-options';
Expand All @@ -37,6 +38,7 @@ const DEFAULT_OPTIONS = Object.freeze({
*/
class CliServiceProvider implements ServiceProvider {
public readonly platform: ReplPlatform;
public readonly initialDb: string;
/**
* Create a new CLI service provider from the provided URI.
*
Expand Down Expand Up @@ -80,6 +82,11 @@ class CliServiceProvider implements ServiceProvider {
this.mongoClient = mongoClient;
this.uri = uri;
this.platform = ReplPlatform.CLI;
try {
this.initialDb = mongoClient.s.options.dbName || DEFAULT_DB;
} catch (err) {
this.initialDb = DEFAULT_DB;
}
}

async getNewConnection(uri: string, options: NodeOptions = {}): Promise<CliServiceProvider> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CliServiceProvider from '../cli-service-provider';
import { MongoClient } from 'mongodb';
import { ReplPlatform } from '@mongosh/service-provider-core';
import { DEFAULT_DB, ReplPlatform } from '@mongosh/service-provider-core';

interface DataService {
client: {
Expand All @@ -13,6 +13,7 @@ interface DataService {
*/
class CompassServiceProvider extends CliServiceProvider {
public readonly platform: ReplPlatform;
public readonly initialDb: string;
/**
* Instantiate a new CompassServiceProvider with the data-service's connected
* MongoClient instance.
Expand All @@ -23,6 +24,11 @@ class CompassServiceProvider extends CliServiceProvider {
constructor(mongoClient: MongoClient, uri?: string) {
super(mongoClient, uri);
this.platform = ReplPlatform.Compass;
try {
this.initialDb = mongoClient.s.options.dbName || DEFAULT_DB;
} catch (err) {
this.initialDb = DEFAULT_DB;
}
}
/**
* Creates a new CompassServiceProvider that uses compass
Expand Down
7 changes: 6 additions & 1 deletion packages/service-provider-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import NodeOptions from './node/node-options';

import CliServiceProvider from './cli-service-provider';
import CompassServiceProvider from './compass/compass-service-provider';
import { DEFAULT_DB, CliOptions, Scheme, generateUri } from '@mongosh/service-provider-core';
export {
CliServiceProvider,
CompassServiceProvider,
NodeOptions
NodeOptions,
DEFAULT_DB,
CliOptions,
Scheme,
generateUri
};
1 change: 1 addition & 0 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('Collection', () => {
beforeEach(() => {
bus = stubInterface<EventEmitter>();
serviceProvider = stubInterface<ServiceProvider>();
serviceProvider.initialDb = 'test';
internalState = new ShellInternalState(serviceProvider, bus);
mongo = new Mongo(internalState);
database = new Database(mongo, 'db1');
Expand Down
1 change: 1 addition & 0 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('Database', () => {
beforeEach(() => {
bus = stubInterface<EventEmitter>();
serviceProvider = stubInterface<ServiceProvider>();
serviceProvider.initialDb = 'test';
internalState = new ShellInternalState(serviceProvider, bus);
mongo = new Mongo(internalState);
database = new Database(mongo, 'db1');
Expand Down
1 change: 1 addition & 0 deletions packages/shell-api/src/explainable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('Explainable', () => {
beforeEach(() => {
bus = stubInterface<EventEmitter>();
serviceProvider = stubInterface<ServiceProvider>();
serviceProvider.initialDb = 'test';
internalState = new ShellInternalState(serviceProvider, bus);
mongo = new Mongo(internalState);
database = new Database(mongo, 'db1');
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ShellApiClass,
shellApiClassDefault
} from './decorators';
import { ReplPlatform } from '@mongosh/service-provider-core';
import { ReplPlatform, generateUri } from '@mongosh/service-provider-core';
import Database from './database';
import ShellInternalState from './shell-internal-state';
import { CommandResult } from './result';
Expand All @@ -34,7 +34,7 @@ export default class Mongo extends ShellApiClass {
super();
this.internalState = internalState;
this.databases = {};
this.uri = uri;
this.uri = generateUri({ _: [uri] });
this.options = fleOptions;
this.serviceProvider = this.internalState.initialServiceProvider;
}
Expand Down
Loading