def predicate(*query_criteria: Union[Tag, List[Tag], Tuple[Tag], QueryObject, TagSet], prefix: Optional[str] = None) -> QueryObject
dbzero-procommercial edition
Build a predicate-only query expression for composition and data filtering.
db0.predicate(...) accepts the same criteria grammar as db0.find(...), but the returned query object is not meant to expose matching objects directly. Use it as an authorization predicate for data_filter, or pass it into db0.find(...) as a reusable query constraint.
Parameters
-
*query_criteriaUnion[Tag, List[Tag], Tuple[Tag], QueryObject, TagSet]
Variable number of criteria using the same grammar asfind():- Type: a memo class to match by type
- String tag: a simple tag
- Object tag: a memo object used as a tag
- Composite tag: a structured tag created with
db0.as_tag(...) - List of tags (OR): objects with at least one of the listed tags
- Tuple of tags (AND): objects with all listed tags
- QueryObject: another query expression
- TagSet: a logical operation such as
db0.no(...)
-
prefixstr, optional
Optional data prefix for the predicate. Use this when the predicate must be resolved in a specific prefix.
Returns
A predicate-only QueryObject.
Predicate objects cannot be directly iterated, counted, truth-tested, indexed, or sliced. These operations raise PermissionError so predicate construction cannot leak protected data.
Examples
Simple Grant Predicate
Use a predicate as a reusable query constraint:
grant = db0.predicate("grant")
visible_documents = list(db0.find(Document, grant))Tenant Predicate
Use a predicate as the active data-filter predicate for row-level security:
from contextvars import ContextVar
filter_predicate = ContextVar("filter_predicate")
tenant_id = "tenant-a"
filter_predicate.set(
db0.predicate(db0.as_tag("TENANT", tenant_id))
)
documents = list(db0.find(Document))Grant and Deny Predicate
Predicates can combine OR lists, composite tags, and negative predicates:
filter_predicate.set(
db0.predicate(
[
db0.as_tag("GRANT", account),
db0.as_tag("GRANT", role),
],
db0.no(db0.as_tag("DENY", account)),
)
)Serialization
Predicate objects can be serialized and deserialized, then used in another query:
predicate_bytes = db0.serialize(db0.predicate("grant"))
grant = db0.deserialize(predicate_bytes)
documents = list(db0.find(Document, grant))Direct Access Is Blocked
grant = db0.predicate("grant")
# Raises PermissionError.
list(grant)See Security for data filtering setup with db0.init(..., data_filter={...}).