Hi all, I’m new to Prisma and love it :) (used to ...
# orm-help
s
Hi all, I’m new to Prisma and love it :) (used to work with Sequelize). I wanted to add multi-tenancy support (mainly for SaaS products) using a shared database strategy, meaning, one database and on each table, a tenant ID column. The idea is that from a developer point of view, there is no need to worry about adding a where clause to each CRUD operation, we will do it for him/her. As I see it, there were 3 challenges: 1. Extracting the tenant id from a request/message - I’ve added an express middleware for that. 2. Storing the tenant id in a way switching context in an async operations won’t affect it - solve it with async local storage. 3. Making sure each database query is with a
where
clause and with the proper tenant ID - I’ve added a Prisma middleware for that. I’ve wrote (me and @yakirgot) a new npm package for all of it here, and the code is here. I have two questions: 1. in different languages, I used to work with database hooks or scope to get this functionality and I wonder if middleware is the right approach in prisma. Am I missing anything? 2. My
where
clause validation is pretty simple and straightforward, I would love to get your opinion on it. PS, you are all more than welcome to contribute, there is still much work needed :) Thanks in advance, Shaul
a
This seems like a good idea. I have multitenant db but so far I've managed it all manually. Right now only some tables in my model contain a reference to the tenant (because I use another relation to navigate to the owner of the row/object). Do you think it is better to have it explicitly on all tables?
For example, let's say the
User
model has an
organizationId
field and also a
posts
. We don't need to relate
Post
directly to
Organization
because
User
already has that information. I've struggled with the idea of adding organizationId / tenantId to every table, seems kind of repetitive, doesn't it? 🤔
s
Hi @Alex Vilchis I don’t think you need to have it explicitly. In my package you set the tenant id column name (the default is account_id) and the package only add the where clause for tables that has that column.