Przejdź do treści
Biblioteka skilli
Programowanie · Budowanie i kod

estack-ecto

/estack-ecto

Implementuje dostęp do danych w Ecto: schematy, zapytania, changesety, transakcje i bezpieczne migracje, w tym konteksty ograniczone do tenanta.

Repozytorium
estack ↗
Licencja
MIT
Ostatnia zmiana repozytorium
11.10.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
3
Przestarzałość

Brak sygnałów. Repozytorium zmienione 11.10.2026, bez odwołań do wycofanych modeli i API.

Licencja

MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.

Co robi i kiedy

Implementuje dostęp do danych w Ecto: schematy, zapytania, changesety, transakcje i bezpieczne migracje, w tym konteksty ograniczone do tenanta.

Oryginalny opis (EN)

Implement Ecto data access, ownership, changesets, transactions, and safe migrations. Use when editing schemas, queries, tenant-scoped contexts, or database writes.

Jak działa

  1. Ownership and action authorization
  2. Scope every operation
  3. Data integrity
  4. Transactions and external effects
  5. Migrations
  6. References
Sekcje z nagłówków SKILL.md, w oryginale · 78 linii, 611 słów
Podgląd SKILL.md
---
name: estack-ecto
description: Implement Ecto data access, ownership, changesets, transactions, and safe migrations. Use when editing schemas, queries, tenant-scoped contexts, or database writes.
---

# Ecto patterns

Read the project's Repo, auth scope, schemas, database adapter, and installed Ecto version. If the app uses Ash resources, use its actions and policies rather than bypassing them with Repo calls.

## Ownership and action authorization

Accept the existing authenticated scope at the context boundary. Cast only client-editable fields; set ownership from trusted identity. The following is an illustrative context fragment; `Scope`, `Report`, `Policy`, and `Repo` are application modules:

```elixir
def create_report(%Scope{user: user} = scope, attrs) do
  with :ok <- Policy.authorize(scope, :create_report) do
    %Report{user_id: user.id}
    |> Report.create_changeset(attrs)
    |> Repo.insert()
  end
end
```

In `create_changeset`, omit `:user_id`, `:tenant_id`, roles, and other protected fields from `cast`. Do not atomize arbitrary client keys. Use action-specific changesets so update input cannot change ownership. Keep caller parameters consistently string-keyed or atom-keyed as required by Ecto; do not mix them while stamping ownership.

## Scope every operation

```elixir
# Illustrative owner-only lookup using the project's Scope struct.
def get_report(%Scope{user: %{id: user_id}}, id) do
  case Repo.one(from r in Report, where: r.user_id == ^user_id and r.id == ^id) do
    nil -> {:error, :not_found}
    report -> {:ok, report}
  end
end
```

Scope reads, updates, deletes, aggregates, preloads, and background work. For organizations, scope to authorized membership and the selected tenant; a user ID alone is not a tenancy model. Avoid blanket admin bypasses unless the product's policy explicitly grants that access.

Authorize the requested action as well as resource visibility. Use scoped lookups on writes, and consider constraints or locking when authorization or state may change concurrently. Return `:not_found` for resources whose existence must be hidden; preserve deliberate `:forbidden` semantics for visible resources with disallowed actions.

Test with two users in different tenants, plus same-tenant users with different permissions. Test mutations as well as lists.

## Data integrity

- Use database constraints for uniqueness and references, with corresponding changeset constraints for useful errors. Validation alone cannot prevent races.
- Use integers in documented minor units or Decimal for money; preserve currency and rounding rules. Introduce custom Ecto types when they enforce a useful domain invariant.
- Validate addresses and other domain identifiers explicitly. A custom type is an option, not a requirement for every string field.
- Use UTC timestamps for instants, preserving the project's precision (`:utc_datetime` or `:utc_datetime_usec`). NaiveDateTime has no timezone; its correctness depends on the application's explicit convention.
- Bound pagination and filter inputs. Preload what the caller needs and inspect N+1 queries.

## Transactions and external effects

Group dependent writes in a transaction. This illustrative fragment assumes Oban uses the same Repo/database:

```elixir
Ecto.Multi.new()
|> Ecto.Multi.insert(:order, order_changeset)
|> Oban.insert(:sync_job, fn %{order: order} ->
  SyncJob.new(%{order_id: order.id})
end)
|> Repo.transaction()
```

Match the Multi result, including `{:error, step, reason, changes_so_far}`. Test that failed steps roll back both the domain write and queued job. Prefer the transaction API supported by the locked Ecto version; `Repo.transaction/1` is shown for compatibility.

Do not make an irreversible vendor request inside a database transaction and assume rollback undoes it. Persist intent/outbox work and make external effects idempotent. Choose optimistic or pessimistic locking based on the contested invariant.

## Migrations

Generate migrations with `mix ecto.gen.migration`. Never edit an already-applied migration as a shortcut. Distinguish writing/reviewing a migration from executing it against production; use the authorization provided for the task.

Use expand/backfill/contract for rolling deployments. Consider table locks, concurrent indexes, database support, nullable transitions, and old application versions. Run the project's migration checker if installed. Scope and justify exceptions using the installed checker's syntax; a suppression comment does not make a change safe.

Keep seeds idempotent. Separate optional development sample data from production bootstrap operations and never require vendor credentials for ordinary tests.

## References

- [Ecto.Multi](https://hexdocs.pm/ecto/Ecto.Multi.html)
- [Ecto.Changeset](https://hexdocs.pm/ecto/Ecto.Changeset.html)
- [Phoenix scopes](https://hexdocs.pm/phoenix/scopes.html)
Pliki w paczce (2)
  • SKILL.md 5 KB
  • agents/openai.yaml 1 KB

Wersje

W bazie nie ma innych skilli o nazwie estack-ecto.

Migawka bazy z 13.11.2026 · wersja paczki 85e933a31d02 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100ecto
    Prowadzi przez schematy Ecto, changesety, zapytania, migracje i Ecto.Multi oraz pomaga wybrać między DSL zapytań Ecto a surowym SQL.
  • 100ecto
    Pomaga projektować i debugować warstwę danych w Elixirze z Ecto: schematy, changesety, zapytania, preloady, transakcje, migracje, multi-tenancy oraz wydajność i pulę połączeń.
  • 100elixir-ecto-patterns
    Opisuje wzorce Ecto w Elixirze: schematy, changesety, zapytania i transakcje, przy budowie aplikacji opartych na bazie danych.
  • 100ecto-patterns
    Podpowiada wzorce pracy z Ecto: schematy, changesety, walidacje, zapytania, preloady i Multi, ładowane przed pisaniem kodu Ecto.
  • 100ecto-patterns
    Podpowiada wzorce Ecto: schematy, changesety, walidacje, zapytania, preloady, Multi, migracje, ograniczenia i pola pieniężne, przed pisaniem kodu Ecto, z wyjątkiem projektów na Ash.
  • 100ecto-thinking
    Pomaga dodawać tabele, konteksty i pola schematów w Ecto, pisać zapytania, walidować dane formularzy, naprawiać N+1 oraz układać komunikację między kontekstami.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.