ExTenant (ExTenant v0.1.0) View Source

by calling the use macro we should be able to inject all the required behaviour to allow us to handle multi-tenancy - most of it at least - apart from

(1) placing the tenant_id into the process dictionary in Phoenix

--> we should create a `use macro` for a plug for this

(2) injecting the tenant_id into the changeset for creating & updating structs like this

def changeset_with_tenant_id_from_process(params \ :empty) do
  params 
  |> Repo.inject_tenant_id()
  |> default_changeset()
end

defp default_changeset(params) do
  %__MODULE__{}
  |> cast(params, [:name, :body, :tenant_id])
end 

Link to this section Summary

Functions

generate a function signature that does this..

generate a function signature that does this.. @tenant_key is handled through the incoming attrs

generate a function signature that does this..

generate a function signature that does this

generate a function signature that does this.. @tenant_key is handled through the incoming attrs

Link to this section Functions

Link to this macro

generate_default_options_callback()

View Source (macro)

generate a function signature that does this..

def default_options(_operation) do

[tenant_id: get_tenant_id()]

end

Link to this macro

generate_get_tenant_function(tenant_key)

View Source (macro)

generate a function signature that does this.. @tenant_key is handled through the incoming attrs

def get_tenant_id() do

Process.get(@tenant_key)

end

Link to this macro

generate_inject_tenant_id_callback(tenant_field)

View Source (macro)

generate a function signature that does this..

def inject_tenant_id(params) do

params    
|> params_as_string_based()
|> Map.put("tenant_id", get_tenant_id())

end

Link to this macro

generate_prepare_query_callback()

View Source (macro)

generate a function signature that does this

def prepare_query(operation, query, opts) do

inspect_query_data(operation, query, opts)

cond do
  opts[:skip_tenant_id] || opts[:schema_migration] ->
    {query, opts}
  tenant_id = opts[:tenant_id] ->
    {Ecto.Query.where(query, tenant_id: ^tenant_id), opts}
  true ->
    raise ExTenant.TenantNotSetError
end

end

Link to this macro

generate_put_tenant_function(tenant_key)

View Source (macro)

generate a function signature that does this.. @tenant_key is handled through the incoming attrs

def put_tenant_id(tenant_id) do
  Process.put(@tenant_key, tenant_id)
end