View Source Vtc.Ecto.Postgres.PgFramerate (vtc v0.17.5)

Defines a composite type for storing rational values as a PgRational + list of tags. These values are cast to Framerate structs for use in application code.

The composite types are defined as follows:

CREATE TYPE framerate_tags AS ENUM (
  "drop",
  "non_drop"
)
CREATE TYPE framerate as (
  playback rational,
  tags framerate_tags[]
)

framerate_tags is designed as such to guarantee forwards-compatibility with future support for features like interlaced timecode.

Framerate values can be cast in SQL expressions like so:

SELECT ((24000, 1001), '{non_drop}')::framerate

framerate-tags

Framerate tags

The following values are valid tags:

  • drop: Indicates NTSC, drop-frame timecode
  • non_drop: Indicated NTSC, non-drop timecode

field-migrations

Field migrations

You can create framerate fields during a migration like so:

alias Vtc.Framerate

create table("rationals") do
  add(:a, Framerate.type())
  add(:b, Framerate.type())
end

Framerate re-exports the Ecto.Type implementation of this module, and can be used any place this module would be used.

schema-fields

Schema fields

Then in your schema module:

defmodule MyApp.Framerates do
@moduledoc false
use Ecto.Schema

alias Vtc.Framerate

@type t() :: %__MODULE__{
        a: Framerate.t(),
        b: Framerate.t()
      }

schema "rationals_01" do
  field(:a, Framerate)
  field(:b, Framerate)
end

changesets

Changesets

With the above setup, changesets should just work:

def changeset(schema, attrs) do
  schema
  |> Changeset.cast(attrs, [:a, :b])
  |> Changeset.validate_required([:a, :b])
end

Framerate values can be cast from the following values in changesets:

  • Framerate structs.

  • Maps with the following format:

    {
      "playback": [24000, 1001],
      "ntsc": "non_drop"
    }

    Where playback is a value supported by PgRational casting and ntsc can be null, "drop" or "non_drop".

fragments

Fragments

Framerate values must be explicitly cast using type/2:

framerate = Rates.f23_98()
query = Query.from(f in fragment("SELECT ? as r", type(^framerate, Framerate)), select: f.r)

Link to this section Summary

Types

Type of the raw composite value that will be sent to / received from the database.

Ecto Migrations

The database type for PgFramerate.

Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.

Link to this section Types

@type db_record() :: {Vtc.Ecto.Postgres.PgRational.db_record(), [String.t()]}

Type of the raw composite value that will be sent to / received from the database.

Link to this section Ecto Migrations

@spec type() :: atom()

The database type for PgFramerate.

Can be used in migrations as the fields type.

Link to this section Functions

Callback implementation for Ecto.Type.embed_as/1.

Callback implementation for Ecto.Type.equal?/2.