Skip to content

Commit aa24eda

Browse files
committed
bypass pulling the schema field from C['schema']
1 parent ee6fd50 commit aa24eda

File tree

7 files changed

+67
-24
lines changed

7 files changed

+67
-24
lines changed

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/mongodb-ts-autocomplete/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@
6363
"@mongodb-js/mocha-config-devtools": "^1.0.5",
6464
"@mongodb-js/prettier-config-devtools": "^1.0.2",
6565
"@mongodb-js/tsconfig-devtools": "^1.0.3",
66+
"@mongodb-js/mql-typescript": "^0.2.3",
6667
"@mongosh/shell-api": "^3.13.0",
6768
"@types/chai": "^4.2.21",
6869
"@types/mocha": "^9.1.1",
6970
"@types/node": "^22.15.30",
7071
"@types/sinon-chai": "^3.2.5",
7172
"bson": "^6.10.3",
73+
"mongodb": "^6.9.0",
7274
"chai": "^4.5.0",
7375
"depcheck": "^1.4.7",
7476
"eslint": "^7.25.0",

packages/mongodb-ts-autocomplete/scripts/extract-types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ async function loadSources(sources: Record<string, string>) {
1616
async function run() {
1717
const input: Record<string, string> = {
1818
'/bson.ts': path.join(require.resolve('bson'), '..', '..', 'bson.d.ts'),
19-
'/mql.ts': path.join(__dirname, '..', 'src', 'fixtures', 'mql.ts'),
19+
'/mongodb.ts': path.join(
20+
require.resolve('mongodb'),
21+
'..',
22+
'..',
23+
'mongodb.d.ts',
24+
),
25+
'/mql.ts': path.join(
26+
require.resolve('@mongodb-js/mql-typescript'),
27+
'..',
28+
'..',
29+
'out',
30+
'schema.d.ts',
31+
),
2032
};
2133
const files = await loadSources(input);
2234
const code = `

packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,12 @@ describe('MongoDBAutocompleter', function () {
152152
// TODO: We need MONGOSH-2170 so that we can use the generated MQL types via
153153
// the Shell API to autocomplete fields in
154154
// ServerSchema[databaseName][collectionName].schema
155-
it.skip('completes a collection field name in a query', async function () {
155+
it('completes a collection field name in a query', async function () {
156156
const completions = await autocompleter.autocomplete('db.foo.find({ fo');
157157

158158
expect(
159159
completions.filter((c) => /^(foo|bar|baz)$/.test(c.name)),
160160
).to.deep.equal([
161-
{
162-
kind: 'property',
163-
name: 'bar',
164-
result: 'db.foo.find({ bar',
165-
},
166-
{
167-
kind: 'property',
168-
name: 'baz',
169-
result: 'db.foo.find({ baz',
170-
},
171161
{
172162
kind: 'property',
173163
name: 'foo',
@@ -196,8 +186,8 @@ describe('MongoDBAutocompleter', function () {
196186
connection.addCollectionSchema(databaseName, collectionName, schema);
197187
const code = autocompleter.getConnectionCode(connectionId);
198188
expect(code).to.equal(`
199-
import * as ShellAPI from '/shell-api.ts';
200189
import * as bson from '/bson.ts';
190+
import * as mql from '/mql.ts';
201191
202192
export type ServerSchema = {
203193
${JSON.stringify(databaseName)}: {

packages/mongodb-ts-autocomplete/src/autocompleter.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class MongoDBAutocompleter {
163163

164164
this.autocompleter.updateCode({
165165
...autocompleteTypes,
166-
'/shell-api.ts': replaceImports(ShellApiText),
166+
//'/shell-api.ts': replaceImports(ShellApiText),
167167
});
168168
}
169169

@@ -176,29 +176,55 @@ export class MongoDBAutocompleter {
176176

177177
getConnectionCode(connectionId: string): string {
178178
return `
179-
import * as ShellAPI from '/shell-api.ts';
180179
import * as bson from '/bson.ts';
180+
import * as mql from '/mql.ts';
181181
182182
export type ServerSchema = ${this.connectionSchemas[
183183
connectionId
184184
].toTypescriptTypeDefinition()};
185185
`;
186186
}
187187

188-
getCurrentGlobalsCode(connectionId: string, databaseName: string) {
189-
return `
190-
import * as ShellAPI from '/shell-api.ts';
188+
getCurrentGlobalsCode(
189+
connectionId: string,
190+
databaseName: string,
191+
schemaType: string,
192+
): string {
193+
const template = `
194+
import * as mql from '/mql.ts';
191195
import { ServerSchema } from '/${connectionId}.ts';
192196
193197
type CurrentDatabaseSchema = ServerSchema[${JSON.stringify(databaseName)}];
194198
199+
type MyMQLQuery = mql.Query<${schemaType}>;
200+
201+
/*
202+
This will have to be restructured and cached maybe as part of
203+
/{connectionId}.ts and not filled in every time we autocomplete, but let's
204+
just see if it works first.
205+
*/
206+
SHELL_API_GOES_HERE
207+
195208
declare global {
196-
const db: ShellAPI.DatabaseWithSchema<ServerSchema, CurrentDatabaseSchema>;
197-
const rs: ShellAPI.ReplicaSet<ServerSchema, CurrentDatabaseSchema>;
198-
const sh: ShellAPI.Shard<ServerSchema, CurrentDatabaseSchema>;
199-
const sp: ShellAPI.Streams<ServerSchema, CurrentDatabaseSchema>;
209+
const db: DatabaseWithSchema<ServerSchema, CurrentDatabaseSchema>;
210+
const rs: ReplicaSet<ServerSchema, CurrentDatabaseSchema>;
211+
const sh: Shard<ServerSchema, CurrentDatabaseSchema>;
212+
const sp: Streams<ServerSchema, CurrentDatabaseSchema>;
200213
}
201214
`;
215+
216+
const shellAPI = replaceImports(ShellApiText as string)
217+
/*
218+
Shell API would ideally have this as find(query: MQLQuery,) and then specify type MQLQuery = Document;
219+
Then in mongosh' shell-api it is unchanged from what it is now, but MQLQuery,
220+
MQLPipeline, etc. are things we can then easily find/replace.
221+
222+
For now this pattern is just a hack.
223+
*/
224+
.replace('find(query?: Document_2,', 'find(query: MyMQLQuery,');
225+
226+
// This is just in two steps so we can print the template without all of the shell API.
227+
return template.replace('SHELL_API_GOES_HERE', shellAPI);
202228
}
203229

204230
async autocomplete(code: string): Promise<AutoCompletion[]> {
@@ -232,10 +258,14 @@ declare global {
232258
this.autocompleter.updateCode({
233259
[`/${connectionId}.ts`]: this.getConnectionCode(connectionId),
234260
});
261+
const schemaType = collectionNames.includes(collectionName)
262+
? `CurrentDatabaseSchema[${JSON.stringify(collectionName)}]['schema']`
263+
: '{}';
235264
this.autocompleter.updateCode({
236265
'/current-globals.ts': this.getCurrentGlobalsCode(
237266
connectionId,
238267
databaseName,
268+
schemaType,
239269
),
240270
});
241271

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export function replaceImports(code: string) {
22
// This just makes it possible to work on mql.ts because then the
33
// IDE finds the import.
4-
return code.replace(/'bson'/g, "'/bson.ts'");
4+
return code
5+
.replace(/'bson'/g, "'/bson.ts'")
6+
.replace(/'mongodb'/g, "'/mongodb.ts'");
57
}

packages/ts-autocomplete/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ function filterDiagnostics(diagnostics: ts.Diagnostic[]): {
165165
..._.pick(item, 'messageText'),
166166
};
167167

168-
if (result.fileName === '/shell-api.ts') {
168+
if (
169+
result.fileName === '/shell-api.ts' ||
170+
result.fileName === '/current-globals.ts'
171+
) {
169172
delete result.text;
170173
}
171174

0 commit comments

Comments
 (0)