Question: Would you consider global scopes for models?
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 38/100
Research direction
Start with crates/database/src/model/query.rs and trace how __query::<M>() feeds the model query methods, relation queries, eager loads, and destroy. Read the discussions in #387, #413, and #431 to understand the related design constraints, especially disabling scopes and handling missing scope values. Done means the scope mechanism, tests, and docs cover the agreed behavior and its limits.
Written by the indexing model from the issue text.
Description
Would you be open to someone adding global scopes, as in Eloquent? I know there are a lot of open PRs right now, so I wanted to ask before writing any code.
I'm building a multi-tenancy package for Laravel for Rust. Database-per-tenant works well with db::using_connection and add_connection. Single-database tenancy is the part I can't make safe. Scopes are opt-in today, so one forgotten filter returns every tenant's rows. Project::find(id) with another tenant's ID returns that row. A package can't fix this, because it can't change the functions that #[model] generates.
From what I can see, the change would be small. Almost every model query starts in __query::<M>() in crates/database/src/model/query.rs. query() calls it, and all, find, find_or_fail, find_many, and first_where go through query(). Relation queries, eager loads, and destroy call it directly. I checked this against the 0.1.2 source, and #387 uses the same spot for its soft delete condition.
Here is roughly what I have in mind. It follows #[model(hooks)], so models without the option would behave exactly as they do now.
#[model(scope)]
pub struct Project {
pub id: i64,
pub tenant_id: i64,
pub name: String,
}
impl Scope for Project {
fn scope(query: Builder<Self>) -> Builder<Self> {
query.filter(Project::tenant_id.eq(current_tenant_id()))
}
}
__query::<M>() would then return M::scope(query).
There are two things I'm not sure about. The first is how central code, like an admin screen, should turn a global scope off. with_trashed in #387 and model::without_events both look like reasonable models for that. The second is what should happen when a scope needs a value that isn't there, such as the current tenant. __query returns a builder and not a Result, so the scope would need some way to fail when the query runs.
#431 makes a fair point that global scopes don't cover raw db::query! calls, db::table, or insert and upsert. That's true, but a filter that covers every model query and a filter that covers every query are two different things. Eloquent's global scopes have the same limit, and the single-database mode of stancl/tenancy is still built on them. I think the limit is fine as long as the docs state it plainly.
It may be worth deciding this together with #387, since soft deletes could be one built-in global scope on the same mechanism. #413 is a separate problem with models in queued jobs. I mention it only because it affects the same use case.
Would you want a PR for this? I'd keep it to the mechanism alone, with tests and docs.
- Dominant language
- Rust
- Stars
- 847
- Forks
- 174
- Avg merge
- 5h 40m
- Merged PRs (30d)
- 167
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust-framework
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
laravel/rust-framework#876 ·
Maintainers usually reply within 1 day
-
Case-sensitive contains, starts_with and ends_with that match % and _ literally on every databaseOpen
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#874 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
laravel/rust-framework#873 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#872 ·
Maintainers usually reply within 1 day
All issues in laravel/rust-framework
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days