I would like to develop a Saas application that is based on multi-tenancy. Each customer should have their own database to ensure general performance and security. I would like to implement the whole thing in NextJS, Prisma and Postgresql. How is it possible to retrieve the databases depending on the user?
Do you have a good approach or have you already gained experience with it?
I set this up, but with MongoDB (probably a little trickier than SQL and I did consider switching from NoSQL for that reason). I use a session token in the form of a cookie to store the tenant id upon login. The client does have to enter a company id on login that gets validated and maps to the tenant id. Other approaches I tried was a subdomain and searchParam, both seemed a little too haphazard and required additional client side logic and middleware that got a little exhausting.
Once you're able to get the tenant name from the cookie, you can use it establish a connection or retrieve a database. Generally these links are stored in separate database from your clients with a CompanyID, DBName type schema.