-
Notifications
You must be signed in to change notification settings - Fork 21
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
Discussion: Current tenant and user for the domain API #28
Comments
i'd go with explicit arguments, but that's lack of experience. it seems like every method will need these facts, so tucking them away in a reader seems like the Haskell way of doing things. |
I believe with a little work you can make Servant automatically pass these around for you as arguments to the handlers that require them. |
With the example We can use IO to check with the DB and return a user type or any type we prefer and this will be automatically passed to the handler as the first argument. We are using it and it works! Servant documentation explains clearly with code examples. |
I would create a user Here we can create multiple system users with different role and permissions so that we can track system actions and also define restrictions for each system user too. btw.. I hope you will be ok to open another ticket to discuss security! |
So, when I say domain API I mean the Haskell library that implements actions, like Functions in the domain API may need the current user and tenant for the following:
I can think of three ways to do this: Option 1: Explicit arguments
Simplest to implement. However, tedious to pass around a Option 2a: Reader monad with the entire "request context" as the reader env
The obvious advantage is that one doesn't have to pass around a
Option 2b: Monad with type-class contraints
Reference for this idea: https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/ Actually, I'm not sure if using type-class constraings for Option 3: Implicit parameters
More about implicit paramaters extension: https://ocharles.org.uk/blog/posts/2014-12-11-implicit-params.html Does this really give any advantages over a PS: Updating this issue's description with this comment. |
Domain API refers to the Haskell library that implements actions, like
createTenant
,createProduct
, etc. Functions in the domain API may need the current user and tenant for the following:There are three possible ways to implement this:
Option 1: Explicit arguments
Simplest to implement. However, tedious to pass around a
tenant
anduser
to every single function in the domain API.Option 2a: Reader monad with the entire "request context" as the reader env
The obvious advantage is that one doesn't have to pass around a
user
andtenant
value to every domain function. Our domain API will anyways be in aReaderT
transformer stack (to be able to access things like the dbpool or the logger) and this integrates nicely with it. However, due to two edge cases we need to have aMaybe User
andMaybe Tenant
, (instead of a regularUser
and `Tenant):createTenant
which logically can not have auser
andtenant
valuecretaeUser
which logically may have auser
value only if it's NOT the first user of the tenant being created.Option 2b: Monad with type-class contraints
Reference for this idea: https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/
Actually, I'm not sure if using type-class constraings for
HasTenant
andHasUser
is bringing any advantages to the table here. The one idea that can be merged with Option 2a is to parameterize theRequestContext
with tenant and user so that we can run thecreateTenant
andcreateFirstUser
functions in a monad where user & tenant are unit types()
easily, thus avoidingMaybe User
andMaybe Tenant
easily.Option 3: Implicit parameters
More about implicit paramaters extenstion: https://ocharles.org.uk/blog/posts/2014-12-11-implicit-params.html Does this really give any advantages over a
ReaderT
monad?The text was updated successfully, but these errors were encountered: