-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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 () { |
There was a problem hiding this comment.
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.
aa24eda
to
54c9a9e
Compare
54c9a9e
to
0db8a85
Compare
0db8a85
to
b398740
Compare
|
||
const completions = await autocompleter.autocomplete('db.'); | ||
expect(completions).to.deep.equal([]); | ||
for (let i = 0; i < 2; i++) { |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
b398740
to
0d6be54
Compare
}); | ||
} | ||
|
||
debug({ |
There was a problem hiding this comment.
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
b2282cd
to
d1b8d13
Compare
d1b8d13
to
92e31a5
Compare
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
andMQLDocument which are just (from the point of view of shell-api inside mongosh) aliased to
Document,
Document[]and
Document`. 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>
andmql.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.