-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalog.ex
277 lines (221 loc) · 7.78 KB
/
alog.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
defmodule Alog do
@moduledoc """
Behaviour that defines functions for accessing and inserting data in an
Append Only database.
## Usage
At the top of the schema you wish to use append only functions for, `use` this module:
use Alog
The append only functions will then be available to call as part of your schema.
## Example
defmodule MyApp.User do
use Ecto.Schema
use Alog
import Ecto.Changeset
schema "users" do
...
end
def changeset(user, attrs) do
...
end
end
Alog expects your `Repo` to belong to the same base module as the schema.
So if your schema is `MyApp.User`, or `MyApp.Accounts.User`, your Repo should be `MyApp.Repo`.
Any schema that uses Alog must include the fields `:deleted` of type `:boolean` and default false,
and `:entry_id` of type `:string`.
field(:deleted, :boolean, default: false)
field(:entry_id, :string)
"""
@callback insert(map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback get(String.t()) :: Ecto.Schema.t() | nil | no_return()
@callback get_by(Keyword.t() | map()) :: Ecto.Schema.t() | nil | no_return()
@callback update(Ecto.Schema.t(), map()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback get_history(Ecto.Schema.t()) :: [Ecto.Schema.t()] | no_return()
@callback delete(Ecto.Schema.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback preload(Ecto.Schema.t() | list(Ecto.Schema.t()), atom() | list()) ::
Ecto.Schema.t() | list(Ecto.Schema.t())
defmacro __using__(_opts) do
quote location: :keep do
@behaviour Alog
@before_compile unquote(__MODULE__)
end
end
defmacro __before_compile__(_env) do
quote generated: true, location: :keep do
import Ecto.Query
import Ecto.Query.API, only: [field: 2]
@repo __MODULE__ |> Module.split() |> List.first() |> Module.concat("Repo")
if not Map.has_key?(%__MODULE__{}, :deleted) || not is_boolean(%__MODULE__{}.deleted) do
raise """
Your Schema must have a key :deleted, with type :boolean and default false.
Add the following line to your schema:
field(:deleted, :boolean, default: false)
"""
end
if not Map.has_key?(%__MODULE__{}, :entry_id) do
raise """
Your Schema must have a key :entry_id, with type :string
Add the following line to your schema:
field(:entry_id, :string)
"""
end
@doc """
Applies a schema's changeset function and inserts it into the database.
Adds an entry id to link it to future updates of the item.
User.insert(attributes)
"""
def insert(attrs) do
%__MODULE__{}
|> insert_entry_id()
|> __MODULE__.changeset(attrs)
|> @repo.insert()
end
@doc """
Gets the item from the database that matches the given entry id.
Gets the most recently inserted item if it is not marked as deleted.
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
"""
def get(entry_id) do
sub =
from(
m in __MODULE__,
where: m.entry_id == ^entry_id,
order_by: [desc: :inserted_at],
limit: 1,
select: m
)
query = from(m in subquery(sub), where: not m.deleted, select: m)
item = @repo.one(query)
end
@doc """
Gets an item from the database that matches the given clause.
User.get_by(username: "admin")
User.get_by(first_name: "Charlie", age: 27)
"""
def get_by(clauses) do
sub =
__MODULE__
|> (fn q ->
Enum.reduce(clauses, q, fn {key, value}, q ->
q |> where([m], field(m, ^key) == ^value)
end)
end).()
|> order_by([m], desc: m.inserted_at)
|> limit([m], 1)
|> select([m], m)
query = from(m in subquery(sub), where: not m.deleted, select: m)
item = @repo.one(query)
end
@doc """
Updates an item in the database.
Copies the current row, updates the relevant fields and appends
it to the database table.
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.update(%{age: 44})
"""
def update(%__MODULE__{} = item, attrs) do
item
|> @repo.preload(__MODULE__.__schema__(:associations))
|> Map.put(:id, nil)
|> Map.put(:inserted_at, nil)
|> Map.put(:updated_at, nil)
|> __MODULE__.changeset(attrs)
|> @repo.insert()
end
@doc """
Gets the full history of an item in the database.
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.get_history()
"""
def get_history(%__MODULE__{} = item) do
query =
from(m in __MODULE__,
where: m.entry_id == ^item.entry_id,
select: m
)
@repo.all(query)
end
@doc """
Gets all of the distinct instances of a schema that have not been deleted.
User.all()
"""
def all do
sub =
from(m in __MODULE__,
distinct: m.entry_id,
order_by: [desc: :inserted_at],
select: m
)
query = from(m in subquery(sub), where: not m.deleted, select: m)
@repo.all(query)
end
@doc """
Marks an item as deleted in the database.
Deleted items will not show up in queries.
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.delete()
"""
def delete(item) do
item
|> @repo.preload(__MODULE__.__schema__(:associations))
|> Map.put(:id, nil)
|> Map.put(:inserted_at, nil)
|> Map.put(:updated_at, nil)
|> __MODULE__.changeset(%{deleted: true})
|> @repo.insert()
end
@doc """
Preloads an item's (or list of items') multiple associations.
Also preloads any nested associations.
# Load all of a user's friends and comments
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.preload([:friends, :comments])
# Load all of a user's friends, and all of their friends' comments
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.preload([friends: [:comments]])
"""
def preload(item, assocs) when is_list(assocs) do
@repo.preload(
item,
Enum.map(assocs, fn a ->
preload_map(a, __MODULE__)
end)
)
end
@doc """
Preloads an item's (or list of items') association.
User.get("5ds4fg31-a7f1-2hd8-x56a-d4s3g7ded1vv2")
|> User.preload(:friends)
"""
def preload(item, assoc) do
@repo.preload(item, [{assoc, preload_query(assoc)}])
end
defp preload_map(assoc, owner) do
case assoc do
{k, v} ->
assoc_module = owner.__schema__(:association, k).queryable
{k, {preload_query(k, owner), Enum.map(v, fn a -> preload_map(a, assoc_module) end)}}
k ->
{k, preload_query(k, owner)}
end
end
@doc false
def preload_query(assoc, module \\ __MODULE__) do
sub =
from(mod in Map.get(module.__schema__(:association, assoc), :queryable),
distinct: mod.entry_id,
order_by: [desc: :inserted_at],
select: mod
)
from(m in subquery(sub), where: not m.deleted, select: m)
end
defp insert_entry_id(entry) do
case Map.fetch(entry, :entry_id) do
{:ok, nil} -> %{entry | entry_id: Ecto.UUID.generate()}
_ -> entry
end
end
defoverridable Alog
end
end
end