GithubHelp home page GithubHelp logo

Comments (9)

joshgoebel avatar joshgoebel commented on August 15, 2024

I feel like the right injection point might be a new Repo module or wrapper function around the repo itself vs the changeset...

changeset = Model.changeset(data)
VersioningRepo.insert!(changeset)

or

changeset = Model.changeset(data)
TrackChanges.insert!(changeset, MyApp.Repo)

Just some thoughts.

from whatwasit.

smpallen99 avatar smpallen99 commented on August 15, 2024

My original attempt was to override the insert and delete actions on the repo. José’s feedback was to use the changeset, so that is what I did.

I don’t think there is anything stopping someone one doing at the repo level. Just don’t use the prepare_version api. The basic APIs should be available to do the inserts at the repo level.

Steve

On Jul 23, 2016, at 3:04 PM, Josh Goebel [email protected] wrote:

I feel like the right injection point might be a new Repo module or wrapper function around the repo itself vs the changeset...

changeset = Model.changeset(data)
VersioningRepo.insert!(changeset)
or

changeset = Model.changeset(data)
TrackChanges.insert!(changeset, MyApp.Repo)
Just some thoughts.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub #4 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AA2YpeWXMHGTptWMF8qUG-3sCUvhAgHIks5qYmXJgaJpZM4JTa52.

from whatwasit.

joshgoebel avatar joshgoebel commented on August 15, 2024

I pinged him on Twitter with my question. It just seems like you're making (suggesting) a change to changeset() that entirely alters the behavior of it.

I'm very curious to see the "right" way to do this in Elixir (it's the exact reason i came to read this code in the first place), but this doesn't feel like it. And imagine we start to have a bunch of other "plugins" that do things with outside side effects (send an email, text, etc)... is the recommendation really to put them ALL inside changeset?

from whatwasit.

joshgoebel avatar joshgoebel commented on August 15, 2024

Also for most systems shouldn't we really include a transaction in the examples? What about something like:

Repo.transaction fn ->
  changeset = Model.changeset(model, new_data)
  |> Model.save_prior_version!
  |> Repo.insert!
end

from whatwasit.

smpallen99 avatar smpallen99 commented on August 15, 2024

@yyyc514 Actually, there are no side effects in the changeset. prepare_versions uses the Changeset.prepare_changes api. It is passed an ann fun that gets called during the Repo action. If the Repo action fails, the prepare_changes function does not get called. So, there is no need for a transaction.

If you read the docs for prepare_changes you'll see that it is designed for this type of post processing.

from whatwasit.

josevalim avatar josevalim commented on August 15, 2024

So to clarify: changesets are split in two stages. The casting, validation, preparing are first and are side-effect free. Then, once you send it to the repository, all side-effects happen. This includes inserting to the database, running prepare_changes, and setting up transactions (if prepare changes or associations are included). So I wouldn't say changesets are side-effects free but rather they split the stages with side-effects and without side-effects in two very clear steps. There is still a lot of benefit in splitting, you get to have completely sane and immutable semantics until the database comes into action. Btw, the transaction is automatically taken care of if you have associations or set prepare_changes.

from whatwasit.

joshgoebel avatar joshgoebel commented on August 15, 2024

Changeset.prepare_changes Ah that is what I was somehow missing. Thank you! Now it's all so very clear. :-) Closing issue.

from whatwasit.

joshgoebel avatar joshgoebel commented on August 15, 2024

@smpallen99 Sorry for the confusion. :)

from whatwasit.

smpallen99 avatar smpallen99 commented on August 15, 2024

Josh,

No problem. It was a good discussion. I appreciate José’s description of the two stages of a changeset.

Steve

On Jul 24, 2016, at 12:32 PM, Josh Goebel [email protected] wrote:

@smpallen99 https://github.com/smpallen99 Sorry for the confusion. :)


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #4 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/AA2YpTEX044AvKcdi-dBxIaAZtmYi8ckks5qY5OIgaJpZM4JTa52.

from whatwasit.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.