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

how can I override table definition methods like where() and match() #93

Open
gargraman opened this issue Aug 24, 2023 · 1 comment
Open

Comments

@gargraman
Copy link

gargraman commented Aug 24, 2023

I am new to Elixir and we inhertited a project build on Amnesia. In order to make this multitenant, I am planning to override table definition methods like "where"
https://github.com/meh/amnesia/blob/master/lib/amnesia/table/definition.ex?#L11

I am thinking of putting extra condition of tenant_id at this. I am thinking of using where_tenant() method that adds this extra condition in User.where.
Could some help me in how can I write where_tenant() method ?

use Amnesia
defdatabase Test.Database do
deftable Message, [:user_id, :content], type: :bag do
def user(self) do
User.read(self.user_id)
end

def user!(self) do
  User.read!(self.user_id)
end

end

deftable User, [{ :id, autoincrement }, :name, :email], type: :ordered_set, index: [:email] do
end
end

defmodule DatabaseTest do
use ExUnit.Case
use Test.Database

alias Amnesia.Selection
alias Amnesia.Table.Stream
test "where works" do
Amnesia.transaction! do
%User{id: 1, name: "John"} |> User.write
%User{id: 2, name: "Lucas"} |> User.write
%User{id: 3, name: "David"} |> User.write
end

assert(Amnesia.transaction! do
  assert Selection.values(User.where_tenant(name == "John", select: id)) == [1]
  assert Message.where_tenant(name == "Richard") == nil
end 

end
end

@noizu
Copy link

noizu commented Aug 24, 2023

I added support for overriding methods in my fork.

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

No branches or pull requests

2 participants