Errorname / prisma-multi-tenant

🧭 Use Prisma as a multi-tenant provider for your application
MIT License
395 stars 45 forks source link

Allow tenant connection strings to be aliased #67

Open jimsimon opened 4 years ago

jimsimon commented 4 years ago

The current approach requires storing usernames and passwords for the database connection strings in plain text which isn't great for security. A better approach might be to use an alias and environment variables for retrieving the connection strings.

Tenant Table:

NameAlias
Small Cool Tenantdb1
Small Awesome Tenantdb1
Large Tenantdb2

.env

DB1_URL=postgres://user:password@db1:5432/myapp
DB2_URL=postgres://user:password@db2:5432/myapp

This could be done in a backwards compatible way by making the current URL column nullable and adding a new alias column. Then use URL if it's present and fall back to alias. The CLI would just need to ensure that one of the two are provided, and a check constraint could be used on the table to ensure it never ends up in a situation where both columns are null.

Errorname commented 4 years ago

That's a good idea! Feel free to make a PR to add this feature in pmt

m-leon commented 4 years ago

Adding on to this, I think it would be beneficial to allow us to resolve the URL with a function. This would allow us to create our own adapters to the URLs.

Environment variables could be resolved with this: (alias) => process.env[alias]

Errorname commented 4 years ago

The issue with resolving URL with a function is that other commands of pmt (e.g: migrate, studio,...) would not work.

However, if you only want the pmt client, you could use the .directGet method instead of .get. (See documentation)