Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

[WIP] relation mapping support #60

Merged
merged 16 commits into from
Jul 31, 2013
Merged

[WIP] relation mapping support #60

merged 16 commits into from
Jul 31, 2013

Conversation

solnic
Copy link
Owner

@solnic solnic commented Jul 31, 2013

This will add support for mapped relations. I'm still unsure where the DSL code should be located - here, in rom-mapper or maybe in rom.

The way I imagine it to work is that after you loaded the schema in the env you can call Mapping.build(env) {} to define how relations are mapped, you get a hash of mapped rom relations back. I think it has to support defining those mappings separately in multiple files, kinda like defining DM models in multiple files, because that will be a very common use case.

This leads to a missing object because right now Environment already has a registry of relations from all repositories, but those are axiom ones, not rom mapped relations. Which makes me think we need a separate Registry that will hold rom relations created via Mapping.build. You could create the registry yourself and pass it to the builder method as many times you want, this way we could have mappings defined in multiple files.

Please note that naming is a total WIP as well as implementation. Let me know if you think Mapping or Registry aren't good names.

Currently I made the DSL to look like this:

# this is what we already have
schema = Schema.build do
  base_relation :users do
    repository :test

    attribute :id, Integer
    attribute :user_name, string

    key :id
  end
end

env = Environment.coerce(test: 'memory://test').load_schema(schema)

# this is the new thing
registry = Mapping.build(env) do
  users do
    model User

    map :id
    map :user_name, to: :name
  end
end

^ this will build a rom relation and inject a rom mapper configured based on the mapping definition. I will also add a way to just provide your mapper instance or specify which loading strategies you want to use (we have 3 right now).

In general my motivation behind the DSL is to have it as slick as possible. I also want to add convenient methods to Environment so that you can define schema+mapping as quickly as possible, otherwise that will be a bit discouraging because in order to load a model from a db you need to 1) configure env 2) set up schema 3) load schema into env 4) define mappings which is obviously a great separation and will be used like that in, for example, rails integration but in simple cases when you just want to get up and running quickly it will be an overkill (for instance think a dead-simple-one-file-sinatra-app).

Ugh, that was a bit too long, sorry about that but please let me know your thoughts :)

refs #59

This is just a handy builder method that returns a rom relation that is mapped
by the mapper using `Mapper#call(relation)` interface.

Under the hood the mapper can do *whatever* it's needed to prepare the relation
to be used. It can rename the attributes, perform projection etc.
@snusnu
Copy link
Contributor

snusnu commented Jul 31, 2013

@solnic this looks like a good start to me. i do agree that probably both Schema and Mapping should be able to get mutable registry classes injected, so that for both DSLs definition can be spread across multiple files.

Mapper::Header.build(project_header, map: mapping)
end

def mapper(mapper = Undefined)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should extract the options stuff from axiom-types. I took what was in virtus originally, rewrote it and mutation covered it, and it should be ready to be gemified. Then we can use it here and in virtus, as well as elsewhere.

solnic added a commit that referenced this pull request Jul 31, 2013
@solnic solnic merged commit 523e824 into master Jul 31, 2013
@solnic solnic deleted the relation-mapping-support branch July 31, 2013 19:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants