data:
fragment(
"""
- jsonb_set(?, '{repliesCount}',
+ safe_jsonb_set(?, '{repliesCount}',
(coalesce((?->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
""",
o.data,
data:
fragment(
"""
- jsonb_set(?, '{repliesCount}',
+ safe_jsonb_set(?, '{repliesCount}',
(greatest(0, (?->>'repliesCount')::int - 1))::varchar::jsonb, true)
""",
o.data,
_ -> :noop
end
end
+
+ @doc "Updates data field of an object"
+ def update_data(%Object{data: data} = object, attrs \\ %{}) do
+ object
+ |> Object.change(%{data: Map.merge(data || %{}, attrs)})
+ |> Repo.update()
+ end
end