Skip to content

Show collection count on database tree items 🔢 #658

@tnaum-ms

Description

@tnaum-ms

Problem

When browsing databases in the tree view, there is no indication of how many collections a database contains until you expand it. Users must click to expand every database just to understand its contents.

In contrast, CollectionItem already shows a document count in its description (e.g., 5.2k docs) using a lazy loading pattern. DatabaseItem should follow the same approach.

Current:
▼ my-cluster
    ▶ my-database                    <-- no count

Expected:
▼ my-cluster
    ▶ my-database                    (12 collections)

Expected Behavior

After a DatabaseItem is rendered, the extension should asynchronously fetch the collection count and display it as the tree item's description. The count should load in the background without blocking tree expansion.

Development Hints

Since this is a good first issue, here are some hints on where to start:

Pattern to follow: src/tree/documentdb/CollectionItem.ts

CollectionItem already implements a lazy loading pattern for document count. The approach uses:

  • A cached count field (documentCount) with three states: undefined (not loaded), number (loaded), null (failed)
  • A boolean guard (isLoadingCount) to prevent duplicate fetches
  • A public loadDocumentCount() method called from the parent's getChildren()
  • A private fetchAndUpdateCount() that retrieves the count and calls ext.state.notifyChildrenChanged(this.id) to refresh the tree item

You should replicate this pattern in DatabaseItem:

  1. Add collectionCount, isLoadingCount fields and a loadCollectionCount() / fetchAndUpdateCount() method pair
  2. In fetchAndUpdateCount(), use ClustersClient.listCollections() to get the collection list and take its .length
  3. In getTreeItem(), set description based on the loaded count (e.g., "12 collections" or "1 collection")
  4. In DatabaseItem's parent (ClusterItemBase or similar), call databaseItem.loadCollectionCount() after creating each DatabaseItem in getChildren()

Copying the lazy loading code from CollectionItem is fine for this issue. We can create a follow-up refactoring issue to deduplicate the pattern later. The focus here is the feature, not code reuse.

Key API: ClustersClient.listCollections(databaseName) returns a CollectionItemModel[]. Use its .length for the count.

Files to Modify

File Change
src/tree/documentdb/DatabaseItem.ts Add lazy loading fields, methods, and description in getTreeItem()
Parent tree item that creates DatabaseItem instances Call loadCollectionCount() after creating each DatabaseItem

Acceptance Criteria

  • DatabaseItem shows collection count as description (e.g., 12 collections or 1 collection)
  • Count loads asynchronously without blocking tree expansion
  • If count loading fails, no description is shown (no error displayed to user)
  • When the database has 0 collections, description should still show 0 collections
  • Tooltip behavior unchanged

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions