You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/macros/database/crud.ex
+18-33Lines changed: 18 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ defmodule MishkaDeveloperTools.DB.CRUD do
8
8
```elixir
9
9
use MishkaDeveloperTools.DB.CRUD,
10
10
module: YOURschemaMODULE,
11
-
error_atom: :your_error_tag,
12
-
repo: Your.Repo
11
+
repo: Your.Repo,
12
+
id: :uuid OR ANY_TYPE_YOU_WANT
13
13
```
14
14
It should be noted that the following three parameters must be sent and also make sure you are connected to the database.
15
15
16
16
```elixir
17
17
module
18
-
error_atom
19
18
repo
19
+
id
20
20
```
21
21
"""
22
22
@@ -63,20 +63,20 @@ defmodule MishkaDeveloperTools.DB.CRUD do
63
63
64
64
## Example
65
65
```elixir
66
-
crud_add(map_of_info like: %{name: "trangell"})
66
+
crud_add(map_of_info like: %{"name" => "Mishka"})
67
67
```
68
68
The input of this macro is a map and its output are a map. For example
69
69
70
70
```elixir
71
-
{:ok, :add, error_atom, data}
72
-
{:error, :add, error_atom, changeset}
71
+
{:error, :add, repo_error()}
72
+
{:ok, :add, repo_data()}
73
73
```
74
74
75
75
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
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.
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.
173
169
174
-
175
-
176
170
Outputs:
177
171
178
172
```elixir
179
-
# This message will be returned when your data has been successfully deleted
180
-
{:ok, :delete, error_atom, struct}
181
-
# This error will be returned if the ID sent by you is not a UUID
182
-
{:error, :delete, error_atom, :uuid}
183
-
# This error is reversed when an error occurs while sending data
184
-
{:error, :delete, error_atom, changeset}
185
-
# This error will be reversed when there is no submitted ID in the database
186
-
{:error, :delete, error_atom, :get_record_by_id}
187
-
# This error is reversed when another record is associated with this record
Recently I have been working on [MishkaCms](https://github.com/mishka-group/mishka-cms), an open-source and free **CMS** for **Elixir** and **Phoenix**. It is very easy to use and has many APIs on both API, and HTML render. In this CMS, I should create some custom macros and modules which are helpful for Elixir developers epically Phoenix **LiveView** fans. Then I test them, and if they are usable, I put them into this project as separated tools.
6
+
So when you want to start a project with Elixir, Mishka Developer Tools is an excellent opportunity to finish or start your project as well as possible.
7
+
8
+
> You don't need to configure database for this library. The priority is your project database or ORM.
9
+
10
+
## Installation
11
+
12
+
The package can be installed by adding `mishka_developer_tools` to your list of dependencies in `mix.exs`:
13
+
14
+
```elixir
15
+
def deps do
16
+
[
17
+
{:mishka_developer_tools, "~> 0.0.8"}
18
+
]
19
+
end
20
+
```
21
+
22
+
The docs can be found at [https://hexdocs.pm/mishka_developer_tools](https://hexdocs.pm/mishka_developer_tools).
23
+
24
+
---
25
+
---
26
+
27
+
## Creating basic CRUD
28
+
At first, you need to call the `__using__` macro of the Mishka developer tools.
29
+
30
+
```elixir
31
+
use MishkaDeveloperTools.DB.CRUD,
32
+
module: YOUR_SCHEMA_MODULE,
33
+
repo: YOUR_REPO_MODULE,
34
+
id: :uuid OR ANY_TYPE_YOU_WANT
35
+
```
36
+
37
+
And after that, you can define `@behaviour`; it is optional.
38
+
```elixir
39
+
@behaviour MishkaDeveloperTools.DB.CRUD
40
+
```
41
+
42
+
Now is the time you can have your CRUD function; it should be noted you are not forced to use these macros under functions.
0 commit comments