Skip to content

Commit 46c99c2

Browse files
committed
fix: reverse default
1 parent 85f25a5 commit 46c99c2

File tree

5 files changed

+77
-57
lines changed

5 files changed

+77
-57
lines changed

src/collection.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -822,17 +822,17 @@ export class Collection<TSchema extends Document = Document> {
822822
*/
823823
async findOneAndDelete(
824824
filter: Filter<TSchema>,
825-
options: FindOneAndDeleteOptions & { returnRawResult: false }
825+
options: FindOneAndDeleteOptions & { includeResultMetadata: true }
826826
): Promise<ModifyResult<TSchema>>;
827827
async findOneAndDelete(
828828
filter: Filter<TSchema>,
829-
options: FindOneAndDeleteOptions & { returnRawResult: true }
829+
options: FindOneAndDeleteOptions & { includeResultMetadata: false }
830830
): Promise<WithId<TSchema> | null>;
831831
async findOneAndDelete(
832832
filter: Filter<TSchema>,
833833
options: FindOneAndDeleteOptions
834-
): Promise<WithId<TSchema> | null>;
835-
async findOneAndDelete(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;
834+
): Promise<ModifyResult<TSchema>>;
835+
async findOneAndDelete(filter: Filter<TSchema>): Promise<ModifyResult<TSchema>>;
836836
async findOneAndDelete(
837837
filter: Filter<TSchema>,
838838
options?: FindOneAndDeleteOptions
@@ -857,22 +857,22 @@ export class Collection<TSchema extends Document = Document> {
857857
async findOneAndReplace(
858858
filter: Filter<TSchema>,
859859
replacement: WithoutId<TSchema>,
860-
options: FindOneAndReplaceOptions & { returnRawResult: false }
860+
options: FindOneAndReplaceOptions & { includeResultMetadata: true }
861861
): Promise<ModifyResult<TSchema>>;
862862
async findOneAndReplace(
863863
filter: Filter<TSchema>,
864864
replacement: WithoutId<TSchema>,
865-
options: FindOneAndReplaceOptions & { returnRawResult: true }
865+
options: FindOneAndReplaceOptions & { includeResultMetadata: false }
866866
): Promise<WithId<TSchema> | null>;
867867
async findOneAndReplace(
868868
filter: Filter<TSchema>,
869869
replacement: WithoutId<TSchema>,
870870
options: FindOneAndReplaceOptions
871-
): Promise<WithId<TSchema> | null>;
871+
): Promise<ModifyResult<TSchema>>;
872872
async findOneAndReplace(
873873
filter: Filter<TSchema>,
874874
replacement: WithoutId<TSchema>
875-
): Promise<WithId<TSchema> | null>;
875+
): Promise<ModifyResult<TSchema>>;
876876
async findOneAndReplace(
877877
filter: Filter<TSchema>,
878878
replacement: WithoutId<TSchema>,
@@ -899,22 +899,22 @@ export class Collection<TSchema extends Document = Document> {
899899
async findOneAndUpdate(
900900
filter: Filter<TSchema>,
901901
update: UpdateFilter<TSchema>,
902-
options: FindOneAndUpdateOptions & { returnRawResult: false }
902+
options: FindOneAndUpdateOptions & { includeResultMetadata: true }
903903
): Promise<ModifyResult<TSchema>>;
904904
async findOneAndUpdate(
905905
filter: Filter<TSchema>,
906906
update: UpdateFilter<TSchema>,
907-
options: FindOneAndUpdateOptions & { returnRawResult: true }
907+
options: FindOneAndUpdateOptions & { includeResultMetadata: false }
908908
): Promise<WithId<TSchema> | null>;
909909
async findOneAndUpdate(
910910
filter: Filter<TSchema>,
911911
update: UpdateFilter<TSchema>,
912912
options: FindOneAndUpdateOptions
913-
): Promise<WithId<TSchema> | null>;
913+
): Promise<ModifyResult<TSchema>>;
914914
async findOneAndUpdate(
915915
filter: Filter<TSchema>,
916916
update: UpdateFilter<TSchema>
917-
): Promise<WithId<TSchema> | null>;
917+
): Promise<ModifyResult<TSchema>>;
918918
async findOneAndUpdate(
919919
filter: Filter<TSchema>,
920920
update: UpdateFilter<TSchema>,

src/operations/find_and_modify.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface FindOneAndDeleteOptions extends CommandOperationOptions {
3030
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
3131
let?: Document;
3232
/** Return the raw result document instead of the ModifyResult */
33-
returnRawResult?: boolean;
33+
includeResultMetadata?: boolean;
3434
}
3535

3636
/** @public */
@@ -50,7 +50,7 @@ export interface FindOneAndReplaceOptions extends CommandOperationOptions {
5050
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
5151
let?: Document;
5252
/** Return the raw result document instead of the ModifyResult */
53-
returnRawResult?: boolean;
53+
includeResultMetadata?: boolean;
5454
}
5555

5656
/** @public */
@@ -72,7 +72,7 @@ export interface FindOneAndUpdateOptions extends CommandOperationOptions {
7272
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
7373
let?: Document;
7474
/** Return the raw result document instead of the ModifyResult */
75-
returnRawResult?: boolean;
75+
includeResultMetadata?: boolean;
7676
}
7777

7878
/** @internal */
@@ -134,8 +134,8 @@ class FindAndModifyOperation extends CommandOperation<Document> {
134134
};
135135

136136
// Default to returning the raw result.
137-
if (!('returnRawResult' in options)) {
138-
options.returnRawResult = true;
137+
if (!('includeResultMetadata' in options)) {
138+
options.includeResultMetadata = true;
139139
}
140140

141141
const sort = formatSort(options.sort);
@@ -216,7 +216,7 @@ class FindAndModifyOperation extends CommandOperation<Document> {
216216
// Execute the command
217217
super.executeCommand(server, session, cmd, (err, result) => {
218218
if (err) return callback(err);
219-
return callback(undefined, options.returnRawResult ? result.value ?? null : result);
219+
return callback(undefined, options.includeResultMetadata ? result : result.value ?? null);
220220
});
221221
}
222222
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,23 @@ const fooObjWithArray: FooWithArray = {
288288
const fooFilterWithArray: Filter<FooWithArray> = fooObjWithArray;
289289

290290
declare const coll: Collection<{ a: number; b: string }>;
291-
expectType<WithId<{ a: number; b: string }> | null>(await coll.findOneAndDelete({ a: 3 }));
292291
expectType<WithId<{ a: number; b: string }> | null>(
293-
await coll.findOneAndReplace({ a: 3 }, { a: 5, b: 'new string' })
292+
await coll.findOneAndDelete({ a: 3 }, { includeResultMetadata: false })
294293
);
295294
expectType<WithId<{ a: number; b: string }> | null>(
296-
await coll.findOneAndUpdate({ a: 3 }, { $set: { a: 5 } })
295+
await coll.findOneAndReplace(
296+
{ a: 3 },
297+
{ a: 5, b: 'new string' },
298+
{ includeResultMetadata: false }
299+
)
300+
);
301+
expectType<WithId<{ a: number; b: string }> | null>(
302+
await coll.findOneAndUpdate({ a: 3 }, { $set: { a: 5 } }, { includeResultMetadata: false })
297303
);
298304

299305
// projections do not change the return type - our typing doesn't support this
300306
expectType<WithId<{ a: number; b: string }> | null>(
301-
await coll.findOneAndDelete({ a: 3 }, { projection: { _id: 0 } })
307+
await coll.findOneAndDelete({ a: 3 }, { projection: { _id: 0 }, includeResultMetadata: false })
302308
);
303309

304310
// NODE-3568: Uncomment when ModifyResult is removed in 5.0

test/types/community/transaction.test-d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ const db = client.db();
8282
session.startTransaction();
8383
try {
8484
const opts = { session, returnOriginal: false };
85-
const res = await db
86-
.collection<Account>('Account')
87-
.findOneAndUpdate({ name: from }, { $inc: { balance: -amount } }, opts);
85+
const res = (
86+
await db
87+
.collection<Account>('Account')
88+
.findOneAndUpdate({ name: from }, { $inc: { balance: -amount } }, opts)
89+
).value;
8890
const A = res;
8991
if (A?.balance && A.balance < 0) {
9092
// If A would have negative balance, fail and abort the transaction

test/types/schema_helpers.test-d.ts

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,31 @@ expectAssignable<SchemaWithIdType | null>(await typeTestCollection.findOne());
7171
expectAssignable<SchemaWithIdInterface | null>(await interfaceTestCollection.findOne());
7272
expectAssignable<SchemaWithIdType>((await typeTestCollection.find().toArray())[0]);
7373
expectAssignable<SchemaWithIdInterface>((await interfaceTestCollection.find().toArray())[0]);
74-
expectAssignable<SchemaWithIdType | null>(await typeTestCollection.findOneAndDelete({ a: 1 }));
74+
expectAssignable<SchemaWithIdType | null>(
75+
await typeTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
76+
);
7577
expectAssignable<SchemaWithIdInterface | null>(
76-
await interfaceTestCollection.findOneAndDelete({ a: 1 })
78+
await interfaceTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
7779
);
7880
expectAssignable<SchemaWithIdType | null>(
79-
await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })
81+
await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, { includeResultMetadata: false })
8082
);
8183
expectAssignable<SchemaWithIdInterface | null>(
82-
await interfaceTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })
84+
await interfaceTestCollection.findOneAndReplace(
85+
{ a: 1 },
86+
{ a: 5 },
87+
{ includeResultMetadata: false }
88+
)
8389
);
8490
expectAssignable<SchemaWithIdType | null>(
85-
await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })
91+
await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 }, { includeResultMetadata: false })
8692
);
8793
expectAssignable<SchemaWithIdInterface | null>(
88-
await interfaceTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })
94+
await interfaceTestCollection.findOneAndUpdate(
95+
{ a: 1 },
96+
{ a: 5 },
97+
{ includeResultMetadata: false }
98+
)
8999
);
90100

