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

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 iare 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:

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

schema-fields

Schema fields

Then in your schema module:

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

alias Vtc.Ecto.Postgres.PgFramerate
alias Vtc.Framerate

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

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

... notice that the schema field type is PgFramerate, but the type-spec field uses Framerate.t(), the type that our DB fields will be deserialized into.

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:

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

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

Link to this section Summary

Types

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

Queries

Serialize Framerate for use in a query fragment.

Functions

Callback implementation for Ecto.Type.embed_as/1.

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

The database type for PgFramerate.

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 Queries

@spec dump!(Vtc.Framerate.t()) :: db_record()

Serialize Framerate for use in a query fragment.

The fragment must explicitly cast the value to a ::framerate type.

examples

Examples

alias Ecto.Query
require Ecto.Query

alias Vtc.Rates
alias Vtc.Ecto.Postgres.PgFramerate

framerate = Rates.f23_98()
framerate_composite = PgFramerate.dump!(framerate)

Query.from(f in fragment("SELECT ?::framerate as r", ^framerate_composite), select: f.r)

Link to this section Functions

Callback implementation for Ecto.Type.embed_as/1.

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

The database type for PgFramerate.

Can be used in migrations as the fields type.