How to switch the database connection at runtime (Multi-tenancy) #1108
-
|
Hi, I just started using SQLC and this seems promising! It really helps in separating the boiler plate code with strong typed methods! Now I do have a question: How should I implement switching the database connection at runtime? What are your ideas? A small example would be really helpful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Each database pool should be associated with a single type Tenant struct {
db *sql.DB
q *Queries
}
pools := map[string]Tentant{}Obviously replace The other advantage of this approach is that you can use |
Beta Was this translation helpful? Give feedback.
Each database pool should be associated with a single
Queriesinstance. If you already have a map of pools, I'd change the map struct to be a db pool / queries pair like this:Obviously replace
stringin this example with the type for your tenant ID.The other advantage of this approach is that you can use
Prepareto create prepared queries for each tenant database.