# Commands on repos vs changesets on relations

**URL:** <https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466>\
**Category:** Architecture Design\
**Created:** [May 13, 2021, 12:25pm UTC](https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466 "2021-05-13T12:25:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ineu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ineu/32/235_2.png) [@ineu](https://discourse.rom-rb.org/u/ineu)\
**Post date:** [May 13, 2021, 12:25pm UTC](https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466/1 "2021-05-13T12:25:59Z")

</div>

As stated in [How do you set timestamps in version 4.0? - #7 by buszu](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/7), since v4 changesets are available directly on relations. The documentation at [ROM - Commands](https://rom-rb.org/5.0/learn/core/commands/) says:

> For consistency, you should consider using changesets instead of commands

However, the repositories documentation [ROM - Quick Start](https://rom-rb.org/5.0/learn/repositories/quick-start/) describes only commands. Does it mean that if I want additional changesets functionality (for example, :add\_timestamps), I need to create a record via relation, not repo? So, given I have a repo with commands configured, instead of

```auto
users_repo.create(name: 'foo')

```

I now need to do more verbose

```auto
users_repo.users.changeset(:create, ...).map(:add_timestamps).commit

```

? Is going through relations a recommended approach now?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [May 15, 2021, 8:53am UTC](https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466/2 "2021-05-15T08:53:54Z")

</div>

> [@ineu](#):
>
> I now need to do more verbose
> 
> ```auto
> users_repo.users.changeset(:create, ...).map(:add_timestamps).commit
> 
> ```
> 
> ? Is going through relations a recommended approach now?

Oh, no - it’s just that in repos you can use changesets instead of commands. I’ll be adding a changeset plugin to repos in 6.0 so this will become clearer. Not sure if we event want to keep `commands` plugin enabled by default. Thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![ineu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ineu/32/235_2.png) [@ineu](https://discourse.rom-rb.org/u/ineu)\
**Post date:** [May 26, 2021, 9:34pm UTC](https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466/3 "2021-05-26T21:34:37Z")

</div>

I see. So in 6.0 there’s be a declarative way via a plugin. And for now I can do something like this in a repo:

```ruby
class ContractRepository < ROM::Repository::Root
  # ...
  def upsert(id, attrs)
    changeset =
      if id
        root.by_pk(id).changeset(:update, attrs)
      else
        root.changeset(:create, attrs)
      end

    changeset.map(:add_timestamps).commit
  end
end

```

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [May 27, 2021, 8:33am UTC](https://discourse.rom-rb.org/t/commands-on-repos-vs-changesets-on-relations/466/4 "2021-05-27T08:33:16Z")

</div>

@ineu correct 🙂 In 6.0 we may end up having something like this:

```ruby
class ContractRepository < ROM::Repository[:contracts]
  use :changesets, command: :upsert

  def upsert(id, attrs)
    contracts.changeset(:upsert, id, attrs).commit
  end
end

```

This is just a rough idea. There are many common scenarios that can be covered through a plugin that provides pre-configured changeset classes that you won’t have to deal with directly.
