SimpleSchemaContext.Schema (simple_schema_context v0.1.0)

View Source

Core module that provides a set of standard context functions for Phoenix schemas.

When used, it automatically imports all the necessary Ecto modules and defines a complete set of CRUD operations for the schema:

  • list/0 and list/2 - List all records with optional filtering
  • list_by_ids/2 - Fetch records by IDs
  • fetch/2 - Get a single record with error handling
  • get!/2 - Get a single record or raise if not found
  • create/2 - Create a new record
  • update/3 - Update an existing record
  • change/3 - Create a changeset for a record
  • delete/2 - Delete a record

## Options

When using this module, you must provide the following options:

  • :repo - The Ecto.Repo module to use for database operations

## Example

  defmodule MyApp.Blog.Post do
    use SimpleSchemaContext.Schema, repo: MyApp.Repo

    schema "posts" do
      field :title, :string
      field :body, :text

      timestamps()
    end

    def changeset(post, attrs) do
      post
      |> cast(attrs, [:title, :body])
      |> validate_required([:title, :body])
    end
  end