Skip to content

Commit

Permalink
Merge pull request #14112 from Automattic/vkarpov15/gh-14072
Browse files Browse the repository at this point in the history
types(models): allow specifying `timestamps` as inline option for bulkWrite() operations
  • Loading branch information
vkarpov15 committed Nov 23, 2023
2 parents 3e0a90c + 5ac950e commit d929f29
Show file tree
Hide file tree
Showing 2 changed files with 65 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);
}
18 changes: 16 additions & 2 deletions types/models.d.ts
Expand Up @@ -26,6 +26,14 @@ declare module 'mongoose' {
interface MongooseBulkWriteOptions {
skipValidation?: boolean;
throwOnValidationError?: boolean;
timestamps?: boolean;
}

interface MongooseBulkWritePerWriteOptions {
timestamps?: boolean;
strict?: boolean;
session?: ClientSession;
skipValidation?: boolean;
}

interface InsertManyOptions extends
Expand Down Expand Up @@ -183,11 +191,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 d929f29

Please sign in to comment.