Ecto Patterns logo

Ecto Patterns

Community
bobmatnyc
ecto-patterns

Ecto patterns for Phoenix/Elixir apps. Covers schemas, changesets, migrations, queries, Ecto.Multi, transactions, constraints, associations, pagination, tenant partitioning, performance, and testing.

Overview

Publisherbobmatnyc
Repositoryclaude-mpm-skills
Skill nameecto-patterns
Stars
75
Forks
19
Bundled files
1
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 1 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by bobmatnyc on GitHub. Read the source before you install it.

Installation

Install the Ecto Patterns AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/bobmatnyc/claude-mpm-skills.git /tmp/claude-mpm-skills
mkdir -p .claude/skills
cp -r /tmp/claude-mpm-skills/toolchains/elixir/data/ecto-patterns .claude/skills/ecto-patterns
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Ecto Patterns in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Ecto Patterns on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Ecto Patterns is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Ecto Patterns for Phoenix/Elixir

Ecto is the data layer for Phoenix applications: schemas, changesets, queries, migrations, and transactions. Good Ecto practice keeps domain logic in contexts, enforces constraints in the database, and uses transactions for multi-step workflows.

Schemas and Changesets

elixir
defmodule MyApp.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :email, :string
    field :hashed_password, :string
    field :confirmed_at, :naive_datetime
    has_many :memberships, MyApp.Orgs.Membership
    timestamps()
  end

  def registration_changeset(user, attrs) do
    user
    |> cast(attrs, [:email, :password])
    |> validate_required([:email, :password])
    |> validate_format(:email, ~r/@/)
    |> validate_length(:password, min: 12)
    |> unique_constraint(:email)
    |> hash_password()
  end

  defp hash_password(%{valid?: true} = cs),
    do: put_change(cs, :hashed_password, Argon2.hash_pwd_salt(get_change(cs, :password)))
  defp hash_password(cs), do: cs
end

Guidelines

  • Keep casting/validation in changesets; keep business logic in contexts.
  • Always pair validation with DB constraints (unique_constraint, foreign_key_constraint).
  • Use changeset/2 for updates; avoid mass assigning without casting.

Migrations

elixir
def change do
  create table(:users) do
    add :email, :citext, null: false
    add :hashed_password, :string, null: false
    add :confirmed_at, :naive_datetime
    timestamps()
  end

  create unique_index(:users, [:email])
end

Safe migration tips

  • Prefer additive changes: add columns nullable, backfill, then enforce null: false.
  • For large tables: use concurrently: true for indexes; disable in change and wrap in up/down for Postgres.
  • Data migrations belong in separate modules called from mix ecto.migrate via execute/1 or in distinct scripts; ensure idempotence.
  • Coordinate locks: avoid long transactions; break migrations into small steps.

Queries and Preloads

elixir
import Ecto.Query

def list_users(opts \\ %{}) do
  base =
    from u in MyApp.Accounts.User,
      preload: [:memberships],
      order_by: [desc: u.inserted_at]

  Repo.all(apply_pagination(base, opts))
end

defp apply_pagination(query, %{limit: limit, offset: offset}),
  do: query |> limit(^limit) |> offset(^offset)
defp apply_pagination(query, _), do: query

Patterns

  • Use preload rather than calling Repo in loops; prefer Repo.preload/2 after fetching.
  • Use select to avoid loading large blobs.
  • For concurrency, use Repo.transaction with lock: "FOR UPDATE" in queries that need row-level locks.

Transactions and Ecto.Multi

elixir
alias Ecto.Multi

def onboard_user(attrs) do
  Multi.new()
  |> Multi.insert(:user, User.registration_changeset(%User{}, attrs))
  |> Multi.insert(:org, fn %{user: user} ->
    Org.changeset(%Org{}, %{owner_id: user.id, name: attrs["org_name"]})
  end)
  |> Multi.run(:welcome, fn _repo, %{user: user} ->
    MyApp.Mailer.deliver_welcome(user)
    {:ok, :sent}
  end)
  |> Repo.transaction()
end

Guidelines

  • Prefer Multi.run/3 for side effects that can fail; return {:ok, value} or {:error, reason}.
  • Use Multi.update_all for batch updates; include where guards to prevent unbounded writes.
  • Propagate errors upward; translate them in controllers/LiveViews.

Associations and Constraints

  • Use on_replace: :delete/:nilify to control nested changes.
  • Define foreign_key_constraint/3 and unique_constraint/3 in changesets to surface DB errors cleanly.
  • For many-to-many, prefer join schema (has_many :memberships) instead of automatic many_to_many when you need metadata.

Pagination and Filtering

  • Offset/limit for small datasets; cursor-based for large lists (Scrivener, Flop, Paginator).
  • Normalize filters in contexts; avoid letting controllers build queries directly.
  • Add composite indexes to match filter columns; verify with EXPLAIN ANALYZE.

Multi-Tenancy Patterns

  • Prefix-based: Postgres schemas per tenant (put_source/2 with prefix:) — good isolation, needs per-tenant migrations.
  • Row-based: tenant_id column + row filters — simpler migrations; add partial indexes per tenant when large.
  • Always scope queries by tenant in contexts; consider using policies/guards to enforce.

Performance and Ops

  • Use Repo.stream for large exports; wrap in Repo.transaction.
  • Cache hot reads with ETS/Cachex; invalidate on writes.
  • Watch query counts in LiveView/Channels; preload before rendering to avoid N+1.
  • Telemetry: OpentelemetryEcto exports query timings; add DB connection pool metrics.

Testing

elixir
use MyApp.DataCase, async: true

test "registration changeset validates email" do
  changeset = User.registration_changeset(%User{}, %{email: "bad", password: "secretsecret"})
  refute changeset.valid?
  assert %{email: ["has invalid format"]} = errors_on(changeset)
end
  • DataCase sets up sandboxed DB; keep tests async unless transactions conflict.
  • Use factories/fixtures in test/support to build valid structs quickly.
  • For migrations, add regression tests for constraints (unique/index-backed constraints).

Common Pitfalls

  • Running risky DDL in a single migration step (avoid locks; break apart).
  • Skipping DB constraints and relying only on changesets.
  • Querying associations in loops instead of preloading.
  • Missing transactions for multi-step writes (partial state on failure).
  • Forgetting tenant scoping on read/write in multi-tenant setups.

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Ecto Patterns AI skill do?

Ecto patterns for Phoenix/Elixir apps. Covers schemas, changesets, migrations, queries, Ecto.Multi, transactions, constraints, associations, pagination, tenant partitioning, performance, and testing.

Why use Ecto Patterns on TypingMind?

Because you install it once and use it with any model. Ecto Patterns is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Ecto Patterns in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/bobmatnyc/claude-mpm-skills/tree/main/toolchains/elixir/data/ecto-patterns. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Ecto Patterns?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Ecto Patterns?

As many as you like. As long as a model supports skills, you can use Ecto Patterns with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Ecto Patterns AI skill free?

Yes. It is published on GitHub by bobmatnyc under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