zorn

zorn

Looking for advice on how best a module that manages a multi-tenant data backend should handle prefix values

I’m building a new module for my multi-tenant application and trying to come to terms how a prefix ideally be passed in. Here is a new style using an opts keyword list with a required prefix value. I like this since I could also use a preloads option for get style functions letting the caller decide which relationships should be preloaded; I think this preload style will come in handy for my GraphQL stuff. Anyways would love some API design feedback on this draft:

defmodule Guildflow.Invites do
  @moduledoc """
  Provides functions for creating invites that will be shared with prospective
  members. Using an invite a `Member` account can be created.
  """

  alias Guildflow.Invites.{Invite}
  alias Guildflow.Repo

  @expiration_length_in_seconds 60 * 60 * 24 * 7

  @doc """
  Generates and stores a fresh invite.
  ## Options
    * `:prefix` - The database prefix to use for the insert. (required)
    * `:preloads` - a list of relationships that will be preloaded on the
      returned records. (demo of an option that would be on a get style
      function)
  """
  @spec create_invite(keyword) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
  def create_invite(opts \\ []) do
    expires_on =
      DateTime.utc_now()
      |> DateTime.add(@expiration_length_in_seconds, :second)
      |> DateTime.truncate(:second)

    changeset =
      Invite.changeset(%Invite{}, %{
        "token" => UUID.uuid4(),
        "expires_on" => expires_on
      })

    Repo.insert(changeset, prefix: prefix_from_options(opts))
  end

  def register_member_from_invite(_invite) do
  end

  defp prefix_from_options(opts) do
    case Keyword.get(opts, :prefix, []) do
      nil ->
        throw("required prefix missing")

      prefix ->
        prefix
    end
  end
end

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement