| 918 | executor: WithSessionCallback<T> |
| 919 | ): Promise<T>; |
| 920 | async withSession<T = any>( |
| 921 | optionsOrExecutor: ClientSessionOptions | WithSessionCallback<T>, |
| 922 | executor?: WithSessionCallback<T> |
| 923 | ): Promise<T> { |
| 924 | const options = { |
| 925 | // Always define an owner |
| 926 | owner: Symbol(), |
| 927 | // If it's an object inherit the options |
| 928 | ...(typeof optionsOrExecutor === 'object' ? optionsOrExecutor : {}) |
| 929 | }; |
| 930 | |
| 931 | const withSessionCallback = |
| 932 | typeof optionsOrExecutor === 'function' ? optionsOrExecutor : executor; |
| 933 | |
| 934 | if (withSessionCallback == null) { |
| 935 | throw new MongoInvalidArgumentError('Missing required callback parameter'); |
| 936 | } |
| 937 | |
| 938 | const session = this.startSession(options); |
| 939 | |
| 940 | try { |
| 941 | return await withSessionCallback(session); |
| 942 | } finally { |
| 943 | try { |
| 944 | await session.endSession(); |
| 945 | } catch (error) { |
| 946 | squashError(error); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * Create a new Change Stream, watching for new changes (insertions, updates, |