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

Typescript: InsertMany missing array overload with options #13930

Closed
2 tasks done
t1bb4r opened this issue Oct 4, 2023 · 2 comments
Closed
2 tasks done

Typescript: InsertMany missing array overload with options #13930

t1bb4r opened this issue Oct 4, 2023 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@t1bb4r
Copy link
Contributor

t1bb4r commented Oct 4, 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

6.12.0

Node.js version

16.20.1

MongoDB server version

4.4.x

Typescript version (if applicable)

4.9.5

Description

Using any options ({ordered: false}) with Model.InsertMany causes the return type to change. In the example I've forced the type parameter to cause a typescript error, but this also causes a problem without it. As we are inserting an array but there is no overload for this, it defaults to the 'single' insertion response type which is incorrect.

The issue is also present in mongoose 7.

#13904 seems closely related but not exactly, so I've decided to open a new issue.

Steps to Reproduce

export interface IUser {
  name: string;
}

export const usersSchema = new mongoose.Schema({
  name: {
    type: String,
  },
});

const Users = mongoose.model<IUser>('Users', usersSchema);

// insertOrderedSingle
Users.insertMany<{ name: string; }>({ name: 'Zildjian' });
// insertArray
Users.insertMany<{ name: string; }>([{ name: 'Zildjian' }]);
// insertArrayWithOptions
Users.insertMany<{ name: string; }>([{ name: 'Zildjian' }], {});

Expected Behavior

Normal options should not change the return type of InsertMany

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

import * as mongoose from 'mongoose';

interface IUser {
  name: string;
}

const usersSchema = new mongoose.Schema({
  name: {
    type: String,
  },
});

const Users = mongoose.model<IUser>('Users', usersSchema);

// insertOrderedSingle
Users.insertMany<{ name: string; }>({ name: 'Zildjian' });
// insertArray
Users.insertMany<{ name: string; }>([{ name: 'Zildjian' }]);
// insertArrayWithOptions
Users.insertMany<{ name: string; }>([{ name: 'Zildjian' }], {});

@t1bb4r
Copy link
Contributor Author

t1bb4r commented Oct 5, 2023

Fixed by #13931

@t1bb4r t1bb4r closed this as completed Oct 5, 2023
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
Development

No branches or pull requests

2 participants