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

Support for options argument when creating a single document in Model.create() #14843

Open
2 tasks done
kmarchenko opened this issue Aug 29, 2024 · 3 comments
Open
2 tasks done
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class

Comments

@kmarchenko
Copy link

Prerequisites

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

🚀 Feature Proposal

Enhance the Model.create() method to allow passing an options argument when creating a single document. Currently, this is only possible when documents are passed as an array, which is inconvenient for cases where only a single document needs to be created.

Motivation

The current implementation of the Model.create() method requires documents to be passed as an array when using the options argument, even if only a single document is being created. This approach is less intuitive and can lead to unnecessary complexity, such as having to handle an array in return when only a single document is expected. Allowing the options argument to be used with a single document directly would result in cleaner, more straightforward code and avoid the need to deal with array structures when they aren't necessary.

Example

Current Implementation:

const [document] = await model.create(
  [
    {
      name: 'example',
    },
  ],
  {
    session: ClientSession,
  },
);

Proposed Implementation:

const document = await model.create(
  {
    name: 'example',
  },
  {
    session: ClientSession,
  },
);
@kmarchenko kmarchenko added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Aug 29, 2024
@kmarchenko
Copy link
Author

If this feature is deemed useful, I'm happy to take on the implementation. Please let me know if there are any additional requirements or considerations that should be addressed before proceeding.

@vkarpov15
Copy link
Collaborator

We don't want to make this particular change because doing so would be backwards breaking in a difficult-to-debug way. People using Model.create(doc1, doc2) will start getting unexpected errors and potential data loss.

What do you think about instead adding a Model.insertOne(doc, [options]) function?

@kmarchenko
Copy link
Author

Adding a Model.insertOne(doc, [options]) function does indeed seem like a good solution to address this issue.
I'll start working on implementing this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

2 participants