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

types(models): fix incorrect bulk write options #14513

Merged
merged 1 commit into from Apr 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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<Document> = function(next, opts) {
this.$markValid('name');
Expand Down
6 changes: 2 additions & 4 deletions test/types/models.test.ts
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
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
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