View Source MishkaDeveloperTools.DB.CRUD behaviour (Mishka developer tools v0.0.8)

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,
    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

Link to this section 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.

Link to this section Types

@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, 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()}
Link to this callback

delete(arg1, list)

View Source (optional)
@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()}}
Link to this callback

show_by_field(t, any)

View Source (optional)
@callback show_by_field(String.t(), any()) :: {:error, :not_found, String.t()} | struct()
Link to this callback

show_by_id(arg1)

View Source (optional)
@callback show_by_id(Ecto.UUID.t() | non_neg_integer()) ::
  {:error, :not_found, String.t()} | struct()

Link to this section Functions

Link to this macro

crud_add(attrs)

View Source (macro)

creating-a-record-macro

Creating a record macro

example

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-1

Example

crud_add(map_of_info like: %{"name" => "Mishka"}, ["name"])
Link to this macro

crud_add(attrs, allowed_fields)

View Source (macro)
Link to this macro

crud_delete(id)

View Source (macro)

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")
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()}
Link to this macro

crud_delete(id, assoc)

View Source (macro)
Link to this macro

crud_edit(attrs)

View Source (macro)

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" => "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-1

Example

crud_edit(map_of_info like: %{"id" => "6d80d5f4-781b-4fa8-9796-1821804de6ba", "name" => "Mishka"}, , ["id", "name"])
Link to this macro

crud_edit(attrs, allowed_fields)

View Source (macro)
Link to this macro

crud_get_by_field(field, value)

View Source (macro)

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, :not_found, String.t()} | struct()
Link to this macro

crud_get_record(id)

View Source (macro)

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, :not_found, String.t()} | struct()