Difference between repositories and relations

#1

Hey!

I’ve started using ROM on a little side project, and I’m a little bit confused by all the concepts involved: containers, repositories, relations, structs… I’m starting to feel that plain Sequel is enough for my use case where 10 tables won’t turn into 40 Ruby classes. :slight_smile:

I’d like to understand better, when I’m supposed to use a repository to query a database, and when is it better to use a relation?

Thanks!

#2

A good start point for understanding the difference is the brief concept definition found here https://rom-rb.org/5.0/learn/getting-started/core-concepts/#repositories

The way I use them is that I will have multiple repositories for the same relation in different parts of my domain that will fetch / save that relation as needed for the specific business flow.

The relation will contain all the schema definition and some of the common queries between all my business flows.

#3

I want also to add that the idea of repository is essential, everything else rom can infer for you automatically, including relations, mappers, and structs. Repositories is where you map your domain entities to adapter-specific objects back and forth, at this point this cannot be done automatically in general but we have streamlined API for 1-to-1 cases.

#4

Thanks for your replies. It helps getting some advice here, because the docs are almost all “this is automatic but you can also do it by hand”. I feel that there is not much guidance as to how all the concepts would click together, since every code example (for repositories or relations), show how you use those to make db queries… so I’m like, er… why do I need both then? :slight_smile: