Skip to content

Commit 3553fe4

Browse files
committed
fixup: use WithoutId instead of OptionalId
1 parent 85c1eb8 commit 3553fe4

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/bulk/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { Collection } from '../collection';
3333
import type { Topology } from '../sdam/topology';
3434
import type { CommandOperationOptions, CollationOptions } from '../operations/command';
3535
import type { Hint } from '../operations/operation';
36-
import type { Filter, OneOrMore, OptionalId, UpdateFilter } from '../mongo_types';
36+
import type { Filter, OneOrMore, WithoutId, OptionalId, UpdateFilter } from '../mongo_types';
3737

3838
/** @internal */
3939
const kServerError = Symbol('serverError');
@@ -79,7 +79,7 @@ export interface ReplaceOneModel<TSchema extends Document = Document> {
7979
/** The filter to limit the replaced document. */
8080
filter: Filter<TSchema>;
8181
/** The document with which to replace the matched document. */
82-
replacement: OptionalId<TSchema>;
82+
replacement: WithoutId<TSchema>;
8383
/** Specifies a collation. */
8484
collation?: CollationOptions;
8585
/** The index to use. If specified, then the query system will only consider plans using the hinted index. */

src/collection.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import type {
9393
TODO_NODE_3286,
9494
UpdateFilter,
9595
WithId,
96+
WithoutId,
9697
OptionalId,
9798
Flatten
9899
} from './mongo_types';
@@ -461,27 +462,27 @@ export class Collection<TSchema extends Document = Document> {
461462
*/
462463
replaceOne(
463464
filter: Filter<TSchema>,
464-
replacement: OptionalId<TSchema>
465+
replacement: WithoutId<TSchema>
465466
): Promise<UpdateResult | Document>;
466467
replaceOne(
467468
filter: Filter<TSchema>,
468-
replacement: OptionalId<TSchema>,
469+
replacement: WithoutId<TSchema>,
469470
callback: Callback<UpdateResult | Document>
470471
): void;
471472
replaceOne(
472473
filter: Filter<TSchema>,
473-
replacement: OptionalId<TSchema>,
474+
replacement: WithoutId<TSchema>,
474475
options: ReplaceOptions
475476
): Promise<UpdateResult | Document>;
476477
replaceOne(
477478
filter: Filter<TSchema>,
478-
replacement: OptionalId<TSchema>,
479+
replacement: WithoutId<TSchema>,
479480
options: ReplaceOptions,
480481
callback: Callback<UpdateResult | Document>
481482
): void;
482483
replaceOne(
483484
filter: Filter<TSchema>,
484-
replacement: OptionalId<TSchema>,
485+
replacement: WithoutId<TSchema>,
485486
options?: ReplaceOptions | Callback<UpdateResult | Document>,
486487
callback?: Callback<UpdateResult | Document>
487488
): Promise<UpdateResult | Document> | void {
@@ -1282,26 +1283,29 @@ export class Collection<TSchema extends Document = Document> {
12821283
* @param options - Optional settings for the command
12831284
* @param callback - An optional callback, a Promise will be returned if none is provided
12841285
*/
1285-
findOneAndReplace(filter: Filter<TSchema>, replacement: Document): Promise<ModifyResult<TSchema>>;
12861286
findOneAndReplace(
12871287
filter: Filter<TSchema>,
1288-
replacement: Document,
1288+
replacement: WithoutId<TSchema>
1289+
): Promise<ModifyResult<TSchema>>;
1290+
findOneAndReplace(
1291+
filter: Filter<TSchema>,
1292+
replacement: WithoutId<TSchema>,
12891293
callback: Callback<ModifyResult<TSchema>>
12901294
): void;
12911295
findOneAndReplace(
12921296
filter: Filter<TSchema>,
1293-
replacement: Document,
1297+
replacement: WithoutId<TSchema>,
12941298
options: FindOneAndReplaceOptions
12951299
): Promise<ModifyResult<TSchema>>;
12961300
findOneAndReplace(
12971301
filter: Filter<TSchema>,
1298-
replacement: Document,
1302+
replacement: WithoutId<TSchema>,
12991303
options: FindOneAndReplaceOptions,
13001304
callback: Callback<ModifyResult<TSchema>>
13011305
): void;
13021306
findOneAndReplace(
13031307
filter: Filter<TSchema>,
1304-
replacement: Document,
1308+
replacement: WithoutId<TSchema>,
13051309
options?: FindOneAndReplaceOptions | Callback<ModifyResult<TSchema>>,
13061310
callback?: Callback<ModifyResult<TSchema>>
13071311
): Promise<ModifyResult<TSchema>> | void {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ collectionType.bulkWrite([
181181
}
182182
}
183183
]);
184-
// allow a replacement doc without an _id
184+
// allow a literal replacement doc without an _id
185185
collectionType.bulkWrite([
186186
{
187187
replaceOne: {
@@ -199,6 +199,20 @@ collectionType.bulkWrite([
199199
}
200200
}
201201
]);
202+
// disallow a literal replacement doc with an _id
203+
expectError(
204+
collectionType.bulkWrite([
205+
{
206+
replaceOne: {
207+
filter: {},
208+
replacement: {
209+
_id: new ObjectId()
210+
},
211+
upsert: true
212+
}
213+
}
214+
])
215+
);
202216

203217
expectError(
204218
collectionType.bulkWrite([

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ interface TestModel {
1212

1313
const collection = db.collection<TestModel>('testCollection');
1414

15-
// should accept a replacement doc with an _id
16-
await collection.replaceOne({}, { _id: new ObjectId(), stringField: 'a' });
17-
1815
// should accept a replacement doc without an _id
1916
await collection.replaceOne({}, { stringField: 'b' });
2017

21-
// should not allow _id with a non-ObjectId type
22-
expectError(await collection.replaceOne({}, { _id: 1, stringField: 'c' }));
18+
// should not accept a literal replacement doc with an _id
19+
expectError(await collection.replaceOne({}, { _id: new ObjectId(), stringField: 'a' }));

0 commit comments

Comments
 (0)