11import * as vscode from 'vscode' ;
22const path = require ( 'path' ) ;
33
4+ import { createLogger } from '../logging' ;
45import DocumentListTreeItem , {
56 CollectionTypes ,
67 MAX_DOCUMENTS_VISIBLE
@@ -10,6 +11,8 @@ import TreeItemParent from './treeItemParentInterface';
1011import SchemaTreeItem from './schemaTreeItem' ;
1112import { getImagesPath } from '../extensionConstants' ;
1213
14+ const log = createLogger ( 'tree view collection folder' ) ;
15+
1316type 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
0 commit comments