I am confused in finding the best approach to my problem.
I have a table called accounts
which is pretty much the Generalization table. Many of the fields values actually depends of the specialized record like type
which is an ENUM but based on the specialized record, the same with status.
defmodule CreateAccounts do
use Ecto.Migration
def change do
create table(:accounts) do
add(:username, :string, null: false)
add(:encrypted_password, :string, null: false)
add(:uid, :string, null: false, size: 36)
add(:type, :string, null: false, size: 25) # it will depends of the specialization
add(:status, :string, null: false, size: 25) # it will depends of the specialization
add(:data, :map)
timestamps()
end
create(index(:accounts, [:username], unique: true))
create(index(:accounts, [:uid], unique: true))
end
end
So basically now I will have a bunch of specialized schemas like Costumer
, Admin
, Company
or whatever I want, specially that data
is just a :map
so I can put whatever I want inside as JSON, so I dont have to deal with table columns, anyway.
So far I am good but I am confused in how should I manage my workflows and the changesets. I want to be able to say Customer.register_costumer(attrs)
and that will validate the data according to the Customer specialization
but I don’t know if I should replicate the schema
on my Customer module
and just put on Account module
common functions around the generalization. Should I use embedded_schema
or just schema
on my specialization.
I am struggling because if I duplicate the schema definition then I will have to add all the fields again and stuff like that but if I am not using it then I need to transform at some point Customer
to Account
changeset I guess?
Sorry for the confusion, but I can’t explain what I dont understand. So I would love to know your feedbacks in how do you handle Generalization & Specialization and your workflows + changesets.