91101
// OptionalId assignability when wrapping a schema with _id: number
@@ -107,55 +117,57 @@ expectAssignable<SchemaWithIdNumberInterface>(
107117
(await interfaceNumberTestCollection.find().toArray())[0]
108118
);
109119
expectAssignable<SchemaWithIdNumberType | null>(
110-
await typeNumberTestCollection.findOneAndDelete({ a: 1 })
120+
(await typeNumberTestCollection.findOneAndDelete({ a: 1 })).value
111121
);
112122
expectAssignable<SchemaWithIdNumberInterface | null>(
113-
await interfaceNumberTestCollection.findOneAndDelete({ a: 1 })
123+
(await interfaceNumberTestCollection.findOneAndDelete({ a: 1 })).value
114124
);
115125
expectAssignable<SchemaWithIdNumberType | null>(
116-
await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, {})
126+
(await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, {})).value
117127
);
118128
expectAssignable<SchemaWithIdNumberInterface | null>(
119-
await interfaceNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })
129+
(await interfaceNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value
120130
);
121131
expectAssignable<SchemaWithIdNumberType | null>(
122-
await typeNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })
132+
(await typeNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
123133
);
124134
expectAssignable<SchemaWithIdNumberInterface | null>(
125-
await interfaceNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })
135+
(await interfaceNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value
126136
);
127137

