Description
This is pretty much just a small quality-of-life improvement.
That being said, I'd like to be able to access IDocumentSchemaProvider
from within extension methods that extend IQueryable
and mix LINQ functions with raw SQL. Best shown with an example:
public static IQueryable<T> WhereAccountHasPermission<T>(
this IQueryable<T> query,
IDocumentSchemaResolver schema,
Permission permission,
Guid accountId
) where T : IEntity
{
var sql = $"""
(
SELECT
(perms.data -> 'AccountEntries' -> ? -> 'EffectivePermission')::int
FROM {schema.For<EntityPermissionInfo>()} AS perms
WHERE perms.id = d.id
)::int & ? = ?
""";
return query.Where(e => e.MatchesSql(
sql,
accountId.ToString(),
(int)permission,
(int)permission));
}
The extension method above applies a where clause that filters documents of type T
based on whether a user with accountId
has the correct permission flags for each of the documents. To do this, it needs to access documents of type EntityPermissionInfo
. The type's table doesn't necessarily have to be in the public
schema, so we have to use IDocumentSchemaResolver.For
.
Right now, we pass the resolver each time we call the method, which is a bit cumbersome. Especially if I consider that Marten's queryable has a reference to a query session, from which one can navigate to the schema resolver.
The easiest thing to do would be to just make IMartenLinqQueryable
public, then I could just check if the queryable implements it and use the query session within. I'm fine with making a PR for it, but I have no idea if it would break anything else, so I'm opening this issue instead.
Activity