From 9df3e280010eb3edf8d29028432f301185e822e6 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Mon, 14 Oct 2024 12:47:52 +0100 Subject: [PATCH 1/8] WIP --- .../src/stores/crud-store.spec.ts | 87 ++++++++++++++++--- .../compass-crud/src/stores/crud-store.ts | 38 ++++++-- .../src/utils/cancellable-queries.spec.ts | 3 +- .../src/utils/cancellable-queries.ts | 2 +- .../src/constants/query-option-definition.ts | 2 +- 5 files changed, 109 insertions(+), 23 deletions(-) diff --git a/packages/compass-crud/src/stores/crud-store.spec.ts b/packages/compass-crud/src/stores/crud-store.spec.ts index fcbac4d3d89..d2dd860c8a6 100644 --- a/packages/compass-crud/src/stores/crud-store.spec.ts +++ b/packages/compass-crud/src/stores/crud-store.spec.ts @@ -2250,7 +2250,16 @@ describe('store', function () { }); it('should call find with $bsonSize projection when mongodb version is >= 4.4, not connected to ADF and csfle is disabled', async function () { - await fetchDocuments(dataService, '5.0.0', false, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '5.0.0', + isDataLake: false, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect(find).to.have.been.calledOnce; expect(find.getCall(0)) .to.have.nested.property('args.2.projection') @@ -2261,8 +2270,11 @@ describe('store', function () { findResult = [{ __size: new Int32(42), __doc: { _id: 1 } }]; const docs = await fetchDocuments( dataService, - '4.0.0', - false, + { + serverVersion: '4.0.0', + isDataLake: false, + isTimeSeries: false, + }, 'test.test', {} ); @@ -2272,7 +2284,16 @@ describe('store', function () { }); it('should NOT call find with $bsonSize projection when mongodb version is < 4.4', async function () { - await fetchDocuments(dataService, '4.0.0', false, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '4.0.0', + isDataLake: false, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect(find).to.have.been.calledOnce; expect(find.getCall(0)).to.have.nested.property( 'args.2.projection', @@ -2281,7 +2302,16 @@ describe('store', function () { }); it('should NOT call find with $bsonSize projection when connected to ADF', async function () { - await fetchDocuments(dataService, '5.0.0', true, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '5.0.0', + isDataLake: true, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect(find).to.have.been.calledOnce; expect(find.getCall(0)).to.have.nested.property( 'args.2.projection', @@ -2291,7 +2321,16 @@ describe('store', function () { it('should NOT call find with $bsonSize projection when csfle is enabled', async function () { csfleMode = 'enabled'; - await fetchDocuments(dataService, '5.0.0', false, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '5.0.0', + isDataLake: false, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect(find).to.have.been.calledOnce; expect(find.getCall(0)).to.have.nested.property( 'args.2.projection', @@ -2302,8 +2341,11 @@ describe('store', function () { it('should keep user projection when provided', async function () { await fetchDocuments( dataService, - '5.0.0', - false, + { + serverVersion: '5.0.0', + isDataLake: false, + isTimeSeries: false, + }, 'test.test', {}, { @@ -2326,8 +2368,11 @@ describe('store', function () { const docs = await fetchDocuments( dataService, - '5.0.0', - false, + { + serverVersion: '5.0.0', + isDataLake: false, + isTimeSeries: false, + }, 'test.test', {} ); @@ -2346,7 +2391,16 @@ describe('store', function () { find = sinon.stub().rejects(new TypeError('🤷‍♂️')); try { - await fetchDocuments(dataService, '5.0.0', false, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '5.0.0', + isDataLake: false, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect.fail('Expected fetchDocuments to fail with error'); } catch (err) { expect(find).to.have.been.calledOnce; @@ -2358,7 +2412,16 @@ describe('store', function () { find = sinon.stub().rejects(new MongoServerError('Nope')); try { - await fetchDocuments(dataService, '3.0.0', true, 'test.test', {}); + await fetchDocuments( + dataService, + { + serverVersion: '3.0.0', + isDataLake: true, + isTimeSeries: false, + }, + 'test.test', + {} + ); expect.fail('Expected fetchDocuments to fail with error'); } catch (err) { expect(find).to.have.been.calledOnce; diff --git a/packages/compass-crud/src/stores/crud-store.ts b/packages/compass-crud/src/stores/crud-store.ts index 14ef0e39b79..55a8b9dffe5 100644 --- a/packages/compass-crud/src/stores/crud-store.ts +++ b/packages/compass-crud/src/stores/crud-store.ts @@ -3,6 +3,7 @@ import Reflux from 'reflux'; import toNS from 'mongodb-ns'; import { findIndex, isEmpty, isEqual } from 'lodash'; import semver from 'semver'; +import type { Sort } from 'mongodb'; import StateMixin from '@mongodb-js/reflux-state-mixin'; import type { Element } from 'hadron-document'; import { Document } from 'hadron-document'; @@ -109,20 +110,34 @@ const INITIAL_BULK_UPDATE_TEXT = `{ }, }`; +type FetchDocumentsOptions = { + serverVersion: string; + isDataLake: boolean; + isTimeSeries: boolean; +}; + +function getDefaultSortOrder(isTimeSeries: boolean): Sort { + if (isTimeSeries) { + return { $natural: 1 }; + } + + return { _id: -1 }; +} + export const fetchDocuments: ( dataService: DataService, - serverVersion: string, - isDataLake: boolean, + fetchDocumentsOptions: FetchDocumentsOptions, ...args: Parameters ) => Promise = async ( dataService: DataService, - serverVersion, - isDataLake, + fetchDocumentsOptions, ns, filter, options, executionOptions ) => { + const { isTimeSeries, isDataLake, serverVersion } = fetchDocumentsOptions; + const canCalculateDocSize = // $bsonSize is only supported for mongodb >= 4.4.0 semver.gte(serverVersion, '4.4.0') && @@ -138,6 +153,7 @@ export const fetchDocuments: ( const modifiedOptions = { ...options, + sort: options?.sort ? options.sort : getDefaultSortOrder(isTimeSeries), projection: canCalculateDocSize ? { _id: 0, __doc: '$$ROOT', __size: { $bsonSize: '$$ROOT' } } : options?.projection, @@ -884,8 +900,11 @@ class CrudStoreImpl try { documents = await fetchDocuments( this.dataService, - this.state.version, - this.state.isDataLake, + { + serverVersion: this.state.version, + isDataLake: this.state.isDataLake, + isTimeSeries: this.state.isTimeSeries, + }, ns, filter ?? {}, opts as any, @@ -1712,8 +1731,11 @@ class CrudStoreImpl ), fetchDocuments( this.dataService, - this.state.version, - this.state.isDataLake, + { + serverVersion: this.state.version, + isDataLake: this.state.isDataLake, + isTimeSeries: this.state.isTimeSeries, + }, ns, query.filter ?? {}, findOptions as any, diff --git a/packages/compass-crud/src/utils/cancellable-queries.spec.ts b/packages/compass-crud/src/utils/cancellable-queries.spec.ts index 5db15055ff7..0f49c566677 100644 --- a/packages/compass-crud/src/utils/cancellable-queries.spec.ts +++ b/packages/compass-crud/src/utils/cancellable-queries.spec.ts @@ -99,7 +99,7 @@ describe('cancellable-queries', function () { dataService, preferences, 'cancel.numbers', - null, + undefined, { signal, } @@ -138,6 +138,7 @@ describe('cancellable-queries', function () { dataService, preferences, 'cancel.numbers', + // @ts-expect-error this is deliberately wrong 'this is not a filter', { signal, diff --git a/packages/compass-crud/src/utils/cancellable-queries.ts b/packages/compass-crud/src/utils/cancellable-queries.ts index 8e477c80983..e1ec9ed8058 100644 --- a/packages/compass-crud/src/utils/cancellable-queries.ts +++ b/packages/compass-crud/src/utils/cancellable-queries.ts @@ -7,7 +7,7 @@ export async function countDocuments( dataService: DataService, preferences: PreferencesAccess, ns: string, - filter: BSONObject, + filter: BSONObject | undefined, { signal, skip, diff --git a/packages/compass-query-bar/src/constants/query-option-definition.ts b/packages/compass-query-bar/src/constants/query-option-definition.ts index cffb563d608..3e7861937be 100644 --- a/packages/compass-query-bar/src/constants/query-option-definition.ts +++ b/packages/compass-query-bar/src/constants/query-option-definition.ts @@ -30,7 +30,7 @@ export const OPTION_DEFINITION: { sort: { name: 'sort', type: 'document', - placeholder: "{ field: -1 } or [['field', -1]]", + placeholder: '{ $_id: -1}', link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/', }, hint: { From d3039e9692e86b6e9be0752a226bef1ab23daa69 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 16 Oct 2024 17:10:04 +0100 Subject: [PATCH 2/8] hack hack hack --- packages/collection-model/index.d.ts | 3 +- packages/collection-model/lib/model.js | 1 + .../src/components/crud-toolbar.tsx | 4 ++ .../src/components/document-list.tsx | 3 ++ .../compass-crud/src/stores/crud-store.ts | 43 ++++++++++++------- .../src/components/query-bar-row.tsx | 15 ++++++- .../src/components/query-bar.tsx | 4 ++ .../src/constants/query-option-definition.ts | 2 +- packages/data-service/src/data-service.ts | 2 + packages/data-service/src/types.ts | 1 + 10 files changed, 58 insertions(+), 20 deletions(-) diff --git a/packages/collection-model/index.d.ts b/packages/collection-model/index.d.ts index a1059c38dcc..45f4e5802a4 100644 --- a/packages/collection-model/index.d.ts +++ b/packages/collection-model/index.d.ts @@ -1,4 +1,4 @@ -import type { DataService } from 'mongodb-data-service'; +import type { DataService, CollStatsIndexDetails } from 'mongodb-data-service'; type CollectionMetadata = { /** @@ -76,6 +76,7 @@ interface CollectionProps { free_storage_size: number; index_count: number; index_size: number; + index_details: CollStatsIndexDetails isTimeSeries: boolean; isView: boolean; sourceName: string | null; diff --git a/packages/collection-model/lib/model.js b/packages/collection-model/lib/model.js index 25f4357cfbd..ece186429f0 100644 --- a/packages/collection-model/lib/model.js +++ b/packages/collection-model/lib/model.js @@ -141,6 +141,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), { free_storage_size: 'number', index_count: 'number', index_size: 'number', + index_details: 'object' }, derived: { ns: { diff --git a/packages/compass-crud/src/components/crud-toolbar.tsx b/packages/compass-crud/src/components/crud-toolbar.tsx index 875e7e83456..f41008fae55 100644 --- a/packages/compass-crud/src/components/crud-toolbar.tsx +++ b/packages/compass-crud/src/components/crud-toolbar.tsx @@ -1,5 +1,6 @@ import React, { useCallback, useMemo } from 'react'; import { useTelemetry } from '@mongodb-js/compass-telemetry/provider'; +import type { Sort } from 'mongodb'; import { Body, DropdownMenuButton, @@ -120,6 +121,7 @@ export type CrudToolbarProps = { querySkip?: number; docsPerPage: number; updateMaxDocumentsPerPage: (docsPerPage: number) => void; + defaultSort: Sort; }; const CrudToolbar: React.FunctionComponent = ({ @@ -150,6 +152,7 @@ const CrudToolbar: React.FunctionComponent = ({ querySkip, docsPerPage, updateMaxDocumentsPerPage, + defaultSort, }) => { const track = useTelemetry(); const connectionInfoRef = useConnectionInfoRef(); @@ -192,6 +195,7 @@ const CrudToolbar: React.FunctionComponent = ({ onReset={onResetClicked} showExplainButton={enableExplainPlan} insights={insights} + defaultSort={defaultSort} />
diff --git a/packages/compass-crud/src/components/document-list.tsx b/packages/compass-crud/src/components/document-list.tsx index ba113fb32de..a4e1fae32e4 100644 --- a/packages/compass-crud/src/components/document-list.tsx +++ b/packages/compass-crud/src/components/document-list.tsx @@ -137,6 +137,7 @@ export type DocumentListProps = { | 'resultId' | 'docsPerPage' | 'updateMaxDocumentsPerPage' + | 'defaultSort' >; const DocumentViewComponent: React.FunctionComponent< @@ -318,6 +319,7 @@ const DocumentList: React.FunctionComponent = (props) => { runBulkUpdate, docsPerPage, updateMaxDocumentsPerPage, + defaultSort, } = props; const onOpenInsert = useCallback( @@ -552,6 +554,7 @@ const DocumentList: React.FunctionComponent = (props) => { )} docsPerPage={docsPerPage} updateMaxDocumentsPerPage={handleMaxDocsPerPageChanged} + defaultSort={defaultSort} /> } > diff --git a/packages/compass-crud/src/stores/crud-store.ts b/packages/compass-crud/src/stores/crud-store.ts index 55a8b9dffe5..8cbe27006f0 100644 --- a/packages/compass-crud/src/stores/crud-store.ts +++ b/packages/compass-crud/src/stores/crud-store.ts @@ -113,17 +113,9 @@ const INITIAL_BULK_UPDATE_TEXT = `{ type FetchDocumentsOptions = { serverVersion: string; isDataLake: boolean; - isTimeSeries: boolean; + defaultSort: Sort; }; -function getDefaultSortOrder(isTimeSeries: boolean): Sort { - if (isTimeSeries) { - return { $natural: 1 }; - } - - return { _id: -1 }; -} - export const fetchDocuments: ( dataService: DataService, fetchDocumentsOptions: FetchDocumentsOptions, @@ -136,7 +128,7 @@ export const fetchDocuments: ( options, executionOptions ) => { - const { isTimeSeries, isDataLake, serverVersion } = fetchDocumentsOptions; + const { isDataLake, serverVersion, defaultSort } = fetchDocumentsOptions; const canCalculateDocSize = // $bsonSize is only supported for mongodb >= 4.4.0 @@ -153,7 +145,7 @@ export const fetchDocuments: ( const modifiedOptions = { ...options, - sort: options?.sort ? options.sort : getDefaultSortOrder(isTimeSeries), + sort: options?.sort ? options.sort : defaultSort, projection: canCalculateDocSize ? { _id: 0, __doc: '$$ROOT', __size: { $bsonSize: '$$ROOT' } } : options?.projection, @@ -190,7 +182,11 @@ export const fetchDocuments: ( type CollectionStats = Pick< Collection, - 'document_count' | 'storage_size' | 'free_storage_size' | 'avg_document_size' + | 'document_count' + | 'storage_size' + | 'free_storage_size' + | 'avg_document_size' + | 'index_details' >; const extractCollectionStats = (collection: Collection): CollectionStats => { const coll = collection.toJSON(); @@ -199,9 +195,18 @@ const extractCollectionStats = (collection: Collection): CollectionStats => { storage_size: coll.storage_size, free_storage_size: coll.free_storage_size, avg_document_size: coll.avg_document_size, + index_details: coll.index_details, }; }; +function getDefaultSort(collectionStats: CollectionStats): Sort { + if (collectionStats?.index_details._id_) { + return { _id: -1 }; + } + + return { $natural: -1 }; +} + /** * Default number of docs per page. */ @@ -350,6 +355,7 @@ type CrudState = { bulkDelete: BulkDeleteState; docsPerPage: number; collectionStats: CollectionStats | null; + defaultSort: Sort; }; type CrudStoreActionsOptions = { @@ -424,6 +430,8 @@ class CrudStoreImpl const isDataLake = !!this.instance.dataLake.isDataLake; const isReadonly = !!this.options.isReadonly; + const collectionStats = extractCollectionStats(this.collection); + return { ns: this.options.namespace, collection: toNS(this.options.namespace).collection, @@ -455,7 +463,8 @@ class CrudStoreImpl isUpdatePreviewSupported: this.instance.topologyDescription.type !== 'Single', docsPerPage: this.getInitialDocsPerPage(), - collectionStats: extractCollectionStats(this.collection), + collectionStats, + defaultSort: getDefaultSort(collectionStats), }; } @@ -903,7 +912,7 @@ class CrudStoreImpl { serverVersion: this.state.version, isDataLake: this.state.isDataLake, - isTimeSeries: this.state.isTimeSeries, + defaultSort: this.state.defaultSort, }, ns, filter ?? {}, @@ -1558,8 +1567,10 @@ class CrudStoreImpl } collectionStatsFetched(model: Collection) { + const collectionStats = extractCollectionStats(model); this.setState({ - collectionStats: extractCollectionStats(model), + collectionStats, + defaultSort: getDefaultSort(collectionStats), }); } @@ -1734,7 +1745,7 @@ class CrudStoreImpl { serverVersion: this.state.version, isDataLake: this.state.isDataLake, - isTimeSeries: this.state.isTimeSeries, + defaultSort: this.state.defaultSort, }, ns, query.filter ?? {}, diff --git a/packages/compass-query-bar/src/components/query-bar-row.tsx b/packages/compass-query-bar/src/components/query-bar-row.tsx index 0dbed1ddff8..d75e0e00f7a 100644 --- a/packages/compass-query-bar/src/components/query-bar-row.tsx +++ b/packages/compass-query-bar/src/components/query-bar-row.tsx @@ -1,4 +1,6 @@ import React from 'react'; +import type { Sort } from 'mongodb'; +import { toJSString } from 'mongodb-query-parser'; import { css, spacing } from '@mongodb-js/compass-components'; import type { QueryOption, @@ -20,6 +22,7 @@ type QueryBarRowProps = { onApply?(): void; placeholders?: Record; disabled?: boolean; + defaultSort: Sort; }; export const QueryBarRow: React.FunctionComponent = ({ @@ -27,7 +30,15 @@ export const QueryBarRow: React.FunctionComponent = ({ onApply, placeholders, disabled, + defaultSort, }) => { + const getPlaceholder = (name: QueryOption): string | undefined => { + if (name === 'sort') { + return toJSString(defaultSort)?.replace(/\s+/gm, ' '); + } + return placeholders?.[name]; + }; + return (
{typeof queryOptionsLayout === 'string' ? ( @@ -36,7 +47,7 @@ export const QueryBarRow: React.FunctionComponent = ({ name={queryOptionsLayout} id={`query-bar-option-input-${queryOptionsLayout}`} onApply={onApply} - placeholder={placeholders?.[queryOptionsLayout]} + placeholder={getPlaceholder(queryOptionsLayout)} disabled={disabled} /> ) : ( @@ -46,7 +57,7 @@ export const QueryBarRow: React.FunctionComponent = ({ name={optionName} id={`query-bar-option-input-${optionName}`} onApply={onApply} - placeholder={placeholders?.[optionName]} + placeholder={getPlaceholder(optionName)} disabled={disabled} /> )) diff --git a/packages/compass-query-bar/src/components/query-bar.tsx b/packages/compass-query-bar/src/components/query-bar.tsx index d2383c88092..7c083f87da0 100644 --- a/packages/compass-query-bar/src/components/query-bar.tsx +++ b/packages/compass-query-bar/src/components/query-bar.tsx @@ -1,4 +1,5 @@ import React, { useCallback, useMemo } from 'react'; +import type { Sort } from 'mongodb'; import { Button, Icon, @@ -129,6 +130,7 @@ type QueryBarProps = { isAIFetching?: boolean; onShowAIInputClick: () => void; onHideAIInputClick: () => void; + defaultSort: Sort; }; export const QueryBar: React.FunctionComponent = ({ @@ -158,6 +160,7 @@ export const QueryBar: React.FunctionComponent = ({ isAIFetching = false, onShowAIInputClick, onHideAIInputClick, + defaultSort, }) => { const darkMode = useDarkMode(); const isAIFeatureEnabled = useIsAIFeatureEnabled(); @@ -311,6 +314,7 @@ export const QueryBar: React.FunctionComponent = ({ onApply={onApply} disabled={isAIFetching} placeholders={placeholders} + defaultSort={defaultSort} /> ))}
diff --git a/packages/compass-query-bar/src/constants/query-option-definition.ts b/packages/compass-query-bar/src/constants/query-option-definition.ts index 3e7861937be..73019e7ae9e 100644 --- a/packages/compass-query-bar/src/constants/query-option-definition.ts +++ b/packages/compass-query-bar/src/constants/query-option-definition.ts @@ -30,7 +30,7 @@ export const OPTION_DEFINITION: { sort: { name: 'sort', type: 'document', - placeholder: '{ $_id: -1}', + placeholder: '{ _id: -1 } or { $natural: 1 }', link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/', }, hint: { diff --git a/packages/data-service/src/data-service.ts b/packages/data-service/src/data-service.ts index 2761fd7ba59..4d22fb9c366 100644 --- a/packages/data-service/src/data-service.ts +++ b/packages/data-service/src/data-service.ts @@ -1108,6 +1108,7 @@ class DataServiceImpl extends WithLogContext implements DataService { ], }, }, + indexDetails: { $first: '$storageStats.indexDetails' }, nindexes: { $max: '$storageStats.nindexes' }, }, }, @@ -2581,6 +2582,7 @@ class DataServiceImpl extends WithLogContext implements DataService { storage_size: data.storageSize ?? 0, free_storage_size: data.freeStorageSize ?? 0, index_count: data.nindexes ?? 0, + index_details: data.indexDetails ?? {}, index_size: data.totalIndexSize ?? 0, }; } diff --git a/packages/data-service/src/types.ts b/packages/data-service/src/types.ts index 5c51f09c003..e1bc07504aa 100644 --- a/packages/data-service/src/types.ts +++ b/packages/data-service/src/types.ts @@ -9,6 +9,7 @@ export interface CollectionStats { storage_size: number; free_storage_size: number; index_count: number; + index_details: CollStatsIndexDetails; index_size: number; } From 3762d23b63b502063eb68f6517fa9a44593de733 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 16 Oct 2024 17:11:14 +0100 Subject: [PATCH 3/8] 1 not -1 --- packages/compass-aggregations/src/modules/collections-fields.ts | 2 +- packages/compass-crud/src/stores/crud-store.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compass-aggregations/src/modules/collections-fields.ts b/packages/compass-aggregations/src/modules/collections-fields.ts index c3173741867..a3ad32c240f 100644 --- a/packages/compass-aggregations/src/modules/collections-fields.ts +++ b/packages/compass-aggregations/src/modules/collections-fields.ts @@ -175,7 +175,7 @@ export const fetchCollectionFields = ( namespaceToQuery, {}, { - sort: { $natural: -1 }, + sort: { $natural: 1 }, limit: 1, maxTimeMS: FETCH_SCHEMA_MAX_TIME_MS, } diff --git a/packages/compass-crud/src/stores/crud-store.ts b/packages/compass-crud/src/stores/crud-store.ts index 8cbe27006f0..258468e5126 100644 --- a/packages/compass-crud/src/stores/crud-store.ts +++ b/packages/compass-crud/src/stores/crud-store.ts @@ -204,7 +204,7 @@ function getDefaultSort(collectionStats: CollectionStats): Sort { return { _id: -1 }; } - return { $natural: -1 }; + return { $natural: 1 }; } /** From 143a7f74bb7cdca7cc1db97fda589bddfd5342e0 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 17 Oct 2024 13:35:47 +0100 Subject: [PATCH 4/8] only override sort if the _id_ index exists, otherwise leave it undefined --- .../compass-crud/src/components/crud-toolbar.tsx | 4 ---- .../compass-crud/src/components/document-list.tsx | 3 --- packages/compass-crud/src/stores/crud-store.ts | 15 +++++++-------- .../src/components/query-bar-row.tsx | 15 ++------------- .../src/components/query-bar.tsx | 4 ---- .../src/constants/query-option-definition.ts | 2 +- 6 files changed, 10 insertions(+), 33 deletions(-) diff --git a/packages/compass-crud/src/components/crud-toolbar.tsx b/packages/compass-crud/src/components/crud-toolbar.tsx index f41008fae55..875e7e83456 100644 --- a/packages/compass-crud/src/components/crud-toolbar.tsx +++ b/packages/compass-crud/src/components/crud-toolbar.tsx @@ -1,6 +1,5 @@ import React, { useCallback, useMemo } from 'react'; import { useTelemetry } from '@mongodb-js/compass-telemetry/provider'; -import type { Sort } from 'mongodb'; import { Body, DropdownMenuButton, @@ -121,7 +120,6 @@ export type CrudToolbarProps = { querySkip?: number; docsPerPage: number; updateMaxDocumentsPerPage: (docsPerPage: number) => void; - defaultSort: Sort; }; const CrudToolbar: React.FunctionComponent = ({ @@ -152,7 +150,6 @@ const CrudToolbar: React.FunctionComponent = ({ querySkip, docsPerPage, updateMaxDocumentsPerPage, - defaultSort, }) => { const track = useTelemetry(); const connectionInfoRef = useConnectionInfoRef(); @@ -195,7 +192,6 @@ const CrudToolbar: React.FunctionComponent = ({ onReset={onResetClicked} showExplainButton={enableExplainPlan} insights={insights} - defaultSort={defaultSort} />
diff --git a/packages/compass-crud/src/components/document-list.tsx b/packages/compass-crud/src/components/document-list.tsx index a4e1fae32e4..ba113fb32de 100644 --- a/packages/compass-crud/src/components/document-list.tsx +++ b/packages/compass-crud/src/components/document-list.tsx @@ -137,7 +137,6 @@ export type DocumentListProps = { | 'resultId' | 'docsPerPage' | 'updateMaxDocumentsPerPage' - | 'defaultSort' >; const DocumentViewComponent: React.FunctionComponent< @@ -319,7 +318,6 @@ const DocumentList: React.FunctionComponent = (props) => { runBulkUpdate, docsPerPage, updateMaxDocumentsPerPage, - defaultSort, } = props; const onOpenInsert = useCallback( @@ -554,7 +552,6 @@ const DocumentList: React.FunctionComponent = (props) => { )} docsPerPage={docsPerPage} updateMaxDocumentsPerPage={handleMaxDocsPerPageChanged} - defaultSort={defaultSort} /> } > diff --git a/packages/compass-crud/src/stores/crud-store.ts b/packages/compass-crud/src/stores/crud-store.ts index 258468e5126..34343607b06 100644 --- a/packages/compass-crud/src/stores/crud-store.ts +++ b/packages/compass-crud/src/stores/crud-store.ts @@ -113,7 +113,7 @@ const INITIAL_BULK_UPDATE_TEXT = `{ type FetchDocumentsOptions = { serverVersion: string; isDataLake: boolean; - defaultSort: Sort; + defaultSort?: Sort; }; export const fetchDocuments: ( @@ -199,12 +199,14 @@ const extractCollectionStats = (collection: Collection): CollectionStats => { }; }; -function getDefaultSort(collectionStats: CollectionStats): Sort { +function getDefaultSort( + collectionStats: CollectionStats | null +): Sort | undefined { if (collectionStats?.index_details._id_) { return { _id: -1 }; } - return { $natural: 1 }; + return undefined; } /** @@ -355,7 +357,6 @@ type CrudState = { bulkDelete: BulkDeleteState; docsPerPage: number; collectionStats: CollectionStats | null; - defaultSort: Sort; }; type CrudStoreActionsOptions = { @@ -464,7 +465,6 @@ class CrudStoreImpl this.instance.topologyDescription.type !== 'Single', docsPerPage: this.getInitialDocsPerPage(), collectionStats, - defaultSort: getDefaultSort(collectionStats), }; } @@ -912,7 +912,7 @@ class CrudStoreImpl { serverVersion: this.state.version, isDataLake: this.state.isDataLake, - defaultSort: this.state.defaultSort, + defaultSort: getDefaultSort(this.state.collectionStats), }, ns, filter ?? {}, @@ -1570,7 +1570,6 @@ class CrudStoreImpl const collectionStats = extractCollectionStats(model); this.setState({ collectionStats, - defaultSort: getDefaultSort(collectionStats), }); } @@ -1745,7 +1744,7 @@ class CrudStoreImpl { serverVersion: this.state.version, isDataLake: this.state.isDataLake, - defaultSort: this.state.defaultSort, + defaultSort: getDefaultSort(this.state.collectionStats), }, ns, query.filter ?? {}, diff --git a/packages/compass-query-bar/src/components/query-bar-row.tsx b/packages/compass-query-bar/src/components/query-bar-row.tsx index d75e0e00f7a..0dbed1ddff8 100644 --- a/packages/compass-query-bar/src/components/query-bar-row.tsx +++ b/packages/compass-query-bar/src/components/query-bar-row.tsx @@ -1,6 +1,4 @@ import React from 'react'; -import type { Sort } from 'mongodb'; -import { toJSString } from 'mongodb-query-parser'; import { css, spacing } from '@mongodb-js/compass-components'; import type { QueryOption, @@ -22,7 +20,6 @@ type QueryBarRowProps = { onApply?(): void; placeholders?: Record; disabled?: boolean; - defaultSort: Sort; }; export const QueryBarRow: React.FunctionComponent = ({ @@ -30,15 +27,7 @@ export const QueryBarRow: React.FunctionComponent = ({ onApply, placeholders, disabled, - defaultSort, }) => { - const getPlaceholder = (name: QueryOption): string | undefined => { - if (name === 'sort') { - return toJSString(defaultSort)?.replace(/\s+/gm, ' '); - } - return placeholders?.[name]; - }; - return (
{typeof queryOptionsLayout === 'string' ? ( @@ -47,7 +36,7 @@ export const QueryBarRow: React.FunctionComponent = ({ name={queryOptionsLayout} id={`query-bar-option-input-${queryOptionsLayout}`} onApply={onApply} - placeholder={getPlaceholder(queryOptionsLayout)} + placeholder={placeholders?.[queryOptionsLayout]} disabled={disabled} /> ) : ( @@ -57,7 +46,7 @@ export const QueryBarRow: React.FunctionComponent = ({ name={optionName} id={`query-bar-option-input-${optionName}`} onApply={onApply} - placeholder={getPlaceholder(optionName)} + placeholder={placeholders?.[optionName]} disabled={disabled} /> )) diff --git a/packages/compass-query-bar/src/components/query-bar.tsx b/packages/compass-query-bar/src/components/query-bar.tsx index 7c083f87da0..d2383c88092 100644 --- a/packages/compass-query-bar/src/components/query-bar.tsx +++ b/packages/compass-query-bar/src/components/query-bar.tsx @@ -1,5 +1,4 @@ import React, { useCallback, useMemo } from 'react'; -import type { Sort } from 'mongodb'; import { Button, Icon, @@ -130,7 +129,6 @@ type QueryBarProps = { isAIFetching?: boolean; onShowAIInputClick: () => void; onHideAIInputClick: () => void; - defaultSort: Sort; }; export const QueryBar: React.FunctionComponent = ({ @@ -160,7 +158,6 @@ export const QueryBar: React.FunctionComponent = ({ isAIFetching = false, onShowAIInputClick, onHideAIInputClick, - defaultSort, }) => { const darkMode = useDarkMode(); const isAIFeatureEnabled = useIsAIFeatureEnabled(); @@ -314,7 +311,6 @@ export const QueryBar: React.FunctionComponent = ({ onApply={onApply} disabled={isAIFetching} placeholders={placeholders} - defaultSort={defaultSort} /> ))}
diff --git a/packages/compass-query-bar/src/constants/query-option-definition.ts b/packages/compass-query-bar/src/constants/query-option-definition.ts index 73019e7ae9e..cffb563d608 100644 --- a/packages/compass-query-bar/src/constants/query-option-definition.ts +++ b/packages/compass-query-bar/src/constants/query-option-definition.ts @@ -30,7 +30,7 @@ export const OPTION_DEFINITION: { sort: { name: 'sort', type: 'document', - placeholder: '{ _id: -1 } or { $natural: 1 }', + placeholder: "{ field: -1 } or [['field', -1]]", link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/', }, hint: { From bad5a2313f38b638c39be650bb095e6a89b80fd5 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 17 Oct 2024 13:39:15 +0100 Subject: [PATCH 5/8] oops --- packages/compass-aggregations/src/modules/collections-fields.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-aggregations/src/modules/collections-fields.ts b/packages/compass-aggregations/src/modules/collections-fields.ts index a3ad32c240f..c3173741867 100644 --- a/packages/compass-aggregations/src/modules/collections-fields.ts +++ b/packages/compass-aggregations/src/modules/collections-fields.ts @@ -175,7 +175,7 @@ export const fetchCollectionFields = ( namespaceToQuery, {}, { - sort: { $natural: 1 }, + sort: { $natural: -1 }, limit: 1, maxTimeMS: FETCH_SCHEMA_MAX_TIME_MS, } From d734f1a73521073cd8e42c5156a159e837525a1c Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 17 Oct 2024 20:09:38 +0100 Subject: [PATCH 6/8] type fixes --- packages/collection-model/index.d.ts | 2 +- packages/compass-indexes/test/setup-store.ts | 1 + packages/data-service/src/types.ts | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/collection-model/index.d.ts b/packages/collection-model/index.d.ts index 45f4e5802a4..e7facd3f6b0 100644 --- a/packages/collection-model/index.d.ts +++ b/packages/collection-model/index.d.ts @@ -76,7 +76,7 @@ interface CollectionProps { free_storage_size: number; index_count: number; index_size: number; - index_details: CollStatsIndexDetails + index_details: Record; isTimeSeries: boolean; isView: boolean; sourceName: string | null; diff --git a/packages/compass-indexes/test/setup-store.ts b/packages/compass-indexes/test/setup-store.ts index 2b8a7676ece..b1e3be5f02b 100644 --- a/packages/compass-indexes/test/setup-store.ts +++ b/packages/compass-indexes/test/setup-store.ts @@ -73,6 +73,7 @@ const NOOP_DATA_PROVIDER: IndexesDataService = { free_storage_size: 0, index_count: 0, index_size: 0, + index_details: {}, name: collectionName, ns: `${databaseName}.${collectionName}`, storage_size: 0, diff --git a/packages/data-service/src/types.ts b/packages/data-service/src/types.ts index e1bc07504aa..9393b36206c 100644 --- a/packages/data-service/src/types.ts +++ b/packages/data-service/src/types.ts @@ -9,7 +9,7 @@ export interface CollectionStats { storage_size: number; free_storage_size: number; index_count: number; - index_details: CollStatsIndexDetails; + index_details: Record; index_size: number; } From d3d793d9b3713397948652e69b84b3cb9f4e945e Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Fri, 18 Oct 2024 08:41:14 +0100 Subject: [PATCH 7/8] mongodb dep --- package-lock.json | 2 ++ packages/compass-crud/package.json | 1 + 2 files changed, 3 insertions(+) diff --git a/package-lock.json b/package-lock.json index 71e5a8b4954..37d8c926ae2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44097,6 +44097,7 @@ "hadron-type-checker": "^7.2.3", "jsondiffpatch": "^0.5.0", "lodash": "^4.17.21", + "mongodb": "^6.9.0", "mongodb-data-service": "^22.23.5", "mongodb-ns": "^2.4.2", "mongodb-query-parser": "^4.2.3", @@ -56149,6 +56150,7 @@ "jsondiffpatch": "^0.5.0", "lodash": "^4.17.21", "mocha": "^10.2.0", + "mongodb": "^6.9.0", "mongodb-data-service": "^22.23.5", "mongodb-instance-model": "^12.24.5", "mongodb-ns": "^2.4.2", diff --git a/packages/compass-crud/package.json b/packages/compass-crud/package.json index f2a6da20246..a979c49a58e 100644 --- a/packages/compass-crud/package.json +++ b/packages/compass-crud/package.json @@ -94,6 +94,7 @@ "hadron-type-checker": "^7.2.3", "jsondiffpatch": "^0.5.0", "lodash": "^4.17.21", + "mongodb": "^6.9.0", "mongodb-data-service": "^22.23.5", "mongodb-ns": "^2.4.2", "mongodb-query-parser": "^4.2.3", From 93b557e3783d89740e7c9e75fbb29d4126d35573 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Fri, 18 Oct 2024 09:20:10 +0100 Subject: [PATCH 8/8] tests --- .../src/stores/crud-store.spec.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/compass-crud/src/stores/crud-store.spec.ts b/packages/compass-crud/src/stores/crud-store.spec.ts index d2dd860c8a6..6a1e53ea4d6 100644 --- a/packages/compass-crud/src/stores/crud-store.spec.ts +++ b/packages/compass-crud/src/stores/crud-store.spec.ts @@ -134,6 +134,7 @@ const mockCollection = { document_count: 10, free_storage_size: 10, storage_size: 20, + index_details: {}, fetchMetadata() { return Promise.resolve(defaultMetadata); }, @@ -334,6 +335,7 @@ describe('store', function () { avg_document_size: 1, document_count: 10, free_storage_size: 10, + index_details: {}, storage_size: 20, }, }); @@ -2255,7 +2257,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2273,7 +2275,7 @@ describe('store', function () { { serverVersion: '4.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2289,7 +2291,7 @@ describe('store', function () { { serverVersion: '4.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2307,7 +2309,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: true, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2326,7 +2328,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2344,7 +2346,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {}, @@ -2371,7 +2373,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2396,7 +2398,7 @@ describe('store', function () { { serverVersion: '5.0.0', isDataLake: false, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {} @@ -2417,7 +2419,7 @@ describe('store', function () { { serverVersion: '3.0.0', isDataLake: true, - isTimeSeries: false, + defaultSort: undefined, }, 'test.test', {}