Impelementing multi tenant database solution with Yesod persistent

11 views
Skip to first unread message

Juuso Vuorinen

unread,
Feb 9, 2024, 2:23:02 AM2/9/24
to Yesod Web Framework
Hi!

I am trying to figure out how to rewrite/change/utilize functions such as selectList (and others), so that it would by default add a where statement such as ..."where company_id = 3". This would be applied to all tables that need to have their records being shared by multiple tenants referred to by company_id or equivalent.  

The type of the selectList function is as follows: 

selectListSecure :: forall record backend (m :: * -> *). (MonadIO m, PersistQueryRead backend,
 PersistRecordBackend record backend) => [Filter record] -> [SelectOpt record] -> ReaderT backend m [Entity record]

I was wondering if I could somehow figure out the type of the record in my own version of the function, so that I could add a new filter such as [MyId ==. myid] to the filter and let the db action happen after that? 

Somehow I have the feeling that we cannot figure out (use pattern matching) to get that type out and create the filters in a function with such a signature, but be need to do something else? How would you do it?

Regards

Juuso 

Regards

Juuso

Juuso Vuorinen

unread,
Feb 12, 2024, 6:31:10 AM2/12/24
to yeso...@googlegroups.com
I ended up doing it this way but wonder if there were a more elegant solution available? 

selectList'
    :: forall record backend . (PersistQueryRead backend, PersistRecordBackend record backend)
    => EntityField record (Key Company) -> [Filter record]
    -> [SelectOpt record]
    -> ReaderT backend Handler [Entity record]

selectList' tenantIdField filts opts = do

    c <- liftHandler getCompanyId
    let g =  Filter    { filterField  = tenantIdField
                       , filterValue  = FilterValue  c
                       , filterFilter = Eq }
    selectList (g:filts) opts

--
You received this message because you are subscribed to the Google Groups "Yesod Web Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yesodweb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/yesodweb/adba6cd0-f824-4dcc-87a4-5630a709f29dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages