You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my application, I have different attributes format between schema (legacy tables) and my domain models. So I need to use mappers to both save and restore an entity.
While it's easy to use mappers for Relation, I haven't found a way to use for Command. So, I use command's input for this purpose.
It works great, until I want to set association for my relation. schema(infer: true) breaks everything.
After some investigation, I've found that inferred schema overrides my custom input for any commands.
So, without schema(infer: true), I get custom mapper as input
From: /Users/kukunin/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rom-sql-0.8.0/lib/rom/sql/commands/create.rb @ line 24 ROM::SQL::Commands::Create#execute:
22: def execute(tuples)
23: insert_tuples = with_input_tuples(tuples) do |tuple|
=> 24: attributes = input[tuple]
25: validator.call(attributes)
26: attributes.to_h
27: end
28:
29: if insert_tuples.length > 1
30: multi_insert(insert_tuples)
31: else
32: insert(insert_tuples)
33: end
34: end
[6] pry(#<Persistence::Commands::Hoi::CreateCaseFromModel>)> input
=> #<Method: Persistence::Mappers::Hoi::CasesTuple#to_tuple>
But with schema(infer: true), it is:
[5] pry(#<Persistence::Commands::Hoi::CreateCaseFromModel>)> input
=> #<Dry::Types::Constructor type=#<Dry::Types::Hash::Schema primitive=Hash options={:member_types=>{:id=>#<Dry::Types::Constrained type=#<Dry::Types::Definition primitive=Integer options={}> options={:rule=>#<Dry::Logic::Operations::And rules=[#<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#type?> options={:args=>[Integer]}>, #<Dry::Logic::Rule::Predicate predicate=#<Method: Module(Dry::Logic::Predicates::Methods)#gt?> options={:args=>[0]}>] options={}>, :meta=>{:primary_key=>true, :name=>:id}}.....A LOT OF TEXT
So, because It doesn't map my domain entity, it crashes.
We can tweak commands in a way that if there's an input handler configured which is not the default proc, then we'll compose it with the schema handler. Would that work?
This is possible to override Command.build method in this way
classCommands::CreateCaseFromModel < ROM::Command::Create[:sql]relation:hoi_casesregister_as:create_from_modeldefself.build(relation,options={})# pass any input you want, you can access schema's input via relation.schema_hashsuper(relation,options.merge(input: ...))endend
In my application, I have different attributes format between schema (legacy tables) and my domain models. So I need to use mappers to both save and restore an entity.
While it's easy to use mappers for Relation, I haven't found a way to use for Command. So, I use command's
input
for this purpose.It works great, until I want to set association for my relation.
schema(infer: true)
breaks everything.After some investigation, I've found that inferred schema overrides my custom input for any commands.
So, without
schema(infer: true)
, I get custom mapper asinput
But with
schema(infer: true)
, it is:So, because It doesn't map my domain entity, it crashes.
and command with custom mapper:
and just for clarity, my mapper code:
P.S. If you know a better way to map entity to underlying schema in 2-directions more easily - tell me, please.
Thanks!
The text was updated successfully, but these errors were encountered: