From 2ba1ba0313104b7567bc8214f4fa7889a1344b6c Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Mon, 25 Nov 2024 15:15:02 +0100 Subject: [PATCH] chore(connections): replace connections registry and manager with a single interface --- .../create-view-modal/create-view-modal.tsx | 152 ++++++-------- packages/compass-aggregations/src/index.ts | 6 +- .../src/modules/create-view/index.ts | 13 +- .../src/stores/create-view.ts | 14 +- packages/compass-app-stores/src/plugin.tsx | 14 +- .../src/stores/instance-store.ts | 37 ++-- .../src/hooks/use-export-connections.ts | 26 +-- .../src/hooks/use-import-connections.ts | 25 +-- .../hooks/use-connection-repository.spec.ts | 198 ------------------ .../src/hooks/use-connection-repository.ts | 126 ----------- packages/compass-connections/src/provider.ts | 37 +--- .../src/stores/store-context.tsx | 49 +++-- packages/compass-field-store/src/index.tsx | 4 +- .../compass-field-store/src/stores/store.ts | 9 +- .../src/components/export-modal.spec.tsx | 7 +- packages/compass-import-export/src/index.ts | 9 +- .../src/modules/export.ts | 22 +- .../src/modules/import.ts | 20 +- .../src/stores/export-store.ts | 16 +- .../src/stores/import-store.ts | 16 +- .../src/index.ts | 4 +- .../src/stores/index.ts | 8 +- .../src/stores/open-item.ts | 18 +- packages/compass-sidebar/src/index.ts | 15 +- .../src/modules/data-service.ts | 6 +- packages/compass-sidebar/src/modules/index.ts | 4 +- .../src/modules/instance.spec.ts | 7 +- .../compass-sidebar/src/modules/instance.ts | 5 +- .../compass-sidebar/src/stores/store.spec.ts | 2 +- packages/compass-sidebar/src/stores/store.ts | 8 +- packages/compass-telemetry/src/provider.tsx | 4 + .../src/components/workspaces.tsx | 21 +- packages/compass-workspaces/src/index.ts | 14 +- .../src/stores/workspaces.ts | 4 +- .../compass/src/app/components/workspace.tsx | 8 +- packages/databases-collections/src/index.ts | 11 +- .../src/modules/create-namespace.spec.ts | 8 +- .../src/modules/create-namespace.ts | 14 +- .../rename-collection.spec.ts | 8 +- .../rename-collection/rename-collection.ts | 9 +- .../src/stores/create-namespace.ts | 17 +- .../src/stores/drop-namespace.tsx | 8 +- .../src/stores/rename-collection.spec.tsx | 4 +- .../src/stores/rename-collection.ts | 8 +- 44 files changed, 284 insertions(+), 731 deletions(-) delete mode 100644 packages/compass-connections/src/hooks/use-connection-repository.spec.ts delete mode 100644 packages/compass-connections/src/hooks/use-connection-repository.ts diff --git a/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx b/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx index 9d66684f366..2091af50f67 100644 --- a/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx +++ b/packages/compass-aggregations/src/components/create-view-modal/create-view-modal.tsx @@ -1,4 +1,4 @@ -import React, { PureComponent } from 'react'; +import React, { useEffect } from 'react'; import { connect } from 'react-redux'; import { Banner, @@ -11,13 +11,9 @@ import { } from '@mongodb-js/compass-components'; import { createView, changeViewName, close } from '../../modules/create-view'; import type { CreateViewRootState } from '../../stores/create-view'; -import { withTelemetry } from '@mongodb-js/compass-telemetry/provider'; -import type { TrackFunction } from '@mongodb-js/compass-telemetry'; -import { - type ConnectionRepository, - withConnectionRepository, - type ConnectionInfo, -} from '@mongodb-js/compass-connections/provider'; +import { useTelemetry } from '@mongodb-js/compass-telemetry/provider'; +import type { ConnectionInfo } from '@mongodb-js/compass-connections/provider'; +import { useConnectionsListRef } from '@mongodb-js/compass-connections/provider'; const progressContainerStyles = css({ display: 'flex', @@ -32,108 +28,80 @@ type CreateViewModalProps = { name?: string; changeViewName: (name: string) => void; isDuplicating?: boolean; - source?: string; - pipeline?: unknown[]; connectionId: ConnectionInfo['id']; isRunning?: boolean; - error: Error | null; - track: TrackFunction; - connectionRepository: ConnectionRepository; + error: string | null; }; -class CreateViewModal extends PureComponent { - static defaultProps = { - name: '', - source: '', - pipeline: [], - isRunning: false, - isVisible: false, - isDuplicating: false, - }; +const CreateViewModal: React.FunctionComponent = ({ + createView, + isVisible, + closeModal, + name, + changeViewName, + isDuplicating, + connectionId, + isRunning, + error, +}) => { + const track = useTelemetry(); + const { getConnectionById } = useConnectionsListRef(); - componentDidUpdate(prevProps: CreateViewModalProps) { - if (prevProps.isVisible !== this.props.isVisible && this.props.isVisible) { - const connectionInfo = - this.props.connectionRepository.getConnectionInfoById( - this.props.connectionId - ); - this.props.track('Screen', { name: 'create_view_modal' }, connectionInfo); + useEffect(() => { + if (isVisible && connectionId) { + track( + 'Screen', + { name: 'create_view_modal' }, + getConnectionById(connectionId)?.info + ); } - } - - onNameChange = (evt: React.ChangeEvent) => { - this.props.changeViewName(evt.currentTarget.value); - }; - - onFormSubmit = () => { - this.props.createView(); - }; + }, [isVisible, connectionId, getConnectionById, track]); - onCancel = () => { - this.props.closeModal(); - }; - - /** - * Render the save pipeline component. - */ - render() { - return ( - - - {this.props.error ? ( - {this.props.error.message} - ) : null} - {this.props.isRunning ? ( - - - Creating view… - - ) : null} - - ); - } -} + return ( + + { + changeViewName(evt.currentTarget.value); + }} + label="Name" + name="name" + /> + {error ? {error} : null} + {isRunning ? ( + + + Creating view… + + ) : null} + + ); +}; -/** - * Map the store state to properties to pass to the components. - */ const mapStateToProps = (state: CreateViewRootState) => ({ isRunning: state.isRunning, isVisible: state.isVisible, isDuplicating: state.isDuplicating, name: state.name, - error: state.error, + error: state.error?.message ?? null, source: state.source, pipeline: state.pipeline, connectionId: state.connectionId, }); -/** - * Connect the redux store to the component. - * (dispatch) - */ -const MappedCreateViewModal = withTelemetry( - withConnectionRepository( - connect(mapStateToProps, { - createView, - changeViewName, - closeModal: close, - })(CreateViewModal) - ) -); +const MappedCreateViewModal = connect(mapStateToProps, { + createView, + changeViewName, + closeModal: close, +})(CreateViewModal); export default MappedCreateViewModal; export { CreateViewModal }; diff --git a/packages/compass-aggregations/src/index.ts b/packages/compass-aggregations/src/index.ts index 967b2e091bb..bf9a7c4d3f0 100644 --- a/packages/compass-aggregations/src/index.ts +++ b/packages/compass-aggregations/src/index.ts @@ -9,7 +9,7 @@ import CreateViewModal from './components/create-view-modal'; import { connectionInfoRefLocator, connectionScopedAppRegistryLocator, - connectionsManagerLocator, + connectionsLocator, dataServiceLocator, type DataServiceLocator, } from '@mongodb-js/compass-connections/provider'; @@ -27,7 +27,6 @@ import { workspacesServiceLocator } from '@mongodb-js/compass-workspaces/provide import { preferencesLocator } from 'compass-preferences-model/provider'; import { atlasAiServiceLocator } from '@mongodb-js/compass-generative-ai/provider'; import { pipelineStorageLocator } from '@mongodb-js/my-queries-storage/provider'; -import { connectionRepositoryAccessLocator } from '@mongodb-js/compass-connections/provider'; import { AggregationsTabTitle } from './plugin-title'; const CompassAggregationsHadronPlugin = registerHadronPlugin( @@ -71,8 +70,7 @@ export const CreateViewPlugin = registerHadronPlugin( activate: activateCreateViewPlugin, }, { - connectionsManager: connectionsManagerLocator, - connectionRepository: connectionRepositoryAccessLocator, + connections: connectionsLocator, logger: createLoggerLocator('COMPASS-CREATE-VIEW-UI'), track: telemetryLocator, workspaces: workspacesServiceLocator, diff --git a/packages/compass-aggregations/src/modules/create-view/index.ts b/packages/compass-aggregations/src/modules/create-view/index.ts index 0b6b3e5c736..fbe7defe654 100644 --- a/packages/compass-aggregations/src/modules/create-view/index.ts +++ b/packages/compass-aggregations/src/modules/create-view/index.ts @@ -202,13 +202,7 @@ export const createView = (): CreateViewThunkAction> => { return async ( dispatch, getState, - { - globalAppRegistry, - connectionsManager, - connectionRepository, - track, - workspaces, - } + { globalAppRegistry, connections, track, workspaces } ) => { const { name: viewName, @@ -222,8 +216,7 @@ export const createView = (): CreateViewThunkAction> => { dispatch(clearError()); try { - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); dispatch(toggleIsRunning(true)); await dataService.createView( @@ -236,7 +229,7 @@ export const createView = (): CreateViewThunkAction> => { track( 'Aggregation Saved As View', { num_stages: viewPipeline.length }, - connectionRepository.getConnectionInfoById(connectionId) + connections.getConnectionById(connectionId)?.info ); globalAppRegistry.emit('view-created', ns, { connectionId, diff --git a/packages/compass-aggregations/src/stores/create-view.ts b/packages/compass-aggregations/src/stores/create-view.ts index aff00f1979d..684339c52b1 100644 --- a/packages/compass-aggregations/src/stores/create-view.ts +++ b/packages/compass-aggregations/src/stores/create-view.ts @@ -7,17 +7,13 @@ import reducer, { open } from '../modules/create-view'; import type AppRegistry from 'hadron-app-registry'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import type { WorkspacesService } from '@mongodb-js/compass-workspaces/provider'; -import type { - ConnectionRepositoryAccess, - ConnectionsManager, -} from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { ActivateHelpers } from 'hadron-app-registry'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; type CreateViewServices = { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; - connectionRepository: ConnectionRepositoryAccess; + connections: ConnectionsService; logger: Logger; track: TrackFunction; workspaces: WorkspacesService; @@ -53,8 +49,7 @@ export function activateCreateViewPlugin( _: unknown, { globalAppRegistry, - connectionsManager, - connectionRepository, + connections, logger, track, workspaces, @@ -63,8 +58,7 @@ export function activateCreateViewPlugin( ) { const store = configureStore({ globalAppRegistry, - connectionsManager, - connectionRepository, + connections, logger, track, workspaces, diff --git a/packages/compass-app-stores/src/plugin.tsx b/packages/compass-app-stores/src/plugin.tsx index 8903e22be57..47790d4320f 100644 --- a/packages/compass-app-stores/src/plugin.tsx +++ b/packages/compass-app-stores/src/plugin.tsx @@ -6,10 +6,8 @@ import type { ActivateHelpers } from 'hadron-app-registry'; import { registerHadronPlugin } from 'hadron-app-registry'; import { MongoDBInstancesManagerContext } from './provider'; import { createInstancesStore } from './stores'; -import { - connectionsManagerLocator, - type ConnectionsManager, -} from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; import { type MongoDBInstancesManager } from './instances-manager'; interface MongoDBInstancesProviderProps { @@ -37,11 +35,11 @@ export const CompassInstanceStorePlugin = registerHadronPlugin( activate( _: unknown, { - connectionsManager, + connections, logger, globalAppRegistry, }: { - connectionsManager: ConnectionsManager; + connections: ConnectionsService; logger: Logger; globalAppRegistry: AppRegistry; }, @@ -49,7 +47,7 @@ export const CompassInstanceStorePlugin = registerHadronPlugin( ) { const store = createInstancesStore( { - connectionsManager, + connections, logger, globalAppRegistry, }, @@ -65,6 +63,6 @@ export const CompassInstanceStorePlugin = registerHadronPlugin( }, { logger: createLoggerLocator('COMPASS-INSTANCE-STORE'), - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, } ); diff --git a/packages/compass-app-stores/src/stores/instance-store.ts b/packages/compass-app-stores/src/stores/instance-store.ts index ff70a5f5e58..d99dbbccb68 100644 --- a/packages/compass-app-stores/src/stores/instance-store.ts +++ b/packages/compass-app-stores/src/stores/instance-store.ts @@ -1,11 +1,13 @@ import type { MongoDBInstanceProps } from 'mongodb-instance-model'; import { MongoDBInstance } from 'mongodb-instance-model'; import toNS from 'mongodb-ns'; -import type { DataService } from '@mongodb-js/compass-connections/provider'; +import type { + ConnectionsService, + DataService, +} from '@mongodb-js/compass-connections/provider'; import type { ActivateHelpers, AppRegistry } from 'hadron-app-registry'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import { openToast } from '@mongodb-js/compass-components'; -import { type ConnectionsManager } from '@mongodb-js/compass-connections/provider'; import { MongoDBInstancesManager } from '../instances-manager'; function serversArray( @@ -40,10 +42,10 @@ function getTopologyDescription( export function createInstancesStore( { globalAppRegistry, - connectionsManager, + connections, logger: { log, mongoLogId }, }: { - connectionsManager: ConnectionsManager; + connections: ConnectionsService; logger: Logger; globalAppRegistry: AppRegistry; }, @@ -60,8 +62,7 @@ export function createInstancesStore( } const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); // It is possible to get here before the databases finished loading. We have // to wait for the databases, otherwise it will load all the collections for // 0 databases. @@ -99,8 +100,7 @@ export function createInstancesStore( } const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); isFirstRun = instance.status === 'initial'; await instance.refresh({ dataService, ...refreshOptions }); } catch (err: any) { @@ -148,8 +148,7 @@ export function createInstancesStore( } const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); await instance.fetchDatabases({ dataService, force: true }); await Promise.allSettled( instance.databases.map((db) => @@ -179,8 +178,7 @@ export function createInstancesStore( } const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); const { database } = toNS(ns); const db = instance.databases.get(database); const coll = db?.collections.get(ns); @@ -215,8 +213,7 @@ export function createInstancesStore( } const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); const { database } = toNS(namespace); const db = instance.databases.get(database) ?? @@ -258,7 +255,7 @@ export function createInstancesStore( } }; - on(connectionsManager, 'disconnected', function (connectionInfoId: string) { + on(connections, 'disconnected', function (connectionInfoId: string) { try { const instance = instancesManager.getMongoDBInstanceForConnection(connectionInfoId); @@ -277,9 +274,9 @@ export function createInstancesStore( instancesManager.removeMongoDBInstanceForConnection(connectionInfoId); }); - on(connectionsManager, 'connected', function (instanceConnectionId: string) { + on(connections, 'connected', function (instanceConnectionId: string) { const dataService = - connectionsManager.getDataServiceForConnection(instanceConnectionId); + connections.getDataServiceForConnection(instanceConnectionId); const connectionString = dataService.getConnectionString(); const firstHost = connectionString.hosts[0] || ''; const [hostname, port] = firstHost.split(':'); @@ -337,7 +334,7 @@ export function createInstancesStore( const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + connections.getDataServiceForConnection(connectionId); void instance.databases .get(databaseId) ?.fetchCollections({ dataService }); @@ -431,7 +428,7 @@ export function createInstancesStore( const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + connections.getDataServiceForConnection(connectionId); const { database } = toNS(namespace); const db = instance.databases.get(database); const coll = db?.collections.get(namespace, '_id'); @@ -541,7 +538,7 @@ export function createInstancesStore( const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + connections.getDataServiceForConnection(connectionId); const { database } = toNS(namespace); void instance.databases .get(database) diff --git a/packages/compass-connection-import-export/src/hooks/use-export-connections.ts b/packages/compass-connection-import-export/src/hooks/use-export-connections.ts index 35a6a972660..04944792723 100644 --- a/packages/compass-connection-import-export/src/hooks/use-export-connections.ts +++ b/packages/compass-connection-import-export/src/hooks/use-export-connections.ts @@ -1,5 +1,5 @@ import type React from 'react'; -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { COMMON_INITIAL_STATE, useImportExportConnectionsCommon, @@ -15,7 +15,7 @@ import type { CommonImportExportState, } from './common'; import { usePreference } from 'compass-preferences-model/provider'; -import { useConnectionRepository } from '@mongodb-js/compass-connections/provider'; +import { useConnectionsList } from '@mongodb-js/compass-connections/provider'; type ExportConnectionsState = CommonImportExportState & { removeSecrets: boolean; @@ -55,21 +55,9 @@ export function useExportConnections({ onChangeRemoveSecrets: (evt: React.ChangeEvent) => void; state: ExportConnectionsState; } { - const multipleConnectionsEnabled = usePreference( - 'enableMultipleConnectionSystem' - ); - const { favoriteConnections, nonFavoriteConnections } = - useConnectionRepository(); - const connectionsToExport = useMemo(() => { - // in case of multiple connections all the connections are saved (that used - // to be favorites in the single connection world) so we need to account for - // all the saved connections - if (multipleConnectionsEnabled) { - return [...favoriteConnections, ...nonFavoriteConnections]; - } else { - return favoriteConnections; - } - }, [multipleConnectionsEnabled, favoriteConnections, nonFavoriteConnections]); + const connectionsToExport = useConnectionsList((conn) => { + return !conn.isBeingCreated && !conn.isAutoconnectInfo; + }); const connectionStorage = useConnectionStorageContext(); const exportConnectionsImpl = connectionStorage.exportConnections?.bind(connectionStorage); @@ -97,7 +85,9 @@ export function useExportConnections({ setState((prevState) => ({ ...prevState, connectionList: connectionInfosToConnectionShortInfos( - connectionsToExport, + connectionsToExport.map((conn) => { + return conn.info; + }), prevState.connectionList ), })); diff --git a/packages/compass-connection-import-export/src/hooks/use-import-connections.ts b/packages/compass-connection-import-export/src/hooks/use-import-connections.ts index 4685d422c84..da1b8bfce0c 100644 --- a/packages/compass-connection-import-export/src/hooks/use-import-connections.ts +++ b/packages/compass-connection-import-export/src/hooks/use-import-connections.ts @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { type ConnectionStorage, useConnectionStorageContext, @@ -15,9 +15,8 @@ import type { } from './common'; import { useConnectionActions, - useConnectionRepository, + useConnectionsList, } from '@mongodb-js/compass-connections/provider'; -import { usePreference } from 'compass-preferences-model/provider'; type ConnectionImportInfo = ConnectionShortInfo & { isExistingConnection: boolean; @@ -103,22 +102,10 @@ export function useImportConnections({ onChangeConnectionList: (connectionInfos: ConnectionShortInfo[]) => void; state: ImportConnectionsState; } { - const multipleConnectionsEnabled = usePreference( - 'enableMultipleConnectionSystem' - ); - const { favoriteConnections, nonFavoriteConnections } = - useConnectionRepository(); + const existingConnections = useConnectionsList((conn) => { + return !conn.isBeingCreated && !conn.isAutoconnectInfo; + }); const { importConnections } = useConnectionActions(); - const existingConnections = useMemo(() => { - // in case of multiple connections all the connections are saved (that used - // to be favorites in the single connection world) so we need to account for - // all the saved connections - if (multipleConnectionsEnabled) { - return [...favoriteConnections, ...nonFavoriteConnections]; - } else { - return favoriteConnections; - } - }, [multipleConnectionsEnabled, favoriteConnections, nonFavoriteConnections]); const connectionStorage = useConnectionStorageContext(); const deserializeConnectionsImpl = connectionStorage.deserializeConnections?.bind(connectionStorage); @@ -140,7 +127,7 @@ export function useImportConnections({ }, [open]); const { passphrase, filename, fileContents, connectionList } = state; - const existingConnectionIds = existingConnections.map(({ id }) => id); + const existingConnectionIds = existingConnections.map(({ info }) => info.id); useEffect(() => { // If `existingConnections` changes, update the list of connections that are // displayed in our table. diff --git a/packages/compass-connections/src/hooks/use-connection-repository.spec.ts b/packages/compass-connections/src/hooks/use-connection-repository.spec.ts deleted file mode 100644 index 2f44ab10a95..00000000000 --- a/packages/compass-connections/src/hooks/use-connection-repository.spec.ts +++ /dev/null @@ -1,198 +0,0 @@ -import { useConnectionRepository } from './use-connection-repository'; -import { expect } from 'chai'; -import Sinon from 'sinon'; -import { cleanup } from '@mongodb-js/testing-library-compass'; -import { - createDefaultConnectionInfo, - renderHookWithConnections, -} from '@mongodb-js/testing-library-compass'; - -const favoriteMockConnections = [ - { - ...createDefaultConnectionInfo(), - id: '12', - savedConnectionType: 'favorite', - favorite: { name: 'Bb' }, - }, - { - ...createDefaultConnectionInfo(), - id: '11', - savedConnectionType: 'favorite', - favorite: { name: 'Aa' }, - }, -]; - -const nonFavoriteMockConnections = [ - { - ...createDefaultConnectionInfo(), - id: '23', - savedConnectionType: 'recent', - favorite: { name: 'Cc' }, - }, - { - ...createDefaultConnectionInfo(), - id: '22', - savedConnectionType: 'recent', - favorite: { name: 'Bb' }, - }, - { - ...createDefaultConnectionInfo(), - id: '21', - savedConnectionType: 'recent', - favorite: { name: 'Aa' }, - }, -]; - -describe('useConnectionRepository', function () { - afterEach(() => { - cleanup(); - Sinon.restore(); - }); - - describe('favoriteConnections', function () { - it('should return favourite connections sorted by name alphabetically', function () { - const { result } = renderHookWithConnections(useConnectionRepository, { - connections: favoriteMockConnections, - }); - - const connections = result.current.favoriteConnections; - - expect(connections.length).to.equal(2); - expect(connections[0].id).to.equal('11'); - expect(connections[1].id).to.equal('12'); - }); - - it('should not change if only non favourite connections change', async function () { - const { result, connectionsStore } = renderHookWithConnections( - useConnectionRepository, - { connections: favoriteMockConnections } - ); - - const initialFavoriteConnections = result.current.favoriteConnections; - expect(initialFavoriteConnections.length).to.equal(2); - - await Promise.all( - nonFavoriteMockConnections.map((info) => { - return connectionsStore.actions.saveEditedConnection(info); - }) - ); - - expect(result.current.favoriteConnections).to.eq( - initialFavoriteConnections - ); - }); - }); - - describe('nonFavoriteConnections', function () { - it('should return non favourite connections sorted by name alphabetically', function () { - const { result } = renderHookWithConnections(useConnectionRepository, { - connections: nonFavoriteMockConnections, - }); - - const connections = result.current.nonFavoriteConnections; - - expect(connections.length).to.equal(3); - expect(connections[0].id).to.equal('21'); - expect(connections[1].id).to.equal('22'); - expect(connections[2].id).to.equal('23'); - }); - - it('should not change if only favourite connections change', async function () { - const { result, connectionsStore } = renderHookWithConnections( - useConnectionRepository, - { connections: nonFavoriteMockConnections } - ); - - const initialNonFavoriteConnections = - result.current.nonFavoriteConnections; - - await Promise.all( - favoriteMockConnections.map((info) => { - return connectionsStore.actions.saveEditedConnection(info); - }) - ); - - expect(result.current.nonFavoriteConnections).to.equal( - initialNonFavoriteConnections - ); - }); - }); - - describe('store.saveEditedConnection', function () { - it('should save a new connection if it has a valid connection string', async function () { - const connectionInfo = createDefaultConnectionInfo(); - const { result, connectionsStore, connectionStorage } = - renderHookWithConnections(useConnectionRepository, { - // We don't allow to save connections that are not in state with - // actions, so put one in the store - connections: [connectionInfo], - }); - - const saveSpy = Sinon.spy(connectionStorage, 'save'); - - // Update connection string on existing connection - const connectionToSave = { - ...connectionInfo, - connectionOptions: { connectionString: 'mongodb://example.com:1337' }, - }; - await connectionsStore.actions.saveEditedConnection(connectionToSave); - - expect(saveSpy).to.have.been.calledOnceWith({ - connectionInfo: connectionToSave, - }); - - expect(result.current.nonFavoriteConnections[0]).to.have.nested.property( - 'connectionOptions.connectionString', - 'mongodb://example.com:1337' - ); - }); - - it('should not save a new connection if it has an invalid connection string', async function () { - const connectionInfo = createDefaultConnectionInfo(); - const { result, connectionsStore, connectionStorage } = - renderHookWithConnections(useConnectionRepository, { - // We don't allow to save connections that are not in state with - // actions, so put one in the store - connections: [connectionInfo], - }); - - const saveSpy = Sinon.spy(connectionStorage, 'save'); - - // Update connection string on existing connection - const connectionToSave = { - ...connectionInfo, - connectionOptions: { connectionString: 'foobar' }, - }; - await connectionsStore.actions.saveEditedConnection(connectionToSave); - - expect(saveSpy).not.to.have.been.called; - - expect(result.current.nonFavoriteConnections[0]).to.have.nested.property( - 'connectionOptions.connectionString', - 'mongodb://localhost:27017' - ); - }); - }); - - describe('store.removeConnection', function () { - it('should delete a saved connection from the underlying storage', function () { - const connectionInfo = createDefaultConnectionInfo(); - const { result, connectionsStore, connectionStorage } = - renderHookWithConnections(useConnectionRepository, { - // We don't allow to save connections that are not in state with - // actions, so put one in the store - connections: [connectionInfo], - }); - - const deleteSpy = Sinon.spy(connectionStorage, 'delete'); - - expect(result.current.nonFavoriteConnections).to.have.lengthOf(1); - - connectionsStore.actions.removeConnection(connectionInfo.id); - - expect(deleteSpy).to.have.been.calledOnceWith({ id: connectionInfo.id }); - - expect(result.current.nonFavoriteConnections).to.have.lengthOf(0); - }); - }); -}); diff --git a/packages/compass-connections/src/hooks/use-connection-repository.ts b/packages/compass-connections/src/hooks/use-connection-repository.ts deleted file mode 100644 index 8114ec4fafd..00000000000 --- a/packages/compass-connections/src/hooks/use-connection-repository.ts +++ /dev/null @@ -1,126 +0,0 @@ -import React, { useMemo } from 'react'; -import { - getConnectionTitle, - type ConnectionInfo, -} from '@mongodb-js/connection-info'; -import { useCallback, useRef } from 'react'; -import { createServiceLocator } from 'hadron-app-registry'; -import { useConnections, useConnectionsList } from '../stores/store-context'; - -export type ConnectionRepository = { - favoriteConnections: ConnectionInfo[]; - nonFavoriteConnections: ConnectionInfo[]; - getConnectionInfoById: ( - id: ConnectionInfo['id'] - ) => ConnectionInfo | undefined; - getConnectionTitleById: (id: ConnectionInfo['id']) => string | undefined; -}; - -/** - * @deprecated use connections-store hooks instead - */ -export function useConnectionRepository(): ConnectionRepository { - const nonFavoriteConnections = useConnectionsList((connection) => { - return ( - !connection.isBeingCreated && - !connection.isAutoconnectInfo && - connection.info.savedConnectionType !== 'favorite' - ); - }); - - const nonFavoriteConnectionsInfoOnly = useMemo(() => { - return nonFavoriteConnections.map((connection) => { - return connection.info; - }); - }, [nonFavoriteConnections]); - - const favoriteConnections = useConnectionsList((connection) => { - return ( - !connection.isBeingCreated && - connection.info.savedConnectionType === 'favorite' - ); - }); - - const favoriteConnectionsInfoOnly = useMemo(() => { - return favoriteConnections.map((connection) => { - return connection.info; - }); - }, [favoriteConnections]); - - const { getConnectionById } = useConnections(); - - const getConnectionInfoById = useCallback( - (connectionInfoId: ConnectionInfo['id']) => { - return getConnectionById(connectionInfoId)?.info; - }, - [getConnectionById] - ); - - const getConnectionTitleById = useCallback( - (connectionId: ConnectionInfo['id']) => { - const connectionInfo = getConnectionInfoById(connectionId); - if (connectionInfo) { - return getConnectionTitle(connectionInfo); - } - }, - [getConnectionInfoById] - ); - - return { - getConnectionInfoById, - getConnectionTitleById, - favoriteConnections: favoriteConnectionsInfoOnly, - nonFavoriteConnections: nonFavoriteConnectionsInfoOnly, - }; -} - -type FirstArgument = F extends (...args: [infer A, ...any]) => any - ? A - : F extends { new (...args: [infer A, ...any]): any } - ? A - : never; - -/** - * @deprecated instead of using HOC, refactor class component to functional - * component - */ -function withConnectionRepository< - T extends ((...args: any[]) => any) | { new (...args: any[]): any } ->( - ReactComponent: T -): React.FunctionComponent, 'connectionRepository'>> { - const WithConnectionRepository = ( - props: Omit, 'connectionRepository'> & React.Attributes - ) => { - const connectionRepository = useConnectionRepository(); - return React.createElement(ReactComponent, { - ...props, - connectionRepository, - }); - }; - return WithConnectionRepository; -} - -export { withConnectionRepository }; - -export type ConnectionRepositoryAccess = Pick< - ConnectionRepository, - 'getConnectionInfoById' ->; - -/** - * @deprecated use `connectionsLocator` instead - */ -export const connectionRepositoryAccessLocator = createServiceLocator( - (): ConnectionRepositoryAccess => { - const repository = useConnectionRepository(); - const repositoryRef = useRef(repository); - repositoryRef.current = repository; - return { - getConnectionInfoById(id: ConnectionInfo['id']) { - return repositoryRef.current.getConnectionInfoById(id); - }, - }; - }, - 'connectionRepositoryAccessLocator' -); diff --git a/packages/compass-connections/src/provider.ts b/packages/compass-connections/src/provider.ts index 467621afb6c..c91c45bad5a 100644 --- a/packages/compass-connections/src/provider.ts +++ b/packages/compass-connections/src/provider.ts @@ -1,29 +1,12 @@ import { createServiceLocator } from 'hadron-app-registry'; import { useConnectionInfo } from './connection-info-provider'; import type { DataService } from 'mongodb-data-service'; -import { useConnections } from './stores/store-context'; +import { getDataServiceForConnection } from './stores/connections-store-redux'; export type { DataService }; export { useConnectionsWithStatus } from './hooks/use-connections-with-status'; export { useActiveConnections } from './hooks/use-active-connections'; -export type ConnectionsManager = Pick< - ReturnType, - | 'getDataServiceForConnection' - | 'getConnectionById' - | 'on' - | 'off' - | 'removeListener' ->; - -/** - * @deprecated use `connectionsLocator` instead - */ -export const connectionsManagerLocator = createServiceLocator( - useConnections, - 'connectionsManagerLocator' -); - export type DataServiceLocator< K extends keyof DataService = keyof DataService, L extends keyof DataService = K @@ -40,13 +23,12 @@ export const dataServiceLocator = createServiceLocator( L extends keyof DataService = K >(): Pick & Partial> { const connectionInfo = useConnectionInfo(); - const connectionsManager = connectionsManagerLocator(); if (!connectionInfo) { throw new Error( 'ConnectionInfo for an active connection not available in context. Did you forget to setup ConnectionInfoProvider' ); } - return connectionsManager.getDataServiceForConnection(connectionInfo.id); + return getDataServiceForConnection(connectionInfo.id); } ); @@ -71,26 +53,19 @@ export { export { useTabConnectionTheme } from './hooks/use-tab-connection-theme'; -export type { - ConnectionRepository, - ConnectionRepositoryAccess, -} from './hooks/use-connection-repository'; - -export { - withConnectionRepository, - useConnectionRepository, - connectionRepositoryAccessLocator, -} from './hooks/use-connection-repository'; - export { useConnectionActions, useConnectionForId, useConnectionIds, useConnectionInfoForId, useConnectionInfoRefForId, + useConnectionsList, + useConnectionsListRef, connectionsLocator, } from './stores/store-context'; +export type { ConnectionsService } from './stores/store-context'; + export { useConnectionSupports } from './hooks/use-connection-supports'; const ConnectionStatus = { diff --git a/packages/compass-connections/src/stores/store-context.tsx b/packages/compass-connections/src/stores/store-context.tsx index 03d4d15e9f2..0caf00a55e8 100644 --- a/packages/compass-connections/src/stores/store-context.tsx +++ b/packages/compass-connections/src/stores/store-context.tsx @@ -1,4 +1,4 @@ -import React, { createContext, useCallback, useContext, useState } from 'react'; +import React, { createContext, useContext, useRef, useState } from 'react'; import type { MapStateToProps, ReactReduxContextValue, @@ -169,25 +169,50 @@ export function useConnectionActions() { return actions; } -export function useConnections() { - const store = useStore(); +export function useConnectionsListRef(): { + getConnectionById( + this: void, + connectionId: string + ): (ConnectionState & { title: string }) | undefined; + current: readonly (ConnectionState & { title: string })[]; +} { + const storeRef = useRef(useStore()); + const [ref] = useState(() => { + return { + getConnectionById(connectionId: string) { + const conn = storeRef.current.getState().connections.byId[connectionId]; + if (conn) { + return { ...conn, title: getConnectionTitle(conn.info) }; + } + return undefined; + }, + get current() { + return Object.values(storeRef.current.getState().connections.byId).map( + (conn) => { + return { ...conn, title: getConnectionTitle(conn.info) }; + } + ); + }, + }; + }); + return ref; +} + +function useConnections() { const actions = useConnectionActions(); - const getConnectionById = useCallback( - (connectionId: string): ConnectionState | undefined => { - return store.getState().connections.byId[connectionId]; - }, - [store] - ); - return { + const connectionsListRef = useConnectionsListRef(); + return useRef({ ...actions, - getConnectionById, + ...connectionsListRef, getDataServiceForConnection, on: connectionsEventEmitter.on, off: connectionsEventEmitter.off, removeListener: connectionsEventEmitter.removeListener, - }; + }).current; } +export type ConnectionsService = ReturnType; + export const connectionsLocator = createServiceLocator( useConnections, 'connectionsLocator' diff --git a/packages/compass-field-store/src/index.tsx b/packages/compass-field-store/src/index.tsx index cbc8073e6fa..71bdc65fecc 100644 --- a/packages/compass-field-store/src/index.tsx +++ b/packages/compass-field-store/src/index.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { registerHadronPlugin } from 'hadron-app-registry'; import { activatePlugin } from './stores/store'; -import { connectionsManagerLocator } from '@mongodb-js/compass-connections/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; const FieldStoreComponent: React.FunctionComponent = ({ children }) => { @@ -18,7 +18,7 @@ const FieldStorePlugin = registerHadronPlugin( activate: activatePlugin, }, { - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, logger: createLoggerLocator('COMPASS-FIELDS-STORE'), } ); diff --git a/packages/compass-field-store/src/stores/store.ts b/packages/compass-field-store/src/stores/store.ts index 432eb3707c2..2356fdc764f 100644 --- a/packages/compass-field-store/src/stores/store.ts +++ b/packages/compass-field-store/src/stores/store.ts @@ -1,24 +1,21 @@ import { applyMiddleware, createStore } from 'redux'; import reducer, { connectionDisconnected } from '../modules'; import { FieldStoreContext } from './context'; -import { type ConnectionsManager } from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { ActivateHelpers } from 'hadron-app-registry'; import thunk from 'redux-thunk'; import type { Logger } from '@mongodb-js/compass-logging/provider'; export function activatePlugin( _initialProps: unknown, - { - connectionsManager, - logger, - }: { connectionsManager: ConnectionsManager; logger: Logger }, + { connections, logger }: { connections: ConnectionsService; logger: Logger }, { on, cleanup }: ActivateHelpers ) { const store = createStore( reducer, applyMiddleware(thunk.withExtraArgument({ logger })) ); - on(connectionsManager, 'disconnected', (connectionInfoId: string) => { + on(connections, 'disconnected', (connectionInfoId: string) => { store.dispatch(connectionDisconnected(connectionInfoId)); }); diff --git a/packages/compass-import-export/src/components/export-modal.spec.tsx b/packages/compass-import-export/src/components/export-modal.spec.tsx index c0b9f89eba3..226817c0565 100644 --- a/packages/compass-import-export/src/components/export-modal.spec.tsx +++ b/packages/compass-import-export/src/components/export-modal.spec.tsx @@ -13,7 +13,6 @@ import { closeExport, openExport } from '../modules/export'; import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider'; -import { type ConnectionRepository } from '@mongodb-js/compass-connections/provider'; function renderModal(exportState: any = {}) { // TODO: mutating state directly doesn't guarantee that we are testing the @@ -25,9 +24,9 @@ function renderModal(exportState: any = {}) { globalAppRegistry: {}, logger: createNoopLogger(), track: createNoopTrack(), - connectionRepository: { - getConnectionInfoById: () => ({ id: 'TEST' }), - } as unknown as ConnectionRepository, + connections: { + getConnectionById: () => ({ info: { id: 'TEST' } }), + }, } as any); const state = store.getState(); state.export = { diff --git a/packages/compass-import-export/src/index.ts b/packages/compass-import-export/src/index.ts index 411aa09e62a..e1231263b6c 100644 --- a/packages/compass-import-export/src/index.ts +++ b/packages/compass-import-export/src/index.ts @@ -1,5 +1,4 @@ import { registerHadronPlugin } from 'hadron-app-registry'; -import { connectionsManagerLocator } from '@mongodb-js/compass-connections/provider'; import ImportPluginComponent from './import-plugin'; import { activatePlugin as activateImportPlugin } from './stores/import-store'; import ExportPluginComponent from './export-plugin'; @@ -8,7 +7,7 @@ import { workspacesServiceLocator } from '@mongodb-js/compass-workspaces/provide import { preferencesLocator } from 'compass-preferences-model/provider'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; import { telemetryLocator } from '@mongodb-js/compass-telemetry/provider'; -import { connectionRepositoryAccessLocator } from '@mongodb-js/compass-connections/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; /** * The import plugin. @@ -20,8 +19,7 @@ export const ImportPlugin = registerHadronPlugin( activate: activateImportPlugin, }, { - connectionsManager: connectionsManagerLocator, - connectionRepository: connectionRepositoryAccessLocator, + connections: connectionsLocator, workspaces: workspacesServiceLocator, preferences: preferencesLocator, logger: createLoggerLocator('COMPASS-IMPORT-UI'), @@ -39,8 +37,7 @@ export const ExportPlugin = registerHadronPlugin( activate: activateExportPlugin, }, { - connectionsManager: connectionsManagerLocator, - connectionRepository: connectionRepositoryAccessLocator, + connections: connectionsLocator, preferences: preferencesLocator, logger: createLoggerLocator('COMPASS-EXPORT-UI'), track: telemetryLocator, diff --git a/packages/compass-import-export/src/modules/export.ts b/packages/compass-import-export/src/modules/export.ts index 6b8415b6e90..d2d98613732 100644 --- a/packages/compass-import-export/src/modules/export.ts +++ b/packages/compass-import-export/src/modules/export.ts @@ -133,14 +133,14 @@ type OpenExportAction = { export const openExport = ( exportOptions: Omit ): ExportThunkAction => { - return (dispatch, _getState, { track, connectionRepository }) => { + return (dispatch, _getState, { track, connections }) => { track( 'Export Opened', { type: exportOptions.aggregation ? 'aggregation' : 'query', origin: exportOptions.origin, }, - connectionRepository.getConnectionInfoById(exportOptions.connectionId) + connections.getConnectionById(exportOptions.connectionId)?.info ); dispatch({ type: ExportActionTypes.OpenExport, @@ -288,7 +288,7 @@ export const selectFieldsToExport = (): ExportThunkAction< return async ( dispatch, getState, - { connectionsManager, logger: { log, mongoLogId } } + { connections, logger: { log, mongoLogId } } ) => { dispatch({ type: ExportActionTypes.SelectFieldsToExport, @@ -312,8 +312,7 @@ export const selectFieldsToExport = (): ExportThunkAction< throw new Error('ConnectionId not provided'); } - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); gatherFieldsResult = await gatherFieldsFromQuery({ ns: namespace, @@ -368,13 +367,7 @@ export const runExport = ({ return async ( dispatch, getState, - { - connectionsManager, - connectionRepository, - preferences, - track, - logger: { log, mongoLogId }, - } + { connections, preferences, track, logger: { log, mongoLogId } } ) => { let outputWriteStream: fs.WriteStream; try { @@ -475,8 +468,7 @@ export const runExport = ({ throw new Error('ConnectionId not provided'); } - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); const baseExportOptions = { ns: namespace, @@ -573,7 +565,7 @@ export const runExport = ({ stopped: aborted, duration: Date.now() - startTime, }, - connectionRepository.getConnectionInfoById(connectionId) + connections.getConnectionById(connectionId)?.info ); if (!exportSucceeded) { diff --git a/packages/compass-import-export/src/modules/import.ts b/packages/compass-import-export/src/modules/import.ts index 4ea352386c3..e1df39a9993 100644 --- a/packages/compass-import-export/src/modules/import.ts +++ b/packages/compass-import-export/src/modules/import.ts @@ -190,8 +190,7 @@ export const startImport = (): ImportThunkAction> => { dispatch, getState, { - connectionsManager, - connectionRepository, + connections, globalAppRegistry: appRegistry, workspaces, track, @@ -315,8 +314,7 @@ export const startImport = (): ImportThunkAction> => { throw new Error('ConnectionId not provided'); } - dataService = - connectionsManager.getDataServiceForConnection(connectionId); + dataService = connections.getDataServiceForConnection(connectionId); if (fileType === 'csv') { result = await importCSV({ @@ -363,7 +361,7 @@ export const startImport = (): ImportThunkAction> => { aborted: abortSignal.aborted, ignore_empty_strings: fileType === 'csv' ? ignoreBlanks : undefined, }, - connectionRepository.getConnectionInfoById(connectionId) + connections.getConnectionById(connectionId)?.info ); log.error(mongoLogId(1001000081), 'Import', 'Import failed', { @@ -397,7 +395,7 @@ export const startImport = (): ImportThunkAction> => { aborted: result.aborted, ignore_empty_strings: fileType === 'csv' ? ignoreBlanks : undefined, }, - connectionRepository.getConnectionInfoById(connectionId) + connections.getConnectionById(connectionId)?.info ); log.info(mongoLogId(1001000082), 'Import', 'Import completed', { @@ -414,7 +412,7 @@ export const startImport = (): ImportThunkAction> => { { errorCount: numErrors, }, - connectionRepository.getConnectionInfoById(connectionId) + connections.getConnectionById(connectionId)?.info ); void openFile(errorLogFilePath); } @@ -877,7 +875,7 @@ export const openImport = ({ namespace: string; origin: 'menu' | 'crud-toolbar' | 'empty-state'; }): ImportThunkAction => { - return (dispatch, getState, { track, connectionRepository }) => { + return (dispatch, getState, { track, connections }) => { const { status } = getState().import; if (status === 'STARTED') { dispatch({ @@ -887,10 +885,8 @@ export const openImport = ({ } track( 'Import Opened', - { - origin, - }, - connectionRepository.getConnectionInfoById(connectionId) + { origin }, + connections.getConnectionById(connectionId)?.info ); dispatch({ type: OPEN, namespace, connectionId }); }; diff --git a/packages/compass-import-export/src/stores/export-store.ts b/packages/compass-import-export/src/stores/export-store.ts index b6aecc2149c..13e14232550 100644 --- a/packages/compass-import-export/src/stores/export-store.ts +++ b/packages/compass-import-export/src/stores/export-store.ts @@ -12,10 +12,7 @@ import { import type { PreferencesAccess } from 'compass-preferences-model'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import type { ActivateHelpers } from 'hadron-app-registry'; -import type { - ConnectionRepositoryAccess, - ConnectionsManager, -} from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; export function configureStore(services: ExportPluginServices) { @@ -33,8 +30,7 @@ export type RootExportState = ReturnType< export type ExportPluginServices = { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; - connectionRepository: ConnectionRepositoryAccess; + connections: ConnectionsService; preferences: PreferencesAccess; logger: Logger; track: TrackFunction; @@ -63,8 +59,7 @@ export function activatePlugin( _: unknown, { globalAppRegistry, - connectionsManager, - connectionRepository, + connections, preferences, logger, track, @@ -73,8 +68,7 @@ export function activatePlugin( ) { const store = configureStore({ globalAppRegistry, - connectionsManager, - connectionRepository, + connections, preferences, logger, track, @@ -115,7 +109,7 @@ export function activatePlugin( ); } ); - on(connectionsManager, 'disconnected', function (connectionId: string) { + on(connections, 'disconnected', function (connectionId: string) { store.dispatch(connectionDisconnected(connectionId)); }); diff --git a/packages/compass-import-export/src/stores/import-store.ts b/packages/compass-import-export/src/stores/import-store.ts index c516f69ee18..b617d30751a 100644 --- a/packages/compass-import-export/src/stores/import-store.ts +++ b/packages/compass-import-export/src/stores/import-store.ts @@ -11,10 +11,7 @@ import { } from '../modules/import'; import type { WorkspacesService } from '@mongodb-js/compass-workspaces/provider'; import type { Logger } from '@mongodb-js/compass-logging/provider'; -import type { - ConnectionRepositoryAccess, - ConnectionsManager, -} from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { ActivateHelpers } from 'hadron-app-registry'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; @@ -23,8 +20,7 @@ export type ImportPluginServices = { workspaces: WorkspacesService; logger: Logger; track: TrackFunction; - connectionsManager: ConnectionsManager; - connectionRepository: ConnectionRepositoryAccess; + connections: ConnectionsService; }; export function configureStore(services: ImportPluginServices) { @@ -60,8 +56,7 @@ export function activatePlugin( _: unknown, { globalAppRegistry, - connectionsManager, - connectionRepository, + connections, workspaces, logger, track, @@ -73,8 +68,7 @@ export function activatePlugin( workspaces, logger, track, - connectionsManager, - connectionRepository, + connections, }); addCleanup(() => { @@ -96,7 +90,7 @@ export function activatePlugin( } ); - on(connectionsManager, 'disconnected', function (connectionId: string) { + on(connections, 'disconnected', function (connectionId: string) { store.dispatch(connectionDisconnected(connectionId)); }); diff --git a/packages/compass-saved-aggregations-queries/src/index.ts b/packages/compass-saved-aggregations-queries/src/index.ts index adaacddc538..b760ec05565 100644 --- a/packages/compass-saved-aggregations-queries/src/index.ts +++ b/packages/compass-saved-aggregations-queries/src/index.ts @@ -1,5 +1,4 @@ import { registerHadronPlugin } from 'hadron-app-registry'; -import { connectionsManagerLocator } from '@mongodb-js/compass-connections/provider'; import { mongoDBInstancesManagerLocator } from '@mongodb-js/compass-app-stores/provider'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; import { telemetryLocator } from '@mongodb-js/compass-telemetry/provider'; @@ -12,9 +11,10 @@ import { favoriteQueryStorageAccessLocator, } from '@mongodb-js/my-queries-storage/provider'; import { preferencesLocator } from 'compass-preferences-model/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; const serviceLocators = { - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, instancesManager: mongoDBInstancesManagerLocator, preferencesAccess: preferencesLocator, logger: createLoggerLocator('COMPASS-MY-QUERIES-UI'), diff --git a/packages/compass-saved-aggregations-queries/src/stores/index.ts b/packages/compass-saved-aggregations-queries/src/stores/index.ts index 4307fb745d0..d03e4b7d696 100644 --- a/packages/compass-saved-aggregations-queries/src/stores/index.ts +++ b/packages/compass-saved-aggregations-queries/src/stores/index.ts @@ -6,7 +6,7 @@ import type { ThunkAction } from 'redux-thunk'; import itemsReducer from './aggregations-queries-items'; import openItemReducer from './open-item'; import editItemReducer from './edit-item'; -import { type ConnectionsManager } from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { MongoDBInstancesManager } from '@mongodb-js/compass-app-stores/provider'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import type { workspacesServiceLocator } from '@mongodb-js/compass-workspaces/provider'; @@ -19,7 +19,7 @@ import type { PreferencesAccess } from 'compass-preferences-model'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; type MyQueriesServices = { - connectionsManager: ConnectionsManager; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; preferencesAccess: PreferencesAccess; globalAppRegistry: AppRegistry; @@ -32,7 +32,7 @@ type MyQueriesServices = { export function configureStore({ globalAppRegistry, - connectionsManager, + connections, instancesManager, preferencesAccess, logger, @@ -50,7 +50,7 @@ export function configureStore({ applyMiddleware( thunk.withExtraArgument({ globalAppRegistry, - connectionsManager, + connections, instancesManager, preferencesAccess, logger, diff --git a/packages/compass-saved-aggregations-queries/src/stores/open-item.ts b/packages/compass-saved-aggregations-queries/src/stores/open-item.ts index 391b5b70c04..7cd6cbce17d 100644 --- a/packages/compass-saved-aggregations-queries/src/stores/open-item.ts +++ b/packages/compass-saved-aggregations-queries/src/stores/open-item.ts @@ -296,13 +296,12 @@ const connectionInfoToStateConnections = ( const loadDatabasesForConnection = (connectionId: string): SavedQueryAggregationThunkAction> => - async (dispatch, _getState, { connectionsManager, instancesManager }) => { + async (dispatch, _getState, { connections, instancesManager }) => { dispatch({ type: ActionTypes.LoadDatabases }); try { const instance = instancesManager.getMongoDBInstanceForConnection(connectionId); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); await instance.fetchDatabases({ dataService }); dispatch({ @@ -426,9 +425,8 @@ const openItem = database: string, collection: string ): SavedQueryAggregationThunkAction => - (_dispatch, _getState, { track, workspaces, connectionsManager }) => { - const connectionInfo = - connectionsManager.getConnectionById(connection)?.info; + (_dispatch, _getState, { track, workspaces, connections }) => { + const connectionInfo = connections.getConnectionById(connection)?.info; track( item.type === 'aggregation' ? 'Aggregation Opened' @@ -463,7 +461,7 @@ export const openSavedItem = async ( dispatch, getState, - { instancesManager, connectionsManager, logger: { log, mongoLogId } } + { instancesManager, connections, logger: { log, mongoLogId } } ) => { const { savedItems: { items }, @@ -486,7 +484,7 @@ export const openSavedItem = const connectionsWithNamespace: ConnectionInfo[] = []; for (const connectionInfo of activeConnections) { try { - const dataService = connectionsManager.getDataServiceForConnection( + const dataService = connections.getDataServiceForConnection( connectionInfo.id ); const instance = instancesManager.getMongoDBInstanceForConnection( @@ -592,7 +590,7 @@ export const openSelectedItem = export const databaseSelected = (database: string): SavedQueryAggregationThunkAction> => - async (dispatch, getState, { instancesManager, connectionsManager }) => { + async (dispatch, getState, { instancesManager, connections }) => { const { openItem: { selectedDatabase, selectedConnection }, } = getState(); @@ -610,7 +608,7 @@ export const databaseSelected = } const dataService = - connectionsManager.getDataServiceForConnection(selectedConnection); + connections.getDataServiceForConnection(selectedConnection); const instance = instancesManager.getMongoDBInstanceForConnection(selectedConnection); diff --git a/packages/compass-sidebar/src/index.ts b/packages/compass-sidebar/src/index.ts index d3f614c823c..f6db8837655 100644 --- a/packages/compass-sidebar/src/index.ts +++ b/packages/compass-sidebar/src/index.ts @@ -6,10 +6,9 @@ import { type MongoDBInstancesManager, mongoDBInstancesManagerLocator, } from '@mongodb-js/compass-app-stores/provider'; -import { - type ConnectionsManager, - connectionsManagerLocator, -} from '@mongodb-js/compass-connections/provider'; + +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; @@ -21,12 +20,12 @@ export const CompassSidebarPlugin = registerHadronPlugin( _initialProps, { globalAppRegistry, - connectionsManager, + connections, instancesManager, logger, }: { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; logger: Logger; }, @@ -35,7 +34,7 @@ export const CompassSidebarPlugin = registerHadronPlugin( const { store, deactivate } = createSidebarStore( { globalAppRegistry, - connectionsManager, + connections, instancesManager, logger, }, @@ -48,7 +47,7 @@ export const CompassSidebarPlugin = registerHadronPlugin( }, }, { - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, instancesManager: mongoDBInstancesManagerLocator, logger: createLoggerLocator('COMPASS-SIDEBAR-UI'), } diff --git a/packages/compass-sidebar/src/modules/data-service.ts b/packages/compass-sidebar/src/modules/data-service.ts index b603eeaf79e..3960c8bd1ef 100644 --- a/packages/compass-sidebar/src/modules/data-service.ts +++ b/packages/compass-sidebar/src/modules/data-service.ts @@ -5,10 +5,8 @@ export const setConnectionIsCSFLEEnabled = ( connectionId: ConnectionInfo['id'], enable: boolean ): SidebarThunkAction => { - return (_dispatch, _getState, { globalAppRegistry, connectionsManager }) => { - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); - + return (_dispatch, _getState, { globalAppRegistry, connections }) => { + const dataService = connections.getDataServiceForConnection(connectionId); dataService.setCSFLEEnabled(enable); queueMicrotask(() => { globalAppRegistry?.emit('refresh-data'); diff --git a/packages/compass-sidebar/src/modules/index.ts b/packages/compass-sidebar/src/modules/index.ts index 9e4dd280109..5d529d84df9 100644 --- a/packages/compass-sidebar/src/modules/index.ts +++ b/packages/compass-sidebar/src/modules/index.ts @@ -16,7 +16,7 @@ import type { } from './is-performance-tab-supported'; import isPerformanceTabSupported from './is-performance-tab-supported'; import type { ThunkAction } from 'redux-thunk'; -import type { ConnectionsManager } from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import type { MongoDBInstancesManager } from '@mongodb-js/compass-app-stores/provider'; import type { Logger } from '@mongodb-js/compass-logging/provider'; @@ -38,7 +38,7 @@ export type SidebarThunkAction = ThunkAction< RootState, { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; logger: Logger; }, diff --git a/packages/compass-sidebar/src/modules/instance.spec.ts b/packages/compass-sidebar/src/modules/instance.spec.ts index b61fb47f208..634381d747a 100644 --- a/packages/compass-sidebar/src/modules/instance.spec.ts +++ b/packages/compass-sidebar/src/modules/instance.spec.ts @@ -4,7 +4,6 @@ import { spy, stub, type SinonSpy, type SinonStub } from 'sinon'; import type { DataService } from 'mongodb-data-service'; import { setupInstance } from './instance'; import type { RootState } from '.'; -import type { ConnectionsManager } from '@mongodb-js/compass-connections/provider'; import type AppRegistry from 'hadron-app-registry'; import type { Logger } from '@mongodb-js/compass-logging'; import type { MongoDBInstancesManager } from '@mongodb-js/compass-app-stores/provider'; @@ -13,7 +12,7 @@ describe('sidebar instance', function () { const instance = createInstance(); let instanceOnSpy: SinonSpy; const globalAppRegistry = {} as any as AppRegistry; - const connectionsManager = { + const connectionsService = { getDataServiceForConnection() { return { getConnectionOptions() { @@ -27,7 +26,7 @@ describe('sidebar instance', function () { }, } as unknown as DataService; }, - } as any as ConnectionsManager; + } as any; let instancesManager: MongoDBInstancesManager; let logger: Logger; let listMongoDBInstancesStub: SinonStub; @@ -71,7 +70,7 @@ describe('sidebar instance', function () { } as any as RootState), { globalAppRegistry, - connectionsManager, + connections: connectionsService, instancesManager, logger, } diff --git a/packages/compass-sidebar/src/modules/instance.ts b/packages/compass-sidebar/src/modules/instance.ts index 9c7a3db246f..a115165a527 100644 --- a/packages/compass-sidebar/src/modules/instance.ts +++ b/packages/compass-sidebar/src/modules/instance.ts @@ -78,7 +78,7 @@ export const setupInstance = connectionId: ConnectionInfo['id'], instance: MongoDBInstance ): SidebarThunkAction => - (dispatch, getState, { connectionsManager, logger: { log, mongoLogId } }) => { + (dispatch, getState, { connections, logger: { log, mongoLogId } }) => { const { instance: instanceList } = getState(); if (instanceList[connectionId]) { @@ -181,8 +181,7 @@ export const setupInstance = instance.on('change:collections._id', onDatabasesChange); instance.on('change:collections.status', onDatabasesChange); - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); const connectionOptions = dataService.getConnectionOptions(); dispatch(changeConnectionOptions(connectionId, connectionOptions)); // stores ssh tunnel status diff --git a/packages/compass-sidebar/src/stores/store.spec.ts b/packages/compass-sidebar/src/stores/store.spec.ts index 2d908c0075f..8385f5bc953 100644 --- a/packages/compass-sidebar/src/stores/store.spec.ts +++ b/packages/compass-sidebar/src/stores/store.spec.ts @@ -56,7 +56,7 @@ describe('SidebarStore [Store]', function () { ({ deactivate } = createSidebarStore( { globalAppRegistry, - connectionsManager: { + connections: { getDataServiceForConnection() { return { getConnectionOptions() { diff --git a/packages/compass-sidebar/src/stores/store.ts b/packages/compass-sidebar/src/stores/store.ts index 261f1d7a873..7bde8fdc9ab 100644 --- a/packages/compass-sidebar/src/stores/store.ts +++ b/packages/compass-sidebar/src/stores/store.ts @@ -3,22 +3,22 @@ import thunk from 'redux-thunk'; import reducer from '../modules'; import { closeInstance, setupInstance } from '../modules/instance'; import type { ActivateHelpers, AppRegistry } from 'hadron-app-registry'; -import type { ConnectionsManager } from '@mongodb-js/compass-connections/provider'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import { type MongoDBInstancesManager, MongoDBInstancesManagerEvents, } from '@mongodb-js/compass-app-stores/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; export function createSidebarStore( { globalAppRegistry, - connectionsManager, + connections, instancesManager, logger, }: { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; logger: Logger; }, @@ -29,7 +29,7 @@ export function createSidebarStore( applyMiddleware( thunk.withExtraArgument({ globalAppRegistry, - connectionsManager, + connections, instancesManager, logger, }) diff --git a/packages/compass-telemetry/src/provider.tsx b/packages/compass-telemetry/src/provider.tsx index f701639ad8a..4773ad67db0 100644 --- a/packages/compass-telemetry/src/provider.tsx +++ b/packages/compass-telemetry/src/provider.tsx @@ -52,6 +52,10 @@ type FirstArgument = F extends (...args: [infer A, ...any]) => any ? A : never; +/** + * @deprecated instead of using HOC, refactor class component to functional + * component + */ function withTelemetry< T extends ((...args: any[]) => any) | { new (...args: any[]): any } >(ReactComponent: T): React.FunctionComponent, 'track'>> { diff --git a/packages/compass-workspaces/src/components/workspaces.tsx b/packages/compass-workspaces/src/components/workspaces.tsx index 624b697911c..8f1bd7ebcf1 100644 --- a/packages/compass-workspaces/src/components/workspaces.tsx +++ b/packages/compass-workspaces/src/components/workspaces.tsx @@ -39,9 +39,9 @@ import { NamespaceProvider } from '@mongodb-js/compass-app-stores/provider'; import { ConnectionInfoProvider, useTabConnectionTheme, - useConnectionRepository, } from '@mongodb-js/compass-connections/provider'; import { usePreference } from 'compass-preferences-model/provider'; +import { useConnectionsListRef } from '@mongodb-js/compass-connections/provider'; type Tooltip = [string, string][]; @@ -156,7 +156,7 @@ const CompassWorkspaces: React.FunctionComponent = ({ const { log, mongoLogId } = useLogger('COMPASS-WORKSPACES'); const { getWorkspacePluginByName } = useWorkspacePlugins(); const { getThemeOf } = useTabConnectionTheme(); - const { getConnectionTitleById } = useConnectionRepository(); + const { getConnectionById } = useConnectionsListRef(); const multipleConnectionsEnabled = usePreference( 'enableMultipleConnectionSystem' ); @@ -179,7 +179,8 @@ const CompassWorkspaces: React.FunctionComponent = ({ iconGlyph: 'CurlyBraces', } as const; case 'Shell': { - const connectionName = getConnectionTitleById(tab.connectionId) || ''; + const connectionName = + getConnectionById(tab.connectionId)?.title || ''; const tooltip: Tooltip = []; if (connectionName) { tooltip.push(['mongosh', connectionName || '']); @@ -197,7 +198,8 @@ const CompassWorkspaces: React.FunctionComponent = ({ } as const; } case 'Databases': { - const connectionName = getConnectionTitleById(tab.connectionId) || ''; + const connectionName = + getConnectionById(tab.connectionId)?.title || ''; return { id: tab.id, connectionName, @@ -209,7 +211,8 @@ const CompassWorkspaces: React.FunctionComponent = ({ } as const; } case 'Performance': { - const connectionName = getConnectionTitleById(tab.connectionId) || ''; + const connectionName = + getConnectionById(tab.connectionId)?.title || ''; return { id: tab.id, connectionName, @@ -223,7 +226,8 @@ const CompassWorkspaces: React.FunctionComponent = ({ } as const; } case 'Collections': { - const connectionName = getConnectionTitleById(tab.connectionId) || ''; + const connectionName = + getConnectionById(tab.connectionId)?.title || ''; const database = tab.namespace; return { id: tab.id, @@ -243,7 +247,8 @@ const CompassWorkspaces: React.FunctionComponent = ({ const { database, collection, ns } = toNS(tab.namespace); const info = collectionInfo[ns] ?? {}; const { isTimeSeries, isReadonly, sourceName } = info; - const connectionName = getConnectionTitleById(tab.connectionId) || ''; + const connectionName = + getConnectionById(tab.connectionId)?.title || ''; const collectionType = isTimeSeries ? 'timeseries' : isReadonly @@ -285,7 +290,7 @@ const CompassWorkspaces: React.FunctionComponent = ({ tabs, collectionInfo, getThemeOf, - getConnectionTitleById, + getConnectionById, multipleConnectionsEnabled, ]); diff --git a/packages/compass-workspaces/src/index.ts b/packages/compass-workspaces/src/index.ts index 3c80e01b9e1..f1cfd4f7cc5 100644 --- a/packages/compass-workspaces/src/index.ts +++ b/packages/compass-workspaces/src/index.ts @@ -24,9 +24,9 @@ import { mongoDBInstancesManagerLocator } from '@mongodb-js/compass-app-stores/p import type Collection from 'mongodb-collection-model'; import type Database from 'mongodb-database-model'; import { - connectionsManagerLocator, - type ConnectionsManager, + connectionsLocator, type ConnectionInfo, + type ConnectionsService, } from '@mongodb-js/compass-connections/provider'; import { WorkspacesStoreContext } from './stores/context'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; @@ -39,7 +39,7 @@ import { export type WorkspacesServices = { globalAppRegistry: AppRegistry; instancesManager: MongoDBInstancesManager; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; logger: Logger; }; @@ -74,7 +74,7 @@ export function activateWorkspacePlugin( { globalAppRegistry, instancesManager, - connectionsManager, + connections, logger, }: WorkspacesServices, { on, cleanup, addCleanup }: ActivateHelpers @@ -82,7 +82,7 @@ export function activateWorkspacePlugin( const store = configureStore(initialWorkspaceTabs, { globalAppRegistry, instancesManager, - connectionsManager, + connections, logger, }); @@ -120,7 +120,7 @@ export function activateWorkspacePlugin( ); on( - connectionsManager, + connections, 'disconnected', function (connectionId: ConnectionInfo['id']) { store.dispatch(connectionDisconnected(connectionId)); @@ -197,7 +197,7 @@ const WorkspacesPlugin = registerHadronPlugin( }, { instancesManager: mongoDBInstancesManagerLocator, - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, logger: createLoggerLocator('COMPASS-WORKSPACES-UI'), } ); diff --git a/packages/compass-workspaces/src/stores/workspaces.ts b/packages/compass-workspaces/src/stores/workspaces.ts index 8d9a208141c..312f59949ad 100644 --- a/packages/compass-workspaces/src/stores/workspaces.ts +++ b/packages/compass-workspaces/src/stores/workspaces.ts @@ -658,7 +658,7 @@ const fetchCollectionInfo = ( return async ( dispatch, getState, - { connectionsManager, instancesManager, logger } + { connections, instancesManager, logger } ) => { if (getState().collectionInfo[workspaceOptions.namespace]) { return; @@ -667,7 +667,7 @@ const fetchCollectionInfo = ( const { database, collection } = toNS(workspaceOptions.namespace); try { - const dataService = connectionsManager.getDataServiceForConnection( + const dataService = connections.getDataServiceForConnection( workspaceOptions.connectionId ); diff --git a/packages/compass/src/app/components/workspace.tsx b/packages/compass/src/app/components/workspace.tsx index 16c6d6335a5..b8294e25708 100644 --- a/packages/compass/src/app/components/workspace.tsx +++ b/packages/compass/src/app/components/workspace.tsx @@ -36,10 +36,10 @@ import { import { ImportPlugin, ExportPlugin } from '@mongodb-js/compass-import-export'; import ExplainPlanCollectionTabModal from '@mongodb-js/compass-explain-plan'; import ExportToLanguageCollectionTabModal from '@mongodb-js/compass-export-to-language'; -import { useConnectionRepository } from '@mongodb-js/compass-connections/provider'; import { usePreference } from 'compass-preferences-model/provider'; import updateTitle from '../utils/update-title'; import { getConnectionTitle } from '@mongodb-js/connection-info'; +import { useConnectionsListRef } from '@mongodb-js/compass-connections/provider'; export default function Workspace({ appName, @@ -54,7 +54,7 @@ export default function Workspace({ 'enableMultipleConnectionSystem' ); - const { getConnectionInfoById } = useConnectionRepository(); + const { getConnectionById } = useConnectionsListRef(); const onWorkspaceTabChange = useCallback( (ws: WorkspaceTab | null, collectionInfo: CollectionTabInfo | null) => { @@ -66,7 +66,7 @@ export default function Workspace({ : undefined; const connectionInfo = ws && ws.type !== 'My Queries' && ws.type !== 'Welcome' - ? getConnectionInfoById(ws.connectionId) + ? getConnectionById(ws.connectionId)?.info : undefined; updateTitle( appName, @@ -75,7 +75,7 @@ export default function Workspace({ namespace ); }, - [appName, getConnectionInfoById, onActiveWorkspaceTabChange] + [appName, getConnectionById, onActiveWorkspaceTabChange] ); return ( diff --git a/packages/databases-collections/src/index.ts b/packages/databases-collections/src/index.ts index 7d4aaab7ba9..e45a71de468 100644 --- a/packages/databases-collections/src/index.ts +++ b/packages/databases-collections/src/index.ts @@ -1,7 +1,7 @@ import { registerHadronPlugin } from 'hadron-app-registry'; import { createLoggerLocator } from '@mongodb-js/compass-logging/provider'; import { telemetryLocator } from '@mongodb-js/compass-telemetry/provider'; -import { connectionsManagerLocator } from '@mongodb-js/compass-connections/provider'; +import { connectionsLocator } from '@mongodb-js/compass-connections/provider'; import { mongoDBInstancesManagerLocator } from '@mongodb-js/compass-app-stores/provider'; import { CollectionsPlugin } from './collections-plugin'; import { @@ -19,7 +19,6 @@ import { favoriteQueryStorageAccessLocator, pipelineStorageLocator, } from '@mongodb-js/my-queries-storage/provider'; -import { connectionRepositoryAccessLocator } from '@mongodb-js/compass-connections/provider'; export const CollectionsWorkspaceTab: WorkspaceComponent<'Collections'> = { name: 'Collections' as const, @@ -40,8 +39,7 @@ export const CreateNamespacePlugin = registerHadronPlugin( { logger: createLoggerLocator('COMPASS-CREATE-NAMESPACE-UI'), track: telemetryLocator, - connectionsManager: connectionsManagerLocator, - connectionRepository: connectionRepositoryAccessLocator, + connections: connectionsLocator, instancesManager: mongoDBInstancesManagerLocator, workspaces: workspacesServiceLocator, } @@ -56,8 +54,7 @@ export const DropNamespacePlugin = registerHadronPlugin( { logger: createLoggerLocator('COMPASS-DROP-NAMESPACE-UI'), track: telemetryLocator, - connectionRepository: connectionRepositoryAccessLocator, - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, } ); @@ -68,7 +65,7 @@ export const RenameCollectionPlugin = registerHadronPlugin( activate: activateRenameCollectionPlugin, }, { - connectionsManager: connectionsManagerLocator, + connections: connectionsLocator, instancesManager: mongoDBInstancesManagerLocator, queryStorage: favoriteQueryStorageAccessLocator, pipelineStorage: pipelineStorageLocator, diff --git a/packages/databases-collections/src/modules/create-namespace.spec.ts b/packages/databases-collections/src/modules/create-namespace.spec.ts index 163ef3fe032..af45b43b1a0 100644 --- a/packages/databases-collections/src/modules/create-namespace.spec.ts +++ b/packages/databases-collections/src/modules/create-namespace.spec.ts @@ -34,8 +34,8 @@ describe('create collection module', function () { () => INITIAL_STATE, { track: createNoopTrack(), - connectionRepository: { - getConnectionInfoById: () => ({ id: 'TEST' }), + connections: { + getConnectionById: () => ({ info: { id: 'TEST' } }), }, } as any ); @@ -61,8 +61,8 @@ describe('create collection module', function () { () => INITIAL_STATE, { track: createNoopTrack(), - connectionRepository: { - getConnectionInfoById: () => ({ id: 'TEST' }), + connections: { + getConnectionById: () => ({ info: { id: 'TEST' } }), }, } as any ); diff --git a/packages/databases-collections/src/modules/create-namespace.ts b/packages/databases-collections/src/modules/create-namespace.ts index b3691509d10..c33424e813a 100644 --- a/packages/databases-collections/src/modules/create-namespace.ts +++ b/packages/databases-collections/src/modules/create-namespace.ts @@ -365,14 +365,7 @@ export const createNamespace = ( return async ( dispatch, getState, - { - globalAppRegistry, - connectionsManager, - connectionRepository, - logger: { debug }, - track, - workspaces, - } + { globalAppRegistry, connections, logger: { debug }, track, workspaces } ) => { const { databaseName, connectionId } = getState(); const kind = databaseName !== null ? 'Collection' : 'Database'; @@ -389,7 +382,7 @@ export const createNamespace = ( try { dispatch(toggleIsRunning(true)); - const ds = connectionsManager.getDataServiceForConnection(connectionId); + const ds = connections.getDataServiceForConnection(connectionId); const options = await handleFLE2Options(ds, data.options); @@ -404,8 +397,7 @@ export const createNamespace = ( expires: !!data.options.expireAfterSeconds, }; - const connectionInfo = - connectionRepository.getConnectionInfoById(connectionId); + const connectionInfo = connections.getConnectionById(connectionId)?.info; track(`${kind} Created`, trackEvent, connectionInfo); diff --git a/packages/databases-collections/src/modules/rename-collection/rename-collection.spec.ts b/packages/databases-collections/src/modules/rename-collection/rename-collection.spec.ts index 17d8d4699ec..ed51e4726d7 100644 --- a/packages/databases-collections/src/modules/rename-collection/rename-collection.spec.ts +++ b/packages/databases-collections/src/modules/rename-collection/rename-collection.spec.ts @@ -16,9 +16,9 @@ describe('rename collection module', function () { const dataService = { renameCollection: sandbox.stub().resolves({}), }; - const connectionsManager = { + const connectionsService = { getDataServiceForConnection: sandbox.stub().returns(dataService), - }; + } as any; const instancesManager = {} as any; const favoriteQueries = { getStorage: () => ({ @@ -31,7 +31,7 @@ describe('rename collection module', function () { const extraThunkArgs: RenameCollectionPluginServices = { globalAppRegistry: appRegistry, - connectionsManager: connectionsManager as any, + connections: connectionsService, instancesManager: instancesManager, queryStorage: favoriteQueries as any, pipelineStorage: pipelineStorage as any, @@ -43,7 +43,7 @@ describe('rename collection module', function () { {}, { globalAppRegistry: appRegistry, - connectionsManager: connectionsManager as any, + connections: connectionsService, instancesManager: instancesManager, queryStorage: favoriteQueries as any, pipelineStorage: pipelineStorage as any, diff --git a/packages/databases-collections/src/modules/rename-collection/rename-collection.ts b/packages/databases-collections/src/modules/rename-collection/rename-collection.ts index fb48efc13f6..fab93ec161c 100644 --- a/packages/databases-collections/src/modules/rename-collection/rename-collection.ts +++ b/packages/databases-collections/src/modules/rename-collection/rename-collection.ts @@ -172,16 +172,11 @@ export const renameCollection = ( RenameCollectionPluginServices, AnyAction > => { - return async ( - dispatch, - getState, - { connectionsManager, globalAppRegistry } - ) => { + return async (dispatch, getState, { connections, globalAppRegistry }) => { const sanitizedNewCollectionName = newCollectionName.trim(); const state = getState(); const { connectionId, databaseName, initialCollectionName } = state; - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); dispatch(renameRequestInProgress()); const oldNamespace = `${databaseName}.${initialCollectionName}`; diff --git a/packages/databases-collections/src/stores/create-namespace.ts b/packages/databases-collections/src/stores/create-namespace.ts index 45193fe971f..1adc54426fe 100644 --- a/packages/databases-collections/src/stores/create-namespace.ts +++ b/packages/databases-collections/src/stores/create-namespace.ts @@ -1,8 +1,6 @@ import type AppRegistry from 'hadron-app-registry'; -import { - type ConnectionsManager, - type ConnectionRepositoryAccess, -} from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; +import {} from '@mongodb-js/compass-connections/provider'; import type { MongoDBInstance } from 'mongodb-instance-model'; import type { Logger } from '@mongodb-js/compass-logging'; import type { Action, AnyAction } from 'redux'; @@ -27,8 +25,7 @@ import type { TrackFunction } from '@mongodb-js/compass-telemetry'; type NS = ReturnType; export type CreateNamespaceServices = { - connectionsManager: ConnectionsManager; - connectionRepository: ConnectionRepositoryAccess; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; globalAppRegistry: AppRegistry; logger: Logger; @@ -57,7 +54,7 @@ export function activatePlugin( services: CreateNamespaceServices, { on, cleanup }: ActivateHelpers ) { - const { instancesManager, connectionsManager, globalAppRegistry } = services; + const { instancesManager, connections, globalAppRegistry } = services; const store = configureStore(services); const onInstanceProvided = ( connectionId: string, @@ -86,8 +83,7 @@ export function activatePlugin( }; const onDataServiceProvided = (connectionId: string) => { - const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + const dataService = connections.getDataServiceForConnection(connectionId); store.dispatch( kmsProvidersRetrieved(connectionId, dataService.configuredKMSProviders()) ); @@ -106,7 +102,8 @@ export function activatePlugin( MongoDBInstancesManagerEvents.InstanceCreated, onInstanceProvided ); - on(connectionsManager, 'connected', onDataServiceProvided); + + on(connections, 'connected', onDataServiceProvided); on( globalAppRegistry, diff --git a/packages/databases-collections/src/stores/drop-namespace.tsx b/packages/databases-collections/src/stores/drop-namespace.tsx index 2b01400c578..41398b3bbd3 100644 --- a/packages/databases-collections/src/stores/drop-namespace.tsx +++ b/packages/databases-collections/src/stores/drop-namespace.tsx @@ -7,23 +7,23 @@ import { } from '@mongodb-js/compass-components'; import type { Logger } from '@mongodb-js/compass-logging/provider'; import type AppRegistry from 'hadron-app-registry'; -import type { ConnectionsManager } from '@mongodb-js/compass-connections/provider'; import toNS from 'mongodb-ns'; import type { ActivateHelpers } from 'hadron-app-registry'; import type { TrackFunction } from '@mongodb-js/compass-telemetry'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; type NS = ReturnType; type DropNamespaceServices = { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; logger: Logger; track: TrackFunction; }; export function activatePlugin( _: unknown, - { globalAppRegistry, connectionsManager, track }: DropNamespaceServices, + { globalAppRegistry, connections, track }: DropNamespaceServices, { on, cleanup, signal }: ActivateHelpers ) { const onDropNamespace = async ( @@ -70,7 +70,7 @@ export function activatePlugin( try { const method = isCollection ? 'dropCollection' : 'dropDatabase'; const dataService = - connectionsManager.getDataServiceForConnection(connectionId); + connections.getDataServiceForConnection(connectionId); await dataService[method](ns); globalAppRegistry.emit( diff --git a/packages/databases-collections/src/stores/rename-collection.spec.tsx b/packages/databases-collections/src/stores/rename-collection.spec.tsx index 54cede318ea..ad984292486 100644 --- a/packages/databases-collections/src/stores/rename-collection.spec.tsx +++ b/packages/databases-collections/src/stores/rename-collection.spec.tsx @@ -13,7 +13,7 @@ import { describe('RenameCollectionPlugin', function () { const sandbox = Sinon.createSandbox(); let appRegistry: RenderWithConnectionsResult['globalAppRegistry']; - const connectionsManager = {}; + const connections = {}; const instanceModel = { databases: { get: function () { @@ -36,7 +36,7 @@ describe('RenameCollectionPlugin', function () { }; beforeEach(function () { const Plugin = RenameCollectionPlugin.withMockServices({ - connectionsManager: connectionsManager as any, + connections: connections as any, instancesManager: instancesManager as any, queryStorage: favoriteQueries as any, pipelineStorage: pipelineStorage as any, diff --git a/packages/databases-collections/src/stores/rename-collection.ts b/packages/databases-collections/src/stores/rename-collection.ts index aaa43c055cf..e4d5bc228ca 100644 --- a/packages/databases-collections/src/stores/rename-collection.ts +++ b/packages/databases-collections/src/stores/rename-collection.ts @@ -1,7 +1,7 @@ import { legacy_createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import type AppRegistry from 'hadron-app-registry'; -import type { ConnectionsManager } from '@mongodb-js/compass-connections/provider'; +import type { ConnectionsService } from '@mongodb-js/compass-connections/provider'; import reducer, { open } from '../modules/rename-collection/rename-collection'; import type { FavoriteQueryStorageAccess, @@ -12,7 +12,7 @@ import type { ActivateHelpers } from 'hadron-app-registry'; export type RenameCollectionPluginServices = { globalAppRegistry: AppRegistry; - connectionsManager: ConnectionsManager; + connections: ConnectionsService; instancesManager: MongoDBInstancesManager; queryStorage?: FavoriteQueryStorageAccess; pipelineStorage?: PipelineStorage; @@ -22,7 +22,7 @@ export function activateRenameCollectionPlugin( _: unknown, { globalAppRegistry, - connectionsManager, + connections, instancesManager, queryStorage, pipelineStorage, @@ -53,7 +53,7 @@ export function activateRenameCollectionPlugin( thunk.withExtraArgument({ globalAppRegistry, instancesManager, - connectionsManager, + connections, }) ) );