Skip to content

Commit 878e819

Browse files
Revert "insertone"
This reverts commit df24c85.
1 parent fcaa705 commit 878e819

File tree

4 files changed

+62
-41
lines changed

4 files changed

+62
-41
lines changed

src/collection.ts

Lines changed: 10 additions & 29 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
/**

src/operations/insert.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ 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';
8+
import type { Server } from '../sdam/server';
69
import type { ClientSession } from '../sessions';
7-
import { type MongoDBNamespace } from '../utils';
10+
import { type TimeoutContext } from '../timeout';
11+
import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils';
812
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
913
import { Aspect, defineAspects } from './operation';
10-
1114
/** @internal */
12-
export interface InsertResult {
13-
n: number;
14-
code?: number;
15-
writeErrors?: Document[];
16-
}
17-
18-
/** @internal */
19-
export class InsertOperation extends ModernizedCommandOperation<InsertResult> {
15+
export class InsertOperation extends ModernizedCommandOperation<Document> {
2016
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2117
override options: BulkWriteOptions;
2218

@@ -72,6 +68,44 @@ export interface InsertOneResult<TSchema = Document> {
7268
insertedId: InferIdType<TSchema>;
7369
}
7470

71+
export class InsertOneOperation extends InsertOperation {
72+
constructor(collection: Collection, doc: Document, options: InsertOneOptions) {
73+
super(collection.s.namespace, [maybeAddIdToDocuments(collection, doc, options)], options);
74+
}
75+
76+
override async execute(
77+
server: Server,
78+
session: ClientSession | undefined,
79+
timeoutContext: TimeoutContext
80+
): Promise<InsertOneResult> {
81+
const res = await super.execute(server, session, timeoutContext);
82+
if (res.code) throw new MongoServerError(res);
83+
if (res.writeErrors) {
84+
// This should be a WriteError but we can't change it now because of error hierarchy
85+
throw new MongoServerError(res.writeErrors[0]);
86+
}
87+
88+
return {
89+
acknowledged: this.writeConcern?.w !== 0,
90+
insertedId: this.documents[0]._id
91+
};
92+
}
93+
94+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
95+
const res = super.handleOk(response);
96+
if (res.code) throw new MongoServerError(res);
97+
if (res.writeErrors) {
98+
// This should be a WriteError but we can't change it now because of error hierarchy
99+
throw new MongoServerError(res.writeErrors[0]);
100+
}
101+
102+
return {
103+
acknowledged: this.writeConcern?.w !== 0,
104+
insertedId: this.documents[0]._id
105+
};
106+
}
107+
}
108+
75109
/** @public */
76110
export interface InsertManyResult<TSchema = Document> {
77111
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
@@ -83,3 +117,4 @@ export interface InsertManyResult<TSchema = Document> {
83117
}
84118

85119
defineAspects(InsertOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
120+
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)