@@ -9,7 +9,7 @@ const CollectionModel = AmpersandModel.extend({
99 props : {
1010 _id : 'string' ,
1111
12- // Normalized values from listCollections command
12+ // Normalized values from collectionInfo command
1313 name : { type : 'string' , required : true } ,
1414 database : { type : 'string' , required : true } ,
1515 type : { type : 'string' , required : true } ,
@@ -20,6 +20,7 @@ const CollectionModel = AmpersandModel.extend({
2020 specialish : { type : 'boolean' , required : true } ,
2121 normal : { type : 'boolean' , required : true } ,
2222 readonly : 'boolean' ,
23+ view_on : 'string' ,
2324 collation : 'object' ,
2425 pipeline : 'array' ,
2526 validation : 'object' ,
@@ -68,36 +69,48 @@ const CollectionCollection = AmpersandCollection.extend({
6869 model : CollectionModel ,
6970 /**
7071 * @param {{ dataService: import('mongodb-data-service').DataService } } dataService
71- * @returns
72+ * @returns { Promise<void> }
7273 */
73- fetch ( { dataService } ) {
74- return new Promise ( ( resolve , reject ) => {
75- const databaseName = this . parent && this . parent . getId ( ) ;
74+ async fetch ( { dataService, fetchInfo = true } ) {
75+ const listCollectionsAsync = promisify (
76+ dataService . listCollections . bind ( dataService )
77+ ) ;
78+ const listCollectionsNameOnlyAsync = promisify (
79+ dataService . listCollectionsNamesOnly . bind ( dataService )
80+ ) ;
81+
82+ const databaseName = this . parent && this . parent . getId ( ) ;
7683
77- if ( ! databaseName ) {
78- throw new Error (
79- "Trying to fetch MongoDBCollectionCollection that doesn't have the parent model"
80- ) ;
84+ if ( ! databaseName ) {
85+ throw new Error (
86+ "Trying to fetch MongoDBCollectionCollection that doesn't have the parent model"
87+ ) ;
88+ }
89+
90+ let collections = [ ] ;
91+
92+ // When trying to fetch additional information about collections during
93+ // collection list fetch we want to fallback to the nameOnly method that
94+ // requires less privileges in case user is missing some required ones
95+ if ( fetchInfo ) {
96+ try {
97+ collections = await listCollectionsAsync ( databaseName , { } ) ;
98+ } catch ( e ) {
99+ collections = await listCollectionsNameOnlyAsync ( databaseName ) ;
81100 }
101+ } else {
102+ collections = await listCollectionsNameOnlyAsync ( databaseName ) ;
103+ }
82104
83- dataService . listCollectionsNamesOnly ( databaseName , ( err , collections ) => {
84- if ( err ) {
85- reject ( err ) ;
86- return ;
87- }
88- resolve (
89- this . set (
90- collections . filter ( ( coll ) => {
91- // TODO: This is not the best place to do this kind of
92- // filtering, but for now this preserves the current behavior
93- // and changing it right away will expand the scope of the
94- // refactor significantly. We can address this in COMPASS-5211
95- return toNs ( `${ databaseName } .${ coll . name } ` ) . system === false ;
96- } )
97- )
98- ) ;
99- } ) ;
100- } ) ;
105+ this . set (
106+ collections . filter ( ( coll ) => {
107+ // TODO: This is not the best place to do this kind of
108+ // filtering, but for now this preserves the current behavior
109+ // and changing it right away will expand the scope of the
110+ // refactor significantly. We can address this in COMPASS-5211
111+ return toNs ( coll . _id ) . system === false ;
112+ } )
113+ ) ;
101114 } ,
102115} ) ;
103116
0 commit comments