View Source MishkaDeveloperTools.DB.CRUD behaviour (Mishka developer tools v0.1.2)
Simplified CRUD macro using Ecto
With this module, you can easily implement CRUD-related items in your file wherever you need to build a query. These modules and their sub-macros were created more to create a one-piece structure, and you can implement your own custom items in umbrella projects. In the first step, to use the following macros, you must bind the requested information in the relevant module that you have already created as follows.
use MishkaDeveloperTools.DB.CRUD,
module: YOURschemaMODULE,
repo: Your.Repo,
id: :uuid OR ANY_TYPE_YOU_WANT
It should be noted that the following three parameters must be sent and also make sure you are connected to the database.
module
repo
id
Summary
Functions
Creating a record macro
Example
crud_add(map_of_info like: %{"name" => "Mishka"})
The input of this macro is a map and its output are a map. For example
delete a record from the database with the help of ID Macro
With the help of this macro, you can delete your requested record from the database. The input of this macro is a UUID and its output is a map
Edit a record in a database Macro
With the help of this macro, you can edit a record in the database with its ID. For this purpose, you must send the requested record ID along with the new Map parameters. Otherwise the macro returns the ID error.
Macro Find a record in the database with the help of the requested field
With the help of this macro, you can find a field with the value you want, if it exists in the database. It should be noted that the field name must be entered as a String.
Macro Finding a record in a database with the help of ID
With the help of this macro, you can send an ID that is of UUID type and call it if there is a record in the database. The output of this macro is map.
Types
@type record_input() :: map()
@type repo_data() :: Ecto.Schema.t()
@type repo_error() :: Ecto.Changeset.t()
Callbacks
@callback create(record_input()) :: {:error, :add, repo_error()} | {:ok, :add, repo_data()}
@callback delete(Ecto.UUID.t() | non_neg_integer()) :: {:error, :delete, repo_error()} | {:error, :delete, {:error, :uuid | :not_found | :force_constraint, String.t()}} | {:ok, :delete, repo_data()}
@callback delete(Ecto.UUID.t() | non_neg_integer(), [atom()]) :: {:error, :delete, {:error, :uuid | :not_found, String.t()}} | {:error, :delete, repo_error()} | {:ok, :delete, repo_data()}
@callback edit(record_input()) :: {:error, :edit, repo_error()} | {:ok, :edit, repo_data()} | {:error, :edit, {:error, :uuid | :not_found, String.t()}}
@callback show_by_id(Ecto.UUID.t() | non_neg_integer()) :: {:error, :not_found, String.t()} | struct()
Functions
Creating a record macro
Example
crud_add(map_of_info like: %{"name" => "Mishka"})
The input of this macro is a map and its output are a map. For example
{:error, :add, repo_error()}
{:ok, :add, repo_data()}
If you want only the selected parameters to be separated from the list of submitted parameters and sent to the database, use the same macro with input 2
Example
crud_add(map_of_info like: %{"name" => "Mishka"}, ["name"])
delete a record from the database with the help of ID Macro
With the help of this macro, you can delete your requested record from the database. The input of this macro is a UUID and its output is a map
Example
crud_delete("6d80d5f4-781b-4fa8-9796-1821804de6ba")
crud_delete("6d80d5f4-781b-4fa8-9796-1821804de6ba", [:comment, :post])
Output: You should note that this macro prevents the orphan data of the record requested to be deleted. So, use this macro when the other data is not dependent on the data with the ID sent by you.
Outputs:
{:error, :delete, repo_error()}
{:error, :delete, {:error, :uuid | :not_found | :force_constraint, String.t()}}
{:ok, :delete, repo_data()}
Edit a record in a database Macro
With the help of this macro, you can edit a record in the database with its ID. For this purpose, you must send the requested record ID along with the new Map parameters. Otherwise the macro returns the ID error.
Example
crud_edit(map_of_info like: %{"id" => "6d80d5f4-781b-4fa8-9796-1821804de6ba", "name" => "Mishka"})
Note that the sending ID must be of UUID type.
The input of this macro is a map and its output are a map. For example
{:error, :edit, repo_error()}
{:ok, :edit, repo_data()}
{:error, :edit, {:error, :uuid | :not_found, String.t()}}
It should be noted that if you want only the selected fields to be separated from the submitted parameters and sent to the database, use the macro with dual input.
Example
crud_edit(map_of_info like: %{"id" => "6d80d5f4-781b-4fa8-9796-1821804de6ba", "name" => "Mishka"}, , ["id", "name"])
Macro Find a record in the database with the help of the requested field
With the help of this macro, you can find a field with the value you want, if it exists in the database. It should be noted that the field name must be entered as a String.
Example
crud_get_by_field("email", "info@trangell.com")
Outputs:
{:error, :not_found, String.t()} | struct()
Macro Finding a record in a database with the help of ID
With the help of this macro, you can send an ID that is of UUID type and call it if there is a record in the database. The output of this macro is map.
Example
crud_get_record("6d80d5f4-781b-4fa8-9796-1821804de6ba")
Outputs:
{:error, :not_found, String.t()} | struct()