Skip to content

Commit 2703545

Browse files
committed
fixed #9 && #4 , change some docs and add CHANGELOG.md
1 parent 61f77a3 commit 2703545

File tree

3 files changed

+113
-33
lines changed

3 files changed

+113
-33
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog for MishkaDeveloperTools 0.0.8
2+
3+
- [x] Improve CRUD macro `callbacks` and `specs` return for `dialyzer`
4+
- [x] Add new delete macro and function
5+
- [x] Support UUID and the other type of ID
6+
- [x] Improve testing
7+
- [x] Improving and updating the coding structure

lib/macros/database/crud.ex

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ defmodule MishkaDeveloperTools.DB.CRUD do
88
```elixir
99
use MishkaDeveloperTools.DB.CRUD,
1010
module: YOURschemaMODULE,
11-
error_atom: :your_error_tag,
12-
repo: Your.Repo
11+
repo: Your.Repo,
12+
id: :uuid OR ANY_TYPE_YOU_WANT
1313
```
1414
It should be noted that the following three parameters must be sent and also make sure you are connected to the database.
1515
1616
```elixir
1717
module
18-
error_atom
1918
repo
19+
id
2020
```
2121
"""
2222

@@ -63,20 +63,20 @@ defmodule MishkaDeveloperTools.DB.CRUD do
6363
6464
## Example
6565
```elixir
66-
crud_add(map_of_info like: %{name: "trangell"})
66+
crud_add(map_of_info like: %{"name" => "Mishka"})
6767
```
6868
The input of this macro is a map and its output are a map. For example
6969
7070
```elixir
71-
{:ok, :add, error_atom, data}
72-
{:error, :add, error_atom, changeset}
71+
{:error, :add, repo_error()}
72+
{:ok, :add, repo_data()}
7373
```
7474
7575
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
7676
7777
### Example
7878
```elixir
79-
crud_add(map_of_info like: %{name: "trangell"}, [:name])
79+
crud_add(map_of_info like: %{"name" => "Mishka"}, ["name"])
8080
```
8181
"""
8282
defmacro crud_add(attrs) do
@@ -105,28 +105,23 @@ defmodule MishkaDeveloperTools.DB.CRUD do
105105
106106
## Example
107107
```elixir
108-
crud_edit(map_of_info like: %{id: "6d80d5f4-781b-4fa8-9796-1821804de6ba",name: "trangell"})
108+
crud_edit(map_of_info like: %{"id" => "6d80d5f4-781b-4fa8-9796-1821804de6ba", "name" => "Mishka"})
109109
```
110110
> Note that the sending ID must be of UUID type.
111111
112112
The input of this macro is a map and its output are a map. For example
113113
114114
```elixir
115-
# If your request has been saved successfully
116-
{:ok, :edit, error_atom, info}
117-
# If your ID is not uuid type
118-
{:error, :edit, error_atom, :uuid}
119-
# If there is an error in sending the data
120-
{:error, :edit, error_atom, changeset}
121-
# If no record is found for your ID
122-
{:error, :delete, error_atom, :get_record_by_id}
115+
{:error, :edit, repo_error()}
116+
{:ok, :edit, repo_data()}
117+
{:error, :edit, {:error, :uuid | :not_found, String.t()}}
123118
```
124119
125120
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.
126121
127122
## Example
128123
```elixir
129-
crud_edit(map_of_info like: %{id: "6d80d5f4-781b-4fa8-9796-1821804de6ba", name: "trangell"}, [:id, :name])
124+
crud_edit(map_of_info like: %{"id" => "6d80d5f4-781b-4fa8-9796-1821804de6ba", "name" => "Mishka"}, , ["id", "name"])
130125
```
131126
"""
132127
defmacro crud_edit(attrs) do
@@ -167,25 +162,17 @@ defmodule MishkaDeveloperTools.DB.CRUD do
167162
## Example
168163
```elixir
169164
crud_delete("6d80d5f4-781b-4fa8-9796-1821804de6ba")
165+
crud_delete("6d80d5f4-781b-4fa8-9796-1821804de6ba", [:comment, :post])
170166
```
171167
Output:
172168
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.
173169
174-
175-
176170
Outputs:
177171
178172
```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
188-
{:error, :delete, error_atom, :forced_to_delete}
173+
{:error, :delete, repo_error()}
174+
{:error, :delete, {:error, :uuid | :not_found | :force_constraint, String.t()}}
175+
{:ok, :delete, repo_data()}
189176
```
190177
"""
191178
defmacro crud_delete(id) do
@@ -228,8 +215,7 @@ defmodule MishkaDeveloperTools.DB.CRUD do
228215
Outputs:
229216
230217
```
231-
{:error, error_atom, :get_record_by_id}
232-
{:ok, error_atom, :get_record_by_id, record_info}
218+
{:error, :not_found, String.t()} | struct()
233219
```
234220
235221
"""
@@ -254,8 +240,7 @@ defmodule MishkaDeveloperTools.DB.CRUD do
254240
Outputs:
255241
256242
```
257-
{:error, error_atom, :get_record_by_field}
258-
{:ok, error_atom, :get_record_by_field, record_info}
243+
{:error, :not_found, String.t()} | struct()
259244
```
260245
261246
"""

lib/mishka_developer_tools.ex

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
11
defmodule MishkaDeveloperTools do
2+
@moduledoc """
3+
# Mishka Elixir Developer Tools
4+
5+
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.
43+
44+
45+
```elixir
46+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :create, 1}
47+
def create(attrs) do
48+
crud_add(attrs)
49+
end
50+
51+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :create, 1}
52+
def create(attrs, allowed_fields) do
53+
crud_add(attrs, allowed_fields)
54+
end
55+
56+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :edit, 1}
57+
def edit(attrs) do
58+
crud_edit(attrs)
59+
end
60+
61+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :edit, 1}
62+
def edit(attrs, allowed_fields) do
63+
crud_edit(attrs, allowed_fields)
64+
end
65+
66+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :delete, 1}
67+
def delete(id) do
68+
crud_delete(id)
69+
end
70+
71+
# It is optional
72+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :delete, 2}
73+
def delete(id) do
74+
crud_delete(id)
75+
end
76+
77+
# It is optional
78+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :show_by_id, 1}
79+
def show_by_id(id) do
80+
crud_get_record(id)
81+
end
82+
83+
# It is optional
84+
@doc delegate_to: {MishkaDeveloperTools.DB.CRUD, :show_by_field, 1}
85+
def show_by_field(field) do
86+
crud_get_by_field("field", field)
87+
end
88+
```
89+
"""
290
end

0 commit comments

Comments
 (0)