Skip to content

feat: bypass pulling the schema field from C['schema'] MONGOSH-2170 #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lerouxb
Copy link
Collaborator

@lerouxb lerouxb commented Jun 25, 2025

This is not quite the way this was originally planned to work. For that (and the problems encountered with it) see mongodb-js/mongosh#2478.

In this case shell-api has placeholder types called MQLQuery, MQLPipeline and MQLDocument which are just (from the point of view of shell-api inside mongosh) aliased to Document, Document[]andDocument`. So Typescript never sees complicated generics and then never crashes as it recursively tries to figure out.. something.

So what we're doing here is find/replace on those three types to alias them to something specific to the active collection that we're doing autocomplete for. ie. mql.Query<theCollectionSchema>, mql.Pipeline<theCollectionSchema> and mql.Document<theCollectionSchema>. This way the mql types still see the schema generic parameter and therefore we can autocomplete inferred schema field names, but mongosh is none the wiser.

This does mean that we have to make a customised shell-api per connection/database/collection which is potentially relatively expensive given that the language server has to parse/analyse a lot of code, so to mitigate that I also added caching so that we only recalculate these types when necessary. This was already a "TODO" for the future anyway.

@@ -152,22 +152,12 @@ describe('MongoDBAutocompleter', function () {
// TODO: We need MONGOSH-2170 so that we can use the generated MQL types via
// the Shell API to autocomplete fields in
// ServerSchema[databaseName][collectionName].schema
it.skip('completes a collection field name in a query', async function () {
it('completes a collection field name in a query', async function () {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test got unskipped, showing it now works.

@lerouxb lerouxb force-pushed the replace-mql-types branch from aa24eda to 54c9a9e Compare June 26, 2025 14:15
@lerouxb lerouxb changed the title [WIP] bypass pulling the schema field from C['schema'] feat: bypass pulling the schema field from C['schema'] MONGOSH-2170 Jun 26, 2025
@lerouxb lerouxb force-pushed the replace-mql-types branch from 54c9a9e to 0db8a85 Compare June 26, 2025 14:59
@lerouxb lerouxb marked this pull request as ready for review June 26, 2025 15:00
@lerouxb lerouxb force-pushed the replace-mql-types branch from 0db8a85 to b398740 Compare June 26, 2025 15:08

const completions = await autocompleter.autocomplete('db.');
expect(completions).to.deep.equal([]);
for (let i = 0; i < 2; i++) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did all these twice so that the caching code gets exercised.

}

// then hit a different collection to make sure the caching works
const completions = await autocompleter.autocomplete('db.bar.find({ myF');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then here I check that autocompleting on a different collection does break through the cache.

@lerouxb lerouxb force-pushed the replace-mql-types branch from b398740 to 0d6be54 Compare June 26, 2025 15:10
});
}

debug({
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is handy for debugging the caching:

~/mongo/devtools-shared/packages/mongodb-ts-autocomplete % DEBUG=ts-autocomplete:*,mongodb-ts-autocomplete npm test -- --bail

@lerouxb lerouxb force-pushed the replace-mql-types branch 2 times, most recently from b2282cd to d1b8d13 Compare June 26, 2025 15:17
@lerouxb lerouxb force-pushed the replace-mql-types branch from d1b8d13 to 92e31a5 Compare June 26, 2025 16:38
@lerouxb lerouxb requested a review from addaleax June 26, 2025 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants