Packages
potionx
0.2.1
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.1.1
0.1.0
Potionx is a set of generators and functions that speeds up the process of setting up and deploying a full-stack application that uses Elixir with GraphQL for the server-side component and Vue for the frontend component.
Current section
Files
Jump to
Current section
Files
lib/potionx/repo.ex
defmodule Potionx.Repo do
@tenant_key_org {:potionx, :organization_id}
@tenant_key_user {:potionx, :user_id}
defmacro __using__(opts) do
quote do
require Ecto.Query
@scoped_by_organization unquote(opts[:scoped_by_organization]) || []
@scoped_by_user unquote(opts[:scoped_by_user]) || []
def default_options(_operation) do
[org_id: Potionx.Repo.get_org_id(), user_id: Potionx.Repo.get_user_id()]
end
def prepare_query(_operation, %{from: %{source: {_, model}}} = query, opts) do
cond do
opts[:schema_migration] ->
{query, opts}
Enum.member?(@scoped_by_organization, model) and is_nil(opts[:org_id]) ->
raise "expected organization_id to be set"
Enum.member?(@scoped_by_user, model) and is_nil(opts[:user_id]) ->
raise "expected user_id to be set"
true ->
[
{:user_id, @scoped_by_user},
{:organization_id, @scoped_by_organization}
]
|> Enum.reduce({query, opts}, fn {key, list}, {q, opts} ->
if Enum.member?(list, model) do
{
q |> Ecto.Query.where(^[{key, opts[key]}]),
opts
}
else
{q, opts}
end
end)
true ->
raise "expected org_id or skip_org_id to be set"
end
end
end
end
def get_org_id() do
Process.get(@tenant_key_org)
end
def get_user_id() do
Process.get(@tenant_key_user)
end
def put_org_id(org_id) do
Process.put(@tenant_key_org, org_id)
end
def put_user_id(user_id) do
Process.put(@tenant_key_user, user_id)
end
end