|
1 | | -import { DEFAULT_PK_FACTORY, emitWarningOnce, maybePromise, resolveOptions } from './utils'; |
| 1 | +import { DEFAULT_PK_FACTORY, emitWarningOnce, resolveOptions } from './utils'; |
2 | 2 | import { ReadPreference, ReadPreferenceLike } from './read_preference'; |
3 | 3 | import { |
4 | 4 | normalizeHintField, |
@@ -99,7 +99,7 @@ import type { |
99 | 99 |
|
100 | 100 | /** @public */ |
101 | 101 | export interface ModifyResult<TSchema = Document> { |
102 | | - value?: TSchema; |
| 102 | + value: TSchema | null; |
103 | 103 | lastErrorObject?: Document; |
104 | 104 | ok: 0 | 1; |
105 | 105 | } |
@@ -676,51 +676,48 @@ export class Collection<TSchema extends Document = Document> { |
676 | 676 | * @param options - Optional settings for the command |
677 | 677 | * @param callback - An optional callback, a Promise will be returned if none is provided |
678 | 678 | */ |
679 | | - findOne(): Promise<TSchema | undefined>; |
680 | | - findOne(callback: Callback<TSchema | undefined>): void; |
681 | | - findOne(filter: Filter<TSchema>): Promise<TSchema | undefined>; |
682 | | - findOne(filter: Filter<TSchema>, callback: Callback<TSchema | undefined>): void; |
683 | | - findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema | undefined>; |
684 | | - findOne( |
685 | | - filter: Filter<TSchema>, |
686 | | - options: FindOptions, |
687 | | - callback: Callback<TSchema | undefined> |
688 | | - ): void; |
| 679 | + findOne(): Promise<TSchema | null>; |
| 680 | + findOne(callback: Callback<TSchema | null>): void; |
| 681 | + findOne(filter: Filter<TSchema>): Promise<TSchema | null>; |
| 682 | + findOne(filter: Filter<TSchema>, callback: Callback<TSchema | null>): void; |
| 683 | + findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema | null>; |
| 684 | + findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema | null>): void; |
689 | 685 |
|
690 | 686 | // allow an override of the schema. |
691 | | - findOne<T = TSchema>(): Promise<T | undefined>; |
692 | | - findOne<T = TSchema>(callback: Callback<T | undefined>): void; |
693 | | - findOne<T = TSchema>(filter: Filter<TSchema>): Promise<T | undefined>; |
694 | | - findOne<T = TSchema>(filter: Filter<TSchema>, options?: FindOptions): Promise<T | undefined>; |
| 687 | + findOne<T = TSchema>(): Promise<T | null>; |
| 688 | + findOne<T = TSchema>(callback: Callback<T | null>): void; |
| 689 | + findOne<T = TSchema>(filter: Filter<TSchema>): Promise<T | null>; |
| 690 | + findOne<T = TSchema>(filter: Filter<TSchema>, options?: FindOptions): Promise<T | null>; |
695 | 691 | findOne<T = TSchema>( |
696 | 692 | filter: Filter<TSchema>, |
697 | 693 | options?: FindOptions, |
698 | | - callback?: Callback<T | undefined> |
| 694 | + callback?: Callback<T | null> |
699 | 695 | ): void; |
700 | 696 |
|
701 | 697 | findOne( |
702 | | - filter?: Filter<TSchema> | Callback<TSchema | undefined>, |
703 | | - options?: FindOptions | Callback<TSchema | undefined>, |
704 | | - callback?: Callback<TSchema> |
705 | | - ): Promise<TSchema | undefined> | void { |
| 698 | + filter?: Filter<TSchema> | Callback<TSchema | null>, |
| 699 | + options?: FindOptions | Callback<TSchema | null>, |
| 700 | + callback?: Callback<TSchema | null> |
| 701 | + ): Promise<TSchema | null> | void { |
706 | 702 | if (callback != null && typeof callback !== 'function') { |
707 | 703 | throw new MongoInvalidArgumentError( |
708 | 704 | 'Third parameter to `findOne()` must be a callback or undefined' |
709 | 705 | ); |
710 | 706 | } |
711 | 707 |
|
712 | | - if (typeof filter === 'function') |
713 | | - (callback = filter as Callback<Document | undefined>), (filter = {}), (options = {}); |
714 | | - if (typeof options === 'function') (callback = options), (options = {}); |
| 708 | + if (typeof filter === 'function') { |
| 709 | + callback = filter as Callback<TSchema | null>; |
| 710 | + filter = {}; |
| 711 | + options = {}; |
| 712 | + } |
| 713 | + if (typeof options === 'function') { |
| 714 | + callback = options; |
| 715 | + options = {}; |
| 716 | + } |
715 | 717 |
|
716 | 718 | const finalFilter = filter ?? {}; |
717 | 719 | const finalOptions = options ?? {}; |
718 | | - return maybePromise(callback, callback => |
719 | | - this.find(finalFilter, finalOptions) |
720 | | - .limit(-1) |
721 | | - .batchSize(1) |
722 | | - .next((error, result) => callback(error, result ?? undefined)) |
723 | | - ); |
| 720 | + return this.find(finalFilter, finalOptions).limit(-1).batchSize(1).next(callback); |
724 | 721 | } |
725 | 722 |
|
726 | 723 | /** |
|
0 commit comments