Skip to content

Commit

Permalink
chore: refactor ConnectionsManager and InstancesManager to throw when…
Browse files Browse the repository at this point in the history
… DataService / Instance is not available (#5827)
  • Loading branch information
himanshusinghs committed May 23, 2024
1 parent cbb9acc commit 8409035
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,6 @@ export const createView = (): CreateViewThunkAction<Promise<void>> => {
try {
const dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
throw new Error(
`DataService for connectionId ${connectionId} not found`
);
}

dispatch(toggleIsRunning(true));
await dataService.createView(
Expand Down
19 changes: 9 additions & 10 deletions packages/compass-app-stores/src/instances-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ describe('InstancesManager', function () {
});

it('should be able return a MongoDBInstance if available', function () {
const nonExistentInstance =
expect(() => {
instancesManager.getMongoDBInstanceForConnection('1234'); // non-existent
expect(nonExistentInstance).to.be.undefined;
}).to.throw;
instancesManager.createMongoDBInstanceForConnection(
TEST_CONNECTION_INFO.id,
{
Expand All @@ -89,10 +89,9 @@ describe('InstancesManager', function () {
},
}
);
const existingInstance = instancesManager.getMongoDBInstanceForConnection(
TEST_CONNECTION_INFO.id
);
expect(existingInstance).to.not.be.undefined;
expect(() =>
instancesManager.getMongoDBInstanceForConnection(TEST_CONNECTION_INFO.id)
).to.not.throw;
});

it('should be able to remove MongoDBInstance for a connection', function () {
Expand All @@ -109,15 +108,15 @@ describe('InstancesManager', function () {
},
}
);
expect(
expect(() =>
instancesManager.getMongoDBInstanceForConnection(TEST_CONNECTION_INFO.id)
).to.not.be.undefined;
).to.not.throw;
instancesManager.removeMongoDBInstanceForConnection(
TEST_CONNECTION_INFO.id
);
expect(
expect(() =>
instancesManager.getMongoDBInstanceForConnection(TEST_CONNECTION_INFO.id)
).to.be.undefined;
).to.throw;
});

it('should emit instances removed event when an instance is removed', function () {
Expand Down
12 changes: 10 additions & 2 deletions packages/compass-app-stores/src/instances-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ export class MongoDBInstancesManager extends EventEmitter {
return new Map(this.instances);
}

getMongoDBInstanceForConnection(connectionInfoId: ConnectionInfo['id']) {
return this.instances.get(connectionInfoId);
getMongoDBInstanceForConnection(
connectionInfoId: ConnectionInfo['id']
): MongoDBInstance {
const instance = this.instances.get(connectionInfoId);
if (!instance) {
throw new Error(
`MongoDBInstance for connectionId - ${connectionInfoId} not present in InstancesManager.`
);
}
return instance;
}

removeMongoDBInstanceForConnection(connectionInfoId: ConnectionInfo['id']) {
Expand Down
8 changes: 1 addition & 7 deletions packages/compass-app-stores/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ export const mongoDBInstanceLocator = createServiceLocator(
function useMongoDBInstance(): MongoDBInstance {
const connectionInfo = useConnectionInfo();
const instancesManager = mongoDBInstancesManagerLocator();
const instance = instancesManager.getMongoDBInstanceForConnection(
connectionInfo.id
);
if (!instance) {
throw new Error('No MongoDBInstance available in this context');
}
return instance;
return instancesManager.getMongoDBInstanceForConnection(connectionInfo.id);
},
'mongoDBInstanceLocator'
);
Expand Down
16 changes: 8 additions & 8 deletions packages/compass-app-stores/src/stores/instance-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ describe('InstanceStore [Store]', function () {
);
}

expect(instancesManager.getMongoDBInstanceForConnection('1')).to.not.be
.undefined;
expect(instancesManager.getMongoDBInstanceForConnection('2')).to.not.be
.undefined;
expect(instancesManager.getMongoDBInstanceForConnection('3')).to.not.be
.undefined;
expect(() => instancesManager.getMongoDBInstanceForConnection('1')).to.not
.throw;
expect(() => instancesManager.getMongoDBInstanceForConnection('2')).to.not
.throw;
expect(() => instancesManager.getMongoDBInstanceForConnection('3')).to.not
.throw;
});

context('when connected', function () {
Expand All @@ -129,7 +129,7 @@ describe('InstanceStore [Store]', function () {
connectedConnectionInfoId
);
expect(instance).to.not.be.undefined;
connectedInstance = instance as MongoDBInstance;
connectedInstance = instance;
initialInstanceRefreshedPromise =
waitForInstanceRefresh(connectedInstance);
});
Expand All @@ -145,7 +145,7 @@ describe('InstanceStore [Store]', function () {
);
expect(instance).to.have.nested.property('build.version', '1.2.3');
globalAppRegistry.emit('refresh-data');
await waitForInstanceRefresh(instance as MongoDBInstance);
await waitForInstanceRefresh(instance);
});

it('calls instance model fetch', function () {
Expand Down
28 changes: 14 additions & 14 deletions packages/compass-connections/src/connections-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ describe('ConnectionsManager', function () {
getConnectionConfigurationOptions()
),
]);
expect(
expect(() =>
connectionsManager.getDataServiceForConnection(
connectedConnectionInfo1.id
)
).to.be.undefined;
expect(
).to.throw;
expect(() =>
connectionsManager.getDataServiceForConnection(
connectedConnectionInfo2.id
)
).to.be.undefined;
).to.throw;
});

context('when all connection attempts are cancelled', function () {
Expand Down Expand Up @@ -279,16 +279,16 @@ describe('ConnectionsManager', function () {
});
connectionsManager.cancelAllConnectionAttempts();
await canceledPromise;
expect(
expect(() =>
connectionsManager.getDataServiceForConnection(
connectedConnectionInfo1.id
)
).to.be.undefined;
expect(
).to.throw;
expect(() =>
connectionsManager.getDataServiceForConnection(
connectedConnectionInfo2.id
)
).to.be.undefined;
).to.throw;
});
});
}
Expand Down Expand Up @@ -370,11 +370,11 @@ describe('ConnectionsManager', function () {
});
void connectionsManager.closeConnection(connectedConnectionInfo1.id);
await canceledPromise;
expect(
expect(() =>
connectionsManager.getDataServiceForConnection(
connectedConnectionInfo1.id
)
).to.be.undefined;
).to.throw;
});

