Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context Multitenancy for sqlite #127

Open
firesidewing opened this issue Feb 8, 2025 · 1 comment
Open

Context Multitenancy for sqlite #127

firesidewing opened this issue Feb 8, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@firesidewing
Copy link

Was spinning up a project and was going to do a database per organization with sqlite, as that combination seems very appealing for my use case but I got

** (Spark.Error.DslError) multitenancy -> strategy:
  Data layer does not support multitenancy

Postgres just seems too heavy weight for what i want to do, and i don't really want to have to deal with setting up a cloud database instance, was hoping to just keep it simple with sqlite. Was wondering if this is on the roadmap at all?

@firesidewing firesidewing added the enhancement New feature or request label Feb 8, 2025
@zachdaniel
Copy link
Contributor

Per-database multi tenancy is something we'd like to support both in postgres & sqlite, but at the moment it's something you'd effectively need to roll yourself. You can set the repo in the context:

defmodule YourChange do
  use Ash.Resource.Change

  def change(changeset, _, _) do
    Ash.Changeset.set_context(changeset, %{data_layer: %{repo: compute_repo(changeset.tenant)}})
  end

  def atomic(changeset, opts, context) do
    {:ok, change(changeset, opts, context)}
  end
end

defmodule YourPreparation do
  use Ash.Resource.Preparation

  def prepare(query, _, _) do
    Ash.Query.set_context(query, %{data_layer: %{repo: compute_repo(query.tenant)}})
  end
end

You could then add this to each resource's global change & preparation blocks:

changes do
  change YourChange, on: [:create, :update, :destroy] # destroy is off by default
end

preparations do
  prepare YourPreparation
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants