Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): change "bulkWrite" to not be strict by default and allow overwriting #13292

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,50 @@ async function gh12277() {
]);
}

async function overwriteBulkWriteContents() {
type DocumentType<T> = Document<any, any, T> & T;

interface BaseModelClassDoc {
firstname: string;
}

const baseModelClassSchema = new Schema({
firstname: String
});

const BaseModel = model<BaseModelClassDoc>('test', baseModelClassSchema);

expectError(BaseModel.bulkWrite<{ testy: string }>([
{
insertOne: {
document: {
test: 'hello'
}
}
}
]));

BaseModel.bulkWrite<{ testy: string }>([
{
insertOne: {
document: {
testy: 'hello'
}
}
}
]);

BaseModel.bulkWrite([
{
insertOne: {
document: {
randomPropertyNotInTypes: 'hello'
}
}
}
]);
}

export function autoTypedModel() {
const AutoTypedSchema = autoTypedSchema();
const AutoTypedModel = model('AutoTypeModel', AutoTypedSchema);
Expand Down
8 changes: 4 additions & 4 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ declare module 'mongoose' {
* if you use `create()`) because with `bulkWrite()` there is only one network
* round trip to the MongoDB server.
*/
bulkWrite(
writes: Array<mongodb.AnyBulkWriteOperation<T extends Document ? any : (T extends {} ? T : any)>>,
bulkWrite<DocContents = T>(
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
bulkWrite(
writes: Array<mongodb.AnyBulkWriteOperation<T extends Document ? any : (T extends {} ? T : any)>>,
bulkWrite<DocContents = T>(
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
): Promise<mongodb.BulkWriteResult>;

Expand Down