Skip to content

Commit

Permalink
types(models): fix incorrect bulk write options
Browse files Browse the repository at this point in the history
  • Loading branch information
emiljanitzek committed Apr 9, 2024
1 parent e359b99 commit 3c0e1eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
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

0 comments on commit 3c0e1eb

Please sign in to comment.