128138
expectAssignable<SchemaWithIdNumberType | null>(
129-
(await typeNumberTestCollection.findOneAndDelete({ a: 1 }, { returnRawResult: false })).value
139+
await typeNumberTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
130140
);
131141
expectAssignable<SchemaWithIdNumberInterface | null>(
132-
(await interfaceNumberTestCollection.findOneAndDelete({ a: 1 }, { returnRawResult: false })).value
142+
await interfaceNumberTestCollection.findOneAndDelete({ a: 1 }, { includeResultMetadata: false })
133143
);
134144
expectAssignable<SchemaWithIdNumberType | null>(
135-
(await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 }, { returnRawResult: false }))
136-
.value
145+
await typeNumberTestCollection.findOneAndReplace(
146+
{ a: 1 },
147+
{ a: 5 },
148+
{ includeResultMetadata: false }
149+
)
137150
);
138151
expectAssignable<SchemaWithIdNumberInterface | null>(
139-
(
140-
await interfaceNumberTestCollection.findOneAndReplace(
141-
{ a: 1 },
142-
{ a: 5 },
143-
{ returnRawResult: false }
144-
)
145-
).value
152+
await interfaceNumberTestCollection.findOneAndReplace(
153+
{ a: 1 },
154+
{ a: 5 },
155+
{ includeResultMetadata: false }
156+
)
146157
);
147158
expectAssignable<SchemaWithIdNumberType | null>(
148-
(await typeNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 }, { returnRawResult: false }))
149-
.value
159+
await typeNumberTestCollection.findOneAndUpdate(
160+
{ a: 1 },
161+
{ a: 5 },
162+
{ includeResultMetadata: false }
163+
)
150164
);
151165
expectAssignable<SchemaWithIdNumberInterface | null>(
152-
(
153-
await interfaceNumberTestCollection.findOneAndUpdate(
154-
{ a: 1 },
155-
{ a: 5 },
156-
{ returnRawResult: false }
157-
)
158-
).value
166+
await interfaceNumberTestCollection.findOneAndUpdate(
167+
{ a: 1 },
168+
{ a: 5 },
169+
{ includeResultMetadata: false }
170+
)
159171
);
160172

161173
/** ----------------------------------------------------------------------

0 commit comments

Comments
 (0)