SaaS- Tenant Specific Lookup Data in Shared Database

前端 未结 4 1948
野性不改
野性不改 2021-01-28 06:17

I am developing multitenant SaaS based application and going with Shared Database for storing all the tenant records with help of TenantId column.

Now the problem is i h

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 07:17

    This is a classic example of "many to many".

    Table: Games
    ------------
    GameID
    GameName
    IsMasterGame
    
    
    TennantGames
    ------------------
    GameID
    TennantID
    
    Tennants
    ------------
    TennantID
    ...
    

    To get the games for a given tennant, you would run a query like:

    select *
    from   Games
    where isMasterGame = true
    union
    select * 
    from Games g, 
    TennantGames tg
    where g.GameID = tg.GameID
    and   isMasterGame = false
    and   tg.TennantID = $currentTennant
    

    (Apologies for archaic join syntax)

    The union allows you to ask two questions: which games apply to everyone (isMasterGame = true), and secondly which games apply to the current tennant (tg.TennantID = $currentTennant). Logically, tennant games cannot also be master games.

提交回复
热议问题