go-saas / saas

go data framework for saas(multi-tenancy)
MIT License
253 stars 44 forks source link

Docs: Minimal example how to setup the tenancy modes #3

Closed mirusky closed 2 years ago

mirusky commented 2 years ago

Hello contributors, that was an incredible well done job. I was reading and it was better than what I did in some projects. But I getting confused with multi-tenant mode, like where I need to configure to get a Single-tenancy, Multi-tenancy, Hybrid mode?

I've saw this part but I dont know how it suppose to work, could u provide some examples with the 3 ways?

goxiaoy commented 2 years ago

The core part which impacts the database pattern is in the TenantStore interface https://github.com/Goxiaoy/go-saas/blob/5a4eb8da2153a394b7989aa314201cd5b70bfb41/common/tenant_store.go#L10-L13

In the test example https://github.com/Goxiaoy/go-saas/blob/5a4eb8da2153a394b7989aa314201cd5b70bfb41/gorm/sqlite_db_test.go#L71-L77

Tenant Test2 overrides additional connection string which affects the data access layer later https://github.com/Goxiaoy/go-saas/blob/5a4eb8da2153a394b7989aa314201cd5b70bfb41/common/multi_tenancy_conn_str_resolver.go#L39-L51

There is no need to explicit configure DatabaseStyleType, but it's a preserved option for further usage

mirusky commented 2 years ago

Alright, so if I understood, I could create a tenant store item with many connections and then it will be resolved by the MultiTenancyConnStrResolver... Something like this:

ts := common.NewMemoryTenantStore( 
    []common.TenantConfig{ 
        {ID: "1", Name: "Tenant1"}, 
        {ID: "2", Name: "Tenant2", Conn: map[string]string{ 
            "1": "connStr1", 
            "2": "connStr2", 
        }}, 
    }) 

How it will be resolved ? Like I reciveed a cookie with Tenant2 it will connect where ?

goxiaoy commented 2 years ago

For most of web application, you need to add a middleware to resolve current tenant

You can resolve it by cookie/ query string/ header/ domain fomat