Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 80 additions & 33 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import {
} from './operations/map_reduce';
import { OptionsOperation } from './operations/options_operation';
import { RenameOperation, RenameOptions } from './operations/rename';
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
import { CollStats, CollStatsOperation, CollStatsOptions } from './operations/stats';
import { executeOperation } from './operations/execute_operation';
import type { Db } from './db';
import type { OperationOptions, Hint } from './operations/operation';
Expand All @@ -89,7 +89,14 @@ import type { PkFactory } from './mongo_client';
import type { Logger, LoggerOptions } from './logger';
import { FindCursor } from './cursor/find_cursor';
import type { CountOptions } from './operations/count';
import type { Filter, TODO_NODE_3286, UpdateQuery, WithId, OptionalId } from './mongo_types';
import type {
Filter,
TODO_NODE_3286,
UpdateQuery,
WithId,
OptionalId,
FlattenIfArray
} from './mongo_types';

/** @public */
export interface ModifyResult<TSchema = Document> {
Expand Down Expand Up @@ -665,25 +672,42 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
findOne(): Promise<TSchema>;
findOne(callback: Callback<TSchema>): void;
findOne(filter: Filter<TSchema>): Promise<TSchema>;
findOne(filter: Filter<TSchema>, callback?: Callback<TSchema>): void;
findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema>;
findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema>): void;
findOne(): Promise<TSchema | undefined>;
findOne(callback: Callback<TSchema | undefined>): void;
findOne(filter: Filter<TSchema>): Promise<TSchema | undefined>;
findOne(filter: Filter<TSchema>, callback: Callback<TSchema | undefined>): void;
findOne(filter: Filter<TSchema>, options: FindOptions<TSchema>): Promise<TSchema | undefined>;
findOne(
filter?: Filter<TSchema> | Callback<TSchema>,
options?: FindOptions | Callback<TSchema>,
filter: Filter<TSchema>,
options: FindOptions<TSchema>,
callback: Callback<TSchema | undefined>
): void;

// allow an override of the schema.
findOne<T = TSchema>(): Promise<T | undefined>;
findOne<T = TSchema>(callback: Callback<T | undefined>): void;
findOne<T = TSchema>(filter: Filter<T>): Promise<T | undefined>;
findOne<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): Promise<T | undefined>;
findOne<T = TSchema>(
filter: Filter<T>,
options?: FindOptions<T>,
callback?: Callback<T | undefined>
): void;

findOne(
filter?: Filter<TSchema> | Callback<TSchema | undefined>,
options?: FindOptions<TSchema> | Callback<TSchema | undefined>,
callback?: Callback<TSchema>
): Promise<TSchema> | void {
): Promise<TSchema | undefined> | void {
if (callback !== undefined && typeof callback !== 'function') {
throw new MongoDriverError('Third parameter to `findOne()` must be a callback or undefined');
}

if (typeof filter === 'function')
(callback = filter as Callback<Document>), (filter = {}), (options = {});
(callback = filter as Callback<Document | undefined>), (filter = {}), (options = {});
if (typeof options === 'function') (callback = options), (options = {});
filter = filter || {};

filter ??= {};

return executeOperation(
getTopology(this),
Expand All @@ -692,7 +716,7 @@ export class Collection<TSchema extends Document = Document> {
filter,
resolveOptions(this, options)
) as TODO_NODE_3286,
callback
callback as TODO_NODE_3286
);
}

Expand All @@ -702,8 +726,8 @@ export class Collection<TSchema extends Document = Document> {
* @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
*/
find(): FindCursor<TSchema>;
find(filter: Filter<TSchema>): FindCursor<TSchema>;
find(filter: Filter<TSchema>, options: FindOptions<TSchema>): FindCursor<TSchema>;
find(filter: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema>;
find<T = TSchema>(filter: Filter<T>, options?: FindOptions<T>): FindCursor<T>;
find(filter?: Filter<TSchema>, options?: FindOptions<TSchema>): FindCursor<TSchema> {
if (arguments.length > 2) {
throw new MongoDriverError('Third parameter to `collection.find()` must be undefined');
Expand Down Expand Up @@ -1054,6 +1078,7 @@ export class Collection<TSchema extends Document = Document> {
options: CountDocumentsOptions,
callback: Callback<number>
): void;
countDocuments(filter: Filter<TSchema>, callback: Callback<number>): void;
countDocuments(
filter?: Document | CountDocumentsOptions | Callback<number>,
options?: CountDocumentsOptions | Callback<number>,
Expand Down Expand Up @@ -1089,25 +1114,47 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
distinct<Key extends keyof WithId<TSchema>>(key: Key): Promise<any[]>;
distinct<Key extends keyof WithId<TSchema>>(key: Key, callback: Callback<any[]>): void;
distinct<Key extends keyof WithId<TSchema>>(key: Key, filter: Filter<TSchema>): Promise<any[]>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
): void;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>,
callback: Callback<any[]>
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
): void;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>,
options: DistinctOptions
): Promise<any[]>;
): Promise<Array<FlattenIfArray<WithId<TSchema>[Key]>>>;
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter: Filter<TSchema>,
options: DistinctOptions,
callback: Callback<Array<FlattenIfArray<WithId<TSchema>[Key]>>>
): void;

