Skip to content

feat: add mongodb-ts-autocomplete package MONGOSH-2034 #520

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 45 commits into
base: main
Choose a base branch
from

Conversation

lerouxb
Copy link
Collaborator

@lerouxb lerouxb commented Mar 19, 2025

Currently still works with fake shell api and bson expression types, but the bson types are at least the real ones.

TODO: separate tickets

TODO: this PR

@lerouxb lerouxb force-pushed the add-autocomplete-package branch from 24c2ad2 to 8b624a4 Compare March 19, 2025 12:32
@lerouxb lerouxb marked this pull request as draft March 19, 2025 12:33
@lerouxb lerouxb added the wip label Mar 19, 2025
position = code.length;
}

// TODO: we're now compiling the source twice: autocomplete will also
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 kind of thing is IMHO an argument for just merging the ts-autocomplete package into this one. Not insurmountable, but it becomes a non-issue if it is just one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(but this would also totally be a micro-optimisation at this stage)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Again - I'll just remove the TODO before merging. Just thought I'd point this out to reviewers. I think this is OK since it is just a line or small multi-line snippet of code and triggering autocomplete is not exactly something that happens in a tight loop. We can optimise it later if we ever have to.

Just wanted to point out that I did think about it 😆

);
activeConnection.setDatabaseCollectionNames(databaseName, collectionNames);

// TODO: the problem with doing it this way is that, while the collection
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 think this is probably fine. Getting the sample docs and calculating a schema is what's relatively expensive, probably not walking it and writing a few lines of typescript. Only way this becomes an issue is if you have a really large collection schema or very many databases/collections in the same server schema. Each individual database's list of collections is lazy loaded, though and also each collection's schema. So you'd likely have to have quite the long lived and meandering session for that to build up.

We would likely load the list of databases or collections for at least one database early on, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Similar to comments above, I think I'll remove this comment before merging. Just wanted to point out that I'm aware.

// names from a listCollections() or it could just be all the ones we've
// auto-completed for. The schemas can come from the autocompletion context.
// This can probably be added to the autocompletion context.
this.collectionNames = new Set();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thinking about whether DatabaseSchema and ConnectionSchema can just be merged into the autocompletion context and then it keeps track of the known database names and for each database the known collection names.

@lerouxb lerouxb force-pushed the add-autocomplete-package branch from 185e497 to 6d9a9d7 Compare March 20, 2025 15:47
private readonly databaseSchemas: Record<string, DatabaseSchema>;

constructor() {
// TODO: this is kinda the only real reason for this class: So we can keep
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'll remove the TODO before merging, just keen on hearing people's thoughts first.

private collectionSchemas: Record<string, JSONSchema | undefined>;

constructor() {
// TODO: this is kinda the only real reason for this class: So we can keep
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'll remove the TODO before merging, just keen on hearing people's thoughts first.


dbRef: new DBRef('namespace', new ObjectId('642d76b4b7ebfab15d3c4a78')), // not actually a separate type, just a convention

// TODO: what about arrays of objects or arrays of arrays or heterogynous types in general
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 can ticket this, but it is the kind of thing that we'll probably never do. It doesn't break in those cases: At worst you just get you just get foo: any. It might start to matter more if we use this in vscode or compass, but it can be for that project to enhance this.

@lerouxb lerouxb marked this pull request as ready for review April 2, 2025 15:40
@lerouxb lerouxb changed the title feat: add autocomplete package MONGOSH-2034 feat: add mongodb-ts-autocomplete package MONGOSH-2034 Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants