Skip to content

Commit 4b5be21

Browse files
fix(NODE-5171): allow upsertedId to be null in UpdateResult (#3631)
1 parent b439893 commit 4b5be21

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export class Collection<TSchema extends Document = Document> {
334334
filter: Filter<TSchema>,
335335
update: UpdateFilter<TSchema> | Partial<TSchema>,
336336
options?: UpdateOptions
337-
): Promise<UpdateResult> {
337+
): Promise<UpdateResult<TSchema>> {
338338
return executeOperation(
339339
this.s.db.s.client,
340340
new UpdateOneOperation(
@@ -357,7 +357,7 @@ export class Collection<TSchema extends Document = Document> {
357357
filter: Filter<TSchema>,
358358
replacement: WithoutId<TSchema>,
359359
options?: ReplaceOptions
360-
): Promise<UpdateResult | Document> {
360+
): Promise<UpdateResult<TSchema> | Document> {
361361
return executeOperation(
362362
this.s.db.s.client,
363363
new ReplaceOneOperation(
@@ -380,7 +380,7 @@ export class Collection<TSchema extends Document = Document> {
380380
filter: Filter<TSchema>,
381381
update: UpdateFilter<TSchema>,
382382
options?: UpdateOptions
383-
): Promise<UpdateResult> {
383+
): Promise<UpdateResult<TSchema>> {
384384
return executeOperation(
385385
this.s.db.s.client,
386386
new UpdateManyOperation(

src/operations/update.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { Document, ObjectId } from '../bson';
1+
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
33
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
4+
import type { InferIdType } from '../mongo_types';
45
import type { Server } from '../sdam/server';
56
import type { ClientSession } from '../sessions';
67
import { Callback, hasAtomicOperators, MongoDBNamespace } from '../utils';
@@ -23,8 +24,11 @@ export interface UpdateOptions extends CommandOperationOptions {
2324
let?: Document;
2425
}
2526

26-
/** @public */
27-
export interface UpdateResult {
27+
/**
28+
* @public
29+
* `TSchema` is the schema of the collection
30+
*/
31+
export interface UpdateResult<TSchema extends Document = Document> {
2832
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
2933
acknowledged: boolean;
3034
/** The number of documents that matched the filter */
@@ -34,7 +38,7 @@ export interface UpdateResult {
3438
/** The number of documents that were upserted */
3539
upsertedCount: number;
3640
/** The identifier of the inserted document if an upsert took place */
37-
upsertedId: ObjectId;
41+
upsertedId: InferIdType<TSchema> | null;
3842
}
3943

4044
/** @public */

test/types/community/collection/updateX.test-d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { expectAssignable, expectError, expectNotAssignable, expectNotType } fro
33
import type {
44
AddToSetOperators,
55
ArrayOperator,
6+
Collection,
67
MatchKeysAndValues,
78
PullAllOperator,
89
PullOperator,
@@ -423,3 +424,25 @@ export async function testPushWithId(): Promise<void> {
423424
}
424425
);
425426
}
427+
428+
{
429+
// NODE-5171 - UpdateResult is generic over the collection schema and infers the id type
430+
const collection = {} as any as Collection;
431+
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateOne({}, {}));
432+
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateMany({}, {}));
433+
434+
expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateOne({}, {}));
435+
expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateMany({}, {}));
436+
437+
const collectionWithSchema = {} as any as Collection<{ _id: number }>;
438+
expectAssignable<Promise<{ upsertedId: number | null }>>(
439+
collectionWithSchema.updateOne({ _id: 1234 }, {})
440+
);
441+
expectAssignable<Promise<{ upsertedId: number | null }>>(collectionWithSchema.updateMany({}, {}));
442+
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
443+
collectionWithSchema.updateOne({ _id: 1234 }, {})
444+
);
445+
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
446+
collectionWithSchema.updateMany({}, {})
447+
);
448+
}

0 commit comments

Comments
 (0)