// Embedded documents overload
distinct(key: string): Promise<any[]>;
distinct(key: string, callback: Callback<any[]>): void;
distinct(key: string, filter: Filter<TSchema>): Promise<any[]>;
distinct(key: string, filter: Filter<TSchema>, callback: Callback<any[]>): void;
distinct(key: string, filter: Filter<TSchema>, options: DistinctOptions): Promise<any[]>;
distinct(
key: string,
filter: Filter<TSchema>,
options: DistinctOptions,
callback: Callback<any[]>
): void;
// Implementation
distinct<Key extends keyof WithId<TSchema>>(
key: Key,
filter?: Filter<TSchema> | DistinctOptions | Callback<any[]>,
Expand Down Expand Up @@ -1164,14 +1211,14 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
stats(): Promise<Document>;
stats(callback: Callback<Document>): void;
stats(options: CollStatsOptions): Promise<Document>;
stats(options: CollStatsOptions, callback: Callback<Document>): void;
stats(): Promise<CollStats>;
stats(callback: Callback<CollStats>): void;
stats(options: CollStatsOptions): Promise<CollStats>;
stats(options: CollStatsOptions, callback: Callback<CollStats>): void;
stats(
options?: CollStatsOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: CollStatsOptions | Callback<CollStats>,
callback?: Callback<CollStats>
): Promise<CollStats> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};

Expand Down Expand Up @@ -1372,27 +1419,27 @@ export class Collection<TSchema extends Document = Document> {
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TValue>,
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>
): Promise<Document | Document[]>;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TValue>,
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
callback: Callback<Document | Document[]>
): void;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TValue>,
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options: MapReduceOptions<TKey, TValue>
): Promise<Document | Document[]>;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TValue>,
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options: MapReduceOptions<TKey, TValue>,
callback: Callback<Document | Document[]>
): void;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TValue>,
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options?: MapReduceOptions<TKey, TValue> | Callback<Document | Document[]>,
callback?: Callback<Document | Document[]>
Expand Down
4 changes: 3 additions & 1 deletion src/cursor/aggregation_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
}

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

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

/** Add a unwind stage to the aggregation pipeline */
unwind($unwind: number): this {
unwind($unwind: Document | string): this {
assertUninitialized(this);
this[kPipeline].push({ $unwind });
return this;
Expand Down
5 changes: 4 additions & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { ClientSession } from '../sessions';
import { formatSort, Sort, SortDirection } from '../sort';
import type { Callback, MongoDBNamespace } from '../utils';
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
import type { Projection, ProjectionOperators, SchemaMember } from '../mongo_types';

/** @internal */
const kFilter = Symbol('filter');
Expand Down Expand Up @@ -341,7 +342,9 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
*
* @param value - The field projection object.
*/
project(value: Document): this {
// TODO(NODE-3343): add parameterized cursor return type
project<T = TSchema>(value: SchemaMember<T, ProjectionOperators | number | boolean | any>): this;
project(value: Projection<TSchema>): this {
assertUninitialized(this);
this[kBuiltOptions].projection = value;
return this;
Expand Down
31 changes: 24 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Admin } from './admin';
import { MongoClient } from './mongo_client';
import { Db } from './db';
import { Collection } from './collection';
import { ReadPreference } from './read_preference';
import { Logger } from './logger';
import { GridFSBucket } from './gridfs-stream';
import { CancellationToken } from './mongo_types';
Expand Down Expand Up @@ -49,7 +48,6 @@ export {
MongoClient,
Db,
Collection,
ReadPreference,
Logger,
AbstractCursor,
AggregationCursor,
Expand All @@ -74,6 +72,13 @@ export { ExplainVerbosity } from './explain';
export { ReadConcernLevel } from './read_concern';
export { ReadPreferenceMode } from './read_preference';
export { ServerApiVersion } from './mongo_client';
export { BSONType } from './mongo_types';

// Helper classes
export { WriteConcern } from './write_concern';
export { ReadConcern } from './read_concern';
export { ReadPreference } from './read_preference';

// events
export {
CommandStartedEvent,
Expand Down Expand Up @@ -274,15 +279,20 @@ export type { RemoveUserOptions } from './operations/remove_user';
export type { RenameOptions } from './operations/rename';
export type { RunCommandOptions } from './operations/run_command';
export type { SetProfilingLevelOptions } from './operations/set_profiling_level';
export type { CollStatsOptions, DbStatsOptions } from './operations/stats';
export type {
CollStatsOptions,
DbStatsOptions,
CollStats,
WiredTigerData
} from './operations/stats';
export type {
UpdateResult,
UpdateOptions,
ReplaceOptions,
UpdateStatement
} from './operations/update';
export type { ValidateCollectionOptions } from './operations/validate_collection';
export type { ReadConcern, ReadConcernLike } from './read_concern';
export type { ReadConcernLike } from './read_concern';
export type {
ReadPreferenceLike,
ReadPreferenceOptions,
Expand Down Expand Up @@ -340,7 +350,7 @@ export type {
HostAddress,
EventEmitterWithState
} from './utils';
export type { WriteConcern, W, WriteConcernOptions, WriteConcernSettings } from './write_concern';
export type { W, WriteConcernOptions, WriteConcernSettings } from './write_concern';
export type { ExecutionResult } from './operations/execute_operation';
export type { InternalAbstractCursorOptions } from './cursor/abstract_cursor';
export type { BulkOperationBase, BulkOperationPrivate, FindOperators, Batch } from './bulk/common';
Expand All @@ -357,7 +367,14 @@ export type {
Projection,
InferIdType,
ProjectionOperators,
MetaProjectionOperators,
MetaSortOperators
FlattenIfArray,
SchemaMember,
Condition,
RootFilterOperators,
AlternativeType,
FilterOperators,
BSONTypeAlias,
BitwiseFilter,
RegExpOrString
} from './mongo_types';
export type { serialize, deserialize } from './bson';
Loading