@@ -71,6 +71,7 @@ export enum WorkspacesActions {
7171 DatabaseRemoved = 'compass-workspaces/DatabaseRemoved' ,
7272 ConnectionDisconnected = 'compass-workspaces/ConnectionDisconnected' ,
7373 FetchCollectionTabInfo = 'compass-workspaces/FetchCollectionTabInfo' ,
74+ FetchDatabaseTabInfo = 'compass-workspaces/FetchDatabaseTabInfo' ,
7475 CollectionSubtabSelected = 'compass-workspaces/CollectionSubtabSelected' ,
7576}
7677
@@ -100,6 +101,11 @@ export type CollectionTabInfo = {
100101 isTimeSeries : boolean ;
101102 isReadonly : boolean ;
102103 sourceName ?: string | null ;
104+ ns_source : 'provisioned' | 'privileges' ;
105+ } ;
106+
107+ export type DatabaseTabInfo = {
108+ ns_source : 'provisioned' | 'privileges' | 'roles' ;
103109} ;
104110
105111export type WorkspacesState = {
@@ -116,6 +122,11 @@ export type WorkspacesState = {
116122 * icon)
117123 */
118124 collectionInfo : Record < string , CollectionTabInfo > ;
125+ /**
126+ * Extra info for the collections tab namespace (where we show collections
127+ * of a database)
128+ */
129+ databaseInfo : Record < string , DatabaseTabInfo > ;
119130} ;
120131
121132const getTabId = ( ) => {
@@ -236,6 +247,7 @@ const getInitialState = () => {
236247 tabs : [ ] as WorkspaceTab [ ] ,
237248 activeTabId : null ,
238249 collectionInfo : { } ,
250+ databaseInfo : { } ,
239251 } ;
240252} ;
241253
@@ -568,7 +580,22 @@ const reducer: Reducer<WorkspacesState, Action> = (
568580 ...state ,
569581 collectionInfo : {
570582 ...state . collectionInfo ,
571- [ action . namespace ] : action . info ,
583+ [ action . namespaceId ] : action . info ,
584+ } ,
585+ } ;
586+ }
587+
588+ if (
589+ isAction < FetchDatabaseInfoAction > (
590+ action ,
591+ WorkspacesActions . FetchDatabaseTabInfo
592+ )
593+ ) {
594+ return {
595+ ...state ,
596+ databaseInfo : {
597+ ...state . databaseInfo ,
598+ [ action . namespaceId ] : action . info ,
572599 } ,
573600 } ;
574601 }
@@ -639,10 +666,18 @@ type OpenWorkspaceAction = {
639666
640667type FetchCollectionInfoAction = {
641668 type : WorkspacesActions . FetchCollectionTabInfo ;
642- namespace : string ;
669+ // This uniquely identifies the collection tab for a given connection
670+ namespaceId : string ;
643671 info : CollectionTabInfo ;
644672} ;
645673
674+ type FetchDatabaseInfoAction = {
675+ type : WorkspacesActions . FetchDatabaseTabInfo ;
676+ // This uniquely identifies the database tab for a given connection
677+ namespaceId : string ;
678+ info : DatabaseTabInfo ;
679+ } ;
680+
646681export type TabOptions = {
647682 /**
648683 * Optional. If set to `true`, always opens workspace in a new tab, otherwise
@@ -660,7 +695,8 @@ const fetchCollectionInfo = (
660695 getState ,
661696 { connections, instancesManager, logger }
662697 ) => {
663- if ( getState ( ) . collectionInfo [ workspaceOptions . namespace ] ) {
698+ const namespaceId = `${ workspaceOptions . connectionId } .${ workspaceOptions . namespace } ` ;
699+ if ( getState ( ) . collectionInfo [ namespaceId ] ) {
664700 return ;
665701 }
666702
@@ -685,11 +721,58 @@ const fetchCollectionInfo = (
685721 await coll . fetch ( { dataService } ) ;
686722 dispatch ( {
687723 type : WorkspacesActions . FetchCollectionTabInfo ,
688- namespace : workspaceOptions . namespace ,
724+ namespaceId ,
689725 info : {
690726 isTimeSeries : coll . isTimeSeries ,
691727 isReadonly : coll . readonly ?? coll . isView ,
692728 sourceName : coll . sourceName ,
729+ ns_source : coll . ns_source ,
730+ } ,
731+ } ) ;
732+ }
733+ } catch ( err ) {
734+ logger . debug (
735+ 'Collection Metadata' ,
736+ logger . mongoLogId ( 1_001_000_306 ) ,
737+ 'Error fetching collection metadata for tab' ,
738+ { namespace : workspaceOptions . namespace } ,
739+ err
740+ ) ;
741+ }
742+ } ;
743+ } ;
744+
745+ const fetchDatabaseInfo = (
746+ workspaceOptions : Extract < OpenWorkspaceOptions , { type : 'Collections' } >
747+ ) : WorkspacesThunkAction < Promise < void > , FetchDatabaseInfoAction > => {
748+ return async (
749+ dispatch ,
750+ getState ,
751+ { connections, instancesManager, logger }
752+ ) => {
753+ const { databaseInfo } = getState ( ) ;
754+ const namespaceId = `${ workspaceOptions . connectionId } .${ workspaceOptions . namespace } ` ;
755+ if ( databaseInfo [ namespaceId ] ) {
756+ return ;
757+ }
758+
759+ try {
760+ const dataService = connections . getDataServiceForConnection (
761+ workspaceOptions . connectionId
762+ ) ;
763+
764+ const instance = instancesManager . getMongoDBInstanceForConnection (
765+ workspaceOptions . connectionId
766+ ) ;
767+
768+ const db = instance . databases . get ( workspaceOptions . namespace ) ;
769+ if ( db ) {
770+ await db . fetch ( { dataService } ) ;
771+ dispatch ( {
772+ type : WorkspacesActions . FetchDatabaseTabInfo ,
773+ namespaceId,
774+ info : {
775+ ns_source : db . ns_source ,
693776 } ,
694777 } ) ;
695778 }
@@ -717,6 +800,11 @@ export const openWorkspace = (
717800 void dispatch ( fetchCollectionInfo ( workspaceOptions ) ) ;
718801 }
719802
803+ if ( workspaceOptions . type === 'Collections' ) {
804+ // Fetching extra metadata for database should not block tab opening
805+ void dispatch ( fetchDatabaseInfo ( workspaceOptions ) ) ;
806+ }
807+
720808 dispatch ( {
721809 type : WorkspacesActions . OpenWorkspace ,
722810 workspace : workspaceOptions ,
0 commit comments