Skip to content

Commit 22e3a47

Browse files
authored
chore(shell-api): deprecate Collection.mapReduce MONGOSH-854 (#976)
1 parent 84032cb commit 22e3a47

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

packages/autocomplete/index.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,13 @@ describe('completer.completer', () => {
255255
it('returns all suggestions', async() => {
256256
const i = 'db.shipwrecks.';
257257
const collComplete = Object.keys(shellSignatures.Collection.attributes as any);
258-
const adjusted = collComplete.filter(c => !['count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex'].includes(c)).map(c => `${i}${c}`);
258+
const adjusted = collComplete
259+
.filter(c =>
260+
![
261+
'count', 'update', 'remove', 'insert', 'save', 'findAndModify', 'reIndex', 'mapReduce'
262+
].includes(c)
263+
)
264+
.map(c => `${i}${c}`);
259265

260266
expect(await completer(sharded440, i)).to.deep.equal([adjusted, i]);
261267
});

packages/shell-api/src/collection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,14 @@ export default class Collection extends ShellApiWithMongoClass {
15311531
}
15321532

15331533
@returnsPromise
1534+
@deprecated
1535+
@serverVersions([ServerVersions.earliest, '4.9.0'])
15341536
@apiVersions([])
15351537
async mapReduce(map: Function | string, reduce: Function | string, optionsOrOutString: MapReduceShellOptions): Promise<Document> {
1538+
printDeprecationWarning(
1539+
'Collection.mapReduce() is deprecated. Use an aggregation instead.\nSee https://docs.mongodb.com/manual/core/map-reduce for details.',
1540+
this._mongo._internalState.context.print
1541+
);
15361542
assertArgsDefinedType([map, reduce, optionsOrOutString], [true, true, true], 'Collection.mapReduce');
15371543
this._emitCollectionApiCall('mapReduce', { map, reduce, out: optionsOrOutString });
15381544

packages/shell-api/src/decorators.ts

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ type ClassHelp = {
279279
};
280280

281281
export const toIgnore = ['constructor', 'help'];
282-
export function shellApiClassGeneric(constructor: Function, hasHelp: boolean): void {
282+
function shellApiClassGeneric(constructor: Function, hasHelp: boolean): void {
283283
const className = constructor.name;
284284
const classHelpKeyPrefix = `shell-api.classes.${className}.help`;
285285
const classHelp: ClassHelp = {
@@ -405,10 +405,16 @@ export function shellApiClassGeneric(constructor: Function, hasHelp: boolean): v
405405
}
406406
}
407407

408+
/**
409+
* Marks a class as being a Shell API class including help information.
410+
*/
408411
export function shellApiClassDefault(constructor: Function): void {
409412
shellApiClassGeneric(constructor, true);
410413
}
411414

415+
/**
416+
* Marks a class as being a Shell API class without help information
417+
*/
412418
export function shellApiClassNoHelp(constructor: Function): void {
413419
shellApiClassGeneric(constructor, false);
414420
}
@@ -424,6 +430,19 @@ function markImplicitlyAwaited<T extends(...args: any) => Promise<any>>(orig: T)
424430
}
425431

426432
export { signatures };
433+
/**
434+
* Marks the decorated method as being supported for the given range of server versions.
435+
* Server versions are given as `[min, max]` where both boundaries are **inclusive**.
436+
* If the version of the server the user is connected to is not inside the range, the method
437+
* will not be included in autocompletion.
438+
*
439+
* When a method is deprecated after a specific server version, the `versionArray` should include
440+
* this version as the `max` value.
441+
*
442+
* See also `ServerVersions.earliest` and `ServerVersions.latest`.
443+
*
444+
* @param versionArray An array of supported server versions
445+
*/
427446
export function serverVersions(versionArray: [ string, string ]): Function {
428447
return function(
429448
_target: any,
@@ -433,6 +452,15 @@ export function serverVersions(versionArray: [ string, string ]): Function {
433452
descriptor.value.serverVersions = versionArray;
434453
};
435454
}
455+
456+
/**
457+
* Marks the decorated method as being supported for the given range of API versions.
458+
* API versions are given as `[version]` or `[min, max]`.
459+
* If the API version the user specified during connection is not inside the range, the method
460+
* will not be included in autocompletion.
461+
*
462+
* @param versionArray An array of supported API versions
463+
*/
436464
export function apiVersions(versionArray: [] | [ number ] | [ number, number ]): Function {
437465
return function(
438466
_target: any,
@@ -447,9 +475,27 @@ export function apiVersions(versionArray: [] | [ number ] | [ number, number ]):
447475
descriptor.value.apiVersions = versionArray;
448476
};
449477
}
478+
479+
/**
480+
* Marks the decorated class/method as deprecated.
481+
* A deprecated method will not be included in autocompletion.
482+
*
483+
* Calling a deprecated method will automatically emit a telemetry event but
484+
* will **not** print an automatic deprecation warning (see `printDeprecationWarning`).
485+
*
486+
* **Important:** To exclude the method from autocompletion use `@serverVersions`.
487+
*/
450488
export function deprecated(_target: any, _propertyKey: string, descriptor: PropertyDescriptor): void {
451489
descriptor.value.deprecated = true;
452490
}
491+
492+
/**
493+
* Marks the decorated method as only being available for the given topologies.
494+
* The method will not be included in autocomplete if the user is connected to a cluster
495+
* of a topology type that is not present in `topologiesArray`.
496+
*
497+
* @param topologiesArray The topologies for which the method is available
498+
*/
453499
export function topologies(topologiesArray: Topologies[]): Function {
454500
return function(
455501
_target: any,
@@ -459,7 +505,15 @@ export function topologies(topologiesArray: Topologies[]): Function {
459505
descriptor.value.topologies = topologiesArray;
460506
};
461507
}
508+
462509
export const nonAsyncFunctionsReturningPromises: string[] = []; // For testing.
510+
/**
511+
* Marks the decorated method as having a synthetic promise return value that needs to be implicitly
512+
* awaited by the async rewriter.
513+
*
514+
* Note: a test will verify that the `nonAsyncFunctionsReturningPromises` is empty, i.e. **every**
515+
* method that is decorated with `@returnsPromise` must be an `async` method.
516+
*/
463517
export function returnsPromise(_target: any, _propertyKey: string, descriptor: PropertyDescriptor): void {
464518
const originalFunction = descriptor.value;
465519
originalFunction.returnsPromise = true;
@@ -482,11 +536,24 @@ export function returnsPromise(_target: any, _propertyKey: string, descriptor: P
482536
nonAsyncFunctionsReturningPromises.push(originalFunction.name);
483537
}
484538
}
485-
// This is use to mark functions that are executable in the shell in a POSIX-shell-like
486-
// fashion, e.g. `show foo` which is translated into a call to `show('foo')`.
539+
540+
/**
541+
* Marks the deocrated method as executable in the shell in a POSIX-shell-like
542+
* fashion, e.g. `show foo` which is translated into a call to `show('foo')`.
543+
*/
487544
export function directShellCommand(_target: any, _propertyKey: string, descriptor: PropertyDescriptor): void {
488545
descriptor.value.isDirectShellCommand = true;
489546
}
547+
548+
/**
549+
* Marks the decorated method to provide a specific `completer` function to be
550+
* called for autocomplete.
551+
*
552+
* This can be used to provide autocompletion for POSIX-shell-like commands,
553+
* e.g. `show ...`.
554+
*
555+
* @param completer The completer to use for autocomplete
556+
*/
490557
export function shellCommandCompleter(completer: ShellCommandCompleter): Function {
491558
return function(
492559
_target: any,
@@ -496,7 +563,15 @@ export function shellCommandCompleter(completer: ShellCommandCompleter): Functio
496563
descriptor.value.shellCommandCompleter = completer;
497564
};
498565
}
499-
export function returnType(type: string | TypeSignature): Function {
566+
567+
/**
568+
* Marks the decorated method as returning a (resolved) value of the given Shell API type.
569+
* The type is given as string being the classname of the Shell API class.
570+
* Specify `'this'` in order to return a value of the methods surrounding class type.
571+
*
572+
* @param type The Shell API return type of the method
573+
*/
574+
export function returnType(type: string): Function {
500575
return function(
501576
_target: any,
502577
_propertyKey: string,
@@ -505,12 +580,21 @@ export function returnType(type: string | TypeSignature): Function {
505580
descriptor.value.returnType = type;
506581
};
507582
}
508-
export function classReturnsPromise(constructor: Function): void {
509-
constructor.prototype.returnsPromise = true;
510-
}
583+
584+
/**
585+
* Marks the constructor of the decorated class as being deprecated.
586+
*
587+
* Calling the constructor will automatically emit a telemetry event but
588+
* will **not** print an automatic deprecation warning (see `printDeprecationWarning`).
589+
*/
511590
export function classDeprecated(constructor: Function): void {
512591
constructor.prototype.deprecated = true;
513592
}
593+
594+
/**
595+
* Marks the decorated method as only being supported on the given platforms.
596+
* @param platformsArray The platforms the method is supported on
597+
*/
514598
export function platforms(platformsArray: any[]): Function {
515599
return function(
516600
_target: any,
@@ -520,11 +604,21 @@ export function platforms(platformsArray: any[]): Function {
520604
descriptor.value.platforms = platformsArray;
521605
};
522606
}
607+
608+
/**
609+
* Marks the constructor of the decorated class as only being supported on the given platforms.
610+
* @param platformsArray The platforms the method is supported on
611+
*/
523612
export function classPlatforms(platformsArray: any[]): Function {
524613
return function(constructor: Function): void {
525614
constructor.prototype.platforms = platformsArray;
526615
};
527616
}
617+
618+
/**
619+
* Marks the decorated class that for all methods in the class additional
620+
* source information of the call will be added to the calls returned result.
621+
*/
528622
export function addSourceToResults(constructor: Function): void {
529623
(constructor as any)[addSourceToResultsSymbol] = true;
530624
}

packages/shell-api/src/mongo.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from '@mongosh/errors';
1010
import {
1111
classPlatforms,
12-
classReturnsPromise,
1312
platforms,
1413
returnsPromise,
1514
returnType,
@@ -56,7 +55,6 @@ import {
5655
import { ShellApiErrors } from './error-codes';
5756

5857
@shellApiClassDefault
59-
@classReturnsPromise
6058
@classPlatforms([ ReplPlatform.CLI ] )
6159
export default class Mongo extends ShellApiClass {
6260
private __serviceProvider: ServiceProvider | null = null;

packages/shell-api/src/session.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
classPlatforms,
3-
classReturnsPromise,
43
returnsPromise,
54
shellApiClassDefault,
65
ShellApiWithMongoClass
@@ -23,7 +22,6 @@ import { blockedByDriverMetadata } from './error-codes';
2322
import { assertArgsDefinedType } from './helpers';
2423

2524
@shellApiClassDefault
26-
@classReturnsPromise
2725
@classPlatforms([ ReplPlatform.CLI ] )
2826
export default class Session extends ShellApiWithMongoClass {
2927
public id: ServerSessionId | undefined;

0 commit comments

Comments
 (0)