context(
Expand Down Expand Up @@ -573,9 +573,9 @@ describe('ConnectionsManager', function () {
} catch (error) {
// nothing
}
expect(
expect(() =>
connectionsManager.getDataServiceForConnection(failedConnectionInfo.id)
).to.be.undefined;
).to.throw;
});
});

Expand Down Expand Up @@ -638,9 +638,9 @@ describe('ConnectionsManager', function () {
getConnectionConfigurationOptions()
);
await connectionsManager.closeConnection(activeConnectionInfo.id);
expect(
expect(() =>
connectionsManager.getDataServiceForConnection(activeConnectionInfo.id)
).to.be.undefined;
).to.throw;
});
});

Expand Down
15 changes: 9 additions & 6 deletions packages/compass-connections/src/connections-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ export class ConnectionsManager extends EventEmitter {
this.__TEST_CONNECT_FN = __TEST_CONNECT_FN;
}

getDataServiceForConnection(
connectionInfoId: ConnectionInfoId
): DataService | undefined {
return this.dataServices.get(connectionInfoId);
getDataServiceForConnection(connectionInfoId: ConnectionInfoId): DataService {
const dataService = this.dataServices.get(connectionInfoId);
if (!dataService) {
throw new Error(
`DataService for connectionId - ${connectionInfoId} not present in ConnectionsManager.`
);
}
return dataService;
}

statusOf(connectionInfoId: ConnectionInfoId): ConnectionStatus {
Expand Down Expand Up @@ -172,8 +176,7 @@ export class ConnectionsManager extends EventEmitter {
}: ConnectionConfiguration = {}
): Promise<DataService> {
try {
const existingDataService =
this.getDataServiceForConnection(connectionId);
const existingDataService = this.dataServices.get(connectionId);

if (
existingDataService &&
Expand Down
3 changes: 0 additions & 3 deletions packages/compass-connections/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ export const dataServiceLocator = createServiceLocator(
const ds = connectionsManager.getDataServiceForConnection(
connectionInfo.id
);
if (!ds) {
throw new Error('DataService is not available for the active connection');
}
return ds;
}
);
Expand Down
6 changes: 0 additions & 6 deletions packages/compass-import-export/src/modules/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ export const selectFieldsToExport = (): ExportThunkAction<

const dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
throw new Error(`DataService for connection ${connectionId} not found`);
}

gatherFieldsResult = await gatherFieldsFromQuery({
ns: namespace,
Expand Down Expand Up @@ -456,9 +453,6 @@ export const runExport = ({

const dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
throw new Error(`DataService for connection ${connectionId} not found`);
}

const baseExportOptions = {
ns: namespace,
Expand Down
5 changes: 0 additions & 5 deletions packages/compass-import-export/src/modules/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ export const startImport = (): ImportThunkAction<Promise<void>> => {

dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
throw new Error(
`DataService for connectionId ${connectionId} not found`
);
}

if (fileType === 'csv') {
result = await importCSV({
Expand Down
6 changes: 0 additions & 6 deletions packages/compass-sidebar/src/modules/data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ export const setConnectionIsCSFLEEnabled = (
const dataService =
connectionsManager.getDataServiceForConnection(connectionId);

if (!dataService) {
throw new Error(
'unreachable: This is only visible when we are connected.'
);
}

dataService.setCSFLEEnabled(enable);
queueMicrotask(() => {
globalAppRegistry?.emit('refresh-data');
Expand Down
5 changes: 0 additions & 5 deletions packages/compass-sidebar/src/modules/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ export const setupInstance =

const dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
// This should be unreachable, because the instance state is only available
// whenever we have a Data Service connected, as it reads server metadata.
return;
}

const connectionOptions = dataService.getConnectionOptions();
dispatch(changeConnectionOptions(connectionId, connectionOptions)); // stores ssh tunnel status
Expand Down
10 changes: 0 additions & 10 deletions packages/compass-workspaces/src/stores/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,20 +609,10 @@ export const openWorkspace = (
const dataService = connectionsManager.getDataServiceForConnection(
workspaceOptions.connectionId
);
if (!dataService) {
throw new Error(
`DataService not available for connectionId=${workspaceOptions.connectionId}`
);
}

const instance = instancesManager.getMongoDBInstanceForConnection(
workspaceOptions.connectionId
);
if (!instance) {
throw new Error(
`MongoDBInstance not available for connectionId=${workspaceOptions.connectionId}`
);
}

const coll = await instance.getNamespace({
dataService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,6 @@ export const createNamespace = (
try {
dispatch(toggleIsRunning(true));
const ds = connectionsManager.getDataServiceForConnection(connectionId);
if (!ds) {
throw new Error('DataService not available for connection');
}

const options = await handleFLE2Options(ds, data.options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ export const renameCollection = (
const dataService =
connectionsManager.getDataServiceForConnection(connectionId);

if (!dataService) {
throw new Error(
'unreachable: The modal is only shown when the connection is active.'
);
}

dispatch(renameRequestInProgress());
const oldNamespace = `${databaseName}.${initialCollectionName}`;
const newNamespace = `${databaseName}.${newCollectionName}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe('CreateNamespacePlugin', function () {
} else if (id === '2') {
return dataService2;
}
throw new Error('unknown id provided');
});

const instancesManager = new TestMongoDBInstanceManager();
Expand Down
5 changes: 0 additions & 5 deletions packages/databases-collections/src/stores/drop-namespace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ export function activatePlugin(
const method = isCollection ? 'dropCollection' : 'dropDatabase';
const dataService =
connectionsManager.getDataServiceForConnection(connectionId);
if (!dataService) {
throw new Error(
'unreachable: only available when there is an open connection.'
);
}

await dataService[method](ns);
globalAppRegistry.emit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ export function activateRenameCollectionPlugin(

const instance =
instancesManager.getMongoDBInstanceForConnection(connectionId);
if (!instance) {
throw new Error(
'unreachable: this modal is only shown when the connection is open.'
);
}

const collections: { name: string }[] =
instance.databases.get(ns.database)?.collections ?? [];
Expand Down

0 comments on commit 8409035

Please sign in to comment.