Command output as model class?


I have just started a new project and I decided to try and use a combination of grape, dry-rb and rom-rb instead of a rails stack.

So far things are going slow but okay. I have however finally hit an issue that I would like some advice on.

I am using rom-sql and rom-repository with a postgres backend.

The methods I have put into my repos return models rather than raw ROM::Structs.


def by_id(id)

This works fine.

My question is what invocation do I need to perform to get the create command to return a model type rather than a ROM::Struct?

I am using ‘commands’ in the repo and creation works, I simply do not understand how to (easily) tell it to return the output as a model type of my choosing.



You can do this:


This should work :slight_smile:

Thanks, I will try this out.

I guess I have a really bad mental model of what a ‘command’ is currently.

I guess I have a really bad mental model of what a ‘command’ is currently.

Commands are objects that make changes in the db, they are tuned for specific db types and can provide custom features depending on what a given db supports. Repositories can create commands for you, which is done through Repository#command method. Furthermore, repository changesets use commands too.

The difference between using a command vs changeset is that base commands are simpler than changesets. ie changesets can transform data, update changesets check the diff between original tuple and new data, and also we have conventional usage in update/delete changesets as they restrict relations by the PK.

Commands should be considered as a more advanced API. Even though the built-in base commands (create/update/delete) are fairly simple, you can also have more advanced types, like PG’s Upsert. You can also define your own command classes, and use them directly in repos or with custom changesets.

1 Like