Skip to content

Commit 20aa4f5

Browse files
authored
chore(data-service, home): create a service provider / locator for data service COMPASS-7410 (#5085)
* chore(data-service, compass-home): add provider for data service; set up provider in home * chore(import-export, home): setup connected app registry scope; refactor export store to use service injection * chore(collection): remove unused dev dependency * chore(import-export): refactor import store to use service injection
1 parent 5a73fc1 commit 20aa4f5

34 files changed

+217
-468
lines changed

package-lock.json

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-collection/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"depcheck": "^1.4.1",
9090
"eslint": "^7.25.0",
9191
"mocha": "^10.2.0",
92-
"mongodb": "^6.0.0",
9392
"mongodb-collection-model": "^5.15.1",
9493
"mongodb-data-service": "^22.15.1",
9594
"mongodb-instance-model": "^12.15.1",

packages/compass-home/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"@mongodb-js/compass-import-export": "^7.19.1",
4848
"compass-preferences-model": "^2.15.6",
4949
"hadron-app-registry": "^9.0.14",
50-
"hadron-ipc": "^3.2.4"
50+
"hadron-ipc": "^3.2.4",
51+
"mongodb-data-service": "^22.15.1"
5152
},
5253
"peerDependencies": {
5354
"@mongodb-js/atlas-service": "^0.10.1",
@@ -64,6 +65,7 @@
6465
"compass-preferences-model": "^2.15.6",
6566
"hadron-app-registry": "^9.0.14",
6667
"hadron-ipc": "^3.2.4",
68+
"mongodb-data-service": "^22.15.1",
6769
"react": "^17.0.2"
6870
},
6971
"devDependencies": {
@@ -82,7 +84,6 @@
8284
"eventemitter3": "^4.0.0",
8385
"mocha": "^10.2.0",
8486
"mongodb-collection-model": "^5.15.1",
85-
"mongodb-data-service": "^22.15.1",
8687
"mongodb-ns": "^2.4.0",
8788
"nyc": "^15.1.0",
8889
"prettier": "^2.7.1",

packages/compass-home/src/components/home.spec.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { once } from 'events';
33
import { cleanup, render, screen, waitFor } from '@testing-library/react';
44
import { expect } from 'chai';
5-
import AppRegistry, { AppRegistryProvider } from 'hadron-app-registry';
5+
import { AppRegistryProvider, globalAppRegistry } from 'hadron-app-registry';
66
import { ipcRenderer } from 'hadron-ipc';
77
import sinon from 'sinon';
88
import Home from '.';
@@ -32,9 +32,9 @@ describe('Home [Component]', function () {
3232
}
3333
});
3434

