Fleet.Schema (fleet v0.1.0)

Copy Markdown View Source

Defines an embedded Ecto schema optimized for Fleet job arguments.

Provides strong typing, automatic runtime casting, and enqueue-time validations using standard Ecto changesets.

Features

  • Automatically configures use Ecto.Schema with @primary_key false.
  • Imports Ecto.Changeset for building validations.
  • Generates a default, overridable changeset/2 that casts all schema fields.
  • Provides cast_args/1 and cast_args!/1 helpers for validation and coercion.
  • Automatically serializes and hydrates typed structs between PostgreSQL and Broadway.

Example (Inline inside a Worker)

defmodule MyApp.Workers.SendInvoice do
  use Fleet.Worker, queue: :billing
  use Fleet.Schema

  embedded_schema do
    field :invoice_id, :integer
    field :amount, :decimal
    field :currency, :string, default: "USD"
  end

  def changeset(schema, attrs) do
    schema
    |> cast(attrs, [:invoice_id, :amount, :currency])
    |> validate_required([:invoice_id, :amount])
  end

  @impl Fleet.Worker
  def perform(%Fleet.Job{args: %__MODULE__{invoice_id: id, amount: amount, currency: curr}}) do
    # `amount` is Decimal, `id` is integer, `curr` defaults to "USD"
    MyApp.Billing.charge(id, amount, curr)
  end
end

Example (Standalone Schema)

defmodule MyApp.Schemas.EmailPayload do
  use Fleet.Schema

  embedded_schema do
    field :to, :string
    field :subject, :string
  end
end

defmodule MyApp.Workers.SendEmail do
  use Fleet.Worker, queue: :mailers, schema: MyApp.Schemas.EmailPayload

  @impl Fleet.Worker
  def perform(%Fleet.Job{args: %MyApp.Schemas.EmailPayload{to: to, subject: sub}}) do
    MyApp.Mailer.send(to, sub)
  end
end

Summary

Functions

Returns true if the given module is an Ecto schema or uses Fleet.Schema.

Functions

schema?(module)

Returns true if the given module is an Ecto schema or uses Fleet.Schema.