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

feat(model): add create option "immediateError" #13544

Merged
merged 5 commits into from Jul 11, 2023

Conversation

hasezoey
Copy link
Collaborator

Summary

This PR adds option immediateError to create, to not throw on the first error encountered and instead add the error to the result list (only applicable if the input is a array)

Default: true for backwards compatibility (current master behavior)

works with both ordered: false and ordered: true

Result of immediateError: false:

const result = await Model.create([{ test: "yes" }, {}, { test: "no" }], { immediateError: false });
// index 0 is the "test: yes" document
// index 1 is a error instance
// index 2 is the "test: no" document

is this implementation OK or should it maybe be split into returning a object of values and errors or some other way?

also moves option ordered from "SaveOptions" (on .save) to "CreateOptions" (to .create) because from what i can tell, the option ordered has no effect on .save itself

fixes #1731

@hasezoey hasezoey added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jun 26, 2023
res = await Promise.all(args.map(async doc => {
// ".bind(Promise)" is required, otherwise results in "TypeError: Promise.allSettled called on non-object"
const promiseType = !immediateError ? Promise.allSettled.bind(Promise) : Promise.all.bind(Promise);
let p = promiseType(args.map(async doc => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't this be just Promise.allSettled(args.map(...)) ? bind() is relatively slow, so I would prefer to avoid using that.

Copy link
Collaborator Author

@hasezoey hasezoey Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had avoided that because using allSettled in all cases would mean to both wait until all operations have finished, only to then throw the first error and second the allSettled result has to be mapped because of what it returns (ie to keep the current behavior unless requested)

lib/model.js Outdated
@@ -2802,6 +2801,8 @@ Model.findByIdAndRemove = function(id, options) {
*
* @param {Array|Object} docs Documents to insert, as a spread or array
* @param {Object} [options] Options passed down to `save()`. To specify `options`, `docs` **must** be an array, not a spread. See [Model.save](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) for available options.
* @param {Boolean} [options.ordered] saves the docs in series rather than parallel.
* @param {Boolean} [options.immediateError] If a Error occurs, throw the error immediately, instead of collecting in a result, default: true (backwards compatability)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like this option name. I was thinking continueOnError to be consistent with eachAsync() and createCollections(), but continueOnError aggregates an error object to throw when an error occurs. Do you have any other naming ideas? Maybe useAllSettled or aggregateErrors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had named it immediateError because of the current (master) behavior where once a error is encountered the first one is directly thrown / rejected, but all operations should still run and any subsequent error is just silent

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we name the option aggregateErrors instead? I think that options that are treated as false by default are cleaner to work with.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs some TS changes. Also, please update this PR to target the 7.4 branch.

@@ -217,7 +221,7 @@ declare module 'mongoose' {
>;

/** Creates a new document or documents */
create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options?: SaveOptions): Promise<THydratedDocumentType[]>;
create<DocContents = AnyKeys<TRawDocType>>(docs: Array<TRawDocType | DocContents>, options?: CreateOptions): Promise<THydratedDocumentType[]>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add TypeScript types to indicate that, if immediateError: false, then the return type is Promise<Array<THydratedDocumentType | Error>>

@vkarpov15 vkarpov15 added this to the 7.4.0 milestone Jul 4, 2023
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will merge this into 7.4.

@vkarpov15 vkarpov15 changed the base branch from master to 7.4 July 11, 2023 19:02
@vkarpov15 vkarpov15 merged commit 05cfd01 into Automattic:7.4 Jul 11, 2023
21 checks passed
@hasezoey hasezoey deleted the fix1731 branch July 11, 2023 19:25
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model.create(doc, fn) should save all valid documents.
2 participants