Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make _id optional in replacement for replaceOne operations
The _id field is immutable in a replaceOne operation, so it does not make sense to require passing it. Also, this makes replaceOne consistent with insertOne.
- Loading branch information
Showing
4 changed files
with
49 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expectError } from 'tsd'; | ||
import { MongoClient, ObjectId } from '../../../../src'; | ||
|
||
// test collection.replaceX functions | ||
const client = new MongoClient(''); | ||
const db = client.db('test'); | ||
|
||
interface TestModel { | ||
_id: ObjectId; | ||
stringField: string; | ||
} | ||
|
||
const collection = db.collection<TestModel>('testCollection'); | ||
|
||
// should accept a replacement doc with an _id | ||
await collection.replaceOne({}, { _id: new ObjectId(), stringField: 'a' }); | ||
|
||
// should accept a replacement doc without an _id | ||
await collection.replaceOne({}, { stringField: 'b' }); | ||
|
||
// should not allow _id with a non-ObjectId type | ||
expectError(await collection.replaceOne({}, { _id: 1, stringField: 'c' })); |