Skip to content

Commit

Permalink
types(models): allow specifying timestamps as inline option for bul…
Browse files Browse the repository at this point in the history
…kWrite() operations

Fix #14072
  • Loading branch information
vkarpov15 committed Nov 22, 2023
1 parent d53b1c5 commit aaa6269
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
49 changes: 49 additions & 0 deletions test/types/models.test.ts
Expand Up @@ -796,3 +796,52 @@ async function gh14026() {

expectType<string[]>(await TestModel.distinct('bar'));
}

async function gh14072() {
type Test = {
_id: mongoose.Types.ObjectId;
num: number;
created_at: number;
updated_at: number;
};

const schema = new mongoose.Schema<Test>(
{
num: { type: Number },
created_at: { type: Number },
updated_at: { type: Number }
},
{
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at',
currentTime: () => new Date().valueOf() / 1000
}
}
);

const M = mongoose.model<Test>('Test', schema);
const bulkWriteArray = [
{
insertOne: {
document: { num: 3 }
}
},
{
updateOne: {
filter: { num: 6 },
update: { num: 8 },
timestamps: false
}
},
{
updateMany: {
filter: { num: 5 },
update: { num: 10 },
timestamps: false
}
}
];

await M.bulkWrite(bulkWriteArray);
}
16 changes: 14 additions & 2 deletions types/models.d.ts
Expand Up @@ -26,6 +26,12 @@ declare module 'mongoose' {
interface MongooseBulkWriteOptions {
skipValidation?: boolean;
throwOnValidationError?: boolean;
timestamps?: boolean;
}

interface MongooseBulkWritePerWriteOptions {
timestamps?: boolean;
strict?: boolean;
}

interface InsertManyOptions extends
Expand Down Expand Up @@ -183,11 +189,17 @@ declare module 'mongoose' {
* round trip to the MongoDB server.
*/
bulkWrite<DocContents = TRawDocType>(
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
writes: Array<
mongodb.AnyBulkWriteOperation<
DocContents extends mongodb.Document ? DocContents : any
> & MongooseBulkWritePerWriteOptions>,
options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false }
): Promise<mongodb.BulkWriteResult & { mongoose?: { validationErrors: Error[] } }>;
bulkWrite<DocContents = TRawDocType>(
writes: Array<mongodb.AnyBulkWriteOperation<DocContents extends Document ? any : (DocContents extends {} ? DocContents : any)>>,
writes: Array<
mongodb.AnyBulkWriteOperation<
DocContents extends mongodb.Document ? DocContents : any
> & MongooseBulkWritePerWriteOptions>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
): Promise<mongodb.BulkWriteResult>;

Expand Down

0 comments on commit aaa6269

Please sign in to comment.