Skip to content

Commit 9df3e28

Browse files
committed
WIP
1 parent 0828207 commit 9df3e28

File tree

5 files changed

+109
-23
lines changed

5 files changed

+109
-23
lines changed

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

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,16 @@ describe('store', function () {
22502250
});
22512251

22522252
it('should call find with $bsonSize projection when mongodb version is >= 4.4, not connected to ADF and csfle is disabled', async function () {
2253-
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
2253+
await fetchDocuments(
2254+
dataService,
2255+
{
2256+
serverVersion: '5.0.0',
2257+
isDataLake: false,
2258+
isTimeSeries: false,
2259+
},
2260+
'test.test',
2261+
{}
2262+
);
22542263
expect(find).to.have.been.calledOnce;
22552264
expect(find.getCall(0))
22562265
.to.have.nested.property('args.2.projection')
@@ -2261,8 +2270,11 @@ describe('store', function () {
22612270
findResult = [{ __size: new Int32(42), __doc: { _id: 1 } }];
22622271
const docs = await fetchDocuments(
22632272
dataService,
2264-
'4.0.0',
2265-
false,
2273+
{
2274+
serverVersion: '4.0.0',
2275+
isDataLake: false,
2276+
isTimeSeries: false,
2277+
},
22662278
'test.test',
22672279
{}
22682280
);
@@ -2272,7 +2284,16 @@ describe('store', function () {
22722284
});
22732285

22742286
it('should NOT call find with $bsonSize projection when mongodb version is < 4.4', async function () {
2275-
await fetchDocuments(dataService, '4.0.0', false, 'test.test', {});
2287+
await fetchDocuments(
2288+
dataService,
2289+
{
2290+
serverVersion: '4.0.0',
2291+
isDataLake: false,
2292+
isTimeSeries: false,
2293+
},
2294+
'test.test',
2295+
{}
2296+
);
22762297
expect(find).to.have.been.calledOnce;
22772298
expect(find.getCall(0)).to.have.nested.property(
22782299
'args.2.projection',
@@ -2281,7 +2302,16 @@ describe('store', function () {
22812302
});
22822303

22832304
it('should NOT call find with $bsonSize projection when connected to ADF', async function () {
2284-
await fetchDocuments(dataService, '5.0.0', true, 'test.test', {});
2305+
await fetchDocuments(
2306+
dataService,
2307+
{
2308+
serverVersion: '5.0.0',
2309+
isDataLake: true,
2310+
isTimeSeries: false,
2311+
},
2312+
'test.test',
2313+
{}
2314+
);
22852315
expect(find).to.have.been.calledOnce;
22862316
expect(find.getCall(0)).to.have.nested.property(
22872317
'args.2.projection',
@@ -2291,7 +2321,16 @@ describe('store', function () {
22912321

22922322
it('should NOT call find with $bsonSize projection when csfle is enabled', async function () {
22932323
csfleMode = 'enabled';
2294-
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
2324+
await fetchDocuments(
2325+
dataService,
2326+
{
2327+
serverVersion: '5.0.0',
2328+
isDataLake: false,
2329+
isTimeSeries: false,
2330+
},
2331+
'test.test',
2332+
{}
2333+
);
22952334
expect(find).to.have.been.calledOnce;
22962335
expect(find.getCall(0)).to.have.nested.property(
22972336
'args.2.projection',
@@ -2302,8 +2341,11 @@ describe('store', function () {
23022341
it('should keep user projection when provided', async function () {
23032342
await fetchDocuments(
23042343
dataService,
2305-
'5.0.0',
2306-
false,
2344+
{
2345+
serverVersion: '5.0.0',
2346+
isDataLake: false,
2347+
isTimeSeries: false,
2348+
},
23072349
'test.test',
23082350
{},
23092351
{
@@ -2326,8 +2368,11 @@ describe('store', function () {
23262368

23272369
const docs = await fetchDocuments(
23282370
dataService,
2329-
'5.0.0',
2330-
false,
2371+
{
2372+
serverVersion: '5.0.0',
2373+
isDataLake: false,
2374+
isTimeSeries: false,
2375+
},
23312376
'test.test',
23322377
{}
23332378
);
@@ -2346,7 +2391,16 @@ describe('store', function () {
23462391
find = sinon.stub().rejects(new TypeError('🤷‍♂️'));
23472392

23482393
try {
2349-
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
2394+
await fetchDocuments(
2395+
dataService,
2396+
{
2397+
serverVersion: '5.0.0',
2398+
isDataLake: false,
2399+
isTimeSeries: false,
2400+
},
2401+
'test.test',
2402+
{}
2403+
);
23502404
expect.fail('Expected fetchDocuments to fail with error');
23512405
} catch (err) {
23522406
expect(find).to.have.been.calledOnce;
@@ -2358,7 +2412,16 @@ describe('store', function () {
23582412
find = sinon.stub().rejects(new MongoServerError('Nope'));
23592413

23602414
try {
2361-
await fetchDocuments(dataService, '3.0.0', true, 'test.test', {});
2415+
await fetchDocuments(
2416+
dataService,
2417+
{
2418+
serverVersion: '3.0.0',
2419+
isDataLake: true,
2420+
isTimeSeries: false,
2421+
},
2422+
'test.test',
2423+
{}
2424+
);
23622425
expect.fail('Expected fetchDocuments to fail with error');
23632426
} catch (err) {
23642427
expect(find).to.have.been.calledOnce;

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Reflux from 'reflux';
33
import toNS from 'mongodb-ns';
44
import { findIndex, isEmpty, isEqual } from 'lodash';
55
import semver from 'semver';
6+
import type { Sort } from 'mongodb';
67
import StateMixin from '@mongodb-js/reflux-state-mixin';
78
import type { Element } from 'hadron-document';
89
import { Document } from 'hadron-document';
@@ -109,20 +110,34 @@ const INITIAL_BULK_UPDATE_TEXT = `{
109110
},
110111
}`;
111112

113+
type FetchDocumentsOptions = {
114+
serverVersion: string;
115+
isDataLake: boolean;
116+
isTimeSeries: boolean;
117+
};
118+
119+
function getDefaultSortOrder(isTimeSeries: boolean): Sort {
120+
if (isTimeSeries) {
121+
return { $natural: 1 };
122+
}
123+
124+
return { _id: -1 };
125+
}
126+
112127
export const fetchDocuments: (
113128
dataService: DataService,
114-
serverVersion: string,
115-
isDataLake: boolean,
129+
fetchDocumentsOptions: FetchDocumentsOptions,
116130
...args: Parameters<DataService['find']>
117131
) => Promise<HadronDocument[]> = async (
118132
dataService: DataService,
119-
serverVersion,
120-
isDataLake,
133+
fetchDocumentsOptions,
121134
ns,
122135
filter,
123136
options,
124137
executionOptions
125138
) => {
139+
const { isTimeSeries, isDataLake, serverVersion } = fetchDocumentsOptions;
140+
126141
const canCalculateDocSize =
127142
// $bsonSize is only supported for mongodb >= 4.4.0
128143
semver.gte(serverVersion, '4.4.0') &&
@@ -138,6 +153,7 @@ export const fetchDocuments: (
138153

139154
const modifiedOptions = {
140155
...options,
156+
sort: options?.sort ? options.sort : getDefaultSortOrder(isTimeSeries),
141157
projection: canCalculateDocSize
142158
? { _id: 0, __doc: '$$ROOT', __size: { $bsonSize: '$$ROOT' } }
143159
: options?.projection,
@@ -884,8 +900,11 @@ class CrudStoreImpl
884900
try {
885901
documents = await fetchDocuments(
886902
this.dataService,
887-
this.state.version,
888-
this.state.isDataLake,
903+
{
904+
serverVersion: this.state.version,
905+
isDataLake: this.state.isDataLake,
906+
isTimeSeries: this.state.isTimeSeries,
907+
},
889908
ns,
890909
filter ?? {},
891910
opts as any,
@@ -1712,8 +1731,11 @@ class CrudStoreImpl
17121731
),
17131732
fetchDocuments(
17141733
this.dataService,
1715-
this.state.version,
1716-
this.state.isDataLake,
1734+
{
1735+
serverVersion: this.state.version,
1736+
isDataLake: this.state.isDataLake,
1737+
isTimeSeries: this.state.isTimeSeries,
1738+
},
17171739
ns,
17181740
query.filter ?? {},
17191741
findOptions as any,

packages/compass-crud/src/utils/cancellable-queries.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe('cancellable-queries', function () {
9999
dataService,
100100
preferences,
101101
'cancel.numbers',
102-
null,
102+
undefined,
103103
{
104104
signal,
105105
}
@@ -138,6 +138,7 @@ describe('cancellable-queries', function () {
138138
dataService,
139139
preferences,
140140
'cancel.numbers',
141+
// @ts-expect-error this is deliberately wrong
141142
'this is not a filter',
142143
{
143144
signal,

packages/compass-crud/src/utils/cancellable-queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function countDocuments(
77
dataService: DataService,
88
preferences: PreferencesAccess,
99
ns: string,
10-
filter: BSONObject,
10+
filter: BSONObject | undefined,
1111
{
1212
signal,
1313
skip,

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: "{ field: -1 } or [['field', -1]]",
33+
placeholder: '{ $_id: -1}',
3434
link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/',
3535
},
3636
hint: {

0 commit comments

Comments
 (0)