Uni Ecto Plugin Page
When you call set_tenant_prefix("customer_123"), the uni_ecto_plugin modifies the Process Dictionary.
public schema (usually for admin dashboards).field :uuid, UniEctoPlugin.Types.UniID
field :meta, UniEctoPlugin.Types.UniJSON
MyApp.Repo.upsert_uni(changeset, conflict_target: [:uuid], on_conflict: :update_all)
from(u in User, where: uni_match(u.meta, ^filters))
|> MyApp.Repo.all()
step = Ecto.delete(struct_or_changeset)
Returns :ok, deleted_struct on success.
Let’s get our hands dirty. Add the necessary dependencies to your mix.exs: uni ecto plugin
defp deps do
[
:ecto_sql, "~> 3.0",
:uni, "~> 1.0",
:uni_ecto, "~> 0.5" # The Uni Ecto Plugin
]
end
Run mix deps.get.
Uni provides Uni.on_error/2 to rescue failed steps: Fallback Logic: If no tenant is set, queries
Uni.new()
|> Ecto.insert(changeset)
|> Uni.on_error(:insert, fn _step, error, ctx ->
if error.reason == :invalid do
# Log and return a default
:ok, %MyApp.Useremail: "fallback@example.com"
else
:error, error
end
end)
This is where the magic happens. You create a service that wraps the client, handling the reactive stream. If you are using a virtual machine interface (like JInterface) rather than HTTP, the plugin would handle the translation of the BEAM term to a Java object. field :uuid, UniEctoPlugin
@ApplicationScoped
public class EctoProxyService
@Inject
UserClient client;
public Uni<UserDTO> fetchUser(String id)
// This returns a Uni immediately, keeping the thread free
return client.getUser(id)
.onFailure().recoverWithItem(fallbackUser);