Skip to content

Commit 74c0344

Browse files
committed
store preferencesAccess in the instance model
1 parent 8fdefa1 commit 74c0344

File tree

14 files changed

+68
-88
lines changed

14 files changed

+68
-88
lines changed

packages/collection-model/index.d.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { PreferencesAccess } from 'compass-preferences-model';
21
import type { DataService } from 'mongodb-data-service';
32

43
type CollectionMetadata = {
@@ -70,13 +69,13 @@ interface CollectionProps {
7069
pipeline: unknown[];
7170
validation: unknown;
7271
is_capped?: boolean;
73-
document_count?: number;
74-
document_size?: number;
75-
avg_document_size?: number;
76-
storage_size?: number;
77-
free_storage_size?: number;
78-
index_count?: number;
79-
index_size?: number;
72+
document_count: number | undefined;
73+
document_size: number | undefined;
74+
avg_document_size: number | undefined;
75+
storage_size: number | undefined;
76+
free_storage_size: number | undefined;
77+
index_count: number | undefined;
78+
index_size: number | undefined;
8079
isTimeSeries: boolean;
8180
isView: boolean;
8281
/** Only relevant for a view and identifies collection/view from which this view was created. */
@@ -97,13 +96,11 @@ type CollectionDataService = Pick<
9796
interface Collection extends CollectionProps {
9897
fetch(opts: {
9998
dataService: CollectionDataService;
100-
preferences: PreferencesAccess;
10199
fetchInfo?: boolean;
102100
force?: boolean;
103101
}): Promise<void>;
104102
fetchMetadata(opts: {
105103
dataService: CollectionDataService;
106-
preferences: PreferencesAccess;
107104
}): Promise<CollectionMetadata>;
108105
on(evt: string, fn: (...args: any) => void);
109106
off(evt: string, fn: (...args: any) => void);

packages/collection-model/lib/model.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,24 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
244244
/**
245245
* @param {{
246246
* dataService: import('mongodb-data-service').DataService,
247-
* preferences: import('compass-preferences-model').PreferencesAccess,
248247
* fetchInfo: boolean,
249248
* force: boolean
250249
* }} options
251250
* @returns
252251
*/
253-
async fetch({ dataService, fetchInfo = true, preferences, force = false }) {
252+
async fetch({ dataService, fetchInfo = true, force = false }) {
254253
if (!shouldFetch(this.status, force)) {
255254
console.log('[collection-model]', 'not should fetch', this.status, force);
256255
return;
257256
}
258257

259-
const { enableDbAndCollStats } = preferences.getPreferences();
258+
const instanceModel = getParentByType(this, 'Instance');
259+
console.log(
260+
'PREFERENCES in COLL',
261+
instanceModel.preferences,
262+
instanceModel
263+
);
264+
const { enableDbAndCollStats } = instanceModel.preferences.getPreferences();
260265

261266
try {
262267
console.log('[collection-model]', 'starting');
@@ -296,12 +301,11 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
296301
* that events like open-in-new-tab, select-namespace, edit-view require
297302
* @param {{
298303
* dataService: import('mongodb-data-service').DataService,
299-
* preferences: import('compass-preferences-model').PreferencesAccess,
300304
* }} options
301305
*/
302-
async fetchMetadata({ dataService, preferences }) {
306+
async fetchMetadata({ dataService }) {
303307
try {
304-
await this.fetch({ dataService, preferences });
308+
await this.fetch({ dataService });
305309
} catch (e) {
306310
if (e.name !== 'MongoServerError') {
307311
throw e;

packages/compass-app-stores/src/provider.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { MongoDBInstancesManager } from './instances-manager';
2020
import toNS from 'mongodb-ns';
2121
import type Collection from 'mongodb-collection-model';
2222
import type Database from 'mongodb-database-model';
23-
import { usePreferences } from 'compass-preferences-model/provider';
2423

2524
export {
2625
MongoDBInstancesManagerEvents,
@@ -131,7 +130,6 @@ export const NamespaceProvider = createServiceProvider(
131130
const ns = useMemo(() => {
132131
return toNS(namespace);
133132
}, [namespace]);
134-
const preferences = usePreferences(['enableDbAndCollStats']);
135133
const [namespaceModel, setNamespaceModel] = useState<
136134
Database | Collection | null
137135
>(() => {
@@ -168,7 +166,7 @@ export const NamespaceProvider = createServiceProvider(
168166
return;
169167
}
170168

171-
await db.fetchCollections({ dataService, preferences }).catch(() => {
169+
await db.fetchCollections({ dataService }).catch(() => {
172170
// See above
173171
});
174172
const coll = db.collections.get(ns.ns);
@@ -192,7 +190,6 @@ export const NamespaceProvider = createServiceProvider(
192190
ns.collection,
193191
ns.database,
194192
ns.ns,
195-
preferences,
196193
]);
197194

198195
if (!namespaceModel) {

packages/compass-app-stores/src/stores/instance-store.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function createInstancesStore(
7272
await instance.fetchDatabases({ dataService });
7373
await Promise.all(
7474
instance.databases.map((db) => {
75-
return db.fetchCollections({ dataService, preferences });
75+
return db.fetchCollections({ dataService });
7676
})
7777
);
7878
} catch (error) {
@@ -159,7 +159,7 @@ export function createInstancesStore(
159159
await instance.fetchDatabases({ dataService, force: true });
160160
await Promise.allSettled(
161161
instance.databases.map((db) =>
162-
db.fetchCollections({ dataService, preferences, force: true })
162+
db.fetchCollections({ dataService, force: true })
163163
)
164164
);
165165
} catch (err: any) {
@@ -192,8 +192,8 @@ export function createInstancesStore(
192192
console.log('Refresh Namespace Stats');
193193
// We don't care if this fails
194194
await Promise.allSettled([
195-
db?.fetch({ dataService, preferences, force: true }),
196-
coll?.fetch({ dataService, preferences, force: true }),
195+
db?.fetch({ dataService, force: true }),
196+
coll?.fetch({ dataService, force: true }),
197197
]);
198198
} catch (error) {
199199
log.warn(
@@ -239,11 +239,10 @@ export function createInstancesStore(
239239
// collection model updates
240240
console.log('Maybe add and refresh');
241241
await db
242-
.fetch({ dataService, preferences, force: true })
242+
.fetch({ dataService, force: true })
243243
.then(() => {
244244
return coll?.fetch({
245245
dataService,
246-
preferences,
247246
force: true,
248247
// We only need to fetch info in case of new collection being created
249248
fetchInfo: newCollection,
@@ -298,6 +297,7 @@ export function createInstancesStore(
298297
topologyDescription: getTopologyDescription(
299298
dataService.getLastSeenTopology()
300299
),
300+
preferences,
301301
};
302302
const instance = instancesManager.createMongoDBInstanceForConnection(
303303
instanceConnectionId,
@@ -347,7 +347,7 @@ export function createInstancesStore(
347347
connections.getDataServiceForConnection(connectionId);
348348
void instance.databases
349349
.get(databaseId)
350-
?.fetchCollections({ dataService, preferences });
350+
?.fetchCollections({ dataService });
351351
} catch (error) {
352352
log.warn(
353353
mongoLogId(1_001_000_323),
@@ -445,11 +445,9 @@ export function createInstancesStore(
445445
db.collections.remove(coll);
446446
MongoDBInstance.removeAllListeners(coll);
447447
// Update db stats to account for db stats affected by collection stats
448-
void db
449-
?.fetch({ dataService, preferences, force: true })
450-
.catch(() => {
451-
// noop, we ignore stats fetching failures
452-
});
448+
void db?.fetch({ dataService, force: true }).catch(() => {
449+
// noop, we ignore stats fetching failures
450+
});
453451
}
454452
} catch (error) {
455453
log.warn(
@@ -544,7 +542,7 @@ export function createInstancesStore(
544542
void instance.databases
545543
.get(database)
546544
?.collections.get(namespace, '_id')
547-
?.fetch({ dataService, preferences, force: true });
545+
?.fetch({ dataService, force: true });
548546
} catch (error) {
549547
log.warn(
550548
mongoLogId(1_001_000_321),

packages/compass-collection/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { collectionModelLocator } from '@mongodb-js/compass-app-stores/provider';
1010
import type { WorkspaceComponent } from '@mongodb-js/compass-workspaces';
1111
import { workspacesServiceLocator } from '@mongodb-js/compass-workspaces/provider';
12-
import { preferencesLocator } from 'compass-preferences-model/provider';
1312

1413
export const CollectionTabPlugin = registerHadronPlugin(
1514
{
@@ -21,7 +20,6 @@ export const CollectionTabPlugin = registerHadronPlugin(
2120
dataService: dataServiceLocator as DataServiceLocator<keyof DataService>,
2221
collection: collectionModelLocator,
2322
workspaces: workspacesServiceLocator,
24-
preferences: preferencesLocator,
2523
}
2624
);
2725

packages/compass-collection/src/stores/collection-tab.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export type CollectionTabServices = {
3232
collection: Collection;
3333
localAppRegistry: AppRegistry;
3434
workspaces: ReturnType<typeof workspacesServiceLocator>;
35-
preferences: PreferencesAccess;
3635
};
3736

3837
export function activatePlugin(
@@ -45,7 +44,6 @@ export function activatePlugin(
4544
collection: collectionModel,
4645
localAppRegistry,
4746
workspaces,
48-
preferences,
4947
} = services;
5048

5149
if (!collectionModel) {
@@ -87,11 +85,9 @@ export function activatePlugin(
8785
store.dispatch(selectTab('Schema'));
8886
});
8987

90-
void collectionModel
91-
.fetchMetadata({ dataService, preferences })
92-
.then((metadata) => {
93-
store.dispatch(collectionMetadataFetched(metadata));
94-
});
88+
void collectionModel.fetchMetadata({ dataService }).then((metadata) => {
89+
store.dispatch(collectionMetadataFetched(metadata));
90+
});
9591

9692
return {
9793
store,

packages/compass-indexes/src/modules/regular-indexes.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,9 @@ const fetchIndexes = (
424424
// changed, the tab header also gets updated. The check against the
425425
// total is a bit of an optimisation so that we don't also poll the
426426
// collection stats.
427-
collection
428-
.fetch({ dataService, preferences, force: true })
429-
.catch(() => {
430-
/* ignore */
431-
});
427+
collection.fetch({ dataService, force: true }).catch(() => {
428+
/* ignore */
429+
});
432430
}
433431
} catch (err) {
434432
dispatch(fetchIndexesFailed((err as Error).message));

packages/compass-saved-aggregations-queries/src/stores/open-item.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,7 @@ export const openSelectedItem =
600600

601601
export const databaseSelected =
602602
(database: string): SavedQueryAggregationThunkAction<Promise<void>> =>
603-
async (
604-
dispatch,
605-
getState,
606-
{ instancesManager, connections, preferencesAccess: preferences }
607-
) => {
603+
async (dispatch, getState, { instancesManager, connections }) => {
608604
const {
609605
openItem: { selectedDatabase, selectedConnection },
610606
} = getState();
@@ -631,7 +627,7 @@ export const databaseSelected =
631627
throw new Error('Database not found');
632628
}
633629

634-
await db.fetchCollections({ dataService, preferences });
630+
await db.fetchCollections({ dataService });
635631
// Check with the the current value in case db was re-selected while we
636632
// were fetching
637633
if (database === getState().openItem.selectedDatabase) {

packages/compass-workspaces/src/stores/workspaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ const fetchCollectionInfo = (
728728
});
729729

730730
if (coll) {
731-
await coll.fetch({ dataService, preferences });
731+
await coll.fetch({ dataService });
732732
const info = {
733733
isTimeSeries: coll.isTimeSeries,
734734
isReadonly: coll.readonly ?? coll.isView,
@@ -764,7 +764,7 @@ const fetchDatabaseInfo = (
764764
return async (
765765
dispatch,
766766
getState,
767-
{ connections, instancesManager, logger, preferences }
767+
{ connections, instancesManager, logger }
768768
) => {
769769
const { databaseInfo } = getState();
770770
const namespaceId = `${workspaceOptions.connectionId}.${workspaceOptions.namespace}`;
@@ -784,7 +784,7 @@ const fetchDatabaseInfo = (
784784
const db = instance.databases.get(workspaceOptions.namespace);
785785
if (db) {
786786
console.log('DB FETCH');
787-
await db.fetch({ dataService, preferences });
787+
await db.fetch({ dataService });
788788
const info = {
789789
isNonExistent: db.is_non_existent,
790790
};

packages/database-model/index.d.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,26 @@ interface DatabaseProps {
99
statusError: string | null;
1010
collectionsStatus: 'initial' | 'fetching' | 'refreshing' | 'ready' | 'error';
1111
collectionsStatusError: string | null;
12-
collection_count?: number;
13-
document_count?: number;
14-
storage_size?: number;
15-
data_size?: number;
16-
index_count?: number;
17-
index_size?: number;
12+
collection_count: number | undefined;
13+
document_count: number | undefined;
14+
storage_size: number | undefined;
15+
data_size: number | undefined;
16+
index_count: number | undefined;
17+
index_size: number | undefined;
1818
collectionsLength: number;
1919
collections: CollectionCollection;
2020
is_non_existent: boolean;
2121
}
2222

2323
interface Database extends DatabaseProps {
24-
fetch(opts: {
25-
dataService: DataService;
26-
preferences: PreferenceAccess;
27-
force?: boolean;
28-
}): Promise<void>;
24+
fetch(opts: { dataService: DataService; force?: boolean }): Promise<void>;
2925
fetchCollections(opts: {
3026
dataService: DataService;
31-
preferences: PreferenceAccess;
3227
fetchInfo?: boolean;
3328
force?: boolean;
3429
}): Promise<void>;
3530
fetchCollectionsDetails(opts: {
3631
dataService: DataService;
37-
preferences: PreferenceAccess;
3832
nameOnly?: boolean;
3933
force?: boolean;
4034
}): Promise<void>;

0 commit comments

Comments
 (0)