View Source MishkaDeveloperTools.DB.CRUD behaviour (Mishka developer tools v0.0.7)
simplified-crud-macro-using-ecto
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,
error_atom: :your_error_tag,
repo: Your.Repo
It should be noted that the following three parameters must be sent and also make sure you are connected to the database.
module
error_atom
repo
Link to this section Summary
Functions
Creating a record macro
Example
crud_add(map_of_info like: %{name: "trangell"})
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.
Link to this section Types
@type data_uuid() :: Ecto.UUID.t()
@type error_tag() :: atom()
@type record_input() :: map()
@type repo_data() :: Ecto.Schema.t()
@type repo_error() :: Ecto.Changeset.t()
Link to this section Callbacks
@callback create(record_input()) :: {:error, :add, error_tag(), repo_error()} | {:ok, :add, error_tag(), repo_data()}
@callback edit(record_input()) :: {:error, :edit, :uuid, error_tag()} | {:error, :edit, :get_record_by_id, error_tag()} | {:error, :edit, error_tag(), repo_error()} | {:ok, :edit, error_tag(), repo_data()}
Link to this section Functions
creating-a-record-macro
Creating a record macro
example
Example
crud_add(map_of_info like: %{name: "trangell"})
The input of this macro is a map and its output are a map. For example
{:ok, :add, error_atom, data}
{:error, :add, error_atom, changeset}
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-1
Example
crud_add(map_of_info like: %{name: "trangell"}, [:name])
delete-a-record-from-the-database-with-the-help-of-id-macro
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
Example
crud_delete("6d80d5f4-781b-4fa8-9796-1821804de6ba")
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:
# This message will be returned when your data has been successfully deleted
{:ok, :delete, error_atom, struct}
# This error will be returned if the ID sent by you is not a UUID
{:error, :delete, error_atom, :uuid}
# This error is reversed when an error occurs while sending data
{:error, :delete, error_atom, changeset}
# This error will be reversed when there is no submitted ID in the database
{:error, :delete, error_atom, :get_record_by_id}
# This error is reversed when another record is associated with this record
{:error, :delete, error_atom, :forced_to_delete}
edit-a-record-in-a-database-macro
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
Example
crud_edit(map_of_info like: %{id: "6d80d5f4-781b-4fa8-9796-1821804de6ba",name: "trangell"})
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
# If your request has been saved successfully
{:ok, :edit, error_atom, info}
# If your ID is not uuid type
{:error, :edit, error_atom, :uuid}
# If there is an error in sending the data
{:error, :edit, error_atom, changeset}
# If no record is found for your ID
{:error, :delete, error_atom, :get_record_by_id}
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-1
Example
crud_edit(map_of_info like: %{id: "6d80d5f4-781b-4fa8-9796-1821804de6ba", name: "trangell"}, [:id, :name])
macro-find-a-record-in-the-database-with-the-help-of-the-requested-field
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, error_atom, :get_record_by_field}
{:ok, error_atom, :get_record_by_field, record_info}
macro-finding-a-record-in-a-database-with-the-help-of-id
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, error_atom, :get_record_by_id}
{:ok, error_atom, :get_record_by_id, record_info}