Skip to content

Commit f54f9d0

Browse files
committed
types(models): fix incorrect bulk write options
1 parent e359b99 commit f54f9d0

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

test/types/middleware.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult } from 'mongoose';
1+
import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult, AnyBulkWriteOperation } from 'mongoose';
22
import { expectError, expectType, expectNotType, expectAssignable } from 'tsd';
3-
import { AnyBulkWriteOperation, CreateCollectionOptions } from 'mongodb';
3+
import { CreateCollectionOptions } from 'mongodb';
44

55
const preMiddlewareFn: PreSaveMiddlewareFunction<Document> = function(next, opts) {
66
this.$markValid('name');

test/types/models.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ async function gh14072() {
824824
);
825825

826826
const M = mongoose.model<Test>('Test', schema);
827-
const bulkWriteArray = [
827+
await M.bulkWrite([
828828
{
829829
insertOne: {
830830
document: { num: 3 }
@@ -844,9 +844,7 @@ async function gh14072() {
844844
timestamps: false
845845
}
846846
}
847-
];
848-
849-
await M.bulkWrite(bulkWriteArray);
847+
]);
850848
}
851849

852850
async function gh14003() {

types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ declare module 'mongoose' {
431431
fn: (
432432
this: T,
433433
next: (err?: CallbackError) => void,
434-
ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
434+
ops: Array<AnyBulkWriteOperation<any>>,
435435
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
436436
) => void | Promise<void>
437437
): this;

types/models.d.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ declare module 'mongoose' {
2323
): U;
2424
}
2525

26-
interface MongooseBulkWriteOptions {
26+
interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions {
27+
session?: ClientSession;
2728
skipValidation?: boolean;
2829
throwOnValidationError?: boolean;
29-
timestamps?: boolean;
3030
strict?: boolean | 'throw';
3131
}
3232

33+
interface MongooseBulkSaveOptions extends mongodb.BulkWriteOptions {
34+
timestamps?: boolean;
35+
session?: ClientSession;
36+
}
37+
38+
/**
39+
* @deprecated use AnyBulkWriteOperation instead
40+
*/
3341
interface MongooseBulkWritePerWriteOptions {
3442
timestamps?: boolean;
3543
strict?: boolean | 'throw';
@@ -200,6 +208,8 @@ declare module 'mongoose' {
200208
hint?: mongodb.Hint;
201209
/** When true, creates a new document if no document matches the query. */
202210
upsert?: boolean;
211+
/** When false, do not add timestamps. */
212+
timestamps?: boolean;
203213
}
204214

205215
export interface UpdateManyModel<TSchema = AnyObject> {
@@ -215,6 +225,8 @@ declare module 'mongoose' {
215225
hint?: mongodb.Hint;
216226
/** When true, creates a new document if no document matches the query. */
217227
upsert?: boolean;
228+
/** When false, do not add timestamps. */
229+
timestamps?: boolean;
218230
}
219231

220232
export interface DeleteOneModel<TSchema = AnyObject> {
@@ -281,19 +293,19 @@ declare module 'mongoose' {
281293
*/
282294
bulkWrite<DocContents = TRawDocType>(
283295
writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
284-
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
296+
options: MongooseBulkWriteOptions & { ordered: false }
285297
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
286298
bulkWrite<DocContents = TRawDocType>(
287299
writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
288-
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
300+
options?: MongooseBulkWriteOptions
289301
): Promise<mongodb.BulkWriteResult>;
290302

291303
/**
292304
* Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than
293305
* sending multiple `save()` calls because with `bulkSave()` there is only one
294306
* network round trip to the MongoDB server.
295307
*/
296-
bulkSave(documents: Array<Document>, options?: mongodb.BulkWriteOptions & { timestamps?: boolean }): Promise<mongodb.BulkWriteResult>;
308+
bulkSave(documents: Array<Document>, options?: MongooseBulkSaveOptions): Promise<mongodb.BulkWriteResult>;
297309

298310
/** Collection the model uses. */
299311
collection: Collection;

0 commit comments

Comments
 (0)