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

bulkWrite doesn't respect extended schema timestamp option #13409

Closed
2 tasks done
chris-feist opened this issue May 17, 2023 · 1 comment · Fixed by #13445
Closed
2 tasks done

bulkWrite doesn't respect extended schema timestamp option #13409

chris-feist opened this issue May 17, 2023 · 1 comment · Fixed by #13445
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@chris-feist
Copy link

chris-feist commented May 17, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.1.1

Node.js version

16.19

MongoDB server version

5.3

Typescript version (if applicable)

4.9.5

Description

Schema.bulkWrite does not update the record timestamps when another base schema has been added to it that has the timestamps option set to true

Steps to Reproduce

Example schema design:

export interface BaseShape {
  readonly createdAt: Date;
  readonly updatedAt: Date;
}

export const baseOptions = { timestamps: true };

export const BaseSchema = new Schema<BaseShape>({}, baseOptions);

interface ExtendedShape extends BaseShape {
  name: string;
}

const extendedSchema = new Schema<ExtendedShape>({
  name: { type: String },
}).add(BaseSchema);

export const Extended = (models.Extended ||
  model<ExtendedShape>(
    "Extended",
    extendedSchema,
  )) as Model<ExtendedShape>;

In the example above. calling Extended.updateOne will update the timestamps. However, calling Extended.bulkWrite with the updateOne or updateMany option, does not update the timestamps. I have a current workaround to pass the same options:

const extendedSchema = new Schema<ExtendedShape>(
  {
    name: { type: String },
  },
  baseOptions,
).add(BaseSchema);

Expected Behavior

Schema.bulkWrite and Schema.updateOne should behave the same. Ideally, they both respect the base options

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label May 19, 2023
@IslandRhythms
Copy link
Collaborator

import { Schema, model, models, Model, connection, connect } from 'mongoose';

export interface BaseShape {
  readonly createdAt: Date;
  readonly updatedAt: Date;
}

export const baseOptions = { timestamps: true };

export const BaseSchema = new Schema<BaseShape>({}, baseOptions);

interface ExtendedShape extends BaseShape {
  name: string;
}

const extendedSchema = new Schema<ExtendedShape>({
  name: { type: String },
}).add(BaseSchema);

// workaround

// const extendedSchema = new Schema<ExtendedShape>(
//   {
//     name: { type: String },
//   },
//   baseOptions,
// ).add(BaseSchema);

export const Extended = (models.Extended ||
  model<ExtendedShape>(
    "Extended",
    extendedSchema,
)) as Model<ExtendedShape>;

async function run() {
  await connect('mongodb://localhost:27017');
  await connection.dropDatabase();

  const doc = await Extended.create({ name: 'Test' });
  console.log('what is doc', doc);
  console.log('=============================================')

  const upRes = await Extended.updateOne({ name: 'Test Testerson Single'});
  console.log(await Extended.findOne());
  console.log('===========================================')
  for (let i = 0; i < 10; i++) {
    // a small delay
    console.log(i)
  }
  const bulkRes = await Extended.bulkWrite([{ updateOne: {
    filter: { name: 'Test Testerson Single' },
    update: { name: 'Test Testerson Bulk'}
  }}]);
  console.log(await Extended.findOne());
}

run();

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed typescript Types or Types-test related issue / Pull Request labels May 23, 2023
@vkarpov15 vkarpov15 added this to the 7.2.2 milestone May 23, 2023
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 26, 2023
vkarpov15 added a commit that referenced this issue May 26, 2023
vkarpov15 added a commit that referenced this issue May 27, 2023
fix(schema): make bulkWrite updateOne() and updateMany() respect timestamps option when set by merging schemas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
3 participants