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): allow specifying timestamps as inline option for bulkWrite() operations #14112

Merged
merged 2 commits into from Nov 23, 2023
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
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
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
> & 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