35-
let testAppRegistry: AppRegistry;
35+
const testAppRegistry = globalAppRegistry;
36+
3637
beforeEach(function () {
37-
testAppRegistry = new AppRegistry();
3838
[
3939
'Collection.Workspace',
4040
'Database.Workspace',
@@ -55,7 +55,10 @@ describe('Home [Component]', function () {
5555
testAppRegistry.onActivated();
5656
});
5757

58-
afterEach(cleanup);
58+
afterEach(function () {
59+
testAppRegistry.deactivate();
60+
cleanup();
61+
});
5962

6063
describe('is not connected', function () {
6164
beforeEach(function () {

packages/compass-home/src/components/home.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import React, {
3333
} from 'react';
3434
import preferences from 'compass-preferences-model';
3535
import { createLoggerAndTelemetry } from '@mongodb-js/compass-logging';
36-
import { useLocalAppRegistry } from 'hadron-app-registry';
36+
import { AppRegistryProvider, useLocalAppRegistry } from 'hadron-app-registry';
3737
import updateTitle from '../modules/update-title';
3838
import Workspace from './workspace';
3939
import { SignalHooksProvider } from '@mongodb-js/compass-components';
@@ -49,6 +49,7 @@ import {
4949
DropCollectionPlugin,
5050
} from '@mongodb-js/compass-databases-collections';
5151
import { ImportPlugin, ExportPlugin } from '@mongodb-js/compass-import-export';
52+
import { DataServiceProvider } from 'mongodb-data-service/provider';
5253

5354
const { track } = createLoggerAndTelemetry('COMPASS-HOME-UI');
5455

@@ -326,6 +327,12 @@ function Home({
326327
};
327328
}, [appRegistry, onDataServiceDisconnected]);
328329

330+
if (isConnected && !connectedDataService.current) {
331+
throw new Error(
332+
'Application is connected, but DataService is not available'
333+
);
334+
}
335+
329336
return (
330337
<SignalHooksProvider
331338
onSignalMount={(id) => {
@@ -344,7 +351,16 @@ function Home({
344351
track('Signal Closed', { id });
345352
}}
346353
>
347-
{isConnected && <Workspace namespace={namespace} />}
354+
{isConnected && connectedDataService.current && (
355+
// AppRegistry scope for a connected application
356+
<AppRegistryProvider>
357+
<DataServiceProvider value={connectedDataService.current}>
358+
<ImportPlugin></ImportPlugin>
359+
<ExportPlugin></ExportPlugin>
360+
<Workspace namespace={namespace} />
361+
</DataServiceProvider>
362+
</AppRegistryProvider>
363+
)}
348364
{/* Hide <Connections> but keep it in scope if connected so that the connection
349365
import/export functionality can still be used through the application menu */}
350366
<div
@@ -371,8 +387,6 @@ function Home({
371387
<DropDatabasePlugin></DropDatabasePlugin>
372388
<CreateCollectionPlugin></CreateCollectionPlugin>
373389
<DropCollectionPlugin></DropCollectionPlugin>
374-
<ImportPlugin></ImportPlugin>
375-
<ExportPlugin></ExportPlugin>
376390
<AtlasSignIn></AtlasSignIn>
377391
</SignalHooksProvider>
378392
);

packages/compass-home/src/components/workspace-content.spec.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { cleanup, render, screen } from '@testing-library/react';
33
import { expect } from 'chai';
4-
import AppRegistry, { AppRegistryProvider } from 'hadron-app-registry';
4+
import { globalAppRegistry, AppRegistryProvider } from 'hadron-app-registry';
55
import WorkspaceContent from './workspace-content';
66

77
const getComponent = (name: string) => {
@@ -20,28 +20,18 @@ const getComponent = (name: string) => {
2020
};
2121

2222
describe('WorkspaceContent [Component]', function () {
23-
let testAppRegistry: AppRegistry;
24-
beforeEach(function () {
25-
testAppRegistry = new AppRegistry();
26-
23+
before(function () {
2724
['Collection.Workspace', 'Database.Workspace', 'Instance.Workspace'].map(
28-
(name) => testAppRegistry.registerComponent(name, getComponent(name))
25+
(name) => globalAppRegistry.registerComponent(name, getComponent(name))
2926
);
30-
testAppRegistry.onActivated();
27+
globalAppRegistry.onActivated();
3128
});
3229

33-
afterEach(function () {
34-
testAppRegistry = null;
35-
cleanup();
36-
});
30+
afterEach(cleanup);
3731

3832
describe('namespace is unset', function () {
3933
beforeEach(function () {
40-
render(
41-
<AppRegistryProvider localAppRegistry={testAppRegistry}>
42-
<WorkspaceContent namespace={{ database: '', collection: '' }} />
43-
</AppRegistryProvider>
44-
);
34+
render(<WorkspaceContent namespace={{ database: '', collection: '' }} />);
4535
});
4636

4737
it('renders content correctly', function () {
@@ -54,7 +44,7 @@ describe('WorkspaceContent [Component]', function () {
5444
describe('namespace has a db', function () {
5545
beforeEach(function () {
5646
render(
57-
<AppRegistryProvider localAppRegistry={testAppRegistry}>
47+
<AppRegistryProvider>
5848
<WorkspaceContent namespace={{ database: 'db', collection: '' }} />
5949
</AppRegistryProvider>
6050
);
@@ -70,7 +60,7 @@ describe('WorkspaceContent [Component]', function () {
7060
describe('namespace has db and collection', function () {
7161
beforeEach(function () {
7262
render(
73-
<AppRegistryProvider localAppRegistry={testAppRegistry}>
63+
<AppRegistryProvider>
7464
<WorkspaceContent namespace={{ database: 'db', collection: 'col' }} />
7565
</AppRegistryProvider>
7666
);

packages/compass-import-export/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"electron": "^25.9.3",
6666
"hadron-app-registry": "^9.0.14",
6767
"hadron-document": "^8.4.3",
68+
"mongodb-data-service": "^22.15.1",
6869
"react": "^17.0.2"
6970
},
7071
"dependencies": {
@@ -76,7 +77,8 @@
7677
"compass-preferences-model": "^2.15.6",
7778
"electron": "^25.9.3",
7879
"hadron-app-registry": "^9.0.14",
79-
"hadron-document": "^8.4.3"
80+
"hadron-document": "^8.4.3",
81+
"mongodb-data-service": "^22.15.1"
8082
},
8183
"devDependencies": {
8284
"@electron/remote": "^2.0.12",
@@ -106,7 +108,6 @@
106108
"lodash": "^4.17.21",
107109
"mocha": "^10.2.0",
108110
"mongodb": "^6.0.0",
109-
"mongodb-data-service": "^22.15.1",
110111
"mongodb-ns": "^2.4.0",
111112
"mongodb-query-parser": "^3.1.3",
112113
"mongodb-schema": "^12.1.0",

packages/compass-import-export/src/components/export-modal.spec.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ function renderModal(exportState: any = {}) {
1212
// component in a state that can actually be achieved when actions are emitted
1313
// on the store. Refactor this to either test unconnected component, or to
1414
// not mutate state directly for tests
15-
const store = configureStore();
15+
const store = configureStore({
16+
dataService: {},
17+
globalAppRegistry: {},
18+
} as any);
1619
const state = store.getState();
1720
state.export = {
1821
...state.export,

packages/compass-import-export/src/components/import-modal.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,16 @@ function ImportModal({
168168
if (isOpen && !fileName && errors.length === 0) {
169169
// Show the file input when we don't have a file to import yet.
170170
return (
171-
<ImportFileInput
172-
autoOpen
173-
onCancel={handleClose}
174-
fileName={fileName}
175-
selectImportFileName={selectImportFileName}
176-
/>
171+
// Don't actually show it on the screen, just render it to trigger
172+
// autoOpen
173+
<div style={{ display: 'none' }}>
174+
<ImportFileInput
175+
autoOpen
176+
onCancel={handleClose}
177+
fileName={fileName}
178+
selectImportFileName={selectImportFileName}
179+
/>
180+
</div>
177181
);
178182
}
179183

@@ -266,7 +270,7 @@ function ImportModal({
266270
* Map the state of the store to component properties.
267271
*/
268272
const mapStateToProps = (state: RootImportState) => ({
269-
ns: state.ns,
273+
ns: state.import.namespace,
270274
isOpen: state.import.isOpen,
271275
errors: state.import.errors,
272276
fileType: state.import.fileType,

packages/compass-import-export/src/export/export-csv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export async function exportCSVFromAggregation({
259259
...exportOptions
260260
}: Omit<ExportCSVOptions, 'input' | 'columns'> & {
261261
ns: string;
262-
dataService: DataService;
262+
dataService: Pick<DataService, 'aggregateCursor'>;
263263
aggregation: ExportAggregation;
264264
}) {
265265
debug('exportCSVFromAggregation()', { ns: toNS(ns), aggregation });
@@ -319,7 +319,7 @@ export async function exportCSVFromQuery({
319319
...exportOptions
320320
}: Omit<ExportCSVOptions, 'input' | 'columns'> & {
321321
ns: string;
322-
dataService: DataService;
322+
dataService: Pick<DataService, 'findCursor'>;
323323
query?: ExportQuery;
324324
}) {
325325
debug('exportCSVFromQuery()', { ns: toNS(ns), query });

0 commit comments

Comments
 (0)