Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions packages/collection-model/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ interface CollectionProps {
specialish: boolean;
normal: boolean;
readonly: boolean;
view_on: string;
view_on: string | null;
collation: unknown;
pipeline: unknown[];
validation: unknown;
is_capped: boolean;
document_count: number;
document_size: number;
avg_document_size: number;
storage_size: number;
free_storage_size: number;
index_count: number;
index_size: number;
is_capped: boolean | undefined;
document_count: number | undefined;
document_size: number | undefined;
avg_document_size: number | undefined;
storage_size: number | undefined;
free_storage_size: number | undefined;
index_count: number | undefined;
index_size: number | undefined;
isTimeSeries: boolean;
isView: boolean;
/** Only relevant for a view and identifies collection/view from which this view was created. */
Expand All @@ -85,7 +85,13 @@ interface CollectionProps {
is_non_existent: boolean;
}

type CollectionDataService = Pick<DataService, 'collectionStats' | 'collectionInfo' | 'listCollections' | 'isListSearchIndexesSupported'>;
type CollectionDataService = Pick<
DataService,
| 'collectionStats'
| 'collectionInfo'
| 'listCollections'
| 'isListSearchIndexesSupported'
>;

interface Collection extends CollectionProps {
fetch(opts: {
Expand All @@ -106,7 +112,10 @@ interface Collection extends CollectionProps {
}

interface CollectionCollection extends Array<Collection> {
fetch(opts: { dataService: CollectionDataService; fetchInfo?: boolean }): Promise<void>;
fetch(opts: {
dataService: CollectionDataService;
fetchInfo?: boolean;
}): Promise<void>;
toJSON(opts?: { derived: boolean }): Array<CollectionProps>;
at(index: number): Collection | undefined;
get(id: string, key?: '_id' | 'name'): Collection | undefined;
Expand Down
34 changes: 29 additions & 5 deletions packages/collection-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ function pickCollectionInfo({
fle2,
is_non_existent,
}) {
return { type, readonly, view_on, collation, pipeline, validation, clustered, fle2, is_non_existent };
return {
type,
readonly,
view_on,
collation,
pipeline,
validation,
clustered,
fle2,
is_non_existent,
};
}

/**
Expand Down Expand Up @@ -232,18 +242,30 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
},

/**
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
* @param {{
* dataService: import('mongodb-data-service').DataService,
* fetchInfo: boolean,
* force: boolean
* }} options
* @returns
*/
async fetch({ dataService, fetchInfo = true, force = false }) {
if (!shouldFetch(this.status, force)) {
return;
}

const shouldFetchDbAndCollStats = getParentByType(
this,
'Instance'
).shouldFetchDbAndCollStats;

try {
const newStatus = this.status === 'initial' ? 'fetching' : 'refreshing';
this.set({ status: newStatus });
const [collStats, collectionInfo] = await Promise.all([
dataService.collectionStats(this.database, this.name),
shouldFetchDbAndCollStats
? dataService.collectionStats(this.database, this.name)
: null,
fetchInfo ? dataService.collectionInfo(this.database, this.name) : null,
]);
this.set({
Expand All @@ -255,7 +277,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
// If the collection is not unprovisioned `is_non_existent` anymore,
// let's update the parent database model to reflect the change.
// This happens when a user tries to insert first document into a
// collection that doesn't exist yet or creates a new collection
// collection that doesn't exist yet or creates a new collection
// for an unprovisioned database.
if (!this.is_non_existent) {
getParentByType(this, 'Database').set({
Expand All @@ -271,7 +293,9 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
/**
* Fetches collection info and returns a special format of collection metadata
* that events like open-in-new-tab, select-namespace, edit-view require
* @param {{ dataService: import('mongodb-data-service').DataService }} dataService
* @param {{
* dataService: import('mongodb-data-service').DataService,
* }} options
*/
async fetchMetadata({ dataService }) {
try {
Expand Down
37 changes: 26 additions & 11 deletions packages/compass-aggregations/src/modules/aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { runPipelineConfirmationDescription } from '../utils/modal-descriptions'
import type { MongoDBInstance } from 'mongodb-instance-model';
import type { DataService } from '../modules/data-service';
import toNS from 'mongodb-ns';
import type { PreferencesAccess } from 'compass-preferences-model';

const WRITE_STAGE_LINK = {
$merge:
Expand Down Expand Up @@ -225,12 +226,18 @@ const reducer: Reducer<State, Action> = (state = INITIAL_STATE, action) => {
return state;
};

const confirmWriteOperationIfNeeded = async (
instance: MongoDBInstance,
dataService: DataService,
namespace: string,
pipeline: Document[]
) => {
const confirmWriteOperationIfNeeded = async ({
instance,
dataService,
namespace,
pipeline,
}: {
instance: MongoDBInstance;
dataService: DataService;
namespace: string;
pipeline: Document[];
preferences: PreferencesAccess;
}) => {
const lastStageOperator = getStageOperator(pipeline[pipeline.length - 1]);
let typeOfWrite;

Expand Down Expand Up @@ -289,17 +296,25 @@ export const runAggregation = (): PipelineBuilderThunkAction<Promise<void>> => {
return async (
dispatch,
getState,
{ pipelineBuilder, instance, dataService, track, connectionInfoRef }
{
pipelineBuilder,
instance,
dataService,
track,
connectionInfoRef,
preferences,
}
) => {
const pipeline = getPipelineFromBuilderState(getState(), pipelineBuilder);

if (
!(await confirmWriteOperationIfNeeded(
!(await confirmWriteOperationIfNeeded({
instance,
dataService,
getState().namespace,
pipeline
))
namespace: getState().namespace,
pipeline,
preferences,
}))
) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions packages/compass-app-stores/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"mongodb-collection-model": "^5.25.8",
"mongodb-database-model": "^2.25.8",
"mongodb-instance-model": "^12.26.8",
"compass-preferences-model": "^2.33.8",
"mongodb-ns": "^2.4.2",
"react": "^17.0.2"
},
Expand Down
14 changes: 13 additions & 1 deletion packages/compass-app-stores/src/instances-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import {
} from './instances-manager';
import { MongoDBInstance } from 'mongodb-instance-model';
import { createDefaultConnectionInfo } from '@mongodb-js/testing-library-compass';
import {
type PreferencesAccess,
createSandboxFromDefaultPreferences,
} from 'compass-preferences-model';

const TEST_CONNECTION_INFO = createDefaultConnectionInfo();

describe('InstancesManager', function () {
let instancesManager: MongoDBInstancesManager;
beforeEach(function () {
let preferences: PreferencesAccess;
beforeEach(async function () {
instancesManager = new MongoDBInstancesManager();
preferences = await createSandboxFromDefaultPreferences();
});

it('should be able to create and return a MongoDB instance', function () {
Expand All @@ -27,6 +33,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
expect(instance).to.be.instanceOf(MongoDBInstance);
Expand All @@ -44,6 +51,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
expect(instancesManager.listMongoDBInstances()).to.have.lengthOf(1);
Expand All @@ -66,6 +74,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
expect(onInstanceCreatedStub).to.be.calledOnceWithExactly(
Expand All @@ -89,6 +98,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
expect(() =>
Expand All @@ -108,6 +118,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
expect(() =>
Expand Down Expand Up @@ -138,6 +149,7 @@ describe('InstancesManager', function () {
servers: [],
setName: '',
},
preferences,
}
);
instancesManager.removeMongoDBInstanceForConnection(
Expand Down
6 changes: 6 additions & 0 deletions packages/compass-app-stores/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { createInstancesStore } from './stores';
import type { ConnectionsService } from '@mongodb-js/compass-connections/provider';
import { connectionsLocator } from '@mongodb-js/compass-connections/provider';
import { type MongoDBInstancesManager } from './instances-manager';
import type { PreferencesAccess } from 'compass-preferences-model';
import { preferencesLocator } from 'compass-preferences-model/provider';

interface MongoDBInstancesProviderProps {
children?: React.ReactNode;
Expand Down Expand Up @@ -37,10 +39,12 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
{
connections,
logger,
preferences,
globalAppRegistry,
}: {
connections: ConnectionsService;
logger: Logger;
preferences: PreferencesAccess;
globalAppRegistry: AppRegistry;
},
helpers: ActivateHelpers
Expand All @@ -49,6 +53,7 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
{
connections,
logger,
preferences,
globalAppRegistry,
},
helpers
Expand All @@ -63,6 +68,7 @@ export const CompassInstanceStorePlugin = registerHadronPlugin(
},
{
logger: createLoggerLocator('COMPASS-INSTANCE-STORE'),
preferences: preferencesLocator,
connections: connectionsLocator,
}
);
Loading
Loading