Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jungsoft/crudry
Elixir library for DRYing CRUD in Phoenix Contexts and Absinthe Resolvers.
https://github.com/jungsoft/crudry
elixir
Last synced: 17 days ago
JSON representation
Elixir library for DRYing CRUD in Phoenix Contexts and Absinthe Resolvers.
- Host: GitHub
- URL: https://github.com/jungsoft/crudry
- Owner: jungsoft
- License: mit
- Created: 2018-12-13T21:13:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-31T19:09:11.000Z (almost 3 years ago)
- Last Synced: 2024-08-19T15:48:44.129Z (4 months ago)
- Topics: elixir
- Language: Elixir
- Homepage: https://hexdocs.pm/crudry
- Size: 175 KB
- Stars: 90
- Watchers: 4
- Forks: 11
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Crudry is an elixir library for DRYing CRUD of Phoenix Contexts and Absinthe Resolvers. (Macros)
README
![Crudry](img/crudry.svg)
[![Coverage Status](https://coveralls.io/repos/github/jungsoft/crudry/badge.svg?branch=master)](https://coveralls.io/github/jungsoft/crudry?branch=master)
Crudry is an elixir library for DRYing CRUD of Phoenix Contexts and Absinthe Resolvers.
It also provides a simple middleware for translating changeset errors into readable messages.
Documentation can be found at [https://hexdocs.pm/crudry](https://hexdocs.pm/crudry).
The changelog can be found at the [Releases page](https://github.com/jungsoft/crudry/releases).
## Installation
The package can be installed by adding `crudry` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:crudry, "~> 2.4.0"},
]
end
```## Usage
### Context Generation
To generate CRUD functions for a given schema in your context, simply do:
```elixir
defmodule MyApp.MyContext do
alias MyApp.Reporequire Crudry.Context
Crudry.Context.generate_functions MyApp.MySchema
end
```To see the functions that are generated and custom options, refer to the [Crudry.Context docs](https://hexdocs.pm/crudry/Crudry.Context.html#module-usage).
### Resolver Generation
With the context all set up, the resolver is ready to be generated:
```elixir
defmodule MyApp.MyResolver do
alias MyApp.Reporequire Crudry.Resolver
Crudry.Resolver.generate_functions MyApp.MyContext, MyApp.MySchema
end
```To see the functions that are generated and custom options, refer to the [Crudry.Resolver docs](https://hexdocs.pm/crudry/Crudry.Resolver.html#module-usage).
### Translate Errors middleware
Absinthe Middleware to translate errors and changeset errors into human readable messages. It support nested changeset errors and internationalization, using [Gettext](https://github.com/elixir-lang/gettext).
To handle errors for a field, add it after the resolve, using [`middleware/2`](https://hexdocs.pm/absinthe/Absinthe.Middleware.html#module-the-middleware-2-macro):
```elixir
alias Crudry.Middlewares.TranslateErrorsfield :create_user, :user do
arg :params, non_null(:user_params)resolve &UsersResolver.create_user/2
middleware TranslateErrors
end
```To handle errors for all fields, use [middleware/3](https://hexdocs.pm/absinthe/Absinthe.Middleware.html#module-object-wide-authentication):
```elixir
alias Crudry.Middlewares.TranslateErrorsdef middleware(middleware, _field, _object) do
middleware ++ [TranslateErrors]
end
````Crudry.Translator` is used by default to translate error messages to the default locale `en`. You can also use your own Gettext module by adding it to your Absinthe's schema `context/1` function:
```elixir
def context(context) do
Map.put(context, :translator, MyAppWeb.Gettext)
end
```Or just override the default locale in your [Context Plug](https://hexdocs.pm/absinthe/context-and-authentication.html#context-and-plugs):
```elixir
def call(conn, _) do
Absinthe.Plug.put_options(conn, context: %{locale: "pt_BR"})
end
```Refer to the [TranslateErrors docs](https://hexdocs.pm/crudry/Crudry.Middlewares.TranslateErrors.html) for more information.
## Related Projects
* [Rajska](https://github.com/jungsoft/rajska) is an elixir authorization library for Absinthe.
* [Uploadex](https://github.com/jungsoft/uploadex) is an elixir library for handling uploads using Ecto and Arc## License
MIT License.
See [LICENSE](./LICENSE) for more information.