1
1
import { Aspect , OperationBase , OperationOptions } from './operation' ;
2
2
import { ReadConcern } from '../read_concern' ;
3
3
import { WriteConcern , WriteConcernOptions } from '../write_concern' ;
4
- import { maxWireVersion , MongoDBNamespace , Callback } from '../utils' ;
4
+ import { maxWireVersion , MongoDBNamespace , Callback , deepFreeze } from '../utils' ;
5
5
import { ReadPreference , ReadPreferenceLike } from '../read_preference' ;
6
6
import { commandSupportsReadConcern } from '../sessions' ;
7
7
import { MongoError } from '../error' ;
@@ -35,9 +35,14 @@ export interface CommandOperationOptions extends OperationOptions, WriteConcernO
35
35
noResponse ?: boolean ;
36
36
}
37
37
38
+ export interface OperationParentPrivate extends BSONSerializeOptions {
39
+ options ?: BSONSerializeOptions ;
40
+ namespace : MongoDBNamespace ;
41
+ }
42
+
38
43
/** @internal */
39
44
export interface OperationParent {
40
- s : { namespace : MongoDBNamespace } ;
45
+ s : OperationParentPrivate ;
41
46
readConcern ?: ReadConcern ;
42
47
writeConcern ?: WriteConcern ;
43
48
readPreference ?: ReadPreference ;
@@ -54,10 +59,30 @@ export abstract class CommandOperation<
54
59
readPreference : ReadPreference ;
55
60
readConcern ?: ReadConcern ;
56
61
writeConcern ?: WriteConcern ;
57
- explain : boolean ;
58
62
fullResponse ?: boolean ;
59
63
logger ?: Logger ;
60
64
65
+ protected collation ;
66
+ protected maxTimeMS ;
67
+ protected comment ;
68
+ protected retryWrites ;
69
+ protected noResponse ;
70
+
71
+ get builtOptions ( ) : Readonly < CommandOperationOptions > {
72
+ return deepFreeze ( {
73
+ ...super . builtOptions ,
74
+ collation : this . collation ,
75
+ maxTimeMS : this . maxTimeMS ,
76
+ comment : this . comment ,
77
+ retryWrites : this . retryWrites ,
78
+ noResponse : this . noResponse ,
79
+ fullResponse : this . fullResponse
80
+ // Override with proper type
81
+ // writeConcern: this.writeConcern,
82
+ // readConcern: this.readConcern
83
+ } ) ;
84
+ }
85
+
61
86
constructor ( parent ?: OperationParent , options ?: T ) {
62
87
super ( options ) ;
63
88
@@ -76,17 +101,30 @@ export abstract class CommandOperation<
76
101
const propertyProvider = this . hasAspect ( Aspect . NO_INHERIT_OPTIONS ) ? undefined : parent ;
77
102
this . readPreference = this . hasAspect ( Aspect . WRITE_OPERATION )
78
103
? ReadPreference . primary
79
- : ReadPreference . resolve ( propertyProvider , this . options ) ;
80
- this . readConcern = resolveReadConcern ( propertyProvider , this . options ) ;
81
- this . writeConcern = resolveWriteConcern ( propertyProvider , this . options ) ;
104
+ : ReadPreference . resolve ( propertyProvider , options ) ;
105
+ this . readConcern = resolveReadConcern ( propertyProvider , options ) ;
106
+ this . writeConcern = resolveWriteConcern ( propertyProvider , options ) ;
82
107
this . explain = false ;
83
108
this . fullResponse =
84
109
options && typeof options . fullResponse === 'boolean' ? options . fullResponse : false ;
85
110
111
+ // if (this.writeConcern && this.writeConcern.w === 0) {
112
+ // if (this.session && this.session.explicit) {
113
+ // throw new MongoError('Cannot have explicit session with unacknowledged writes');
114
+ // }
115
+ // return;
116
+ // }
117
+
86
118
// TODO: A lot of our code depends on having the read preference in the options. This should
87
119
// go away, but also requires massive test rewrites.
88
120
this . options . readPreference = this . readPreference ;
89
121
122
+ this . collation = options ?. collation ;
123
+ this . maxTimeMS = options ?. maxTimeMS ;
124
+ this . comment = options ?. comment ;
125
+ this . retryWrites = options ?. retryWrites ;
126
+ this . noResponse = options ?. noResponse ;
127
+
90
128
// TODO(NODE-2056): make logger another "inheritable" property
91
129
if ( parent && parent . logger ) {
92
130
this . logger = parent . logger ;
@@ -102,15 +140,14 @@ export abstract class CommandOperation<
102
140
// TODO: consider making this a non-enumerable property
103
141
this . server = server ;
104
142
105
- const options = { ...this . options , ...this . bsonOptions } ;
106
143
const serverWireVersion = maxWireVersion ( server ) ;
107
144
const inTransaction = this . session && this . session . inTransaction ( ) ;
108
145
109
146
if ( this . readConcern && commandSupportsReadConcern ( cmd ) && ! inTransaction ) {
110
147
Object . assign ( cmd , { readConcern : this . readConcern } ) ;
111
148
}
112
149
113
- if ( options . collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION ) {
150
+ if ( this . builtOptions . collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION ) {
114
151
callback (
115
152
new MongoError (
116
153
`Server ${ server . name } , which reports wire version ${ serverWireVersion } , does not support collation`
@@ -124,17 +161,17 @@ export abstract class CommandOperation<
124
161
Object . assign ( cmd , { writeConcern : this . writeConcern } ) ;
125
162
}
126
163
127
- if ( options . collation && typeof options . collation === 'object' ) {
128
- Object . assign ( cmd , { collation : options . collation } ) ;
164
+ if ( this . builtOptions . collation && typeof this . builtOptions . collation === 'object' ) {
165
+ Object . assign ( cmd , { collation : this . builtOptions . collation } ) ;
129
166
}
130
167
}
131
168
132
- if ( typeof options . maxTimeMS === 'number' ) {
133
- cmd . maxTimeMS = options . maxTimeMS ;
169
+ if ( typeof this . builtOptions . maxTimeMS === 'number' ) {
170
+ cmd . maxTimeMS = this . builtOptions . maxTimeMS ;
134
171
}
135
172
136
- if ( typeof options . comment === 'string' ) {
137
- cmd . comment = options . comment ;
173
+ if ( typeof this . builtOptions . comment === 'string' ) {
174
+ cmd . comment = this . builtOptions . comment ;
138
175
}
139
176
140
177
if ( this . logger && this . logger . isDebug ( ) ) {
@@ -144,7 +181,7 @@ export abstract class CommandOperation<
144
181
server . command (
145
182
this . ns . toString ( ) ,
146
183
cmd ,
147
- { fullResult : ! ! this . fullResponse , ...this . options } ,
184
+ { fullResult : ! ! this . fullResponse , ...this . builtOptions } ,
148
185
callback
149
186
) ;
150
187
}
0 commit comments