woylie / flop

Filtering, ordering and pagination for Ecto
MIT License
666 stars 36 forks source link

Multi-tenancy through setting prefix #442

Closed jellevdp closed 10 months ago

jellevdp commented 10 months ago

Is your feature request related to a problem? Please describe. I have created a mulit-tenancy application in which I segregate data per user group in different schemas. With ecto I can do this by using the prefix. For example, a query for all products looks like this:

  def list_products(tenant) do
    query = from p in Product, preload: [:producer]
    Repo.all(query, prefix: tenant)
  end

I would like to use Flop but it seems that I can only set the prefix in the query_opts in adapter_opts in the Flop.Schema, based on this changelog for v0.22.

Describe the solution you'd like I would like to add a prefix per query in order to dynamically change the tenant per query. Perhaps by allowing for adding query options as an option to validate_and_run. For instance:

  def list_products(params, tenant) do
    query = from p in Product, preload: [:producer]

    query
    |> Flop.validate_and_run(params, for: Product, prefix: tenant)
  end

Additional context This is the first time I'm trying out Flop, so not too familiair with the package and its API. It might very well be that there is a way of achieving this, if the case, I'd be happy to hear and close this issue asap.

woylie commented 10 months ago

You can pass the prefix and other query opts directly to the functions:

Flop.validate_and_run(query, params, for: Product, query_opts: [prefix: tenant])