Skip to content

Commit 76e6456

Browse files
authoredMay 2, 2023
Merge pull request #13292 from hasezoey/modifyBulkWriteType6
fix(types): change "bulkWrite" to not be strict by default and allow overwriting
2 parents 2bbbb3c + 6b39c88 commit 76e6456

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed
 

‎test/types/models.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,50 @@ async function gh12277() {
352352
]);
353353
}
354354

355+
async function overwriteBulkWriteContents() {
356+
type DocumentType<T> = Document<any, any, T> & T;
357+
358+
interface BaseModelClassDoc {
359+
firstname: string;
360+
}
361+
362+
const baseModelClassSchema = new Schema({
363+
firstname: String
364+
});
365+
366+
const BaseModel = model<BaseModelClassDoc>('test', baseModelClassSchema);
367+
368+
expectError(BaseModel.bulkWrite<{ testy: string }>([
369+
{
370+
insertOne: {
371+
document: {
372+
test: 'hello'
373+
}
374+
}
375+
}
376+
]));
377+
378+
BaseModel.bulkWrite<{ testy: string }>([
379+
{
380+
insertOne: {
381+
document: {
382+
testy: 'hello'
383+
}
384+
}
385+
}
386+
]);
387+
388+
BaseModel.bulkWrite([
389+
{
390+
insertOne: {
391+
document: {
392+
randomPropertyNotInTypes: 'hello'
393+
}
394+
}
395+
}
396+
]);
397+
}
398+
355399
export function autoTypedModel() {
356400
const AutoTypedSchema = autoTypedSchema();
357401
const AutoTypedModel = model('AutoTypeModel', AutoTypedSchema);

‎types/models.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ declare module 'mongoose' {
162162
* if you use `create()`) because with `bulkWrite()` there is only one network
163163
* round trip to the MongoDB server.
164164
*/
165-
bulkWrite(
166-
writes: Array<mongodb.AnyBulkWriteOperation<T extends Document ? any : (T extends {} ? T : any)>>,
165+
bulkWrite<DocContents = T>(
166+
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
167167
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
168168
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
169-
bulkWrite(
170-
writes: Array<mongodb.AnyBulkWriteOperation<T extends Document ? any : (T extends {} ? T : any)>>,
169+
bulkWrite<DocContents = T>(
170+
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
171171
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
172172
): Promise<mongodb.BulkWriteResult>;
173173

0 commit comments

Comments
 (0)
Please sign in to comment.