-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
find_and_modify.ts
274 lines (237 loc) · 8.86 KB
/
find_and_modify.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { ReadPreference } from '../read_preference';
import { maxWireVersion, decorateWithCollation, hasAtomicOperators, Callback } from '../utils';
import { MongoDriverError } from '../error';
import { CommandOperation, CommandOperationOptions } from './command';
import { defineAspects, Aspect } from './operation';
import type { Document } from '../bson';
import type { Server } from '../sdam/server';
import type { Collection } from '../collection';
import { Sort, SortForCmd, formatSort } from '../sort';
import type { ClientSession } from '../sessions';
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
/** @public */
export const ReturnDocument = Object.freeze({
BEFORE: 'before',
AFTER: 'after'
} as const);
/** @public */
export type ReturnDocument = typeof ReturnDocument[keyof typeof ReturnDocument];
/** @public */
export interface FindOneAndDeleteOptions extends CommandOperationOptions {
/** An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/
hint?: Document;
/** Limits the fields to return for all matching documents. */
projection?: Document;
/** Determines which document the operation modifies if the query selects multiple documents. */
sort?: Sort;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
}
/** @public */
export interface FindOneAndReplaceOptions extends CommandOperationOptions {
/** Allow driver to bypass schema validation in MongoDB 3.2 or higher. */
bypassDocumentValidation?: boolean;
/** An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/
hint?: Document;
/** Limits the fields to return for all matching documents. */
projection?: Document;
/** When set to 'after', returns the updated document rather than the original. The default is 'before'. */
returnDocument?: ReturnDocument;
/** Determines which document the operation modifies if the query selects multiple documents. */
sort?: Sort;
/** Upsert the document if it does not exist. */
upsert?: boolean;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
}
/** @public */
export interface FindOneAndUpdateOptions extends CommandOperationOptions {
/** Optional list of array filters referenced in filtered positional operators */
arrayFilters?: Document[];
/** Allow driver to bypass schema validation in MongoDB 3.2 or higher. */
bypassDocumentValidation?: boolean;
/** An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/
hint?: Document;
/** Limits the fields to return for all matching documents. */
projection?: Document;
/** When set to 'after', returns the updated document rather than the original. The default is 'before'. */
returnDocument?: ReturnDocument;
/** Determines which document the operation modifies if the query selects multiple documents. */
sort?: Sort;
/** Upsert the document if it does not exist. */
upsert?: boolean;
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
let?: Document;
}
/** @internal */
interface FindAndModifyCmdBase {
remove: boolean;
new: boolean;
upsert: boolean;
update?: Document;
sort?: SortForCmd;
fields?: Document;
bypassDocumentValidation?: boolean;
arrayFilters?: Document[];
maxTimeMS?: number;
let?: Document;
writeConcern?: WriteConcern | WriteConcernSettings;
}
function configureFindAndModifyCmdBaseUpdateOpts(
cmdBase: FindAndModifyCmdBase,
options: FindOneAndReplaceOptions | FindOneAndUpdateOptions
): FindAndModifyCmdBase {
cmdBase.new = options.returnDocument === ReturnDocument.AFTER;
cmdBase.upsert = options.upsert === true;
if (options.bypassDocumentValidation === true) {
cmdBase.bypassDocumentValidation = options.bypassDocumentValidation;
}
return cmdBase;
}
/** @internal */
class FindAndModifyOperation extends CommandOperation<Document> {
options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions;
cmdBase: FindAndModifyCmdBase;
collection: Collection;
query: Document;
doc?: Document;
constructor(
collection: Collection,
query: Document,
options: FindOneAndReplaceOptions | FindOneAndUpdateOptions | FindOneAndDeleteOptions
) {
super(collection, options);
this.options = options ?? {};
this.cmdBase = {
remove: false,
new: false,
upsert: false
};
const sort = formatSort(options.sort);
if (sort) {
this.cmdBase.sort = sort;
}
if (options.projection) {
this.cmdBase.fields = options.projection;
}
if (options.maxTimeMS) {
this.cmdBase.maxTimeMS = options.maxTimeMS;
}
// Decorate the findAndModify command with the write Concern
if (options.writeConcern) {
this.cmdBase.writeConcern = options.writeConcern;
}
if (options.let) {
this.cmdBase.let = options.let;
}
// force primary read preference
this.readPreference = ReadPreference.primary;
this.collection = collection;
this.query = query;
}
execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
const coll = this.collection;
const query = this.query;
const options = { ...this.options, ...this.bsonOptions };
// Create findAndModify command object
const cmd: Document = {
findAndModify: coll.collectionName,
query: query,
...this.cmdBase
};
// Have we specified collation
try {
decorateWithCollation(cmd, coll, options);
} catch (err) {
return callback(err);
}
if (options.hint) {
// TODO: once this method becomes a CommandOperation we will have the server
// in place to check.
const unacknowledgedWrite = this.writeConcern?.w === 0;
if (unacknowledgedWrite || maxWireVersion(server) < 8) {
callback(
new MongoDriverError(
'The current topology does not support a hint on findAndModify commands'
)
);
return;
}
cmd.hint = options.hint;
}
if (this.explain && maxWireVersion(server) < 4) {
callback(
new MongoDriverError(`server ${server.name} does not support explain on findAndModify`)
);
return;
}
// Execute the command
super.executeCommand(server, session, cmd, (err, result) => {
if (err) return callback(err);
return callback(undefined, result);
});
}
}
/** @internal */
export class FindOneAndDeleteOperation extends FindAndModifyOperation {
constructor(collection: Collection, filter: Document, options: FindOneAndDeleteOptions) {
// Basic validation
if (filter == null || typeof filter !== 'object') {
throw new MongoDriverError('Filter parameter must be an object');
}
super(collection, filter, options);
this.cmdBase.remove = true;
}
}
/** @internal */
export class FindOneAndReplaceOperation extends FindAndModifyOperation {
constructor(
collection: Collection,
filter: Document,
replacement: Document,
options: FindOneAndReplaceOptions
) {
if (filter == null || typeof filter !== 'object') {
throw new MongoDriverError('Filter parameter must be an object');
}
if (replacement == null || typeof replacement !== 'object') {
throw new MongoDriverError('Replacement parameter must be an object');
}
if (hasAtomicOperators(replacement)) {
throw new MongoDriverError('Replacement document must not contain atomic operators');
}
super(collection, filter, options);
this.cmdBase.update = replacement;
configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
}
}
/** @internal */
export class FindOneAndUpdateOperation extends FindAndModifyOperation {
constructor(
collection: Collection,
filter: Document,
update: Document,
options: FindOneAndUpdateOptions
) {
if (filter == null || typeof filter !== 'object') {
throw new MongoDriverError('Filter parameter must be an object');
}
if (update == null || typeof update !== 'object') {
throw new MongoDriverError('Update parameter must be an object');
}
if (!hasAtomicOperators(update)) {
throw new MongoDriverError('Update document requires atomic operators');
}
super(collection, filter, options);
this.cmdBase.update = update;
configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
if (options.arrayFilters) {
this.cmdBase.arrayFilters = options.arrayFilters;
}
}
}
defineAspects(FindAndModifyOperation, [
Aspect.WRITE_OPERATION,
Aspect.RETRYABLE,
Aspect.EXPLAINABLE
]);