Question:
I have a architecture question about dealing with transient data when creating Polymorphic relational records.
I’m trying to figure out the best scenario for dealing with my current example based the ability to address idempotency, orphaned data (‘garbage management’) and performance.
Below I outline what I’m working with and I would like to know how other people have solved this problem.
Thank you
- Josh Chernoff.
Example
I have two models.
Post
Image
Post
and Image
relate via an images_posts
join table and via ecto’s
many_to_many :images, MyApp.Image, join_through: "images_posts"`
Current Dependancies:
- Arc (image uploader with using ex_aws)
Scenarios
-
Using a Single http
POST
request to create a newPost
record I upload all the form data for thePost
along with the all theImage
data. The controller action handles creating thePost
first then respectively all the images in one request.
Con: Validations will cause a user to re upload images
Pro: The logic is procedural and easy to conceptualize -
Using multiple http
POST
requests I create images one by one passing back ids that thePost
form will use laster to build the relation.
Con: Image data can become orphaned, more logic is needed to build the relations and clean up transient data.
Pro: Form validation has less of a performance impact. -
(‘conceptual idea’) Using elixir’s
processes
make a temp place to store theImage
records and upload eachImage
one by one then later make reference to theprocesses
when creating thePost
.
I have to admit I have no idea what this looks like and it was only mentioned to me in passing by someone as an option. The idea is that you treat theImage
data as transient data as much as possible and only create theImage
record when creating thePost
is successful removing the need to deal with clean up ect…