Clear database middleware and run middleware on batch creation #220
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a
clear()
method toDatabases.Middleware
to clear previously added middleware. This is useful for testing especially.This PR also adds the functionality to the batch creation, that it runs model middleware as well, which it did not previously. Regarding the handling of potential failures inside middleware, I've added two options for now, but if you see any other ones that fit, please lmk.
You can do
arrayOfModels.create(on: db, onMiddlewareFailure: .insertSucceeded)
- This is the default one and saves all of the models where the middleware for that specific model did not fail, any other models where the middleware failed are not saved. I think this is the most useful one, in most cases I wouldn't want a batch of 100 models to fail if a single one had an issue with some middleware. But again open for discussion.The other option is to do:
arrayOfModels.create(on: db, onMiddlewareFailure: .failOnFirst)
which doesn't save any models if any of the middleware fails, it fails fast on the first failure.