Skip to content

Commit 6b688c9

Browse files
feat(query-bar): allow to specify index hints COMPASS-7829 (#5666)
1 parent 9c13876 commit 6b688c9

File tree

8 files changed

+21
-2
lines changed

8 files changed

+21
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('QueryBar Component', function () {
122122

123123
it('renders the expanded inputs', function () {
124124
const queryInputs = screen.getAllByRole('textbox');
125-
expect(queryInputs.length).to.equal(7);
125+
expect(queryInputs.length).to.equal(8);
126126
});
127127
});
128128

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
140140
'project',
141141
['sort', 'maxTimeMS'],
142142
['collation', 'skip', 'limit'],
143+
'hint',
143144
],
144145
queryChanged,
145146
resultId,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
175175
: queryOptionLabelStyles
176176
}
177177
>
178-
{name}
178+
{optionDefinition.label ?? name}
179179
</Label>
180180
</div>
181181
)}

packages/compass-query-bar/src/constants/query-bar-store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const DEFAULT_FIELD_VALUES = {
1616
project: undefined,
1717
collation: undefined,
1818
sort: undefined,
19+
hint: undefined,
1920
skip: undefined,
2021
limit: undefined,
2122
maxTimeMS: undefined,
@@ -29,6 +30,7 @@ const DEFAULT_QUERY_VALUES = {
2930
project: DEFAULT_PROJECT,
3031
collation: DEFAULT_COLLATION,
3132
sort: DEFAULT_SORT,
33+
hint: null,
3234
skip: DEFAULT_SKIP,
3335
limit: DEFAULT_LIMIT,
3436
maxTimeMS: DEFAULT_MAX_TIME_MS,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ export const OPTION_DEFINITION: {
3333
placeholder: "{ field: -1 } or [['field', -1]]",
3434
link: 'https://docs.mongodb.com/manual/reference/method/cursor.sort/',
3535
},
36+
hint: {
37+
name: 'hint',
38+
label: 'Index Hint',
39+
type: 'document',
40+
placeholder: '{ field: -1 }',
41+
link: 'https://docs.mongodb.com/manual/reference/method/cursor.hint/',
42+
},
3643
collation: {
3744
name: 'collation',
3845
type: 'document',

packages/compass-query-bar/src/constants/query-properties.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const QUERY_PROPERTIES = [
66
'project',
77
'collation',
88
'sort',
9+
'hint',
910
'skip',
1011
'limit',
1112
'maxTimeMS',
@@ -26,6 +27,7 @@ export type QueryFormFields = {
2627
project: FormField<Document>;
2728
collation: FormField<Document>;
2829
sort: FormField<Document>;
30+
hint: FormField<Document>;
2931
skip: FormField<number>;
3032
limit: FormField<number>;
3133
maxTimeMS: FormField<number>;

packages/compass-query-bar/src/stores/query-bar-reducer.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ describe('queryBarReducer', function () {
270270
filter: { _id: { $exists: true } },
271271
collation: null,
272272
sort: null,
273+
hint: null,
273274
skip: 0,
274275
limit: 10,
275276
maxTimeMS: 60000,

packages/compass-query-bar/src/utils/query.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ export function validateField(
149149
}
150150
}
151151

152+
// We don't have a validator for indexes, but indexes share the same structure as
153+
// a sort document, so we're leveraging this to validate the hint field
154+
if (field === 'hint') {
155+
return validate('sort', value);
156+
}
157+
152158
return validated;
153159
}
154160

0 commit comments

Comments
 (0)