Some generic top level type configuration?


#1

I know you can define the type for each attribute in a one-by-one basis. However, it would be nice to be able to configure something in a layer above all of them. I’m being quite abstract because I’m not sure which would be the best way to accomplish it. What I have in mind is the idea to configure how nil values are managed. For example, I’d like to configure every optional attribute in my app to be returned as the Some/Nothing in dry-monads gem.

What do you think?


#2

This type of functionality could be provided by the schema inferers. Right now there’s no way of configuring them. You could try experimenting with a customized TypeBuilder class which inferrers use. Here’s how its default implementation looks like.

You could subclass it and adjust its logic however you like, and register it for your database type:

class MyTypeBuilder < ROM::SQL::Schema::TypeBuilder
  # go nuts
end

ROM::SQL::Schema::TypeBuilder.register(:postgres, MyTypeBuilder)

Unfortunately, it wasn’t designed with such adjustments in mind, so you will find yourself duplicating code and changing it. ie here we handle columns that allow null and there’s no way of adjusting just that bit. If we wanted to expose this kind of configuration, it would have to be provided through a nice public API, rather than subclassing and adding new methods.

Another option is to use configuration events to hook into schema setup and adjust schemas for all relations. ie here you can see how this is used in rom-sql.

Try to experiment with this and let me know if you have any questions.


#3

Thanks for your feedback @solnic

I’ll try to experiment with this and report anything I think could be useful.