Skip to content

Commit 8846c94

Browse files
authored
VSCODE-56: Display document count in tree view (#152)
1 parent 346d1c8 commit 8846c94

17 files changed

+731
-464
lines changed

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@
264264
"command": "mdb.viewCollectionDocuments",
265265
"title": "View Documents"
266266
},
267+
{
268+
"command": "mdb.refreshDocumentList",
269+
"title": "Refresh"
270+
},
267271
{
268272
"command": "mdb.copyCollectionName",
269273
"title": "Copy Collection Name"
@@ -423,6 +427,10 @@
423427
"command": "mdb.viewCollectionDocuments",
424428
"when": "view == mongoDB && viewItem == documentListTreeItem"
425429
},
430+
{
431+
"command": "mdb.refreshDocumentList",
432+
"when": "view == mongoDB && viewItem == documentListTreeItem"
433+
},
426434
{
427435
"command": "mdb.refreshSchema",
428436
"when": "view == mongoDB && viewItem == schemaTreeItem"
@@ -504,6 +512,10 @@
504512
"command": "mdb.viewCollectionDocuments",
505513
"when": "false"
506514
},
515+
{
516+
"command": "mdb.refreshDocumentList",
517+
"when": "false"
518+
},
507519
{
508520
"command": "mdb.copyCollectionName",
509521
"when": "false"
@@ -647,6 +659,7 @@
647659
"mongodb-data-service": "^16.8.1",
648660
"mongodb-ns": "^2.2.0",
649661
"mongodb-schema": "^8.2.5",
662+
"numeral": "^2.0.6",
650663
"react": "^16.13.1",
651664
"react-dom": "^16.13.1",
652665
"react-redux": "^7.2.0",

src/explorer/collectionTreeItem.ts

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
const path = require('path');
33

4+
import { createLogger } from '../logging';
45
import DocumentListTreeItem, {
56
CollectionTypes,
67
MAX_DOCUMENTS_VISIBLE
@@ -10,6 +11,8 @@ import TreeItemParent from './treeItemParentInterface';
1011
import SchemaTreeItem from './schemaTreeItem';
1112
import { getImagesPath } from '../extensionConstants';
1213

14+
const log = createLogger('tree view collection folder');
15+
1316
type CollectionModelType = {
1417
name: string;
1518
type: CollectionTypes;
@@ -36,9 +39,11 @@ export default class CollectionTreeItem extends vscode.TreeItem
3639
collection: CollectionModelType;
3740
collectionName: string;
3841
databaseName: string;
42+
namespace: string;
3943

4044
private _dataService: any;
4145
private _type: CollectionTypes;
46+
documentCount: number | null;
4247

4348
isExpanded: boolean;
4449

@@ -50,6 +55,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
5055
dataService: any,
5156
isExpanded: boolean,
5257
cacheIsUpToDate: boolean,
58+
cachedDocumentCount: number | null,
5359
existingDocumentListChild?: DocumentListTreeItem,
5460
existingSchemaChild?: SchemaTreeItem,
5561
existingIndexListChild?: IndexListTreeItem
@@ -64,12 +70,15 @@ export default class CollectionTreeItem extends vscode.TreeItem
6470
this.collection = collection;
6571
this.collectionName = collection.name;
6672
this.databaseName = databaseName;
73+
this.namespace = `${this.databaseName}.${this.collectionName}`;
6774

6875
this._type = collection.type; // Type can be `collection` or `view`.
6976
this._dataService = dataService;
7077

7178
this.isExpanded = isExpanded;
7279

80+
this.documentCount = cachedDocumentCount;
81+
7382
this.cacheIsUpToDate = cacheIsUpToDate;
7483

7584
this._documentListChild = existingDocumentListChild
@@ -81,7 +90,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
8190
this._dataService,
8291
false, // Collapsed.
8392
MAX_DOCUMENTS_VISIBLE,
84-
false, // No more documents to show.
93+
this.documentCount,
94+
this.refreshDocumentCount,
8595
false, // Cache is not up to date.
8696
[] // Empty cache.
8797
);
@@ -119,9 +129,13 @@ export default class CollectionTreeItem extends vscode.TreeItem
119129
return element;
120130
}
121131

122-
getChildren(): Thenable<any[]> {
132+
async getChildren(): Promise<any[]> {
123133
if (!this.isExpanded) {
124-
return Promise.resolve([]);
134+
return [];
135+
}
136+
137+
if (this.documentCount === null) {
138+
await this.refreshDocumentCount();
125139
}
126140

127141
// Update cache if one of the children has been expanded/collapsed.
@@ -130,11 +144,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
130144
}
131145

132146
if (this.cacheIsUpToDate) {
133-
return Promise.resolve([
134-
this._documentListChild,
135-
this._schemaChild,
136-
this._indexListChild
137-
]);
147+
return [this._documentListChild, this._schemaChild, this._indexListChild];
138148
}
139149

140150
this.cacheIsUpToDate = true;
@@ -143,11 +153,7 @@ export default class CollectionTreeItem extends vscode.TreeItem
143153
// is ensure to be set by vscode.
144154
this.rebuildChildrenCache();
145155

146-
return Promise.resolve([
147-
this._documentListChild,
148-
this._schemaChild,
149-
this._indexListChild
150-
]);
156+
return [this._documentListChild, this._schemaChild, this._indexListChild];
151157
}
152158

153159
rebuildDocumentListTreeItem(): void {
@@ -158,7 +164,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
158164
this._dataService,
159165
this._documentListChild.isExpanded,
160166
this._documentListChild.getMaxDocumentsToShow(),
161-
this._documentListChild.hasMoreDocumentsToShow,
167+
this.documentCount,
168+
this.refreshDocumentCount,
162169
this._documentListChild.cacheIsUpToDate,
163170
this._documentListChild.getChildrenCache()
164171
);
@@ -209,15 +216,18 @@ export default class CollectionTreeItem extends vscode.TreeItem
209216
this.cacheIsUpToDate = false;
210217
}
211218

212-
onDidExpand(): Promise<boolean> {
219+
async onDidExpand(): Promise<boolean> {
213220
this.isExpanded = true;
214221
this.cacheIsUpToDate = false;
215222

216-
return Promise.resolve(true);
223+
await this.refreshDocumentCount();
224+
225+
return true;
217226
}
218227

219228
resetCache(): void {
220229
this.cacheIsUpToDate = false;
230+
this.documentCount = null;
221231

222232
this._documentListChild = new DocumentListTreeItem(
223233
this.collectionName,
@@ -226,7 +236,8 @@ export default class CollectionTreeItem extends vscode.TreeItem
226236
this._dataService,
227237
false, // Collapsed.
228238
MAX_DOCUMENTS_VISIBLE,
229-
false, // No more documents to show.
239+
this.documentCount,
240+
this.refreshDocumentCount,
230241
false, // Cache is not up to date.
231242
[] // Empty cache.
232243
);
@@ -268,6 +279,43 @@ export default class CollectionTreeItem extends vscode.TreeItem
268279
return this._documentListChild.getMaxDocumentsToShow();
269280
}
270281

282+
getCount(): Promise<number> {
283+
log.info(`fetching document count from namespace ${this.namespace}`);
284+
285+
return new Promise((resolve, reject) => {
286+
this._dataService.estimatedCount(
287+
this.namespace,
288+
{}, // No options.
289+
(err: Error | undefined, count: number) => {
290+
if (err) {
291+
return reject(
292+
new Error(
293+
`Unable to get collection document count: ${err.message}`
294+
)
295+
);
296+
}
297+
298+
return resolve(count);
299+
}
300+
);
301+
});
302+
}
303+
304+
refreshDocumentCount = async (): Promise<boolean> => {
305+
try {
306+
// We fetch the document when we expand in order to show
307+
// the document count in the document list tree item `description`.
308+
this.documentCount = await this.getCount();
309+
} catch (err) {
310+
vscode.window.showInformationMessage(
311+
`Unable to fetch document count: ${err}`
312+
);
313+
return false;
314+
}
315+
316+
return true;
317+
};
318+
271319
async onDropCollectionClicked(): Promise<boolean> {
272320
const collectionName = this.collectionName;
273321

src/explorer/databaseTreeItem.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export default class DatabaseTreeItem extends vscode.TreeItem
8585
this._dataService,
8686
pastChildrenCache[collectionName].isExpanded,
8787
pastChildrenCache[collectionName].cacheIsUpToDate,
88+
pastChildrenCache[collectionName].documentCount,
8889
pastChildrenCache[collectionName].getDocumentListChild(),
8990
pastChildrenCache[collectionName].getSchemaChild(),
9091
pastChildrenCache[collectionName].getIndexListChild()
@@ -122,6 +123,7 @@ export default class DatabaseTreeItem extends vscode.TreeItem
122123
this._dataService,
123124
pastChildrenCache[collection.name].isExpanded,
124125
pastChildrenCache[collection.name].cacheIsUpToDate,
126+
pastChildrenCache[collection.name].documentCount,
125127
pastChildrenCache[collection.name].getDocumentListChild(),
126128
pastChildrenCache[collection.name].getSchemaChild(),
127129
pastChildrenCache[collection.name].getIndexListChild()
@@ -132,7 +134,8 @@ export default class DatabaseTreeItem extends vscode.TreeItem
132134
this.databaseName,
133135
this._dataService,
134136
false, // Not expanded.
135-
false // No cache.
137+
false, // No cache.
138+
null // No document count yet.
136139
);
137140
}
138141
});

0 commit comments

Comments
 (0)