Changeset pre-configured mapping

Greetings,
Is it possible to use a pre-configured mapping (used for commands) in the change set pipe?

class MyMapper < ROM::Transformer
  relation :my_relation
  register_as :my_relation_mapper

  map do

OR do i need to duplicate the mapping logic in Changeset.map (class method):

class NewUserChangeset < ROM::Changeset::Create
  map do
    <copy duplicate code from my_mapper>

What is your suggestion for this case?

Many Thanks!

There’s no public API for that yet, but for now you could just store a proc somewhere and reuse it like map(&YOUR_MAPPING_PROC) in both places.

Thanks for your reply! I now realize that the relation mappers are mappings going “out” (db to entity) and changeset mappers are going “in” (entity to db) as described here (ROM - Core Concepts). What I’m really trying to articulate is… Is it possible for a changeset commit to return an application domain entity rather than a hash or array (Class: ROM::Changeset — Documentation by YARD 0.9.24)?

Changesets use relation’s mapper. So, if you do users.map_with(:foo).changeset(:create, ...).commit it should use the entity mapper when reading data back.

1 Like

This suggestion does not seem to be working.

relation.map_with(:some_mapper).changeset(CreateSomeChangeset, data).map(:add_timestamps).commit

So the both the CreateSomeChangeset mapping and the add_timestamps map appear to be working. The map_with appears to be ignored as the return value is not reflecting changes in some_mapper.

Ah right, it only works with the auto-mapper. I guess we could make it work with custom mappers too, because now it’s a confusing behavior.