Skip to content

Commit e7f7045

Browse files
delete
1 parent e25bdc3 commit e7f7045

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

src/operations/delete.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Document } from '../bson';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
24
import type { Collection } from '../collection';
35
import { MongoCompatibilityError, MongoServerError } from '../error';
46
import { type TODO_NODE_3286 } from '../mongo_types';
@@ -7,7 +9,11 @@ import type { ClientSession } from '../sessions';
79
import { type TimeoutContext } from '../timeout';
810
import { type MongoDBNamespace } from '../utils';
911
import { type WriteConcernOptions } from '../write_concern';
10-
import { type CollationOptions, CommandOperation, type CommandOperationOptions } from './command';
12+
import {
13+
type CollationOptions,
14+
type CommandOperationOptions,
15+
ModernizedCommandOperation
16+
} from './command';
1117
import { Aspect, defineAspects, type Hint } from './operation';
1218

1319
/** @public */
@@ -43,7 +49,8 @@ export interface DeleteStatement {
4349
}
4450

4551
/** @internal */
46-
export class DeleteOperation extends CommandOperation<DeleteResult> {
52+
export class DeleteOperation extends ModernizedCommandOperation<Document> {
53+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
4754
override options: DeleteOptions;
4855
statements: DeleteStatement[];
4956

@@ -66,12 +73,9 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
6673
return this.statements.every(op => (op.limit != null ? op.limit > 0 : true));
6774
}
6875

69-
override async execute(
70-
server: Server,
71-
session: ClientSession | undefined,
72-
timeoutContext: TimeoutContext
73-
): Promise<DeleteResult> {
74-
const options = this.options ?? {};
76+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
77+
const options = this.options;
78+
7579
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
7680
const command: Document = {
7781
delete: this.ns.collection,
@@ -97,13 +101,7 @@ export class DeleteOperation extends CommandOperation<DeleteResult> {
97101
}
98102
}
99103

100-
const res: TODO_NODE_3286 = await super.executeCommand(
101-
server,
102-
session,
103-
command,
104-
timeoutContext
105-
);
106-
return res;
104+
return command;
107105
}
108106
}
109107

@@ -127,19 +125,37 @@ export class DeleteOneOperation extends DeleteOperation {
127125
deletedCount: res.n
128126
};
129127
}
128+
129+
override handleOk(
130+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
131+
): DeleteResult {
132+
const res = super.handleOk(response);
133+
134+
// @ts-expect-error Explain commands have broken TS
135+
if (this.explain) return res;
136+
137+
if (res.code) throw new MongoServerError(res);
138+
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
139+
140+
return {
141+
acknowledged: this.writeConcern?.w !== 0,
142+
deletedCount: res.n
143+
};
144+
}
130145
}
131146
export class DeleteManyOperation extends DeleteOperation {
132147
constructor(collection: Collection, filter: Document, options: DeleteOptions) {
133148
super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
134149
}
135150

136-
override async execute(
137-
server: Server,
138-
session: ClientSession | undefined,
139-
timeoutContext: TimeoutContext
140-
): Promise<DeleteResult> {
141-
const res: TODO_NODE_3286 = await super.execute(server, session, timeoutContext);
151+
override handleOk(
152+
response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
153+
): DeleteResult {
154+
const res = super.handleOk(response);
155+
156+
// @ts-expect-error Explain commands have broken TS
142157
if (this.explain) return res;
158+
143159
if (res.code) throw new MongoServerError(res);
144160
if (res.writeErrors) throw new MongoServerError(res.writeErrors[0]);
145161

0 commit comments

Comments
 (0)