https://github.com/woylie/ecto_nested_changeset
Helpers for manipulating nested Ecto changesets
https://github.com/woylie/ecto_nested_changeset
changeset dynamic-forms ecto elixir liveview phoenix
Last synced: 5 months ago
JSON representation
Helpers for manipulating nested Ecto changesets
- Host: GitHub
- URL: https://github.com/woylie/ecto_nested_changeset
- Owner: woylie
- License: mit
- Created: 2021-08-27T06:22:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-18T23:33:47.000Z (6 months ago)
- Last Synced: 2025-04-19T09:51:29.354Z (6 months ago)
- Topics: changeset, dynamic-forms, ecto, elixir, liveview, phoenix
- Language: Elixir
- Homepage:
- Size: 483 KB
- Stars: 54
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[](https://github.com/woylie/ecto_nested_changeset/actions/workflows/ci.yml) [](https://hex.pm/packages/ecto_nested_changeset) [](https://coveralls.io/github/woylie/ecto_nested_changeset?branch=main)
# EctoNestedChangeset
This is a package for manipulating nested
[Ecto](https://github.com/elixir-ecto/ecto) changesets.## Installation
Add `ecto_nested_changeset` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ecto_nested_changeset, "~> 1.0.0"}
]
end
```## Usage
The primary use case of this library is the manipulation of
[Ecto](https://github.com/elixir-ecto/ecto) changesets
used as a source for dynamic, nested forms in
[Phoenix LiveView](https://github.com/phoenixframework/phoenix_live_view).```elixir
category = %Category{
posts: [
%Post{
id: 1,
comments: [
%Comment{body: "potato", id: 1},
%Comment{body: "you", id: 2}
],
title: "must"
},
%Post{comments: [], id: 2, title: "young"}
]
}category
|> Ecto.Changeset.change()
|> append_at(:posts, %Post{title: "Padawan", comments: []})
|> prepend_at([:posts, 0, :comments], %Comment{body: "ecneitaP"})
|> delete_at([:posts, 0, :comments, 1], mode: {:action, :delete})
|> insert_at([:posts, 1], %Post{title: "have"})
|> append_at([:posts, 2, :comments], %Comment{body: "my"})
|> update_at([:posts, 0, :comments, 0, :body], &String.reverse/1)
|> Ecto.Changeset.apply_changes()%Category{
posts: [
%Post{
comments: [
%Comment{body: "Patience"},
%Comment{body: "you", id: 2}
],
id: 1,
title: "must"
},
%Post{title: "have"},
%Post{
comments: [%Comment{body: "my"}],
id: 2,
title: "young"
},
%Post{title: "Padawan"}
]
}
```## Example application
There is an example Phoenix application with a dynamic nested LiveView form in
the `/example` folder of the repository.```bash
git clone https://github.com/woylie/ecto_nested_changeset.git
cd ecto_nested_changeset/example
mix setup
mix phx.server
```Note that Postgres needs to be running to use the application.
You can access the application at http://localhost:4000.
## Status
This library is actively maintained, but given its narrow purpose, it will not
see frequent updates.