@@ -2,21 +2,17 @@ import { type Connection } from '..';
2
2
import type { Document } from '../bson' ;
3
3
import type { BulkWriteOptions } from '../bulk/common' ;
4
4
import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
5
+ import type { Collection } from '../collection' ;
6
+ import { MongoServerError } from '../error' ;
5
7
import type { InferIdType } from '../mongo_types' ;
8
+ import type { Server } from '../sdam/server' ;
6
9
import type { ClientSession } from '../sessions' ;
7
- import { type MongoDBNamespace } from '../utils' ;
10
+ import { type TimeoutContext } from '../timeout' ;
11
+ import { maybeAddIdToDocuments , type MongoDBNamespace } from '../utils' ;
8
12
import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
9
13
import { Aspect , defineAspects } from './operation' ;
10
-
11
14
/** @internal */
12
- export interface InsertResult {
13
- n : number ;
14
- code ?: number ;
15
- writeErrors ?: Document [ ] ;
16
- }
17
-
18
- /** @internal */
19
- export class InsertOperation extends ModernizedCommandOperation < InsertResult > {
15
+ export class InsertOperation extends ModernizedCommandOperation < Document > {
20
16
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
21
17
override options : BulkWriteOptions ;
22
18
@@ -72,6 +68,44 @@ export interface InsertOneResult<TSchema = Document> {
72
68
insertedId : InferIdType < TSchema > ;
73
69
}
74
70
71
+ export class InsertOneOperation extends InsertOperation {
72
+ constructor ( collection : Collection , doc : Document , options : InsertOneOptions ) {
73
+ super ( collection . s . namespace , [ maybeAddIdToDocuments ( collection , doc , options ) ] , options ) ;
74
+ }
75
+
76
+ override async execute (
77
+ server : Server ,
78
+ session : ClientSession | undefined ,
79
+ timeoutContext : TimeoutContext
80
+ ) : Promise < InsertOneResult > {
81
+ const res = await super . execute ( server , session , timeoutContext ) ;
82
+ if ( res . code ) throw new MongoServerError ( res ) ;
83
+ if ( res . writeErrors ) {
84
+ // This should be a WriteError but we can't change it now because of error hierarchy
85
+ throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
86
+ }
87
+
88
+ return {
89
+ acknowledged : this . writeConcern ?. w !== 0 ,
90
+ insertedId : this . documents [ 0 ] . _id
91
+ } ;
92
+ }
93
+
94
+ override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : Document {
95
+ const res = super . handleOk ( response ) ;
96
+ if ( res . code ) throw new MongoServerError ( res ) ;
97
+ if ( res . writeErrors ) {
98
+ // This should be a WriteError but we can't change it now because of error hierarchy
99
+ throw new MongoServerError ( res . writeErrors [ 0 ] ) ;
100
+ }
101
+
102
+ return {
103
+ acknowledged : this . writeConcern ?. w !== 0 ,
104
+ insertedId : this . documents [ 0 ] . _id
105
+ } ;
106
+ }
107
+ }
108
+
75
109
/** @public */
76
110
export interface InsertManyResult < TSchema = Document > {
77
111
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
@@ -83,3 +117,4 @@ export interface InsertManyResult<TSchema = Document> {
83
117
}
84
118
85
119
defineAspects ( InsertOperation , [ Aspect . RETRYABLE , Aspect . WRITE_OPERATION ] ) ;
120
+ defineAspects ( InsertOneOperation , [ Aspect . RETRYABLE , Aspect . WRITE_OPERATION ] ) ;
0 commit comments