<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="141919" data-post-id="141919">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Blokh" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Blokh/120/16589_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Blokh
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/bottlenecked" rel="nofollow">@bottlenecked</a>  Thank you very much again.<br>
I agree with all of your points. I will try to redirect my thinking toward the CQRS kind of thinking.</p>
<p><a class="mention" href="/u/benwilson512" rel="nofollow">@benwilson512</a> Thank you very much! I will review the post ASAP.</p>
<p><a class="mention" href="/u/slashdotdash" rel="nofollow">@slashdotdash</a> Thank you very much! I’ll check out them the aggregate_state/3 method.<br>
Although I think I am leaning toward CQRS currently.</p>
<p>I have a lot to learn tho.</p>
<p>Thank you very much to you all that invested your time to respond to my questions. you are awesome.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="141919" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-141919" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="141919"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="141953" data-post-id="141953">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Crowdhailer" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Crowdhailer/120/2438_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Crowdhailer
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Raxx</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Busting some CQRS myths</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/">
  <header class="source">
      <img src="https://lostechies.com/assets/images/favicon.ico" class="site-icon" alt="" width="26" height="38">

      <a href="https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/" target="_blank" rel="noopener nofollow ugc">lostechies.com</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/" target="_blank" rel="noopener nofollow ugc">Busting some CQRS myths · Los Techies</a></h3>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<blockquote>
<p>Myth <span class="hashtag-raw">#1</span> – CQRS = Event Sourcing and vice versa</p>
<p><a href="http://martinfowler.com/eaaDev/EventSourcing.html" rel="noopener nofollow ugc">Event sourcing</a> often fits well with CQRS, as it makes building and updating read stores over time a bit more straightforward in eventually consistent models. Additionally, the aggregates living in the command side for behavior-heavier systems and task-based UIs more obvious.</p>
<p>However, <strong>event sourcing is a completely orthogonal concept to CQRS</strong> . While they fit well together, doing CQRS does not require event sourcing, and doing event sourcing does not automatically mean we’re doing CQRS.</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="141953" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-141953" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="141953"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="142226" data-post-id="142226">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mjaric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mjaric/120/7788_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mjaric
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi, <a class="mention" href="/u/blokh" rel="nofollow">@Blokh</a>,</p>
<p>There is a way to “plug” Event Sourcing easily in combination with Ecto. Here is simplest example</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Sourced.Repo do
  use Ecto.Repo, otp_app: :sourced, adapter: Ecto.Adapters.Postgres
end

defmodule Sourced.DomainEvent do
  use Ecto.Schema
  import Ecto.Changeset

  schema "domain_events" do
    field(:name, :string)
    field(:source, :string)
    field(:data, :string)
  end

  def append(params) do
    fields = [:name, :source, :data]

    __MODULE__
    |&gt; struct([])
    |&gt; cast(params, fields)
    |&gt; validate_required(fields)
  end
end

defmodule Sourced.User do
  use Ecto.Schema
  import Ecto.Changeset
  alias Sourced.DomainEvent

  schema "users" do
    field(:first_name, :string)
    field(:last_name, :string)
    field(:age, :integer)
    field(:email, :string)
  end

  def create(params) do
    required = [:first_name, :last_name, :email]
    fields = [:age | required]
    # this should be regular path, where you cast params to your model
    __MODULE__
    |&gt; struct([])
    |&gt; cast(params, fields)
    |&gt; validate_required(required)
    # at this point we will know if change to model is valid
    |&gt; case do
      %{valid?: false, errors: errors} -&gt;
        # if change didn't pass validation, return error
        {:error, errors}

      %{changes: changes} = model_changeset -&gt;
        # now we want to generate and "pack" events
        # note that we can split `:change` to several events
        # but below I put them all in single event
        event_changeset =
          DomainEvent.append(%{
            name: "user_created",
            source: Atom.to_string(__MODULE__),
            data: Jason.encode!(changes)
          })

        # use ecto multi and explicit repo transaction so we make sure that
        # all data is stored!!! otherwise you will endup with incosistency
        # between state and event journal
        Ecto.Multi.new()
        |&gt; Ecto.Multi.insert("create_user", model_changeset)
        |&gt; Ecto.Multi.insert("user_created", event_changeset)
        |&gt; Sourced.Repo.transaction()
    end
  end
end


</code></pre>
<p>And here is test case (again, very simple)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule SourcedTest do
  use ExUnit.Case

  setup do
    # Explicitly get a connection before each test
    :ok = Ecto.Adapters.SQL.Sandbox.checkout(Sourced.Repo)
  end

  test "should persist new user and domain event" do
    assert {:ok, %{"create_user" =&gt; _, "user_created" =&gt; _}} =
             Sourced.User.create(%{
               first_name: "foo",
               last_name: "bar",
               email: "foo.bar@example.com"
             })
  end
end

</code></pre>
<p>One disclaimer for about example. What ever CQRS is trying to solve, it is NOT solved in this example. So either you have to solve it by your self or use 3rd party lib.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="142226" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-142226" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="142226"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="142230" data-post-id="142230">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mjaric" data-post="14" data-topic="25049">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mjaric/48/7788_2.png" class="avatar"> mjaric:</div>
<blockquote>
<p>There is a way to “plug” Event Sourcing easily in combination with Ecto. Here is simplest example</p>
</blockquote>
</aside>
<p>Event sourcing is more than just writing to a table called events. The “sourcing” bit of event sourcing means that you are driving state exclusively off of the event log. Your example here simply writes an event <em>alongside</em> the normal table insert, it doesn’t drive the table insert off of the event. To do this properly you also need order guarantees: that each event is processed in order to other events.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="142230" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-142230" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="142230"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="142231" data-post-id="142231">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mjaric" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mjaric/120/7788_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mjaric
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That is true, and that is why I put disclaimer at the bottom and this is just one possible path and not solution</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="142231" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-142231" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="142231"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="143270" data-post-id="143270">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Blokh" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Blokh/120/16589_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Blokh
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you <a class="mention" href="/u/mjaric" rel="nofollow">@mjaric</a> and <a class="mention" href="/u/benwilson512" rel="nofollow">@benwilson512</a>.<br>
I’ve took the CQRS approach.<br>
I will take a while to study how to properly code in elixir especially with the CQRS approach. but that’s fine I’ll do my best <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="143270" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/event-sourcing-in-elixir-without-cqrs/25049/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-143270" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="143270"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>