async function searchDbAccounts()

in src/commands/api/findTreeItem.ts [90:146]


async function searchDbAccounts(dbAccounts: AzExtTreeItem[], expected: ParsedConnectionString, context: IActionContext, maxTime: number): Promise<DatabaseAccountTreeItem | DatabaseTreeItem | undefined> {
    try {
        for (const dbAccount of dbAccounts) {
            if (Date.now() > maxTime) {
                return undefined;
            }

            let actual: ParsedConnectionString;
            if (dbAccount instanceof MongoAccountTreeItem) {
                actual = await parseMongoConnectionString(dbAccount.connectionString);
            } else if (dbAccount instanceof DocDBAccountTreeItemBase) {
                actual = parseDocDBConnectionString(dbAccount.connectionString);
            } else if (dbAccount instanceof PostgresServerTreeItem) {
                actual = dbAccount.partialConnectionString;
            } else {
                return undefined;
            }

            if (expected.accountId === actual.accountId) {
                if (expected.databaseName) {
                    const dbs = await dbAccount.getCachedChildren(context);
                    for (const db of dbs) {
                        if ((db instanceof MongoDatabaseTreeItem || db instanceof DocDBDatabaseTreeItemBase) && expected.databaseName === db.databaseName) {
                            return new DatabaseTreeItemInternal(expected, expected.databaseName, dbAccount, db);
                        }
                        if ((db instanceof PostgresDatabaseTreeItem && dbAccount instanceof PostgresServerTreeItem) && expected.databaseName === db.databaseName) {
                            const fullConnectionString = await dbAccount.getFullConnectionString();
                            return new DatabaseTreeItemInternal(fullConnectionString, expected.databaseName, dbAccount, db);
                        }
                    }

                    // We found the right account - just not the db. In this case we can still 'reveal' the account
                    if (dbAccount instanceof PostgresServerTreeItem) {
                        const fullConnectionString = await dbAccount.getFullConnectionString();
                        return new DatabaseTreeItemInternal(fullConnectionString, expected.databaseName, dbAccount);
                    } else {
                        return new DatabaseTreeItemInternal(expected, expected.databaseName, dbAccount);
                    }

                }

                if (dbAccount instanceof PostgresServerTreeItem) {
                    const fullConnectionString = await dbAccount.getFullConnectionString();
                    return new DatabaseAccountTreeItemInternal(fullConnectionString, dbAccount);
                } else {
                    return new DatabaseAccountTreeItemInternal(expected, dbAccount);
                }

            }
        }
    } catch (error) {
        // Swallow all errors to avoid blocking the db account search
        // https://github.com/microsoft/vscode-cosmosdb/issues/966
    }

    return undefined;
}