Skip to content

Commit dab9320

Browse files
delete
1 parent a7b1217 commit dab9320

File tree

5 files changed

+81
-63
lines changed

5 files changed

+81
-63
lines changed

src/collection.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import {
1616
type ListSearchIndexesOptions
1717
} from './cursor/list_search_indexes_cursor';
1818
import type { Db } from './db';
19-
import {
20-
MongoAPIError,
21-
MongoInvalidArgumentError,
22-
MongoOperationTimeoutError,
23-
MongoServerError
24-
} from './error';
19+
import { MongoAPIError, MongoInvalidArgumentError, MongoOperationTimeoutError } from './error';
2520
import type { MongoClient, PkFactory } from './mongo_client';
2621
import type {
2722
Abortable,
@@ -71,9 +66,9 @@ import {
7166
} from './operations/indexes';
7267
import {
7368
type InsertManyResult,
69+
InsertOneOperation,
7470
type InsertOneOptions,
75-
type InsertOneResult,
76-
InsertOperation
71+
type InsertOneResult
7772
} from './operations/insert';
7873
import type { Hint, OperationOptions } from './operations/operation';
7974
import { RenameOperation, type RenameOptions } from './operations/rename';
@@ -96,7 +91,6 @@ import { ReadPreference, type ReadPreferenceLike } from './read_preference';
9691
import { type Sort } from './sort';
9792
import {
9893
DEFAULT_PK_FACTORY,
99-
maybeAddIdToDocuments,
10094
MongoDBCollectionNamespace,
10195
normalizeHintField,
10296
resolveOptions
@@ -290,27 +284,14 @@ export class Collection<TSchema extends Document = Document> {
290284
doc: OptionalUnlessRequiredId<TSchema>,
291285
options?: InsertOneOptions
292286
): Promise<InsertOneResult<TSchema>> {
293-
options = resolveOptions(this, options);
294-
295-
const operation = new InsertOperation(
296-
this.fullNamespace,
297-
[maybeAddIdToDocuments(this, doc, options)],
298-
options
287+
return await executeOperation(
288+
this.client,
289+
new InsertOneOperation(
290+
this as TODO_NODE_3286,
291+
doc,
292+
resolveOptions(this, options)
293+
) as TODO_NODE_3286
299294
);
300-
301-
const acknowledged = operation.writeConcern?.w !== 0;
302-
303-
const response = await executeOperation(this.client, operation);
304-
if (response.code) throw new MongoServerError(response);
305-
if (response.writeErrors) {
306-
// This should be a WriteError but we can't change it now because of error hierarchy
307-
throw new MongoServerError(response.writeErrors[0]);
308-
}
309-
310-
return {
311-
acknowledged,
312-
insertedId: doc._id
313-
};
314295
}
315296

316297
/**
@@ -508,7 +489,8 @@ export class Collection<TSchema extends Document = Document> {
508489
): Promise<DeleteResult> {
509490
return await executeOperation(
510491
this.client,
511-
new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options))
492+
// TODO(NODE-7087): remove unnecessary cast as Any
493+
new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)) as any
512494
);
513495
}
514496

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

src/operations/insert.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ import { type Connection } from '..';
22
import type { Document } from '../bson';
33
import type { BulkWriteOptions } from '../bulk/common';
44
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
5+
import type { Collection } from '../collection';
6+
import { MongoServerError } from '../error';
57
import type { InferIdType } from '../mongo_types';
68
import type { ClientSession } from '../sessions';
7-
import { type MongoDBNamespace } from '../utils';
9+
import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils';
810
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
911
import { Aspect, defineAspects } from './operation';
10-
11-
/** @internal */
12-
export interface InsertResult {
13-
n: number;
14-
code?: number;
15-
writeErrors?: Document[];
16-
}
17-
1812
/** @internal */
19-
export class InsertOperation extends ModernizedCommandOperation<InsertResult> {
13+
export class InsertOperation extends ModernizedCommandOperation<Document> {
2014
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2115
override options: BulkWriteOptions;
2216

@@ -72,6 +66,26 @@ export interface InsertOneResult<TSchema = Document> {
7266
insertedId: InferIdType<TSchema>;
7367
}
7468

69+
export class InsertOneOperation extends InsertOperation {
70+
constructor(collection: Collection, doc: Document, options: InsertOneOptions) {
71+
super(collection.s.namespace, [maybeAddIdToDocuments(collection, doc, options)], options);
72+
}
73+
74+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
75+
const res = super.handleOk(response);
76+
if (res.code) throw new MongoServerError(res);
77+
if (res.writeErrors) {
78+
// This should be a WriteError but we can't change it now because of error hierarchy
79+
throw new MongoServerError(res.writeErrors[0]);
80+
}
81+
82+
return {
83+
acknowledged: this.writeConcern?.w !== 0,
84+
insertedId: this.documents[0]._id
85+
};
86+
}
87+
}
88+
7589
/** @public */
7690
export interface InsertManyResult<TSchema = Document> {
7791
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
@@ -83,3 +97,4 @@ export interface InsertManyResult<TSchema = Document> {
8397
}
8498

8599
defineAspects(InsertOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
100+
defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,8 +1356,8 @@ export async function once<T>(ee: EventEmitter, name: string, options?: Abortabl
13561356
}
13571357
}
13581358

1359-
export function maybeAddIdToDocuments<TSchema extends Document>(
1360-
collection: Collection<TSchema>,
1359+
export function maybeAddIdToDocuments(
1360+
collection: Collection,
13611361
document: Document,
13621362
options: { forceServerObjectId?: boolean }
13631363
): Document {

test/integration/crud/abstract_operation.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ describe('abstract operation', function () {
145145
subclassType: mongodb.InsertOperation,
146146
correctCommandName: 'insert'
147147
},
148+
{
149+
subclassCreator: () => new mongodb.InsertOneOperation(collection, { a: 1 }, {}),
150+
subclassType: mongodb.InsertOneOperation,
151+
correctCommandName: 'insert'
152+
},
148153
{
149154
subclassCreator: () =>
150155
new mongodb.KillCursorsOperation(

0 commit comments

Comments
 (0)