Skip to content

Commit d3039e9

Browse files
committed
hack hack hack
1 parent 9df3e28 commit d3039e9

File tree

10 files changed

+58
-20
lines changed

10 files changed

+58
-20
lines changed

packages/collection-model/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DataService } from 'mongodb-data-service';
1+
import type { DataService, CollStatsIndexDetails } from 'mongodb-data-service';
22

33
type CollectionMetadata = {
44
/**
@@ -76,6 +76,7 @@ interface CollectionProps {
7676
free_storage_size: number;
7777
index_count: number;
7878
index_size: number;
79+
index_details: CollStatsIndexDetails
7980
isTimeSeries: boolean;
8081
isView: boolean;
8182
sourceName: string | null;

packages/collection-model/lib/model.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
141141
free_storage_size: 'number',
142142
index_count: 'number',
143143
index_size: 'number',
144+
index_details: 'object'
144145
},
145146
derived: {
146147
ns: {

packages/compass-crud/src/components/crud-toolbar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useCallback, useMemo } from 'react';
22
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
3+
import type { Sort } from 'mongodb';
34
import {
45
Body,
56
DropdownMenuButton,
@@ -120,6 +121,7 @@ export type CrudToolbarProps = {
120121
querySkip?: number;
121122
docsPerPage: number;
122123
updateMaxDocumentsPerPage: (docsPerPage: number) => void;
124+
defaultSort: Sort;
123125
};
124126

125127
const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
@@ -150,6 +152,7 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
150152
querySkip,
151153
docsPerPage,
152154
updateMaxDocumentsPerPage,
155+
defaultSort,
153156
}) => {
154157
const track = useTelemetry();
155158
const connectionInfoRef = useConnectionInfoRef();
@@ -192,6 +195,7 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
192195
onReset={onResetClicked}
193196
showExplainButton={enableExplainPlan}
194197
insights={insights}
198+
defaultSort={defaultSort}
195199
/>
196200
</div>
197201
<div className={crudBarStyles}>

packages/compass-crud/src/components/document-list.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export type DocumentListProps = {
137137
| 'resultId'
138138
| 'docsPerPage'
139139
| 'updateMaxDocumentsPerPage'
140+
| 'defaultSort'
140141
>;
141142

142143
const DocumentViewComponent: React.FunctionComponent<
@@ -318,6 +319,7 @@ const DocumentList: React.FunctionComponent<DocumentListProps> = (props) => {
318319
runBulkUpdate,
319320
docsPerPage,
320321
updateMaxDocumentsPerPage,
322+
defaultSort,
321323
} = props;
322324

323325
const onOpenInsert = useCallback(
@@ -552,6 +554,7 @@ const DocumentList: React.FunctionComponent<DocumentListProps> = (props) => {
552554
)}
553555
docsPerPage={docsPerPage}
554556
updateMaxDocumentsPerPage={handleMaxDocsPerPageChanged}
557+
defaultSort={defaultSort}
555558
/>
556559
}
557560
>

packages/compass-crud/src/stores/crud-store.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,9 @@ const INITIAL_BULK_UPDATE_TEXT = `{
113113
type FetchDocumentsOptions = {
114114
serverVersion: string;
115115
isDataLake: boolean;
116-
isTimeSeries: boolean;
116+
defaultSort: Sort;
117117
};
118118

119-
function getDefaultSortOrder(isTimeSeries: boolean): Sort {
120-
if (isTimeSeries) {
121-
return { $natural: 1 };
122-
}
123-
124-
return { _id: -1 };
125-
}
126-
127119
export const fetchDocuments: (
128120
dataService: DataService,
129121
fetchDocumentsOptions: FetchDocumentsOptions,
@@ -136,7 +128,7 @@ export const fetchDocuments: (
136128
options,
137129
executionOptions
138130
) => {
139-
const { isTimeSeries, isDataLake, serverVersion } = fetchDocumentsOptions;
131+
const { isDataLake, serverVersion, defaultSort } = fetchDocumentsOptions;
140132

141133
const canCalculateDocSize =
142134
// $bsonSize is only supported for mongodb >= 4.4.0
@@ -153,7 +145,7 @@ export const fetchDocuments: (
153145

154146
const modifiedOptions = {
155147
...options,
156-
sort: options?.sort ? options.sort : getDefaultSortOrder(isTimeSeries),
148+
sort: options?.sort ? options.sort : defaultSort,
157149
projection: canCalculateDocSize
158150
? { _id: 0, __doc: '$$ROOT', __size: { $bsonSize: '$$ROOT' } }
159151
: options?.projection,
@@ -190,7 +182,11 @@ export const fetchDocuments: (
190182

191183
type CollectionStats = Pick<
192184
Collection,
193-
'document_count' | 'storage_size' | 'free_storage_size' | 'avg_document_size'
185+
| 'document_count'
186+
| 'storage_size'
187+
| 'free_storage_size'
188+
| 'avg_document_size'
189+
| 'index_details'
194190
>;
195191
const extractCollectionStats = (collection: Collection): CollectionStats => {
196192
const coll = collection.toJSON();
@@ -199,9 +195,18 @@ const extractCollectionStats = (collection: Collection): CollectionStats => {
199195
storage_size: coll.storage_size,
200196
free_storage_size: coll.free_storage_size,
201197
avg_document_size: coll.avg_document_size,
198+
index_details: coll.index_details,
202199
};
203200
};
204201

202+
function getDefaultSort(collectionStats: CollectionStats): Sort {
203+
if (collectionStats?.index_details._id_) {
204+
return { _id: -1 };
205+
}
206+
207+
return { $natural: -1 };
208+
}
209+
205210
/**
206211
* Default number of docs per page.
207212
*/
@@ -350,6 +355,7 @@ type CrudState = {
350355
bulkDelete: BulkDeleteState;
351356
docsPerPage: number;
352357
collectionStats: CollectionStats | null;
358+
defaultSort: Sort;
353359
};
354360

355361
type CrudStoreActionsOptions = {
@@ -424,6 +430,8 @@ class CrudStoreImpl
424430
const isDataLake = !!this.instance.dataLake.isDataLake;
425431
const isReadonly = !!this.options.isReadonly;
426432

433+
const collectionStats = extractCollectionStats(this.collection);
434+
427435
return {
428436
ns: this.options.namespace,
429437
collection: toNS(this.options.namespace).collection,
@@ -455,7 +463,8 @@ class CrudStoreImpl
455463
isUpdatePreviewSupported:
456464
this.instance.topologyDescription.type !== 'Single',
457465
docsPerPage: this.getInitialDocsPerPage(),
458-
collectionStats: extractCollectionStats(this.collection),
466+
collectionStats,
467+
defaultSort: getDefaultSort(collectionStats),
459468
};
460469
}
461470

@@ -903,7 +912,7 @@ class CrudStoreImpl
903912
{
904913
serverVersion: this.state.version,
905914
isDataLake: this.state.isDataLake,
906-
isTimeSeries: this.state.isTimeSeries,
915+
defaultSort: this.state.defaultSort,
907916
},
908917
ns,
909918
filter ?? {},
@@ -1558,8 +1567,10 @@ class CrudStoreImpl
15581567
}
15591568

15601569
collectionStatsFetched(model: Collection) {
1570+
const collectionStats = extractCollectionStats(model);
15611571
this.setState({
1562-
collectionStats: extractCollectionStats(model),
1572+
collectionStats,
1573+
defaultSort: getDefaultSort(collectionStats),
15631574
});
15641575
}
15651576

@@ -1734,7 +1745,7 @@ class CrudStoreImpl
17341745
{
17351746
serverVersion: this.state.version,
17361747
isDataLake: this.state.isDataLake,
1737-
isTimeSeries: this.state.isTimeSeries,
1748+
defaultSort: this.state.defaultSort,
17381749
},
17391750
ns,
17401751
query.filter ?? {},

packages/compass-query-bar/src/components/query-bar-row.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React from 'react';
2+
import type { Sort } from 'mongodb';
3+
import { toJSString } from 'mongodb-query-parser';
24
import { css, spacing } from '@mongodb-js/compass-components';
35
import type {
46
QueryOption,
@@ -20,14 +22,23 @@ type QueryBarRowProps = {
2022
onApply?(): void;
2123
placeholders?: Record<QueryProperty, string>;
2224
disabled?: boolean;
25+
defaultSort: Sort;
2326
};
2427

2528
export const QueryBarRow: React.FunctionComponent<QueryBarRowProps> = ({
2629
queryOptionsLayout,
2730
onApply,
2831
placeholders,
2932
disabled,
33+
defaultSort,
3034
}) => {
35+
const getPlaceholder = (name: QueryOption): string | undefined => {
36+
if (name === 'sort') {
37+
return toJSString(defaultSort)?.replace(/\s+/gm, ' ');
38+
}
39+
return placeholders?.[name];
40+
};
41+
3142
return (
3243
<div className={rowStyles}>
3344
{typeof queryOptionsLayout === 'string' ? (
@@ -36,7 +47,7 @@ export const QueryBarRow: React.FunctionComponent<QueryBarRowProps> = ({
3647
name={queryOptionsLayout}
3748
id={`query-bar-option-input-${queryOptionsLayout}`}
3849
onApply={onApply}
39-
placeholder={placeholders?.[queryOptionsLayout]}
50+
placeholder={getPlaceholder(queryOptionsLayout)}
4051
disabled={disabled}
4152
/>
4253
) : (
@@ -46,7 +57,7 @@ export const QueryBarRow: React.FunctionComponent<QueryBarRowProps> = ({
4657
name={optionName}
4758
id={`query-bar-option-input-${optionName}`}
4859
onApply={onApply}
49-
placeholder={placeholders?.[optionName]}
60+
placeholder={getPlaceholder(optionName)}
5061
disabled={disabled}
5162
/>
5263
))

packages/compass-query-bar/src/components/query-bar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useCallback, useMemo } from 'react';
2+
import type { Sort } from 'mongodb';
23
import {
34
Button,
45
Icon,
@@ -129,6 +130,7 @@ type QueryBarProps = {
129130
isAIFetching?: boolean;
130131
onShowAIInputClick: () => void;
131132
onHideAIInputClick: () => void;
133+
defaultSort: Sort;
132134
};
133135

134136
export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
@@ -158,6 +160,7 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
158160
isAIFetching = false,
159161
onShowAIInputClick,
160162
onHideAIInputClick,
163+
defaultSort,
161164
}) => {
162165
const darkMode = useDarkMode();
163166
const isAIFeatureEnabled = useIsAIFeatureEnabled();
@@ -311,6 +314,7 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
311314
onApply={onApply}
312315
disabled={isAIFetching}
313316
placeholders={placeholders}
317+
defaultSort={defaultSort}
314318
/>
315319
))}
316320
</div>

packages/compass-query-bar/src/constants/query-option-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const OPTION_DEFINITION: {
3030
sort: {
3131
name: 'sort',
3232
type: 'document',
33-
placeholder: '{ $_id: -1}',
33+
placeholder: '{ _id: -1 } or { $natural: 1 }',
3434
link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/',
3535
},
3636
hint: {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
11081108
],
11091109
},
11101110
},
1111+
indexDetails: { $first: '$storageStats.indexDetails' },
11111112
nindexes: { $max: '$storageStats.nindexes' },
11121113
},
11131114
},
@@ -2581,6 +2582,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
25812582
storage_size: data.storageSize ?? 0,
25822583
free_storage_size: data.freeStorageSize ?? 0,
25832584
index_count: data.nindexes ?? 0,
2585+
index_details: data.indexDetails ?? {},
25842586
index_size: data.totalIndexSize ?? 0,
25852587
};
25862588
}

packages/data-service/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface CollectionStats {
99
storage_size: number;
1010
free_storage_size: number;
1111
index_count: number;
12+
index_details: CollStatsIndexDetails;
1213
index_size: number;
1314
}
1415

0 commit comments

Comments
 (0)