update.js
9.17 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeUpdateStatement = exports.ReplaceOneOperation = exports.UpdateManyOperation = exports.UpdateOneOperation = exports.UpdateOperation = void 0;
const error_1 = require("../error");
const utils_1 = require("../utils");
const command_1 = require("./command");
const operation_1 = require("./operation");
/** @internal */
class UpdateOperation extends command_1.CommandOperation {
constructor(ns, statements, options) {
super(undefined, options);
this.options = options;
this.ns = ns;
this.statements = statements;
}
get canRetryWrite() {
if (super.canRetryWrite === false) {
return false;
}
return this.statements.every(op => op.multi == null || op.multi === false);
}
execute(server, session, callback) {
var _a;
const options = (_a = this.options) !== null && _a !== void 0 ? _a : {};
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
const command = {
update: this.ns.collection,
updates: this.statements,
ordered
};
if (typeof options.bypassDocumentValidation === 'boolean') {
command.bypassDocumentValidation = options.bypassDocumentValidation;
}
if (options.let) {
command.let = options.let;
}
// we check for undefined specifically here to allow falsy values
// eslint-disable-next-line no-restricted-syntax
if (options.comment !== undefined) {
command.comment = options.comment;
}
const statementWithCollation = this.statements.find(statement => !!statement.collation);
if ((0, utils_1.collationNotSupported)(server, options) ||
(statementWithCollation && (0, utils_1.collationNotSupported)(server, statementWithCollation))) {
callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support collation`));
return;
}
const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
if (unacknowledgedWrite || (0, utils_1.maxWireVersion)(server) < 5) {
if (this.statements.find((o) => o.hint)) {
callback(new error_1.MongoCompatibilityError(`Servers < 3.4 do not support hint on update`));
return;
}
}
if (this.explain && (0, utils_1.maxWireVersion)(server) < 3) {
callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on update`));
return;
}
if (this.statements.some(statement => !!statement.arrayFilters) && (0, utils_1.maxWireVersion)(server) < 6) {
callback(new error_1.MongoCompatibilityError('Option "arrayFilters" is only supported on MongoDB 3.6+'));
return;
}
super.executeCommand(server, session, command, callback);
}
}
exports.UpdateOperation = UpdateOperation;
/** @internal */
class UpdateOneOperation extends UpdateOperation {
constructor(collection, filter, update, options) {
super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: false })], options);
if (!(0, utils_1.hasAtomicOperators)(update)) {
throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
}
}
execute(server, session, callback) {
super.execute(server, session, (err, res) => {
var _a, _b;
if (err || !res)
return callback(err);
if (this.explain != null)
return callback(undefined, res);
if (res.code)
return callback(new error_1.MongoServerError(res));
if (res.writeErrors)
return callback(new error_1.MongoServerError(res.writeErrors[0]));
callback(undefined, {
acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
modifiedCount: res.nModified != null ? res.nModified : res.n,
upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
});
});
}
}
exports.UpdateOneOperation = UpdateOneOperation;
/** @internal */
class UpdateManyOperation extends UpdateOperation {
constructor(collection, filter, update, options) {
super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: true })], options);
if (!(0, utils_1.hasAtomicOperators)(update)) {
throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
}
}
execute(server, session, callback) {
super.execute(server, session, (err, res) => {
var _a, _b;
if (err || !res)
return callback(err);
if (this.explain != null)
return callback(undefined, res);
if (res.code)
return callback(new error_1.MongoServerError(res));
if (res.writeErrors)
return callback(new error_1.MongoServerError(res.writeErrors[0]));
callback(undefined, {
acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
modifiedCount: res.nModified != null ? res.nModified : res.n,
upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
});
});
}
}
exports.UpdateManyOperation = UpdateManyOperation;
/** @internal */
class ReplaceOneOperation extends UpdateOperation {
constructor(collection, filter, replacement, options) {
super(collection.s.namespace, [makeUpdateStatement(filter, replacement, { ...options, multi: false })], options);
if ((0, utils_1.hasAtomicOperators)(replacement)) {
throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
}
}
execute(server, session, callback) {
super.execute(server, session, (err, res) => {
var _a, _b;
if (err || !res)
return callback(err);
if (this.explain != null)
return callback(undefined, res);
if (res.code)
return callback(new error_1.MongoServerError(res));
if (res.writeErrors)
return callback(new error_1.MongoServerError(res.writeErrors[0]));
callback(undefined, {
acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
modifiedCount: res.nModified != null ? res.nModified : res.n,
upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
});
});
}
}
exports.ReplaceOneOperation = ReplaceOneOperation;
function makeUpdateStatement(filter, update, options) {
if (filter == null || typeof filter !== 'object') {
throw new error_1.MongoInvalidArgumentError('Selector must be a valid JavaScript object');
}
if (update == null || typeof update !== 'object') {
throw new error_1.MongoInvalidArgumentError('Document must be a valid JavaScript object');
}
const op = { q: filter, u: update };
if (typeof options.upsert === 'boolean') {
op.upsert = options.upsert;
}
if (options.multi) {
op.multi = options.multi;
}
if (options.hint) {
op.hint = options.hint;
}
if (options.arrayFilters) {
op.arrayFilters = options.arrayFilters;
}
if (options.collation) {
op.collation = options.collation;
}
return op;
}
exports.makeUpdateStatement = makeUpdateStatement;
(0, operation_1.defineAspects)(UpdateOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION, operation_1.Aspect.SKIP_COLLATION]);
(0, operation_1.defineAspects)(UpdateOneOperation, [
operation_1.Aspect.RETRYABLE,
operation_1.Aspect.WRITE_OPERATION,
operation_1.Aspect.EXPLAINABLE,
operation_1.Aspect.SKIP_COLLATION
]);
(0, operation_1.defineAspects)(UpdateManyOperation, [
operation_1.Aspect.WRITE_OPERATION,
operation_1.Aspect.EXPLAINABLE,
operation_1.Aspect.SKIP_COLLATION
]);
(0, operation_1.defineAspects)(ReplaceOneOperation, [
operation_1.Aspect.RETRYABLE,
operation_1.Aspect.WRITE_OPERATION,
operation_1.Aspect.SKIP_COLLATION
]);
//# sourceMappingURL=update.js.map