-
Notifications
You must be signed in to change notification settings - Fork 3
/
ecto_rut.ex
422 lines (272 loc) · 11.1 KB
/
ecto_rut.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
defmodule Ecto.Rut do
@moduledoc """
Provides simple, sane and terse shortcuts for Ecto models.
Ecto.Rut is a wrapper around `Ecto.Repo` methods that usually require you to pass
the module as the subject and sometimes even require you do extra work before hand,
(as in the case of `Repo.insert/3`) to perform operations on your database. Ecto.Rut
tries to reduce code repetition by following the "Convention over Configuration"
ideology.
For example, once set up, it allows you to do this on a model called `Post`:
```
# Create a Post
Post.insert(title: "Introduction to Elixir", categories: ["Elixir", "Programming"])
# Get all Posts
Post.all
# Get a Post with its id
Post.get(3)
# Get a Post with another attribute
Post.get_by(published_date: "2016-02-24")
# Delete a Post
Post.delete(lame_post)
```
## Installation
Once added to your mix dependencies, all you need to do is call `use Ecto.Rut` in
your Ecto models:
```
defmodule YourApp.Post do
use Ecto.Schema
use Ecto.Rut
# Schema, Changeset and other stuff...
end
```
### Phoenix Projects
If you're using Ecto with a Phoenix project, instead of calling `use Ecto.Rut` in all of
your models, you can just call it once in the `model/0` method of your `web/web.ex` file:
```
# web/web.ex
def model do
quote do
use Ecto.Schema
use Ecto.Rut
# Other stuff...
end
end
```
## Configuration
You can also pass options to Ecto.Rut when calling `use` on it. These values are inferred
automatically by Ecto.Rut, but you can set them yourself in those special cases where it
can't. The two options are:
- `:model`
You set this when your Ecto Model is different from the module where you are calling `use`
- `:repo`
You set this when your app's Ecto.Repo module is set differently.
```
defmodule YourApp.OtherNamespace.Post do
use Ecto.Schema
use Ecto.Rut, model: YourApp.Post, repo: YourApp.CustomEcto.Repo
# Other Stuff
end
```
## Export Changeset
Methods like `c:insert/1` or `c:update/2` depend on your model exporting a public function called
`changeset(struct, params)` with all your desired validations and constraints applied to
the casted struct.
Ecto.Rut uses this function to convert maps, keyword lists and other types into `Ecto.Changeset`,
before updating or inserting them into the database.
Phoenix projects generate them for your models automatically, but for other Elixir projects,
you can [see an example here](https://hexdocs.pm/ecto/Ecto.Changeset.html).
"""
@doc false
defmacro __using__(opts \\ []) do
quote bind_quoted: [opts: opts] do
@model opts[:model] || __MODULE__
@app opts[:app] || @model |> Module.split |> Enum.drop(-1) |> Module.concat
@repo opts[:repo] || @app |> Module.concat("Repo")
# Default Changeset method
def changeset(struct, params) do
Ecto.Changeset.cast(struct, params, [])
end
defoverridable [changeset: 2]
# Simple Methods
def all, do: call(:all, [@model])
def delete_all, do: call(:delete_all, [@model])
def get(id), do: call(:get, [@model, id])
def get!(id), do: call(:get!, [@model, id])
def get_by(clauses), do: call(:get_by, [@model, clauses])
def get_by!(clauses), do: call(:get_by!, [@model, clauses])
def delete(struct), do: call(:delete, [struct])
def delete!(struct), do: call(:delete!, [struct])
# Insert and Insert!
Enum.each [:insert, :insert!], fn method ->
def unquote(method)(%{__struct__: Ecto.Changeset} = changeset) do
call(unquote(method), [changeset])
end
def unquote(method)(%{__struct__: @model} = struct) do
struct
|> Map.from_struct
|> unquote(method)()
end
def unquote(method)(map) when is_map(map) do
@model
|> Kernel.struct
|> changeset(map)
|> unquote(method)()
end
def unquote(method)(keywords) do
keywords
|> ExUtils.Keyword.to_map
|> unquote(method)()
end
end
# Update and Update!
Enum.each [:update, :update!], fn method ->
def unquote(method)(%{__struct__: Ecto.Changeset} = changeset) do
call(unquote(method), [changeset])
end
def unquote(method)(%{__struct__: @model} = struct, new \\ nil) do
[struct, map] =
cond do
is_nil(new) -> [get!(struct.id), Map.from_struct(struct)]
ExUtils.is_struct?(new) -> [struct, Map.from_struct(new)]
ExUtils.is_pure_map?(new) -> [struct, new]
Keyword.keyword?(new) -> [struct, ExUtils.Keyword.to_map(new)]
end
struct
|> changeset(map)
|> unquote(method)()
end
end
# Private Methods
defp call(method, args \\ []) do
apply(@repo, method, args)
end
end
end
@doc """
Fetches all entries from the Datastore for the Model
See `c:Ecto.Repo.all/2`.
## Example
```
Post.all
```
"""
@callback all :: [Ecto.Schema.t] | no_return
@doc """
Fetches a single struct from the data store where the primary key matches the given id.
Returns nil if no result was found. If the struct in the queryable has no or more than one
primary key, it will raise an argument error. See `c:Ecto.Repo.get/3`.
## Example
```
Post.get(3)
Post.get("0e531047-6bd2-4ab1-94c3-817fba988dbe")
```
"""
@callback get(id :: term) :: Ecto.Schema.t | nil | no_return
@doc """
Similar to `c:get/1` but raises `Ecto.NoResultsError` if no record was found.
Also see `c:Ecto.Repo.get!/3`.
"""
@callback get!(id :: term) :: Ecto.Schema.t | nil | no_return
@doc """
Fetches a single struct from the data store that matches the passed clauses.
Returns `nil` if no result was found. See `c:Ecto.Repo.get_by/3`.
## Example
```
Post.get_by(title: "Introduction to Elixir")
Post.get_by(published_date: "2015-10-15")
```
"""
@callback get_by(clauses :: Keyword.t) :: Ecto.Schema.t | nil | no_return
@doc """
Similar to `c:get_by/1` but raises `Ecto.NoResultsError` if no record was found.
Also see `c:Ecto.Repo.get_by!/3`.
"""
@callback get_by!(clauses :: Keyword.t) :: Ecto.Schema.t | nil | no_return
@doc """
Deletes a struct using its primary key.
Returns `{:ok, struct}` if the struct was successfully deleted or `{:error, changeset}`
if there was a validation or a known constraint error.
See `c:Ecto.Repo.delete/2`.
## Example
```
case Post.delete(post) do
{:ok, struct} -> # Deleted with success
{:error, changeset} -> # Something went wrong
end
```
"""
@callback delete(struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
@doc """
Similar to `c:delete/1` but returns the struct or raises if the changeset is invalid.
Also see `c:Ecto.Repo.delete!/2`.
"""
@callback delete!(struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: Ecto.Schema.t | no_return
@doc """
Deletes all entries of the model
Returns a tuple containing the number of items deleted. Also see `c:Ecto.Repo.delete_all/2`.
## Example
```
Post.delete_all
# => {34, nil}
```
"""
@callback delete_all :: {integer, nil | [term]} | no_return
@doc """
Inserts a new record (Can be a struct, changeset, keyword list or a map).
In case a changeset is given, the changes in the changeset are merges with the struct fields
and all of them are sent to the database.
In case a struct, keyword list or a map is given, they are first converted to a changeset, with
all non-nil fields as part of the changeset and inserted into the database if it's valid.
Returns a {:ok, struct} if it was successfully inserted, or a {:error, changeset} is there was a
validation or a known constraint error.
Also see `c:Ecto.Repo.insert/2`.
## Requires a changeset method
This method depends on your model exporting a public changeset function. [See this for more
details](#module-export-changeset).
## Example
```
Post.insert(title: "Introduction to Elixir")
Post.insert(%{title: "Building your first Phoenix app"})
Post.insert(%Post{title: "Concurrency in Elixir", categories: ["programming", "elixir"]})
Post.changeset(%Post{}, %{title: "Ecto for dummies"}) |> Post.insert
```
"""
@callback insert(struct :: Ecto.Schema.t | Ecto.Changeset.t | Map.t | Keyword.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
@doc """
Similar to `c:insert/1` but returns the struct or raises if the changeset is invalid.
Also see `c:Ecto.Repo.insert!/2`.
"""
@callback insert!(struct :: Ecto.Schema.t | Ecto.Changeset.t | Map.t | Keyword.t) :: Ecto.Schema.t | no_return
@doc """
Updates the database record using a modified struct or a changeset.
This method only accepts one argument; either a modified struct or a changeset. It uses the
struct or changeset's primary key to update the correct record in the database. If no primary
key is found, `Ecto.NoPrimaryKeyFieldError` will be raised.
Returns `{:ok, struct}` if the struct has been successfully updated or `{:error, changeset}`
if there was a validation or a known constraint error.
## Requires a changeset method
This method depends on your model exporting a public changeset function. [See this for more
details](#module-export-changeset).
## Example
```
post = Post.get_by!(id: 3)
post = %{ post | title: "Updated post title"}
Post.update(post)
```
"""
@callback update(modified_struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
@doc """
Updates the database record using a Keyword list or a Map and a Struct for comparison.
This method accepts two arguments, the first being the struct that has to be updated, and
the second being a Map or a Keyword List of the new values.
Returns `{:ok, struct}` if the struct has been successfully updated or `{:error, changeset}`
if there was a validation or a known constraint error. Also see `c:Ecto.Repo.update/2`.
## Requires a changeset method
This method depends on your model exporting a public changeset function. [See this for more
details](#module-export-changeset).
## Example
```
post = Post.get_by!(id: 3)
Post.update(post, title: "New post title", author_id: new_author_id)
```
"""
@callback update(struct :: Ecto.Schema.t, params :: Map.t | Keyword.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
@doc """
Similar to `c:update/1` but returns the struct or raises if the changeset is invalid.
"""
@callback update!(modified_struct_or_changeset :: Ecto.Schema.t | Ecto.Changeset.t) :: Ecto.Schema.t | no_return
@doc """
Similar to `c:update/2` but returns the struct or raises if the changeset is invalid.
"""
@callback update!(struct :: Ecto.Schema.t, params :: Map.t | Keyword.t) :: Ecto.Schema.t | no_return
end