View Source Needle.Mixin (needle v0.8.0)
If a Pointer represents an object, mixins represent data about the object. Mixins collate optional additional information about an object. Different types of object will typically make use of different mixins. You can see these as aspects of the data if you like.
A mixin table starts with an id
column which references Pointer
and forms the default primary
key. It is up to the user to choose which other fields go in the table, and thus what the mixin is for.
Use of a mixin is typically through has_one
:
has_one :my_mixin, MyMixin, foreign_key: :id, references: :id
Sometimes, the user may wish to add fields to the primary key by using the primary_key: true
option to add
in their migrations. This is permitted and in such case we call the resulting
mixin a multimixin
. Use becomes has_many
:
has_many :my_mixin, MyMixin, foreign_key: :id, references: :id
Thus the choice of single or multi comes down to how many times you want to store that data for
the object. A user's profile naturally lends itself to a regular single
mixin, whereas an
object's appearance in a feed would naturally lend itself to being a multimixin since the object
may appear in many feeds.
Declaring a mixin table type
defmodule My.Mixin do
use Needle.Mixin,
otp_app: :my_app,
source: "postgres_table_name"
mixin_schema do
field :is_awesome, :boolean
end
end