Skip to content

Commit

Permalink
Merge pull request #14513 from emiljanitzek/feature/bulk-write-types
Browse files Browse the repository at this point in the history
types(models): fix incorrect bulk write options
  • Loading branch information
vkarpov15 authored Apr 11, 2024
2 parents 480c3f3 + f54f9d0 commit 78bbcb5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions test/types/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -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<Document> = function(next, opts) {
this.$markValid('name');
Expand Down
6 changes: 2 additions & 4 deletions test/types/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ async function gh14072() {
);

const M = mongoose.model<Test>('Test', schema);
const bulkWriteArray = [
await M.bulkWrite([
{
insertOne: {
document: { num: 3 }
Expand All @@ -844,9 +844,7 @@ async function gh14072() {
timestamps: false
}
}
];

await M.bulkWrite(bulkWriteArray);
]);
}

async function gh14003() {
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ declare module 'mongoose' {
fn: (
this: T,
next: (err?: CallbackError) => void,
ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
ops: Array<AnyBulkWriteOperation<any>>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
) => void | Promise<void>
): this;
Expand Down
22 changes: 17 additions & 5 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<TSchema = AnyObject> {
Expand All @@ -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<TSchema = AnyObject> {
Expand Down Expand Up @@ -281,19 +293,19 @@ declare module 'mongoose' {
*/
bulkWrite<DocContents = TRawDocType>(
writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
options: MongooseBulkWriteOptions & { ordered: false }
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
bulkWrite<DocContents = TRawDocType>(
writes: Array<AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
options?: MongooseBulkWriteOptions
): Promise<mongodb.BulkWriteResult>;

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

/** Collection the model uses. */
collection: Collection;
Expand Down

0 comments on commit 78bbcb5

Please sign in to comment.