Skip to content

Commit d929f29

Browse files
authored
Merge pull request #14112 from Automattic/vkarpov15/gh-14072
types(models): allow specifying `timestamps` as inline option for bulkWrite() operations
2 parents 3e0a90c + 5ac950e commit d929f29

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

test/types/models.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,3 +796,52 @@ async function gh14026() {
796796

797797
expectType<string[]>(await TestModel.distinct('bar'));
798798
}
799+
800+
async function gh14072() {
801+
type Test = {
802+
_id: mongoose.Types.ObjectId;
803+
num: number;
804+
created_at: number;
805+
updated_at: number;
806+
};
807+
808+
const schema = new mongoose.Schema<Test>(
809+
{
810+
num: { type: Number },
811+
created_at: { type: Number },
812+
updated_at: { type: Number }
813+
},
814+
{
815+
timestamps: {
816+
createdAt: 'created_at',
817+
updatedAt: 'updated_at',
818+
currentTime: () => new Date().valueOf() / 1000
819+
}
820+
}
821+
);
822+
823+
const M = mongoose.model<Test>('Test', schema);
824+
const bulkWriteArray = [
825+
{
826+
insertOne: {
827+
document: { num: 3 }
828+
}
829+
},
830+
{
831+
updateOne: {
832+
filter: { num: 6 },
833+
update: { num: 8 },
834+
timestamps: false
835+
}
836+
},
837+
{
838+
updateMany: {
839+
filter: { num: 5 },
840+
update: { num: 10 },
841+
timestamps: false
842+
}
843+
}
844+
];
845+
846+
await M.bulkWrite(bulkWriteArray);
847+
}

types/models.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ declare module 'mongoose' {
2626
interface MongooseBulkWriteOptions {
2727
skipValidation?: boolean;
2828
throwOnValidationError?: boolean;
29+
timestamps?: boolean;
30+
}
31+
32+
interface MongooseBulkWritePerWriteOptions {
33+
timestamps?: boolean;
34+
strict?: boolean;
35+
session?: ClientSession;
36+
skipValidation?: boolean;
2937
}
3038

3139
interface InsertManyOptions extends
@@ -183,11 +191,17 @@ declare module 'mongoose' {
183191
* round trip to the MongoDB server.
184192
*/
185193
bulkWrite<DocContents = TRawDocType>(
186-
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
194+
writes: Array<
195+
mongodb.AnyBulkWriteOperation<
196+
DocContents extends mongodb.Document ? DocContents : any
197+
> & MongooseBulkWritePerWriteOptions>,
187198
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
188199
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
189200
bulkWrite<DocContents = TRawDocType>(
190-
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
201+
writes: Array<
202+
mongodb.AnyBulkWriteOperation<
203+
DocContents extends mongodb.Document ? DocContents : any
204+
> & MongooseBulkWritePerWriteOptions>,
191205
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
192206
): Promise<mongodb.BulkWriteResult>;
193207

0 commit comments

Comments
 (0)