Multi-tenancy or at least hooks to modify queries on the fly? #992
Replies: 3 comments 3 replies
-
|
I found https://www.sea-ql.org/SeaORM/docs/generate-entity/expanded-entity-structure/#active-model-behavior. I would like to have a Like https://bun.uptrace.dev/guide/hooks.html#model-query-hooks. (@billy1624 sorry for the tag: is it already possible today someway?) |
Beta Was this translation helpful? Give feedback.
-
|
I am thinking that I could create a Trait perhaps called " But I would like to understand how to hook the hooks of the various actions (SELECT but also UPDATE / DELETE) to these "tenanted" entities. |
Beta Was this translation helpful? Give feedback.
-
|
Hey @frederikhors, welcome and sorry for the delay. I saw your message on Discord as well. Temporary solution: I'd suggest having a method to apply the condition on your select, update and delete. #[derive(Debug, sea_query::Iden)]
pub struct Tenanted;
fn filter_tenanted<Q>(query: Q, tenant_id: i32) -> Q
where
Q: QueryFilter
{
query.filter(Expr::col(Tenanted).eq(tenant_id))
}filter_tenanted(Entity::find(), tenant_id)
filter_tenanted(Entity::update_many(), tenant_id)
filter_tenanted(Entity::delete_many(), tenant_id)In the long-run: I agree we should add hook to alter the query. Let me think and come back to you in a bit. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I'm new to Rust. I'm finally starting to write something with this amazing tech! And I would like to try SeaORM.
I cannot find in Docs a way to have what in other techs are called "hooks". Nor I can find a way to handle multi-tenancy.
My need is to use
tenant_idcolumns in (almost) each table of the DB (Postgres in this case).So I need a way to say to the ORM to add a
WHERE tenant_id = [current_user.tenant_id]to each query is created.I know there are other solutions like RLS but for now we have to stick with this old legacy DB design and architecture.
Is there an easy way to do this with SeaORM?
Beta Was this translation helpful? Give feedback.
All reactions