Skip to content

Commit 11aa1c3

Browse files
committed
use placeholder MQL types for query and pipeline
1 parent 62609f5 commit 11aa1c3

File tree

11 files changed

+59
-42
lines changed

11 files changed

+59
-42
lines changed

packages/shell-api/src/bulk.ts

Lines changed: 2 additions & 1 deletion
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 { MQLQuery } from './mql-types';
2425

2526
@shellApiClassDefault
2627
export class BulkFindOp extends ShellApiWithMongoClass {
@@ -202,7 +203,7 @@ 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
}

packages/shell-api/src/collection.ts

Lines changed: 13 additions & 12 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 { MQLQuery, MQLPipeline } from './mql-types';
99100

100101
export type CollectionWithSchema<
101102
M extends GenericServerSideSchema = GenericServerSideSchema,
@@ -188,20 +189,20 @@ export class Collection<
188189
* @returns {Promise} The promise of aggregation results.
189190
*/
190191
async aggregate(
191-
pipeline: Document[],
192+
pipeline: MQLPipeline,
192193
options: AggregateOptions & { explain: ExplainVerbosityLike }
193194
): Promise<Document>;
194195
async aggregate(
195-
pipeline: Document[],
196+
pipeline: MQLPipeline,
196197
options?: AggregateOptions
197198
): Promise<AggregationCursor>;
198-
async aggregate(...stages: Document[]): Promise<AggregationCursor>;
199+
async aggregate(...stages: MQLPipeline): Promise<AggregationCursor>;
199200
@returnsPromise
200201
@returnType('AggregationCursor')
201202
@apiVersions([1])
202203
async aggregate(...args: unknown[]): Promise<AggregationCursor | Document> {
203204
let options: AggregateOptions;
204-
let pipeline: Document[];
205+
let pipeline: MQLPipeline;
205206
if (args.length === 0 || Array.isArray(args[0])) {
206207
options = args[1] || {};
207208
pipeline = (args[0] as Document[]) || [];
@@ -320,7 +321,7 @@ export class Collection<
320321
@serverVersions(['4.0.3', ServerVersions.latest])
321322
@apiVersions([1])
322323
async countDocuments(
323-
query?: Document,
324+
query?: MQLQuery,
324325
options: CountDocumentsOptions = {}
325326
): Promise<number> {
326327
this._emitCollectionApiCall('countDocuments', { query, options });
@@ -413,17 +414,17 @@ export class Collection<
413414
* @returns {Array} The promise of the result.
414415
*/
415416
async distinct(field: string): Promise<Document>;
416-
async distinct(field: string, query: Document): Promise<Document>;
417+
async distinct(field: string, query: MQLQuery): Promise<Document>;
417418
async distinct(
418419
field: string,
419-
query: Document,
420+
query: MQLQuery,
420421
options: DistinctOptions
421422
): Promise<Document>;
422423
@returnsPromise
423424
@apiVersions([])
424425
async distinct(
425426
field: string,
426-
query?: Document,
427+
query?: MQLQuery,
427428
options: DistinctOptions = {}
428429
): Promise<Document> {
429430
this._emitCollectionApiCall('distinct', { field, query, options });
@@ -476,7 +477,7 @@ export class Collection<
476477
@apiVersions([1])
477478
@returnsPromise
478479
async find(
479-
query?: Document,
480+
query?: MQLQuery,
480481
projection?: Document,
481482
options: FindOptions = {}
482483
): Promise<Cursor> {
@@ -560,7 +561,7 @@ export class Collection<
560561
@returnType('Document')
561562
@apiVersions([1])
562563
async findOne(
563-
query: Document = {},
564+
query: MQLQuery = {},
564565
projection?: Document,
565566
options: FindOptions = {}
566567
): Promise<C['schema'] | null> {
@@ -893,7 +894,7 @@ export class Collection<
893894
@serverVersions([ServerVersions.earliest, '3.2.0'])
894895
@apiVersions([1])
895896
async remove(
896-
query: Document,
897+
query: MQLQuery,
897898
options: boolean | RemoveShellOptions = {}
898899
): Promise<DeleteResult | Document> {
899900
await this._instanceState.printDeprecationWarning(
@@ -2332,7 +2333,7 @@ export class Collection<
23322333
@apiVersions([1])
23332334
@returnsPromise
23342335
async watch(
2335-
pipeline: Document[] | ChangeStreamOptions = [],
2336+
pipeline: MQLPipeline | ChangeStreamOptions = [],
23362337
options: ChangeStreamOptions = {}
23372338
): Promise<ChangeStreamCursor> {
23382339
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: 9 additions & 8 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 { 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 });
@@ -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)) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Document } from '@mongosh/service-provider-core';
2+
3+
export type MQLQuery = Document;
4+
5+
export type MQLPipeline = Document[];

packages/shell-api/src/plan-cache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { CollectionWithSchema } from './collection';
1111
import { asPrintable, ServerVersions } from './enums';
1212
import { MongoshDeprecatedError } from '@mongosh/errors';
1313
import type Mongo from './mongo';
14+
import type { MQLPipeline, MQLQuery } from './mql-types';
1415

1516
@shellApiClassDefault
1617
export default class PlanCache extends ShellApiWithMongoClass {
@@ -41,7 +42,7 @@ export default class PlanCache extends ShellApiWithMongoClass {
4142
@returnsPromise
4243
@apiVersions([])
4344
async clearPlansByQuery(
44-
query: Document,
45+
query: MQLQuery,
4546
projection?: Document,
4647
sort?: Document
4748
): Promise<Document> {
@@ -58,7 +59,7 @@ export default class PlanCache extends ShellApiWithMongoClass {
5859
@serverVersions(['4.4.0', ServerVersions.latest])
5960
@returnsPromise
6061
@apiVersions([])
61-
async list(pipeline?: Document[]): Promise<Document[]> {
62+
async list(pipeline?: MQLPipeline): Promise<Document[]> {
6263
const p = pipeline || [];
6364
const agg = await this._collection.aggregate([
6465
{ $planCacheStats: {} },

packages/shell-api/src/shard.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type Mongo from './mongo';
3131
import type AggregationCursor from './aggregation-cursor';
3232
import type RunCommandCursor from './run-command-cursor';
3333
import semver from 'semver';
34+
import type { MQLQuery } from './mql-types';
3435

3536
@shellApiClassDefault
3637
export default class Shard<
@@ -449,7 +450,7 @@ export default class Shard<
449450

450451
@returnsPromise
451452
@apiVersions([])
452-
async splitAt(ns: string, query: Document): Promise<Document> {
453+
async splitAt(ns: string, query: MQLQuery): Promise<Document> {
453454
assertArgsDefinedType([ns, query], ['string', 'object'], 'Shard.splitAt');
454455
this._emitShardApiCall('splitAt', { ns, query });
455456
return this._database._runAdminCommand({
@@ -460,7 +461,7 @@ export default class Shard<
460461

461462
@returnsPromise
462463
@apiVersions([])
463-
async splitFind(ns: string, query: Document): Promise<Document> {
464+
async splitFind(ns: string, query: MQLQuery): Promise<Document> {
464465
assertArgsDefinedType([ns, query], ['string', 'object'], 'Shard.splitFind');
465466
this._emitShardApiCall('splitFind', { ns, query });
466467
return this._database._runAdminCommand({
@@ -473,7 +474,7 @@ export default class Shard<
473474
@apiVersions([])
474475
async moveChunk(
475476
ns: string,
476-
query: Document,
477+
query: MQLQuery,
477478
destination: string
478479
): Promise<Document> {
479480
assertArgsDefinedType(

packages/shell-api/src/stream-processor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from './decorators';
1111

1212
import type { Streams } from './streams';
13+
import type { MQLPipeline } from './mql-types';
1314

1415
@shellApiClassDefault
1516
export class StreamProcessor extends ShellApiWithMongoClass {
@@ -71,11 +72,11 @@ export class StreamProcessor extends ShellApiWithMongoClass {
7172
* sp.name.modify(newPipeline, {resumeFromCheckpoint: false})
7273
*/
7374
async modify(options: Document): Promise<Document>;
74-
async modify(pipeline: Document[], options?: Document): Promise<Document>;
75+
async modify(pipeline: MQLPipeline, options?: Document): Promise<Document>;
7576

7677
@returnsPromise
7778
async modify(
78-
pipelineOrOptions: Document[] | Document,
79+
pipelineOrOptions: MQLPipeline | Document,
7980
options?: Document
8081
): Promise<Document> {
8182
if (Array.isArray(pipelineOrOptions)) {

0 commit comments

Comments
 (0)