LemonCrud (lemon_crud v0.1.0)
The LemonCrud
module generates common CRUD (Create, Read, Update, Delete) functions for a context, similar to what mix phx gen context
task generates.
Options
:repo
- The Ecto repository module used for database operations (required):schema
- The Ecto schema module representing the resource that these CRUD operations will be generated for (required):exclude
- A list of atoms representing the functions to be excluded from generation (optional):plural_resource_name
- A custom plural version of the resource name to be used in function names (optional). If not provided, singular version with 's' ending will be used to generate list function
Usage
defmodule MyApp.Accounts do
use LemonCrud,
repo: MyApp.Repo,
schema: MyApp.Accounts.User,
exclude: [:delete],
plural_resource_name: "users"
end
This sample usage will generate all CRUD functions for MyApp.Accounts.User
resource, excluding delete_user/1
.
Generated Functions
The following functions are generated by default. Any of them can be excluded by adding their correspoding atom to the :exclude
option.
list_{plural resource name}
- Lists all resources in the schema, optionally filtered by a query or condition list, or preloaded with associations.get_{resource name}
- Retrieves a resource by its ID, optionally preloaded with associations. Returnsnil
if not found.get_{resource name}!
- Retrieves a resource by its ID, optionally preloaded with associations. Raises an error if not found.get_{resource name}_by
- Retrieves a resource by a query or condition list, optionally preloaded with associations. Returnsnil
if not found.get_{resource name}_by!
- Retrieves a resource by a query or condition list, optionally preloaded with associations. Raises an error if not found.create_{resource name}
- Creates a new resource with the provided attributes. Returns an:ok
tuple with the resource or an:error
tuple with changeset.create_{resource name}!
- Creates a new resource with the provided attributes. Raises an error if creation fails.update_{resource name}
- Updates an existing resource with the provided attributes. Returns an:ok
tuple with the resource or an:error
tuple with changeset.update_{resource name}!
- Updates an existing resource with the provided attributes. Raises an error if update fails.delete_{resource name}
- Deletes an existing resource. Returns an:ok
tuple with the resource or an:error
tuple with changeset.delete_{resource name}!
- Deletes an existing resource. Raises an error if delete fails.change_{resource name}
- Returns changeset for given resource.
{resource name} and {plural resource name} will be replaced by the singular and plural forms of the resource name.