1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Repo do
8 adapter: Ecto.Adapters.Postgres,
9 migration_timestamps: [type: :naive_datetime_usec]
11 defmodule Instrumenter do
12 use Prometheus.EctoInstrumenter
16 Dynamically loads the repository url from the
17 DATABASE_URL environment variable.
20 {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
23 @doc "find resource based on prepared query"
24 @spec find_resource(Ecto.Query.t()) :: {:ok, struct()} | {:error, :not_found}
25 def find_resource(%Ecto.Query{} = query) do
26 case __MODULE__.one(query) do
27 nil -> {:error, :not_found}
28 resource -> {:ok, resource}
32 def find_resource(_query), do: {:error, :not_found}
35 Gets association from cache or loads if need
39 iex> Repo.get_assoc(token, :user)
43 @spec get_assoc(struct(), atom()) :: {:ok, struct()} | {:error, :not_found}
44 def get_assoc(resource, association) do
45 case __MODULE__.preload(resource, association) do
46 %{^association => assoc} when not is_nil(assoc) -> {:ok, assoc}
47 _ -> {:error, :not_found}