From f54f9d0349015d4e4b592bee6c96ce1573e838b0 Mon Sep 17 00:00:00 2001 From: Emil Janitzek Date: Tue, 9 Apr 2024 16:48:37 +0200 Subject: [PATCH] types(models): fix incorrect bulk write options --- test/types/middleware.test.ts | 4 ++-- test/types/models.test.ts | 6 ++---- types/index.d.ts | 2 +- types/models.d.ts | 22 +++++++++++++++++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/test/types/middleware.test.ts b/test/types/middleware.test.ts index e127c3b683b..94961c9b6d2 100644 --- a/test/types/middleware.test.ts +++ b/test/types/middleware.test.ts @@ -1,6 +1,6 @@ -import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult } from 'mongoose'; +import { Schema, model, Model, Document, SaveOptions, Query, Aggregate, HydratedDocument, PreSaveMiddlewareFunction, ModifyResult, AnyBulkWriteOperation } from 'mongoose'; import { expectError, expectType, expectNotType, expectAssignable } from 'tsd'; -import { AnyBulkWriteOperation, CreateCollectionOptions } from 'mongodb'; +import { CreateCollectionOptions } from 'mongodb'; const preMiddlewareFn: PreSaveMiddlewareFunction = function(next, opts) { this.$markValid('name'); diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 3e1bd32449f..1633c8d35b5 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -824,7 +824,7 @@ async function gh14072() { ); const M = mongoose.model('Test', schema); - const bulkWriteArray = [ + await M.bulkWrite([ { insertOne: { document: { num: 3 } @@ -844,9 +844,7 @@ async function gh14072() { timestamps: false } } - ]; - - await M.bulkWrite(bulkWriteArray); + ]); } async function gh14003() { diff --git a/types/index.d.ts b/types/index.d.ts index f6835f6b329..0402bfa32fa 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -431,7 +431,7 @@ declare module 'mongoose' { fn: ( this: T, next: (err?: CallbackError) => void, - ops: Array & MongooseBulkWritePerWriteOptions>, + ops: Array>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions ) => void | Promise ): this; diff --git a/types/models.d.ts b/types/models.d.ts index a56f1ee92e4..65893729490 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -23,13 +23,21 @@ declare module 'mongoose' { ): U; } - interface MongooseBulkWriteOptions { + interface MongooseBulkWriteOptions extends mongodb.BulkWriteOptions { + session?: ClientSession; skipValidation?: boolean; throwOnValidationError?: boolean; - timestamps?: boolean; strict?: boolean | 'throw'; } + interface MongooseBulkSaveOptions extends mongodb.BulkWriteOptions { + timestamps?: boolean; + session?: ClientSession; + } + + /** + * @deprecated use AnyBulkWriteOperation instead + */ interface MongooseBulkWritePerWriteOptions { timestamps?: boolean; strict?: boolean | 'throw'; @@ -200,6 +208,8 @@ declare module 'mongoose' { hint?: mongodb.Hint; /** When true, creates a new document if no document matches the query. */ upsert?: boolean; + /** When false, do not add timestamps. */ + timestamps?: boolean; } export interface UpdateManyModel { @@ -215,6 +225,8 @@ declare module 'mongoose' { hint?: mongodb.Hint; /** When true, creates a new document if no document matches the query. */ upsert?: boolean; + /** When false, do not add timestamps. */ + timestamps?: boolean; } export interface DeleteOneModel { @@ -281,11 +293,11 @@ declare module 'mongoose' { */ bulkWrite( writes: Array>, - options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false } + options: MongooseBulkWriteOptions & { ordered: false } ): Promise; bulkWrite( writes: Array>, - options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions + options?: MongooseBulkWriteOptions ): Promise; /** @@ -293,7 +305,7 @@ declare module 'mongoose' { * sending multiple `save()` calls because with `bulkSave()` there is only one * network round trip to the MongoDB server. */ - bulkSave(documents: Array, options?: mongodb.BulkWriteOptions & { timestamps?: boolean }): Promise; + bulkSave(documents: Array, options?: MongooseBulkSaveOptions): Promise; /** Collection the model uses. */ collection: Collection;