Hello ROM Community,
I’m currently exploring ROM for database interactions and have encountered an issue with mapping database results to my custom entity, specifically regarding a custom enum type.
Repository Setup: I have a Users::Repository
set up as follows:
# frozen_string_literal: true
require 'rom-repository'
module Users
class Repository < ROM::Repository[:users]
commands :create, update: :by_pk, delete: :by_pk
struct_namespace Users::Entities
auto_struct true
end
end
I understood that struct_namespace Users::Entities
would automatically map the database results to objects within the specified namespace.
Entity Definition: Here’s the definition of my User
entity:
# frozen_string_literal: true
module Users
module Entities
class User < Users::Entity
attribute :name, Types::String
attribute :password, Types::String
attribute :email, Types::String
attribute :role, Types::Role
end
end
end
Custom Type Definition: The Types::Role
is a custom enum type defined as:
module Types
MEMBER = 'member'
ADMIN = 'admin'
Role = Types::String.default(MEMBER).enum(MEMBER => 0, ADMIN => 1).freeze
end
Issue: When fetching a user from the repository with user_repo.users.by_pk(6).one
, the role
attribute doesn’t map to its string representation (‘member’ or ‘admin’) but remains as an integer:
#<Users::Entities::User name="Name" password="hashed_pwd" email="email@test.com" role=0 id=6>
However, when explicitly mapping to Users::Entities::User
using user_repo.users.by_pk(6).map_to(Users::Entities::User).one
, the mapping works correctly:
#<Users::Entities::User name="Name" password="hashed_pwd" email="email@test.com" role='member' id=6>
Question: Why doesn’t the automatic mapping via struct_namespace
correctly apply my custom enum type? Is there a configuration step I’m missing, or is this an expected behavior requiring explicit mapping?
Any insights or suggestions on how to resolve this would be greatly appreciated.
Thank you!