@@ -113,17 +113,9 @@ const INITIAL_BULK_UPDATE_TEXT = `{
113113type 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-
127119export 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
191183type 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> ;
195191const 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
355361type 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 ?? { } ,
0 commit comments