Skip to content

Commit 6bd506b

Browse files
authored
test(NODE-3297): translate community typings tests to tsd format (#2822)
1 parent 9608c6a commit 6bd506b

32 files changed

+2180
-83
lines changed

src/collection.ts

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import {
7979
} from './operations/map_reduce';
8080
import { OptionsOperation } from './operations/options_operation';
8181
import { RenameOperation, RenameOptions } from './operations/rename';
82-
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
82+
import { CollStats, CollStatsOperation, CollStatsOptions } from './operations/stats';
8383
import { executeOperation } from './operations/execute_operation';
8484
import type { Db } from './db';
8585
import type { OperationOptions, Hint } from './operations/operation';
@@ -89,7 +89,14 @@ import type { PkFactory } from './mongo_client';
8989
import type { Logger, LoggerOptions } from './logger';
9090
import { FindCursor } from './cursor/find_cursor';
9191
import type { CountOptions } from './operations/count';
92-
import type { Filter, TODO_NODE_3286, UpdateQuery, WithId, OptionalId } from './mongo_types';
92+
import type {
93+
Filter,
94+
TODO_NODE_3286,
95+
UpdateQuery,
96+
WithId,
97+
OptionalId,
98+
FlattenIfArray
99+
} from './mongo_types';
93100

94101
/** @public */
95102
export interface ModifyResult<TSchema = Document> {
@@ -665,25 +672,42 @@ export class Collection<TSchema extends Document = Document> {
665672
* @param options - Optional settings for the command
666673
* @param callback - An optional callback, a Promise will be returned if none is provided
667674
*/
668-
findOne(): Promise<TSchema>;
669-
findOne(callback: Callback<TSchema>): void;
670-
findOne(filter: Filter<TSchema>): Promise<TSchema>;
671-
findOne(filter: Filter<TSchema>, callback?: Callback<TSchema>): void;
672-
findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema>;
673-
findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema>): void;
675+
findOne(): Promise<TSchema | undefined>;
676+
findOne(callback: Callback<TSchema | undefined>): void;
677+
findOne(filter: Filter<TSchema>): Promise<TSchema | undefined>;
678+
findOne(filter: Filter<TSchema>, callback: Callback<TSchema | undefined>): void;
679+
findOne(filter: Filter<TSchema>, options: FindOptions<TSchema>): Promise<TSchema | undefined>;
674680
findOne(
675-
filter?: Filter<TSchema> | Callback<TSchema>,
676-
options?: FindOptions | Callback<TSchema>,
681+
filter: Filter<TSchema>,
682+
options: FindOptions<TSchema>,
683+
callback: Callback<TSchema | undefined>
684+
): void;
685+
686+
// allow an override of the schema.
687+
findOne<T = TSchema>(): Promise<T | undefined>;
688+
findOne<T = TSchema>(callback: Callback<T | undefined>): void;
689+
findOne<T = TSchema>(filter: Filter<T>): Promise<T | undefined>;
690+
findOne<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): Promise<T | undefined>;
691+
findOne<T = TSchema>(
692+
filter: Filter<T>,
693+
options?: FindOptions<T>,
694+
callback?: Callback<T | undefined>
695+
): void;
696+
697+
findOne(
698+
filter?: Filter<TSchema> | Callback<TSchema | undefined>,
699+
options?: FindOptions<TSchema> | Callback<TSchema | undefined>,
677700
callback?: Callback<TSchema>
678-
): Promise<TSchema> | void {
701+
): Promise<TSchema | undefined> | void {
679702
if (callback !== undefined && typeof callback !== 'function') {
680703
throw new MongoDriverError('Third parameter to `findOne()` must be a callback or undefined');
681704
}
682705

683706
if (typeof filter === 'function')
684-
(callback = filter as Callback<Document>), (filter = {}), (options = {});
707+
(callback = filter as Callback<Document | undefined>), (filter = {}), (options = {});
685708
if (typeof options === 'function') (callback = options), (options = {});
686-
filter = filter || {};
709+
710+
filter ??= {};
687711

688712
return executeOperation(
689713
getTopology(this),
@@ -692,7 +716,7 @@ export class Collection<TSchema extends Document = Document> {
692716
filter,
693717
resolveOptions(this, options)
694718
) as TODO_NODE_3286,
695-
callback
719+
callback as TODO_NODE_3286
696720
);
697721
}
698722

@@ -702,8 +726,8 @@ export class Collection<TSchema extends Document = Document> {
702726
* @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
703727
*/
704728
find(): FindCursor<TSchema>;
705-
find(filter: Filter<TSchema>): FindCursor<TSchema>;
706-
find(filter: Filter<TSchema>, options: FindOptions<TSchema>): FindCursor<TSchema>;
729+
find(filter: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema>;
730+
find<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): FindCursor<T>;
707731
find(filter?: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema> {
708732
if (arguments.length > 2) {
709733
throw new MongoDriverError('Third parameter to `collection.find()` must be undefined');
@@ -1054,6 +1078,7 @@ export class Collection<TSchema extends Document = Document> {
10541078
options: CountDocumentsOptions,
10551079
callback: Callback<number>
10561080
): void;
1081+
countDocuments(filter: Filter<TSchema>, callback: Callback<number>): void;
10571082
countDocuments(
10581083
filter?: Document | CountDocumentsOptions | Callback<number>,
10591084
options?: CountDocumentsOptions | Callback<number>,
@@ -1089,25 +1114,47 @@ export class Collection<TSchema extends Document = Document> {
10891114
* @param options - Optional settings for the command
10901115
* @param callback - An optional callback, a Promise will be returned if none is provided
10911116
*/
1092-
distinct<Key extends keyof WithId<TSchema>>(key: Key): Promise<any[]>;
1093-
distinct<Key extends keyof WithId<TSchema>>(key: Key, callback: Callback<any[]>): void;
1094-
distinct<Key extends keyof WithId<TSchema>>(key: Key, filter: Filter<TSchema>): Promise<any[]>;
1117+
distinct<Key extends keyof WithId<TSchema>>(
1118+
key: Key
1119+
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
1120+
distinct<Key extends keyof WithId<TSchema>>(
1121+
key: Key,
1122+
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
1123+
): void;
1124+
distinct<Key extends keyof WithId<TSchema>>(
1125+
key: Key,
1126+
filter: Filter<TSchema>
1127+
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
10951128
distinct<Key extends keyof WithId<TSchema>>(
10961129
key: Key,
10971130
filter: Filter<TSchema>,
1098-
callback: Callback<any[]>
1131+
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
10991132
): void;
11001133
distinct<Key extends keyof WithId<TSchema>>(
11011134
key: Key,
11021135
filter: Filter<TSchema>,
11031136
options: DistinctOptions
1104-
): Promise<any[]>;
1137+
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
11051138
distinct<Key extends keyof WithId<TSchema>>(
11061139
key: Key,
11071140
filter: Filter<TSchema>,
11081141
options: DistinctOptions,
1142+
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
1143+
): void;
1144+
1145+
// Embedded documents overload
1146+
distinct(key: string): Promise<any[]>;
1147+
distinct(key: string, callback: Callback<any[]>): void;
1148+
distinct(key: string, filter: Filter<TSchema>): Promise<any[]>;
1149+
distinct(key: string, filter: Filter<TSchema>, callback: Callback<any[]>): void;
1150+
distinct(key: string, filter: Filter<TSchema>, options: DistinctOptions): Promise<any[]>;
1151+
distinct(
1152+
key: string,
1153+
filter: Filter<TSchema>,
1154+
options: DistinctOptions,
11091155
callback: Callback<any[]>
11101156
): void;
1157+
// Implementation
11111158
distinct<Key extends keyof WithId<TSchema>>(
11121159
key: Key,
11131160
filter?: Filter<TSchema> | DistinctOptions | Callback<any[]>,
@@ -1164,14 +1211,14 @@ export class Collection<TSchema extends Document = Document> {
11641211
* @param options - Optional settings for the command
11651212
* @param callback - An optional callback, a Promise will be returned if none is provided
11661213
*/
1167-
stats(): Promise<Document>;
1168-
stats(callback: Callback<Document>): void;
1169-
stats(options: CollStatsOptions): Promise<Document>;
1170-
stats(options: CollStatsOptions, callback: Callback<Document>): void;
1214+
stats(): Promise<CollStats>;
1215+
stats(callback: Callback<CollStats>): void;
1216+
stats(options: CollStatsOptions): Promise<CollStats>;
1217+
stats(options: CollStatsOptions, callback: Callback<CollStats>): void;
11711218
stats(
1172-
options?: CollStatsOptions | Callback<Document>,
1173-
callback?: Callback<Document>
1174-
): Promise<Document> | void {
1219+
options?: CollStatsOptions | Callback<CollStats>,
1220+
callback?: Callback<CollStats>
1221+
): Promise<CollStats> | void {
11751222
if (typeof options === 'function') (callback = options), (options = {});
11761223
options = options ?? {};
11771224

@@ -1372,27 +1419,27 @@ export class Collection<TSchema extends Document = Document> {
13721419
* @param callback - An optional callback, a Promise will be returned if none is provided
13731420
*/
13741421
mapReduce<TKey = any, TValue = any>(
1375-
map: string | MapFunction<TValue>,
1422+
map: string | MapFunction<TSchema>,
13761423
reduce: string | ReduceFunction<TKey, TValue>
13771424
): Promise<Document | Document[]>;
13781425
mapReduce<TKey = any, TValue = any>(
1379-
map: string | MapFunction<TValue>,
1426+
map: string | MapFunction<TSchema>,
13801427
reduce: string | ReduceFunction<TKey, TValue>,
13811428
callback: Callback<Document | Document[]>
13821429
): void;
13831430
mapReduce<TKey = any, TValue = any>(
1384-
map: string | MapFunction<TValue>,
1431+
map: string | MapFunction<TSchema>,
13851432
reduce: string | ReduceFunction<TKey, TValue>,
13861433
options: MapReduceOptions<TKey, TValue>
13871434
): Promise<Document | Document[]>;
13881435
mapReduce<TKey = any, TValue = any>(
1389-
map: string | MapFunction<TValue>,
1436+
map: string | MapFunction<TSchema>,
13901437
reduce: string | ReduceFunction<TKey, TValue>,
13911438
options: MapReduceOptions<TKey, TValue>,
13921439
callback: Callback<Document | Document[]>
13931440
): void;
13941441
mapReduce<TKey = any, TValue = any>(
1395-
map: string | MapFunction<TValue>,
1442+
map: string | MapFunction<TSchema>,
13961443
reduce: string | ReduceFunction<TKey, TValue>,
13971444
options?: MapReduceOptions<TKey, TValue> | Callback<Document | Document[]>,
13981445
callback?: Callback<Document | Document[]>

src/cursor/aggregation_cursor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
106106
}
107107

108108
/** Add a group stage to the aggregation pipeline */
109+
group<T = TSchema>($group: Document): AggregationCursor<T>;
109110
group($group: Document): this {
110111
assertUninitialized(this);
111112
this[kPipeline].push({ $group });
@@ -134,6 +135,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
134135
}
135136

136137
/** Add a project stage to the aggregation pipeline */
138+
project<T = TSchema>($project: Document): AggregationCursor<T>;
137139
project($project: Document): this {
138140
assertUninitialized(this);
139141
this[kPipeline].push({ $project });
@@ -169,7 +171,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
169171
}
170172

171173
/** Add a unwind stage to the aggregation pipeline */
172-
unwind($unwind: number): this {
174+
unwind($unwind: Document | string): this {
173175
assertUninitialized(this);
174176
this[kPipeline].push({ $unwind });
175177
return this;

src/cursor/find_cursor.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { ClientSession } from '../sessions';
1212
import { formatSort, Sort, SortDirection } from '../sort';
1313
import type { Callback, MongoDBNamespace } from '../utils';
1414
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
15+
import type { Projection, ProjectionOperators, SchemaMember } from '../mongo_types';
1516

1617
/** @internal */
1718
const kFilter = Symbol('filter');
@@ -341,7 +342,9 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
341342
*
342343
* @param value - The field projection object.
343344
*/
344-
project(value: Document): this {
345+
// TODO(NODE-3343): add parameterized cursor return type
346+
project<T = TSchema>(value: SchemaMember<T, ProjectionOperators | number | boolean | any>): this;
347+
project(value: Projection<TSchema>): this {
345348
assertUninitialized(this);
346349
this[kBuiltOptions].projection = value;
347350
return this;

src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { Admin } from './admin';
88
import { MongoClient } from './mongo_client';
99
import { Db } from './db';
1010
import { Collection } from './collection';
11-
import { ReadPreference } from './read_preference';
1211
import { Logger } from './logger';
1312
import { GridFSBucket } from './gridfs-stream';
1413
import { CancellationToken } from './mongo_types';
@@ -49,7 +48,6 @@ export {
4948
MongoClient,
5049
Db,
5150
Collection,
52-
ReadPreference,
5351
Logger,
5452
AbstractCursor,
5553
AggregationCursor,
@@ -74,6 +72,13 @@ export { ExplainVerbosity } from './explain';
7472
export { ReadConcernLevel } from './read_concern';
7573
export { ReadPreferenceMode } from './read_preference';
7674
export { ServerApiVersion } from './mongo_client';
75+
export { BSONType } from './mongo_types';
76+
77+
// Helper classes
78+
export { WriteConcern } from './write_concern';
79+
export { ReadConcern } from './read_concern';
80+
export { ReadPreference } from './read_preference';
81+
7782
// events
7883
export {
7984
CommandStartedEvent,
@@ -274,15 +279,20 @@ export type { RemoveUserOptions } from './operations/remove_user';
274279
export type { RenameOptions } from './operations/rename';
275280
export type { RunCommandOptions } from './operations/run_command';
276281
export type { SetProfilingLevelOptions } from './operations/set_profiling_level';
277-
export type { CollStatsOptions, DbStatsOptions } from './operations/stats';
282+
export type {
283+
CollStatsOptions,
284+
DbStatsOptions,
285+
CollStats,
286+
WiredTigerData
287+
} from './operations/stats';
278288
export type {
279289
UpdateResult,
280290
UpdateOptions,
281291
ReplaceOptions,
282292
UpdateStatement
283293
} from './operations/update';
284294
export type { ValidateCollectionOptions } from './operations/validate_collection';
285-
export type { ReadConcern, ReadConcernLike } from './read_concern';
295+
export type { ReadConcernLike } from './read_concern';
286296
export type {
287297
ReadPreferenceLike,
288298
ReadPreferenceOptions,
@@ -340,7 +350,7 @@ export type {
340350
HostAddress,
341351
EventEmitterWithState
342352
} from './utils';
343-
export type { WriteConcern, W, WriteConcernOptions, WriteConcernSettings } from './write_concern';
353+
export type { W, WriteConcernOptions, WriteConcernSettings } from './write_concern';
344354
export type { ExecutionResult } from './operations/execute_operation';
345355
export type { InternalAbstractCursorOptions } from './cursor/abstract_cursor';
346356
export type { BulkOperationBase, BulkOperationPrivate, FindOperators, Batch } from './bulk/common';
@@ -357,7 +367,14 @@ export type {
357367
Projection,
358368
InferIdType,
359369
ProjectionOperators,
360-
MetaProjectionOperators,
361-
MetaSortOperators
370+
FlattenIfArray,
371+
SchemaMember,
372+
Condition,
373+
RootFilterOperators,
374+
AlternativeType,
375+
FilterOperators,
376+
BSONTypeAlias,
377+
BitwiseFilter,
378+
RegExpOrString
362379
} from './mongo_types';
363380
export type { serialize, deserialize } from './bson';

0 commit comments

Comments
 (0)