Skip to content

Commit

Permalink
fix(NODE-5171): allow upsertedId to be null in UpdateResult (#3631)
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Apr 12, 2023
1 parent b439893 commit 4b5be21
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/collection.ts
Expand Up @@ -334,7 +334,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
update: UpdateFilter<TSchema> | Partial<TSchema>,
options?: UpdateOptions
): Promise<UpdateResult> {
): Promise<UpdateResult<TSchema>> {
return executeOperation(
this.s.db.s.client,
new UpdateOneOperation(
Expand All @@ -357,7 +357,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
replacement: WithoutId<TSchema>,
options?: ReplaceOptions
): Promise<UpdateResult | Document> {
): Promise<UpdateResult<TSchema> | Document> {
return executeOperation(
this.s.db.s.client,
new ReplaceOneOperation(
Expand All @@ -380,7 +380,7 @@ export class Collection<TSchema extends Document = Document> {
filter: Filter<TSchema>,
update: UpdateFilter<TSchema>,
options?: UpdateOptions
): Promise<UpdateResult> {
): Promise<UpdateResult<TSchema>> {
return executeOperation(
this.s.db.s.client,
new UpdateManyOperation(
Expand Down
12 changes: 8 additions & 4 deletions src/operations/update.ts
@@ -1,6 +1,7 @@
import type { Document, ObjectId } from '../bson';
import type { Document } from '../bson';
import type { Collection } from '../collection';
import { MongoCompatibilityError, MongoInvalidArgumentError, MongoServerError } from '../error';
import type { InferIdType } from '../mongo_types';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { Callback, hasAtomicOperators, MongoDBNamespace } from '../utils';
Expand All @@ -23,8 +24,11 @@ export interface UpdateOptions extends CommandOperationOptions {
let?: Document;
}

/** @public */
export interface UpdateResult {
/**
* @public
* `TSchema` is the schema of the collection
*/
export interface UpdateResult<TSchema extends Document = Document> {
/** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */
acknowledged: boolean;
/** The number of documents that matched the filter */
Expand All @@ -34,7 +38,7 @@ export interface UpdateResult {
/** The number of documents that were upserted */
upsertedCount: number;
/** The identifier of the inserted document if an upsert took place */
upsertedId: ObjectId;
upsertedId: InferIdType<TSchema> | null;
}

/** @public */
Expand Down
23 changes: 23 additions & 0 deletions test/types/community/collection/updateX.test-d.ts
Expand Up @@ -3,6 +3,7 @@ import { expectAssignable, expectError, expectNotAssignable, expectNotType } fro
import type {
AddToSetOperators,
ArrayOperator,
Collection,
MatchKeysAndValues,
PullAllOperator,
PullOperator,
Expand Down Expand Up @@ -423,3 +424,25 @@ export async function testPushWithId(): Promise<void> {
}
);
}

{
// NODE-5171 - UpdateResult is generic over the collection schema and infers the id type
const collection = {} as any as Collection;
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateOne({}, {}));
expectAssignable<Promise<{ upsertedId: ObjectId | null }>>(collection.updateMany({}, {}));

expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateOne({}, {}));
expectNotAssignable<Promise<{ upsertedId: number | null }>>(collection.updateMany({}, {}));

const collectionWithSchema = {} as any as Collection<{ _id: number }>;
expectAssignable<Promise<{ upsertedId: number | null }>>(
collectionWithSchema.updateOne({ _id: 1234 }, {})
);
expectAssignable<Promise<{ upsertedId: number | null }>>(collectionWithSchema.updateMany({}, {}));
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
collectionWithSchema.updateOne({ _id: 1234 }, {})
);
expectNotAssignable<Promise<{ upsertedId: ObjectId | null }>>(
collectionWithSchema.updateMany({}, {})
);
}

0 comments on commit 4b5be21

Please sign in to comment.