You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The way Model's batch CRUD methods currently work with ModelMiddleware is inconsistent with normal CRUD. When doing a normal, single-model CRUD operation, the actual operation happens when you pass along the request to next. However, during batch CRUD, the request to next is only adding the model to a buffer. This creates issues with code that expects the operation to have been completed by time the next responder finishes. For example, take the following code:
func create(model:User, on db:Database, next:AnyModelResponder)->EventLoopFuture<Void>{
next.create(model, on: db).flatMap{ _ inself.updateIfNeeded(model: model, db: db)}.flatMap{ _ in
model.save(on: db)}}
This middleware code is designed to run after a model is created. After the model is created, it does an update, and another save if needed. When using normal, single-model CRUD it works as expected. However, when using batch CRUD, since the create is not actually happening in next.create, the update / save runs before and is responsible for creating the model. When the batch create finally runs after all of this code, it will likely fail since the models have already been created.
The text was updated successfully, but these errors were encountered:
I guess an issue is that to provide the same functionality we could end up just doing n amount of individual queries instead of utilizing database features to insert multiple rows at once, right? Not sure what the performance cost are, but I am guessing the database is faster than multiple individual INSERT queries.
Thanks to @jdmcd for reporting. I was thinking we could offer a separate method for batch create specifically. Something like:
func batchCreate(models:[Model], on db:Database, next:AnyModelResponder)->EventLoopFuture<Void>
This would allow us to actually do the create operation in the AnyModelResponder callback and then everything should work as intended. I think having access to the entire array could prove beneficial as well. For instance, the middleware could choose to add or remove models from a batch create operation, change it to an individual create, easily set a property on all models to a derived value for that operation, etc.
The way Model's batch CRUD methods currently work with ModelMiddleware is inconsistent with normal CRUD. When doing a normal, single-model CRUD operation, the actual operation happens when you pass along the request to
next
. However, during batch CRUD, the request tonext
is only adding the model to a buffer. This creates issues with code that expects the operation to have been completed by time the next responder finishes. For example, take the following code:This middleware code is designed to run after a model is created. After the model is created, it does an update, and another save if needed. When using normal, single-model CRUD it works as expected. However, when using batch CRUD, since the create is not actually happening in
next.create
, the update / save runs before and is responsible for creating the model. When the batch create finally runs after all of this code, it will likely fail since the models have already been created.The text was updated successfully, but these errors were encountered: