Archeometer.Schema (Archeometer v0.5.0)

Specify data base table information in Elixir modules. The information must be specified inside a defschema/1 macro.

An schema has field properties. Foreign keys are specied with a belongs_to property. And the opposite is spcified with a has property.

example

Example

defmodule ModuleA do
  use Archeometer.Schema

  defschema do
    field :first_module, primary_key: true
    belongs_to ModuleB
    has ModuleB, as: :other_b, key: :recurse_id
  end
end

Link to this section Summary

Functions

This property marks a foreign key. This is usually to specify the many entity in a many-to-one relation.

Starts the definition of a new schema. This definition maps database columns into Elixir data. The field, has and belongs_to macros are available to specify how to map the data base table to Elixir terms.

Specify that the current schema has fields. The first argument is the name of the field, and as an optional argument you can specify if the current field is a primary key.

This property denotes that there is a foreign key in other table that makes reference to the current table. This is usually to specify the one entity in a many-to-one relation.

Link to this section Functions

Link to this macro

belongs_to(module, opts \\ [])

(macro)

This property marks a foreign key. This is usually to specify the many entity in a many-to-one relation.

Using the name of the reference table, a field will be generated. This generated name will be a queryable field in the schema. The name can be specified with the as: name optional parameter.

The name of the foreign key will be inferred from the name of the referenced table. This can be overriden with the key: key_name optional paramenter.

For example

defmodule B do
  use Archeometer.Schema

  defschema :b do
    field :id, primary_key: true
    belongs_to A # with inferred name `:a` and key `:a_id`
    belongs_to A, as: :other_a, key: :other_a_id
  end
end

Where A is defined as follows

defmodule A do
  use Archeometer.Schema

  defschema :schema do
    field :id, primary_key: true
    has B # with infererd name `:b` and foreign key `:a_id`
    has B, as: :other_b, key: other_a
  end
end
Link to this macro

defschema(name, list)

(macro)

Starts the definition of a new schema. This definition maps database columns into Elixir data. The field, has and belongs_to macros are available to specify how to map the data base table to Elixir terms.

See their individual documentation of each for the list of properties they can specify.

Link to this macro

field(name, opts \\ [])

(macro)

Specify that the current schema has fields. The first argument is the name of the field, and as an optional argument you can specify if the current field is a primary key.

For example

defmodule A do
  use Archeometer.Schema

  defschema :a do
    field :id, primary_key: true
    field :name
  end
end
Link to this macro

has(module, opts \\ [])

(macro)

This property denotes that there is a foreign key in other table that makes reference to the current table. This is usually to specify the one entity in a many-to-one relation.

Using the name of the reference table, a field will be generated. This generated name will be a queryable field in the schema. The name can be specified with the as: name optional parameter.

The name of the foreign key in the other table will be inferred from the name of the current table. This can be overriden with the key: key_name optional paramenter.

For example

defmodule A do
  use Archeometer.Schema

  defschema :schema do
    field :id, primary_key: true
    has B # with infererd name `:b` and foreign key `:a_id`
    has B, as: :other_b, key: other_a
  end
end

Where B is defined as follows

defmodule B do
  use Archeometer.Schema

  defschema :b do
    field :id, primary_key: true
    belongs_to A # with inferred name `:a` and key `:a_id`
    belongs_to A, as: :other_a, key: :other_a_id
  end
end