Skip to content

Commit d64d269

Browse files
authored
feat: use placeholder mql types MONGOSH-2333 (#2485)
* use placeholder MQL types for query and pipeline * placeholder type for a document to insert * also in bulk * specify document as return type * MQLQuery and MQLDocument in a bunch of methods * C is now unused
1 parent 62609f5 commit d64d269

File tree

11 files changed

+72
-52
lines changed

11 files changed

+72
-52
lines changed

packages/shell-api/src/bulk.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { asPrintable } from './enums';
2121
import { assertArgsDefinedType, shallowClone } from './helpers';
2222
import { BulkWriteResult } from './result';
2323
import type { CollectionWithSchema } from './collection';
24+
import type { MQLDocument, MQLQuery } from './mql-types';
2425

2526
@shellApiClassDefault
2627
export class BulkFindOp extends ShellApiWithMongoClass {
@@ -202,14 +203,14 @@ export default class Bulk extends ShellApiWithMongoClass {
202203

203204
@returnType('BulkFindOp')
204205
@apiVersions([1])
205-
find(query: Document): BulkFindOp {
206+
find(query: MQLQuery): BulkFindOp {
206207
assertArgsDefinedType([query], [true], 'Bulk.find');
207208
return new BulkFindOp(this._serviceProviderBulkOp.find(query), this);
208209
}
209210

210211
@returnType('Bulk')
211212
@apiVersions([1])
212-
insert(document: Document): Bulk {
213+
insert(document: MQLDocument): Bulk {
213214
this._batchCounts.nInsertOps++;
214215
assertArgsDefinedType([document], [true], 'Bulk.insert');
215216
this._serviceProviderBulkOp.insert(document);

packages/shell-api/src/collection.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { HIDDEN_COMMANDS } from '@mongosh/history';
9696
import PlanCache from './plan-cache';
9797
import ChangeStreamCursor from './change-stream-cursor';
9898
import { ShellApiErrors } from './error-codes';
99+
import type { MQLDocument, MQLQuery, MQLPipeline } from './mql-types';
99100

100101
export type CollectionWithSchema<
101102
M extends GenericServerSideSchema = GenericServerSideSchema,
@@ -116,6 +117,7 @@ export type CollectionWithSchema<
116117
export class Collection<
117118
M extends GenericServerSideSchema = GenericServerSideSchema,
118119
D extends GenericDatabaseSchema = M[keyof M],
120+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
119121
C extends GenericCollectionSchema = D[keyof D],
120122
N extends StringKey<D> = StringKey<D>
121123
> extends ShellApiWithMongoClass {
@@ -188,20 +190,20 @@ export class Collection<
188190
* @returns {Promise} The promise of aggregation results.
189191
*/
190192
async aggregate(
191-
pipeline: Document[],
193+
pipeline: MQLPipeline,
192194
options: AggregateOptions & { explain: ExplainVerbosityLike }
193195
): Promise<Document>;
194196
async aggregate(
195-
pipeline: Document[],
197+
pipeline: MQLPipeline,
196198
options?: AggregateOptions
197199
): Promise<AggregationCursor>;
198-
async aggregate(...stages: Document[]): Promise<AggregationCursor>;
200+
async aggregate(...stages: MQLPipeline): Promise<AggregationCursor>;
199201
@returnsPromise
200202
@returnType('AggregationCursor')
201203
@apiVersions([1])
202204
async aggregate(...args: unknown[]): Promise<AggregationCursor | Document> {
203205
let options: AggregateOptions;
204-
let pipeline: Document[];
206+
let pipeline: MQLPipeline;
205207
if (args.length === 0 || Array.isArray(args[0])) {
206208
options = args[1] || {};
207209
pipeline = (args[0] as Document[]) || [];
@@ -320,7 +322,7 @@ export class Collection<
320322
@serverVersions(['4.0.3', ServerVersions.latest])
321323
@apiVersions([1])
322324
async countDocuments(
323-
query?: Document,
325+
query?: MQLQuery,
324326
options: CountDocumentsOptions = {}
325327
): Promise<number> {
326328
this._emitCollectionApiCall('countDocuments', { query, options });
@@ -413,17 +415,17 @@ export class Collection<
413415
* @returns {Array} The promise of the result.
414416
*/
415417
async distinct(field: string): Promise<Document>;
416-
async distinct(field: string, query: Document): Promise<Document>;
418+
async distinct(field: string, query: MQLQuery): Promise<Document>;
417419
async distinct(
418420
field: string,
419-
query: Document,
421+
query: MQLQuery,
420422
options: DistinctOptions
421423
): Promise<Document>;
422424
@returnsPromise
423425
@apiVersions([])
424426
async distinct(
425427
field: string,
426-
query?: Document,
428+
query?: MQLQuery,
427429
options: DistinctOptions = {}
428430
): Promise<Document> {
429431
this._emitCollectionApiCall('distinct', { field, query, options });
@@ -476,7 +478,7 @@ export class Collection<
476478
@apiVersions([1])
477479
@returnsPromise
478480
async find(
479-
query?: Document,
481+
query?: MQLQuery,
480482
projection?: Document,
481483
options: FindOptions = {}
482484
): Promise<Cursor> {
@@ -560,10 +562,10 @@ export class Collection<
560562
@returnType('Document')
561563
@apiVersions([1])
562564
async findOne(
563-
query: Document = {},
565+
query: MQLQuery = {},
564566
projection?: Document,
565567
options: FindOptions = {}
566-
): Promise<C['schema'] | null> {
568+
): Promise<MQLDocument | null> {
567569
if (projection) {
568570
options.projection = projection;
569571
}
@@ -757,7 +759,7 @@ export class Collection<
757759
@serverVersions([ServerVersions.earliest, '3.6.0'])
758760
@apiVersions([1])
759761
async insert(
760-
docs: Document | Document[],
762+
docs: MQLDocument | MQLDocument[],
761763
options: BulkWriteOptions = {}
762764
): Promise<InsertManyResult> {
763765
await this._instanceState.printDeprecationWarning(
@@ -801,7 +803,7 @@ export class Collection<
801803
@serverVersions(['3.2.0', ServerVersions.latest])
802804
@apiVersions([1])
803805
async insertMany(
804-
docs: Document[],
806+
docs: MQLDocument[],
805807
options: BulkWriteOptions = {}
806808
): Promise<InsertManyResult> {
807809
assertArgsDefinedType([docs], [true], 'Collection.insertMany');
@@ -837,7 +839,7 @@ export class Collection<
837839
@serverVersions(['3.2.0', ServerVersions.latest])
838840
@apiVersions([1])
839841
async insertOne(
840-
doc: Document,
842+
doc: MQLDocument,
841843
options: InsertOneOptions = {}
842844
): Promise<InsertOneResult> {
843845
assertArgsDefinedType([doc], [true], 'Collection.insertOne');
@@ -893,7 +895,7 @@ export class Collection<
893895
@serverVersions([ServerVersions.earliest, '3.2.0'])
894896
@apiVersions([1])
895897
async remove(
896-
query: Document,
898+
query: MQLQuery,
897899
options: boolean | RemoveShellOptions = {}
898900
): Promise<DeleteResult | Document> {
899901
await this._instanceState.printDeprecationWarning(
@@ -2332,7 +2334,7 @@ export class Collection<
23322334
@apiVersions([1])
23332335
@returnsPromise
23342336
async watch(
2335-
pipeline: Document[] | ChangeStreamOptions = [],
2337+
pipeline: MQLPipeline | ChangeStreamOptions = [],
23362338
options: ChangeStreamOptions = {}
23372339
): Promise<ChangeStreamCursor> {
23382340
if (!Array.isArray(pipeline)) {

packages/shell-api/src/database.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import type {
6161
ExplainVerbosityLike,
6262
AggregateOptions,
6363
} from '@mongosh/service-provider-core';
64+
import type { MQLPipeline } from './mql-types';
6465

6566
export type CollectionNamesWithTypes = {
6667
name: string;
@@ -443,26 +444,26 @@ export class Database<
443444
* @returns {Promise} The promise of aggregation results.
444445
*/
445446
async aggregate(
446-
pipeline: Document[],
447+
pipeline: MQLPipeline,
447448
options: AggregateOptions & { explain: ExplainVerbosityLike }
448449
): Promise<Document>;
449450
async aggregate(
450-
pipeline: Document[],
451+
pipeline: MQLPipeline,
451452
options?: AggregateOptions
452453
): Promise<AggregationCursor>;
453-
async aggregate(...stages: Document[]): Promise<AggregationCursor>;
454+
async aggregate(...stages: MQLPipeline): Promise<AggregationCursor>;
454455
@returnsPromise
455456
@returnType('AggregationCursor')
456457
@apiVersions([1])
457458
async aggregate(...args: unknown[]): Promise<AggregationCursor | Document> {
458459
let options: AggregateOptions;
459-
let pipeline: Document[];
460+
let pipeline: MQLPipeline;
460461
if (args.length === 0 || Array.isArray(args[0])) {
461462
options = args[1] || {};
462-
pipeline = (args[0] as Document[]) || [];
463+
pipeline = (args[0] as MQLPipeline) || [];
463464
} else {
464465
options = {};
465-
pipeline = (args as Document[]) || [];
466+
pipeline = (args as MQLPipeline) || [];
466467
}
467468

468469
if ('background' in options) {
@@ -855,7 +856,7 @@ export class Database<
855856
async createView(
856857
name: string,
857858
source: string,
858-
pipeline: Document[],
859+
pipeline: MQLPipeline,
859860
options: CreateCollectionOptions = {}
860861
): Promise<{ ok: number }> {
861862
assertArgsDefinedType(
@@ -1085,7 +1086,7 @@ export class Database<
10851086
? { $all: opts, $ownOps: false }
10861087
: { $all: !!opts.$all, $ownOps: !!opts.$ownOps };
10871088

1088-
const pipeline: Document[] = [
1089+
const pipeline: MQLPipeline = [
10891090
{
10901091
$currentOp: {
10911092
allUsers: !legacyCurrentOpOptions.$ownOps,
@@ -1739,7 +1740,7 @@ export class Database<
17391740
@apiVersions([1])
17401741
@returnsPromise
17411742
async watch(
1742-
pipeline: Document[] | ChangeStreamOptions = [],
1743+
pipeline: MQLPipeline | ChangeStreamOptions = [],
17431744
options: ChangeStreamOptions = {}
17441745
): Promise<ChangeStreamCursor> {
17451746
if (!Array.isArray(pipeline)) {

packages/shell-api/src/explainable.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import type {
3333
FindOneAndUpdateOptions,
3434
FindOptions,
3535
} from '@mongosh/service-provider-core';
36+
import type { MQLDocument, MQLPipeline, MQLQuery } from './mql-types';
3637

3738
@shellApiClassDefault
3839
export default class Explainable extends ShellApiWithMongoClass {
@@ -97,7 +98,7 @@ export default class Explainable extends ShellApiWithMongoClass {
9798
@apiVersions([1])
9899
@returnsPromise
99100
async find(
100-
query?: Document,
101+
query?: MQLQuery,
101102
projection?: Document,
102103
options: FindOptions = {}
103104
): Promise<ExplainableCursor> {
@@ -107,14 +108,14 @@ export default class Explainable extends ShellApiWithMongoClass {
107108
return new ExplainableCursor(this._mongo, cursor, this._verbosity);
108109
}
109110

110-
async aggregate(pipeline: Document[], options: Document): Promise<Document>;
111-
async aggregate(...stages: Document[]): Promise<Document>;
111+
async aggregate(pipeline: MQLPipeline, options: Document): Promise<Document>;
112+
async aggregate(...stages: MQLPipeline): Promise<Document>;
112113
@returnsPromise
113114
@apiVersions([1])
114115
async aggregate(...args: any[]): Promise<Document> {
115116
this._emitExplainableApiCall('aggregate', { args });
116117
let options: Document;
117-
let pipeline: Document[];
118+
let pipeline: MQLPipeline;
118119
if (Array.isArray(args[0])) {
119120
pipeline = args[0];
120121
options = args[1] ?? {};
@@ -147,17 +148,17 @@ export default class Explainable extends ShellApiWithMongoClass {
147148
}
148149

149150
async distinct(field: string): Promise<Document>;
150-
async distinct(field: string, query: Document): Promise<Document>;
151+
async distinct(field: string, query: MQLQuery): Promise<Document>;
151152
async distinct(
152153
field: string,
153-
query: Document,
154+
query: MQLQuery,
154155
options: DistinctOptions
155156
): Promise<Document>;
156157
@returnsPromise
157158
@apiVersions([1])
158159
async distinct(
159160
field: string,
160-
query?: Document,
161+
query?: MQLQuery,
161162
options: DistinctOptions = {}
162163
): Promise<Document> {
163164
this._emitExplainableApiCall('distinct', { field, query, options });
@@ -182,7 +183,7 @@ export default class Explainable extends ShellApiWithMongoClass {
182183
@returnsPromise
183184
@apiVersions([1])
184185
async findOneAndDelete(
185-
filter: Document,
186+
filter: MQLQuery,
186187
options: FindOneAndDeleteOptions = {}
187188
): Promise<Document | null> {
188189
this._emitExplainableApiCall('findOneAndDelete', { filter, options });
@@ -195,8 +196,8 @@ export default class Explainable extends ShellApiWithMongoClass {
195196
@returnsPromise
196197
@apiVersions([1])
197198
async findOneAndReplace(
198-
filter: Document,
199-
replacement: Document,
199+
filter: MQLQuery,
200+
replacement: MQLDocument,
200201
options: FindAndModifyShellOptions<FindOneAndReplaceOptions> = {}
201202
): Promise<Document> {
202203
this._emitExplainableApiCall('findOneAndReplace', { filter, options });
@@ -209,8 +210,8 @@ export default class Explainable extends ShellApiWithMongoClass {
209210
@returnsPromise
210211
@apiVersions([1])
211212
async findOneAndUpdate(
212-
filter: Document,
213-
update: Document,
213+
filter: MQLQuery,
214+
update: MQLDocument,
214215
options: FindAndModifyShellOptions<FindOneAndUpdateOptions> = {}
215216
): Promise<Document> {
216217
this._emitExplainableApiCall('findOneAndUpdate', { filter, options });
@@ -223,7 +224,7 @@ export default class Explainable extends ShellApiWithMongoClass {
223224
@returnsPromise
224225
@apiVersions([1])
225226
async remove(
226-
query: Document,
227+
query: MQLQuery,
227228
options: boolean | RemoveShellOptions = {}
228229
): Promise<Document> {
229230
this._emitExplainableApiCall('remove', { query, options });

packages/shell-api/src/helpers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type { AbstractCursor } from './abstract-cursor';
3232
import type ChangeStreamCursor from './change-stream-cursor';
3333
import type { ShellBson } from './shell-bson';
3434
import { inspect } from 'util';
35+
import type { MQLPipeline, MQLQuery } from './mql-types';
3536

3637
/**
3738
* Helper method to adapt aggregation pipeline options.
@@ -882,7 +883,7 @@ export async function iterate(
882883

883884
// This is only used by collection.findAndModify() itself.
884885
export type FindAndModifyMethodShellOptions = {
885-
query: Document;
886+
query: MQLQuery;
886887
sort?: (
887888
| FindOneAndDeleteOptions
888889
| FindOneAndReplaceOptions
@@ -1111,7 +1112,9 @@ export function isValidCollectionName(name: string): boolean {
11111112
return !!name && !/[$\0]/.test(name);
11121113
}
11131114

1114-
export function shouldRunAggregationImmediately(pipeline: Document[]): boolean {
1115+
export function shouldRunAggregationImmediately(
1116+
pipeline: MQLPipeline
1117+
): boolean {
11151118
return pipeline.some((stage) =>
11161119
Object.keys(stage).some(
11171120
(stageName) => stageName === '$merge' || stageName === '$out'

packages/shell-api/src/mongo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import type { LogEntry } from './log-entry';
6767
import { parseAnyLogEntry } from './log-entry';
6868
import type { CollectionWithSchema } from './collection';
6969
import type { ShellBson } from './shell-bson';
70+
import type { MQLPipeline } from './mql-types';
7071

7172
/* Utility, inverse of Readonly<T> */
7273
type Mutable<T> = {
@@ -845,7 +846,7 @@ export default class Mongo<
845846
@apiVersions([1])
846847
@returnsPromise
847848
async watch(
848-
pipeline: Document[] | ChangeStreamOptions = [],
849+
pipeline: MQLPipeline | ChangeStreamOptions = [],
849850
options: ChangeStreamOptions = {}
850851
): Promise<ChangeStreamCursor> {
851852
if (!Array.isArray(pipeline)) {

packages/shell-api/src/mql-types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Document } from '@mongosh/service-provider-core';
2+
3+
export type MQLQuery = Document;
4+
5+
export type MQLPipeline = Document[];
6+
7+
export type MQLDocument = Document;

0 commit comments

Comments
 (0)