Skip to content

Commit 731ac2d

Browse files
committed
react to changes
1 parent 5a0ec67 commit 731ac2d

File tree

5 files changed

+91
-35
lines changed

5 files changed

+91
-35
lines changed

packages/collection-model/lib/model.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
252252
...collStats,
253253
...(collectionInfo && pickCollectionInfo(collectionInfo)),
254254
});
255+
// If the collection is not unprovisioned `is_non_existent` anymore,
256+
// let's update the parent database model to reflect the change.
257+
// This happens when a user tries to insert first document into a
258+
// collection that doesn't exist yet or creates a new collection
259+
// for an unprovisioned database.
260+
if (!this.is_non_existent) {
261+
getParentByType(this, 'Database').set({
262+
is_non_existent: false,
263+
});
264+
}
255265
} catch (err) {
256266
this.set({ status: 'error', statusError: err.message });
257267
throw err;

packages/compass-workspaces/src/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import workspacesReducer, {
1515
getLocalAppRegistryForTab,
1616
cleanupLocalAppRegistries,
1717
connectionDisconnected,
18+
updateDatabaseInfo,
19+
updateCollectionInfo,
1820
} from './stores/workspaces';
1921
import Workspaces from './components';
2022
import { applyMiddleware, createStore } from 'redux';
@@ -89,12 +91,38 @@ export function activateWorkspacePlugin(
8991

9092
addCleanup(cleanupLocalAppRegistries);
9193

92-
const setupInstanceListeners = (instance: MongoDBInstance) => {
94+
const setupInstanceListeners = (
95+
connectionId: string,
96+
instance: MongoDBInstance
97+
) => {
9398
on(instance, 'change:collections._id', (collection: Collection) => {
9499
const { _id: from } = collection.previousAttributes();
95100
store.dispatch(collectionRenamed(from, collection.ns));
96101
});
97102

103+
on(instance, 'change:databases.is_non_existent', (database: Database) => {
104+
const namespaceId = `${connectionId}.${database._id}`;
105+
const databaseInfo = {
106+
isNonExistent: database.is_non_existent,
107+
};
108+
store.dispatch(updateDatabaseInfo(namespaceId, databaseInfo));
109+
});
110+
111+
on(
112+
instance,
113+
'change:collections.is_non_existent',
114+
(collection: Collection) => {
115+
const namespaceId = `${connectionId}.${collection._id}`;
116+
const collectionInfo = {
117+
isTimeSeries: collection.isTimeSeries,
118+
isReadonly: collection.readonly ?? collection.isView,
119+
sourceName: collection.sourceName,
120+
isNonExistent: collection.is_non_existent,
121+
};
122+
store.dispatch(updateCollectionInfo(namespaceId, collectionInfo));
123+
}
124+
);
125+
98126
on(instance, 'remove:collections', (collection: Collection) => {
99127
store.dispatch(collectionRemoved(collection.ns));
100128
});
@@ -104,9 +132,8 @@ export function activateWorkspacePlugin(
104132
});
105133
};
106134

107-
const existingInstances = instancesManager.listMongoDBInstances();
108-
for (const instance of existingInstances.values()) {
109-
setupInstanceListeners(instance);
135+
for (const [connId, instance] of instancesManager.listMongoDBInstances()) {
136+
setupInstanceListeners(connId, instance);
110137
}
111138

112139
on(
@@ -116,7 +143,7 @@ export function activateWorkspacePlugin(
116143
connectionInfoId: ConnectionInfo['id'],
117144
instance: MongoDBInstance
118145
) {
119-
setupInstanceListeners(instance);
146+
setupInstanceListeners(connectionInfoId, instance);
120147
}
121148
);
122149

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,15 @@ export type TabOptions = {
687687
newTab?: boolean;
688688
};
689689

690+
export const updateCollectionInfo = (
691+
namespaceId: string,
692+
info: CollectionTabInfo
693+
): FetchCollectionInfoAction => ({
694+
type: WorkspacesActions.FetchCollectionTabInfo,
695+
namespaceId,
696+
info,
697+
});
698+
690699
const fetchCollectionInfo = (
691700
workspaceOptions: Extract<OpenWorkspaceOptions, { type: 'Collection' }>
692701
): WorkspacesThunkAction<Promise<void>, FetchCollectionInfoAction> => {
@@ -719,16 +728,13 @@ const fetchCollectionInfo = (
719728

720729
if (coll) {
721730
await coll.fetch({ dataService });
722-
dispatch({
723-
type: WorkspacesActions.FetchCollectionTabInfo,
724-
namespaceId,
725-
info: {
726-
isTimeSeries: coll.isTimeSeries,
727-
isReadonly: coll.readonly ?? coll.isView,
728-
sourceName: coll.sourceName,
729-
isNonExistent: coll.is_non_existent,
730-
},
731-
});
731+
const info = {
732+
isTimeSeries: coll.isTimeSeries,
733+
isReadonly: coll.readonly ?? coll.isView,
734+
sourceName: coll.sourceName,
735+
isNonExistent: coll.is_non_existent,
736+
};
737+
dispatch(updateCollectionInfo(namespaceId, info));
732738
}
733739
} catch (err) {
734740
logger.debug(
@@ -742,6 +748,15 @@ const fetchCollectionInfo = (
742748
};
743749
};
744750

751+
export const updateDatabaseInfo = (
752+
namespaceId: string,
753+
info: DatabaseTabInfo
754+
): FetchDatabaseInfoAction => ({
755+
type: WorkspacesActions.FetchDatabaseTabInfo,
756+
namespaceId,
757+
info,
758+
});
759+
745760
const fetchDatabaseInfo = (
746761
workspaceOptions: Extract<OpenWorkspaceOptions, { type: 'Collections' }>
747762
): WorkspacesThunkAction<Promise<void>, FetchDatabaseInfoAction> => {
@@ -768,13 +783,10 @@ const fetchDatabaseInfo = (
768783
const db = instance.databases.get(workspaceOptions.namespace);
769784
if (db) {
770785
await db.fetch({ dataService });
771-
dispatch({
772-
type: WorkspacesActions.FetchDatabaseTabInfo,
773-
namespaceId,
774-
info: {
775-
isNonExistent: db.is_non_existent,
776-
},
777-
});
786+
const info = {
787+
isNonExistent: db.is_non_existent,
788+
};
789+
dispatch(updateDatabaseInfo(namespaceId, info));
778790
}
779791
} catch (err) {
780792
logger.debug(

packages/data-service/src/data-service.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,13 @@ class DataServiceImpl extends WithLogContext implements DataService {
11751175
collName: string
11761176
): Promise<ReturnType<typeof adaptCollectionInfo> | null> {
11771177
const [collInfo] = await this._listCollections(dbName, { name: collName });
1178-
return adaptCollectionInfo({ db: dbName, ...collInfo }) ?? null;
1178+
return (
1179+
adaptCollectionInfo({
1180+
db: dbName,
1181+
...collInfo,
1182+
is_non_existent: false,
1183+
}) ?? null
1184+
);
11791185
}
11801186

11811187
@op(mongoLogId(1_001_000_031))
@@ -1346,10 +1352,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
13461352
// if they were fetched successfully
13471353
[...collectionsFromPrivileges, ...listedCollections],
13481354
'name'
1349-
).map(({ is_non_existent, ...coll }) => ({
1350-
is_non_existent,
1351-
...adaptCollectionInfo({ db: databaseName, ...coll }),
1352-
}));
1355+
).map((coll) => adaptCollectionInfo({ db: databaseName, ...coll }));
13531356

13541357
return collections;
13551358
}
@@ -1452,11 +1455,10 @@ class DataServiceImpl extends WithLogContext implements DataService {
14521455
// if they were fetched successfully
14531456
[...databasesFromRoles, ...databasesFromPrivileges, ...listedDatabases],
14541457
'name'
1455-
).map(({ name, is_non_existent, ...db }) => {
1458+
).map(({ name, ...db }) => {
14561459
return {
14571460
_id: name,
14581461
name,
1459-
is_non_existent,
14601462
...adaptDatabaseInfo(db),
14611463
};
14621464
});

packages/data-service/src/instance-detail-helper.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,17 @@ function adaptBuildInfo(rawBuildInfo: Partial<BuildInfo>) {
359359
}
360360

361361
export function adaptDatabaseInfo(
362-
databaseStats: Partial<DbStats> & Partial<DatabaseInfo>
363-
): Omit<DatabaseDetails, '_id' | 'collections' | 'name' | 'is_non_existent'> {
362+
databaseStats: Partial<DbStats> &
363+
Partial<DatabaseInfo> & { is_non_existent: boolean }
364+
): Omit<DatabaseDetails, '_id' | 'collections' | 'name'> {
364365
return {
365366
collection_count: databaseStats.collections ?? 0,
366367
document_count: databaseStats.objects ?? 0,
367368
index_count: databaseStats.indexes ?? 0,
368369
storage_size: databaseStats.storageSize ?? 0,
369370
data_size: databaseStats.dataSize ?? 0,
370371
index_size: databaseStats.indexSize ?? 0,
372+
is_non_existent: databaseStats.is_non_existent,
371373
};
372374
}
373375

@@ -377,10 +379,12 @@ export function adaptCollectionInfo({
377379
info,
378380
options,
379381
type,
380-
}: CollectionInfoNameOnly & Partial<CollectionInfo> & { db: string }): Omit<
381-
CollectionDetails,
382-
'is_non_existent'
383-
> {
382+
is_non_existent,
383+
}: CollectionInfoNameOnly &
384+
Partial<CollectionInfo> & {
385+
db: string;
386+
is_non_existent: boolean;
387+
}): CollectionDetails {
384388
const ns = toNS(`${db}.${name}`);
385389
const {
386390
collection,
@@ -428,5 +432,6 @@ export function adaptCollectionInfo({
428432
validation: hasValidation
429433
? { validator, validationAction, validationLevel }
430434
: null,
435+
is_non_existent,
431436
};
432437
}

0 commit comments

Comments
 (0)