delete.js
5.4 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeDeleteStatement = exports.DeleteManyOperation = exports.DeleteOneOperation = exports.DeleteOperation = void 0;
const error_1 = require("../error");
const utils_1 = require("../utils");
const command_1 = require("./command");
const operation_1 = require("./operation");
/** @internal */
class DeleteOperation 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.limit != null ? op.limit > 0 : true));
}
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 = {
delete: this.ns.collection,
deletes: this.statements,
ordered
};
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;
}
if (options.explain != null && (0, utils_1.maxWireVersion)(server) < 3) {
return callback
? callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on delete`))
: undefined;
}
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 delete`));
return;
}
}
const statementWithCollation = this.statements.find(statement => !!statement.collation);
if (statementWithCollation && (0, utils_1.collationNotSupported)(server, statementWithCollation)) {
callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support collation`));
return;
}
super.executeCommand(server, session, command, callback);
}
}
exports.DeleteOperation = DeleteOperation;
class DeleteOneOperation extends DeleteOperation {
constructor(collection, filter, options) {
super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
}
execute(server, session, callback) {
super.execute(server, session, (err, res) => {
var _a, _b;
if (err || res == null)
return callback(err);
if (res.code)
return callback(new error_1.MongoServerError(res));
if (res.writeErrors)
return callback(new error_1.MongoServerError(res.writeErrors[0]));
if (this.explain)
return callback(undefined, res);
callback(undefined, {
acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
deletedCount: res.n
});
});
}
}
exports.DeleteOneOperation = DeleteOneOperation;
class DeleteManyOperation extends DeleteOperation {
constructor(collection, filter, options) {
super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
}
execute(server, session, callback) {
super.execute(server, session, (err, res) => {
var _a, _b;
if (err || res == null)
return callback(err);
if (res.code)
return callback(new error_1.MongoServerError(res));
if (res.writeErrors)
return callback(new error_1.MongoServerError(res.writeErrors[0]));
if (this.explain)
return callback(undefined, res);
callback(undefined, {
acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
deletedCount: res.n
});
});
}
}
exports.DeleteManyOperation = DeleteManyOperation;
function makeDeleteStatement(filter, options) {
const op = {
q: filter,
limit: typeof options.limit === 'number' ? options.limit : 0
};
if (options.single === true) {
op.limit = 1;
}
if (options.collation) {
op.collation = options.collation;
}
if (options.hint) {
op.hint = options.hint;
}
return op;
}
exports.makeDeleteStatement = makeDeleteStatement;
(0, operation_1.defineAspects)(DeleteOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
(0, operation_1.defineAspects)(DeleteOneOperation, [
operation_1.Aspect.RETRYABLE,
operation_1.Aspect.WRITE_OPERATION,
operation_1.Aspect.EXPLAINABLE,
operation_1.Aspect.SKIP_COLLATION
]);
(0, operation_1.defineAspects)(DeleteManyOperation, [
operation_1.Aspect.WRITE_OPERATION,
operation_1.Aspect.EXPLAINABLE,
operation_1.Aspect.SKIP_COLLATION
]);
//# sourceMappingURL=delete.js.map