### Posts.change - Create a changeset Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This example demonstrates the usage of the generated `change/1` function from EctoCooler, which creates a new Ecto changeset for a given schema with provided attributes. ```elixir iex> Posts.change(%{title: "Example Post"}) #Ecto.Changeset< action: nil, changes: %{title: "Example Post"}, errors: [], data: #Post<>, valid?: true > ``` -------------------------------- ### Install EctoCooler Dependency Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Adds the EctoCooler package to your Elixir project's dependencies in the mix.exs file. This allows you to use the library's functionality in your application. ```Elixir def deps do [ # Use the latest 2.x release {:ecto_cooler, "~> 2.0"} ] end ``` -------------------------------- ### Resource.all - Fetch all entries with options Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This example shows how to use the generated `all/1` function from EctoCooler to fetch all entries of a schema. It demonstrates using options like `preloads`, `order_by`, and `where` to filter and sort the results. ```elixir iex> Posts.all() [%Post{id: 1}] ``` ```elixir iex> Posts.all(preloads: [:address]) [%Post{id: 1, comment: %Comment{}}] ``` ```elixir iex> Posts.all(order_by: [desc: :id]) [%Post{id: 2}, %Post{id: 1}] ``` ```elixir iex> Posts.all(preloads: [:comment], order_by: [desc: :id])) [ %Post{ id: 2, comment: %Comment{} }, %Post{ id: 1, comment: %Comment{} } ] ``` ```elixir iex> Posts.all(where: [category: "Testing"]) [ %Post{ id: 42, category: "Testing" }, %Post{ id: 99, category: "Testing" } ] ``` -------------------------------- ### Generate namespaced EctoCooler functions Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This example shows how to generate EctoCooler functions with a suffix, effectively namespacing them. This is useful for avoiding naming conflicts when working with multiple resources or schemas. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, suffix: true) end end ``` -------------------------------- ### Get Post by Attributes! (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Fetches a single Post based on matching attribute values. Returns the Post struct if found, or raises an `Ecto.NoResultsError` if no matching record exists. ```elixir iex> Posts.get_by!(%{title: "Example Title"}) %Post{title: "Example Title"} iex> Posts.get_by!(%{title: "Doesn't Exist"}) ** (Ecto.NoResultsError) ``` -------------------------------- ### Full EctoCooler Configuration Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Provides a comprehensive configuration for EctoCooler, including generator options, migration directory, repository details, and schema namespaces. This setup is necessary for advanced generator usage and project structure. ```Elixir config :ecto_cooler, app_name: "MyApp", app_slug: :my_app, generators: [binary_id: true], migration_dir: "priv/repo/migrations", repo_dir: "lib/my_app/repo", repo_namespace: "Repo", schema_dir: "lib/my_app/schema", schema_namespace: "Schema" ``` -------------------------------- ### Get Post by Attributes (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Fetches a single Post based on matching attribute values. Returns the Post struct if found, or `nil` if no matching record exists. ```elixir iex> Posts.get_by(%{title: "Example Title"}) %Post{title: "Example Title"} iex> Posts.get_by(%{title: "Doesn't Exist"}) nil ``` -------------------------------- ### Get Post by ID! (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Fetches a single Post by its primary key. Returns the Post struct if found, or raises an `Ecto.NoResultsError` if no matching record exists. Supports preloading associated data. ```elixir iex> Posts.get!(1) %Post{id: 1} iex> Posts.get!(999) ** (Ecto.NoResultsError) iex> Posts.get!(1, preloads: [:comments]) %Post{ id: 1, comments: [%Comment{}], ... } ``` -------------------------------- ### Get Post by ID (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Fetches a single Post by its primary key. Returns the Post struct if found, or `nil` if no matching record exists. Supports preloading associated data. ```elixir iex> Posts.get(1) %Post{id: 1} iex> Posts.get(999) nil iex> Posts.get(1, preloads: [:address]) %Post{ id: 1, address: %Address{} } ``` -------------------------------- ### Generate EctoCooler read-write functions (excluding delete) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This example uses the `:read_write` alias with EctoCooler to generate functions for both data access and manipulation, excluding delete operations. This is useful for scenarios where data deletion is handled separately. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, :read_write) end end ``` -------------------------------- ### Generate EctoCooler functions excluding specific ones Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This example demonstrates how to generate most EctoCooler functions while excluding a specific set, using the `except` option. This is useful for creating a custom API that omits certain operations. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, except: [:create, :delete!]) end end ``` -------------------------------- ### Generate Ecto Repo, Schema, and Migration (Mix) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Generates Ecto Repo, Schema, and Migration files using the `mix ectc.gen.repo` command. Requires specifying the repo name, schema name, table name, and attributes. ```bash mix ectc.gen.repo Posts Post posts title:string author:string ``` -------------------------------- ### Create Post! (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Inserts a new Post into the data store, returning the created Post or raising an `Ecto.InvalidChangesetError` for invalid attributes. ```elixir iex> Posts.create!(%{title: "Example Post"}) %Post{id: 123, title: "Example Post"} iex> Posts.create!(%{invalid: "invalid"}) ** (Ecto.InvalidChangesetError) ``` -------------------------------- ### Create Post (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Inserts a new Post into the data store. Returns an `:ok`/`:error` tuple. Handles valid and invalid attributes, returning a changeset on error. ```elixir iex> Posts.create(%{title: "Example Post"}) {:ok, %Post{id: 123, title: "Example Post"}} iex> Posts.create(%{invalid: "invalid"}) {:error, %Ecto.Changeset} ``` -------------------------------- ### Generate all EctoCooler functions Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This code snippet demonstrates the basic usage of EctoCooler to generate all available functions for a given Ecto schema. It defines a module that uses EctoCooler and specifies the repository and schema to be used. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post) end end ``` -------------------------------- ### Update Post (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Updates an existing Post with new attributes. Returns an `{:ok, %Post{}}` tuple on success or `{:error, Ecto.Changeset}` if the update fails due to invalid attributes. ```elixir iex> Posts.update(%Post{id: 1}, %{title: "Updated Title"}) {:ok, %Post{id: 1, title: "Updated Title"}} iex> Posts.update(%Post{id: 1}, %{invalid: "invalid"}) {:error, %Ecto.Changeset} ``` -------------------------------- ### Generate specific EctoCooler functions Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This snippet illustrates how to explicitly define which EctoCooler functions should be generated for a schema, using the `only` option. This allows for fine-grained control over the generated API. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, only: [:create, :delete!]) end end ``` -------------------------------- ### Generate EctoCooler read-only functions Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This code snippet shows how to use the `:read` alias with EctoCooler to generate only the data access functions for a schema, such as `all/1` and `get/2`. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, :read) end end ``` -------------------------------- ### Update Post! (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Updates an existing Post with new attributes. Returns the updated Post struct on success or raises an `Ecto.InvalidChangesetError` if the update fails due to invalid attributes. ```elixir iex> Posts.update!(%Post{id: 1}, %{title: "Updated Title"}) %Post{id: 1, title: "Updated Title"} iex> Posts.update!(%Post{id: 1}, %{invalid: "invalid"}) ** (Ecto.InvalidChangesetError) ``` -------------------------------- ### Configure EctoCooler for Generators Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Configures EctoCooler for use with generators, specifying application name and slug. This is essential for features that rely on generating code or project-specific configurations. ```Elixir config :ecto_cooler, app_name: "MyApp", app_slug: :my_app ``` -------------------------------- ### Generate EctoCooler delete-only functions Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md This snippet demonstrates using the `:delete` alias with EctoCooler to generate only the functions responsible for deleting data, such as `delete/1` and `delete!/1`. ```elixir defmodule MyApp.Repo.Posts do alias MyApp.Repo alias MyApp.Schema.Post use EctoCooler using_repo(Repo) do resource(Post, :delete) end end ``` -------------------------------- ### Delete Post (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Deletes a specified Post from the data store, returning an `:ok`/`:error` tuple. Handles cases where the post exists and where it might not, returning a changeset on error for non-existent posts. ```elixir iex> Posts.delete(%Post{id: 1}) {:ok, %Post{id: 1}} iex> Posts.delete(%Post{id: 999}) {:error, %Ecto.Changeset} ``` -------------------------------- ### Delete Post! (Ecto) Source: https://github.com/daytonn/ecto_cooler/blob/main/README.md Deletes a specified Post from the data store, returning the deleted Post or raising an `Ecto.StaleEntryError` if the record is not found or has been modified. ```elixir iex> Posts.delete!(%Post{id: 1}) %Post{id: 1} iex> Posts.delete!(%Post{id: 999}) ** (Ecto.StaleEntryError) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.