SaaS- Tenant Specific Lookup Data in Shared Database

烈酒焚心 提交于 2019-12-02 07:50:00
DaveMorganTexas

This is the design I would then use.

Games

Id
GameName
IsTenantSpecific
SomeGameSpecificColumn

TenantGames

GameId
TenantId
SomeTenantSpecificColumn
AnotherTenantSpecificColumn

Then you can query that table in a Join with:

...
FROM
    Games
    INNER JOIN UserGames ON
        UserGames.GameId = Games.Id
    LEFT JOIN TenantGames ON
        TenantGames.GameId = Games.Id
WHERE
    TenantGames.TenantId = @tenantId OR
    (
        TenantGames.TenantId IS NULL AND
        IsTenantSpecific = 0
    )

Game specific fields can be put in the Games table. Tenant specific fields can be added to the TenantGames table, and those fields will be NULL if it is not a tenant specific customization.

We have a saas based database and we keep common data and tenant data in the same table.

Concept

GamesTable

    Id NOT NULL
    TenantId NULL
    GameName NOT NULL
    Add a unique key for TenantId and GameName
  • if TenantId is NULL you know it is common data
  • if TenantId is NOT NULL you know it belongs to a specific tenant and who exactly.

"Is there any other DB design which allows me to do mix both common master data and tenant master data with JOIN?"

Yes

SELECT *
  FROM GamesTable where TenantId = 'your tenant id'
  UNION
SELECT *
  FROM GamesTable where TenantId IS NULL  -- common 

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.

You can merge the tables leaving TenantId as NULL for records you wish to not be Tenant specific.

Games

Id
TenantId
GameName

The you can query that table in a Join with:

...
FROM
    Games
    INNER JOIN UserGames ON
        UserGames.GameId = Games.Id

WHERE
    Games.TenantId = @tenantId OR
    Games.TenantId IS NULL

This will save you the trouble of ensuring that the Id is unique between the tables, unless you are using a UNIQUEIDENTIFIER for the Id.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!