Skip to content

Commit d7112f2

Browse files
committed
expose the mql types in mongodb-ts-autocomplete
1 parent 7fc9890 commit d7112f2

File tree

9 files changed

+41
-6
lines changed

9 files changed

+41
-6
lines changed

packages/mongodb-ts-autocomplete/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@mongodb-js/prettier-config-devtools": "^1.0.1",
6262
"@mongodb-js/tsconfig-devtools": "^1.0.2",
6363
"@mongosh/shell-api": "^3.11.0",
64+
"@mongodb-js/mql-typescript": "^0.1.0",
6465
"@types/chai": "^4.2.21",
6566
"@types/mocha": "^9.1.1",
6667
"@types/node": "^22.15.30",

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ 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+
),
2025
};
2126
const files = await loadSources(input);
2227
const code = `

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ 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.only('completes a collection field name in a query', async function () {
156156
const completions = await autocompleter.autocomplete('db.foo.find({ fo');
157157

158158
expect(
@@ -198,6 +198,7 @@ describe('MongoDBAutocompleter', function () {
198198
expect(code).to.equal(`
199199
import * as ShellAPI from '/shell-api.ts';
200200
import * as bson from '/bson.ts';
201+
import * as mql from '/mql.ts';
201202
202203
export type ServerSchema = {
203204
${JSON.stringify(databaseName)}: {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Autocompleter from '@mongodb-js/ts-autocomplete';
22
import type { AutoCompletion } from '@mongodb-js/ts-autocomplete';
33
import autocompleteTypes from './fixtures/autocomplete-types';
44
import { api as ShellApiText } from '@mongosh/shell-api/api';
5+
import { schema as MQLText } from '@mongodb-js/mql-typescript/schema';
56
import { replaceImports } from './utils';
67

78
import type { JSONSchema } from 'mongodb-schema';
@@ -164,6 +165,7 @@ export class MongoDBAutocompleter {
164165
this.autocompleter.updateCode({
165166
...autocompleteTypes,
166167
'/shell-api.ts': replaceImports(ShellApiText),
168+
'/mql.ts': replaceImports(MQLText),
167169
});
168170
}
169171

@@ -178,6 +180,7 @@ export class MongoDBAutocompleter {
178180
return `
179181
import * as ShellAPI from '/shell-api.ts';
180182
import * as bson from '/bson.ts';
183+
import * as mql from '/mql.ts';
181184
182185
export type ServerSchema = ${this.connectionSchemas[
183186
connectionId
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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare const schema;

packages/mql-typescript/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@
2727
},
2828
"license": "Apache-2.0",
2929
"main": "dist/index.js",
30+
"types": "./dist/index.d.ts",
3031
"exports": {
31-
"require": "./dist/index.js",
32-
"import": "./dist/.esm-wrapper.mjs"
32+
".": {
33+
"default": "./dist/index.js",
34+
"types": "./dist/index.d.ts"
35+
},
36+
"./schema": {
37+
"default": "./out/schema-export.js",
38+
"types": "./out/schema-export.d.ts"
39+
}
3340
},
34-
"types": "./dist/index.d.ts",
3541
"scripts": {
3642
"bootstrap": "npm run compile",
3743
"prepublishOnly": "npm run compile",

packages/mql-typescript/src/schema-generator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ export class SchemaGenerator extends GeneratorBase {
2121
'out',
2222
'schema-export.js',
2323
);
24+
private schemaExportDefinitionFile = path.resolve(
25+
__dirname,
26+
'..',
27+
'out',
28+
'schema-export.d.ts',
29+
);
2430

2531
private toTypeName(type: string): string {
2632
return this.trivialTypeMappings[type as ArgType] ?? capitalize(type);
@@ -589,5 +595,11 @@ export class SchemaGenerator extends GeneratorBase {
589595
.replace(/\n/g, '\\n'), // Escape newlines
590596
);
591597
this.emit("'\n");
598+
599+
await fs.writeFile(
600+
this.schemaExportDefinitionFile,
601+
'export declare const schema;',
602+
'utf8',
603+
);
592604
}
593605
}

packages/ts-autocomplete/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ function filterDiagnostics(diagnostics: ts.Diagnostic[]): {
169169
delete result.text;
170170
}
171171

172+
if (result.fileName === '/mql.ts') {
173+
delete result.text;
174+
}
175+
172176
return result;
173177
});
174178
}

0 commit comments

Comments
 (0)