Elixir ATProtocol ingestion and sync library.
1defmodule Drinkup.Tap.Event.Record do
2 @moduledoc """
3 Struct for record events from Tap.
4
5 Represents create, update, or delete operations on records in the repository.
6 """
7
8 use TypedStruct
9
10 typedstruct enforce: true do
11 @type action() :: :create | :update | :delete
12
13 field :id, integer()
14 field :live, boolean()
15 field :rev, String.t()
16 field :did, String.t()
17 field :collection, String.t()
18 field :rkey, String.t()
19 field :action, action()
20 field :cid, String.t() | nil
21 field :record, map() | nil
22 end
23
24 @spec from(map()) :: t()
25 def from(%{
26 "id" => id,
27 "type" => "record",
28 "record" =>
29 %{
30 "live" => live,
31 "rev" => rev,
32 "did" => did,
33 "collection" => collection,
34 "rkey" => rkey,
35 "action" => action
36 } = record_data
37 }) do
38 cid = Map.get(record_data, "cid")
39 record = Map.get(record_data, "record")
40
41 %__MODULE__{
42 id: id,
43 live: live,
44 rev: rev,
45 did: did,
46 collection: collection,
47 rkey: rkey,
48 action: parse_action(action),
49 cid: cid,
50 record: record
51 }
52 end
53
54 @spec parse_action(String.t()) :: action()
55 defp parse_action("create"), do: :create
56 defp parse_action("update"), do: :update
57 defp parse_action("delete"), do: :delete
58end