Skip to content

Potential integration with Ecto? #7

@cblage

Description

@cblage

I think it would be great if we could use the typedstruct macro in conjunction with Ecto. Right now Ecto does not create @type t for the struct, but it defines other struct-related things which collde with typedstruct.

So maybe add a typedschema macro to integrate with Ecto? Right now, one declares Ecto schemas like this:

defmodule Aruki.Database.Schema.User do
  use Ecto.Schema

  @primary_key {:id, :id, autogenerate: true}

  @timestamps_opts [
    inserted_at: "creation_date",
    updated_at: "status_date",
    type: :utc_datetime,
    usec: false
  ]

  schema "t_user" do
    field :id_type,       :integer
    field :id_status,     :integer
    field :creation_date, :utc_datetime
    field :status_date,   :utc_datetime
    field :login_date,    :utc_datetime
    field :email,         :string
    field :first_name,    :string
    field :last_name,     :string
    field :telephone_nr,  :string
    field :fiscal_id,     :string
    field :reference,     :string
    field :mobileos_id,   :integer
    field :birth_day,     :integer
    field :birth_month,   :integer
    field :birth_year,    :integer
    field :national_id,   :string
    field :country,       :string
    field :rating,        :integer
    field :version,       :integer
  end
end

I suggest supporting something like:

defmodule Aruki.Database.Schema.User do
  use TypedStruct.EctoSchema

  @primary_key {:id, :id, autogenerate: true}

  @timestamps_opts [
    inserted_at: "creation_date",
    updated_at: "status_date",
    type: :utc_datetime,
    usec: false
  ]

  typedschema "t_user" do
    field :id_type,       :integer
    field :id_status,     :integer
    field :creation_date, :utc_datetime
    field :status_date,   :utc_datetime
    field :login_date,    :utc_datetime
    field :email,         :string
    field :first_name,    :string
    field :last_name,     :string
    field :telephone_nr,  :string
    field :fiscal_id,     :string
    field :reference,     :string
    field :mobileos_id,   :integer
    field :birth_day,     :integer
    field :birth_month,   :integer
    field :birth_year,    :integer
    field :national_id,   :string
    field :country,       :string
    field :rating,        :integer
    field :version,       :integer
  end
end

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions