View Source Vtc.Ecto.Postgres.PgTimecode (vtc v0.10.9)

Defines a composite type for storing rational values as a PgRational real-world playbck seconds, PgFramerate playback rate pair.

These values are cast to Timecode structs for use in application code.

The composite types is defined as follows:

CREATE TYPE timecode as (
  seconds rational,
  rate framerate
)
SELECT ((10, 1), ((24, 1), '{non_drop}'))::timecode

field-migrations

Field migrations

You can create framerate fields during a migration like so:

alias Vtc.Timecode

create table("events") do
  add(:in, Timecode.type())
  add(:out, Timecode.type())
end

Timecode 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.Event do
@moduledoc false
use Ecto.Schema

alias Vtc.Timecode

@type t() :: %__MODULE__{
        in: Timecode.t(),
        out: Timecode.t()
      }

schema "events" do
  field(:in, Timecode)
  field(:out, Timecode)
end

changesets

Changesets

With the above setup, changesets should just work:

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

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

  • Timecode structs.

  • Maps with the following format:

    {
      "smpte_timecode": "01:00:00:00",
      "rate": {
        "playback": [24000, 1001],
        "ntsc": "non_drop"
      }
    }

    Where smpte_timecode is properly formatted SMPTE timecode string and playback is a map value supported by PgFramerate changeset casts.

fragments

Fragments

Timecode values must be explicitly cast using type/2:

timecode = Timecode.with_frames!("01:00:00:00", Rates.f23_98())
query = Query.from(f in fragment("SELECT ? as r", type(^timecode, Timecode)), select: f.r)

Link to this section Summary

Types

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

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 of the raw composite value that will be sent to / received from the database.

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.