Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function parseMongoConnectionString(connectionString: string): Promise {
let host: string;
let port: string;
const mongoClient: MongoClient = await connectToMongoClient(connectionString, appendExtensionUserAgent());
const serverConfig: Server | ReplSet | Mongos = mongoClient.db(testDb).serverConfig;
// Azure CosmosDB comes back as a ReplSet
if (serverConfig instanceof ReplSet) {
// get the first connection string from the servers for the ReplSet
// this may not be best solution, but the connection (below) gives
// the replicaset host name, which is different than what is in the connection string
// "s" is not part of ReplSet static definition but can't find any official documentation on it. Yet it is definitely there at runtime. Grandfathering in.
// tslint:disable-next-line:no-any
let rs: any = serverConfig;
host = rs.s.options.servers[0].host;
port = rs.s.options.servers[0].port;
} else {
host = serverConfig['host'];
port = serverConfig['port'];
}
public async loadMoreChildrenImpl(_clearCache: boolean): Promise[]> {
let mongoClient: MongoClient | undefined;
try {
let databases: IDatabaseInfo[];
if (!this.connectionString) {
throw new Error('Missing connection string');
}
// Azure MongoDB accounts need to have the name passed in for private endpoints
mongoClient = await connectToMongoClient(this.connectionString, this.databaseAccount ? this.databaseAccount.name : appendExtensionUserAgent());
let databaseInConnectionString = getDatabaseNameFromConnectionString(this.connectionString);
if (databaseInConnectionString && !this.root.isEmulator) { // emulator violates the connection string format
// If the database is in the connection string, that's all we connect to (we might not even have permissions to list databases)
databases = [{
name: databaseInConnectionString,
empty: false
}];
} else {
// https://mongodb.github.io/node-mongodb-native/3.1/api/index.html
let result: { databases: IDatabaseInfo[] } = await mongoClient.db(testDb).admin().listDatabases();
databases = result.databases;
}
return databases
.filter((database: IDatabaseInfo) => !(database.name && database.name.toLowerCase() === "admin" && database.empty)) // Filter out the 'admin' database if it's empty
.map(database => new MongoDatabaseTreeItem(this, database.name, this.connectionString));
public async connectToDb(): Promise {
const accountConnection = await connectToMongoClient(this.connectionString, appendExtensionUserAgent());
return accountConnection.db(this.databaseName);
}