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

Incomplete argument types for Model.bulkWrite #14072

Closed
2 tasks done
sderrow opened this issue Nov 10, 2023 · 0 comments · Fixed by #14112
Closed
2 tasks done

Incomplete argument types for Model.bulkWrite #14072

sderrow opened this issue Nov 10, 2023 · 0 comments · Fixed by #14112
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@sderrow
Copy link
Contributor

sderrow commented Nov 10, 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

8.0.0

Node.js version

18.8.0

MongoDB server version

4.4

Typescript version (if applicable)

4.5.2

Description

The argument types for Model.bulkWrite are missing a couple different things.

  1. The writes argument uses mongodb.AnyBulkWriteOperation[] as its input type, but mongodb.AnyBulkWriteOperation is missing updateOne.timestamps and updateMany.timestamps since that's a mongoose-specific option

  2. The options argument doesn't include timestamps?: boolean even though the implementation looks at that option to know whether to include timestamps for insertOne writes. I would argue that this option should actually belong in the insertOne object, just like it does for updateOne and updateMany, but at a minimum, the type and documentation should be updated to reflect the implementation

Steps to Reproduce

import { AnyBulkWriteOperation } from "mongodb";
import mongoose from "mongoose";

const bulkWriteTypeTest = async () => {
  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: AnyBulkWriteOperation[] = [
    {
      insertOne: {
        document: { num: 3 },
      },
    },
    {
      updateOne: {
        filter: { num: 6 },
        update: { num: 8 },
        timestamps: false,
        /* TYPESCRIPT ERROR FOR ABOVE LINE:
          Type '{ filter: { num: number; }; update: { num: number; }; timestamps: false; }' is not assignable to type 'UpdateOneModel<Document>'.
            Object literal may only specify known properties, and 'timestamps' does not exist in type 'UpdateOneModel<Document>'.
        */
      },
    },
    {
      updateMany: {
        filter: { num: 5 },
        update: { num: 10 },
        timestamps: false,
        /* TYPESCRIPT ERROR FOR ABOVE LINE:
          Type '{ filter: { num: number; }; update: { num: number; }; timestamps: false; }' is not assignable to type 'UpdateManyModel<Document>'.
            Object literal may only specify known properties, and 'timestamps' does not exist in type 'UpdateManyModel<Document>'.
        */
      },
    },
  ];

  // NOTE: the below `timestamps` option applies solely to the `insertOne`
  await M.bulkWrite(bulkWriteArray, { timestamps: false });

  /* TYPESCRIPT ERROR FOR ABOVE LINE:
    No overload matches this call.
      Overload 1 of 2, '(writes: AnyBulkWriteOperation<Document>[], options: BulkWriteOptions & MongooseBulkWriteOptions & { ...; }): Promise<...>', gave the following error.
        Argument of type '{ timestamps: boolean; }' is not assignable to parameter of type 'BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false; }'.
          Object literal may only specify known properties, and 'timestamps' does not exist in type 'BulkWriteOptions & MongooseBulkWriteOptions & { ordered: false; }'.
      Overload 2 of 2, '(writes: AnyBulkWriteOperation<Document>[], options?: (BulkWriteOptions & MongooseBulkWriteOptions) | undefined): Promise<...>', gave the following error.
        Argument of type '{ timestamps: boolean; }' is not assignable to parameter of type 'BulkWriteOptions & MongooseBulkWriteOptions'.
          Object literal may only specify known properties, and 'timestamps' does not exist in type 'BulkWriteOptions & MongooseBulkWriteOptions'.
  */
};

Expected Behavior

None of the above 3 Typescript errors that currently appear.

@vkarpov15 vkarpov15 added this to the 8.0.2 milestone Nov 15, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Nov 15, 2023
vkarpov15 added a commit that referenced this issue Nov 22, 2023
vkarpov15 added a commit that referenced this issue Nov 23, 2023
types(models): allow specifying `timestamps` as inline option for bulkWrite() operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants