def index_of(field: FieldRef, *, prefix: str | None = None) -> IndexObject
def index_of(memo_type: type, field_name: str, *, prefix: str | None = None) -> IndexObject
Open SourceStandard
Return the managed query index for an indexed memo field.
The preferred form passes a field reference from
db0.fields_of(...):
priority = db0.index_of(db0.fields_of(Task).priority)The string form is also supported:
priority = db0.index_of(Task, "priority")Parameters
-
fieldFieldRef
A field reference returned bydb0.fields_of(Type).fieldordb0.fields_of(Type)["field"]. -
memo_typetype
A dbzero memo type. -
field_namestr
Name of an indexed field onmemo_type. -
prefixstr, optional
Prefix to resolve the managed index in. If omitted, dbzero uses the current prefix, or the fixed prefix for a scoped memo type.
Returns
An IndexObject sealed for query use.
Examples
@db0.indexed_fields("priority")
@db0.memo
class Task:
def __init__(self, title, priority):
self.title = title
self.priority = priority
Task("Fix outage", 1)
Task("Update docs", 4)
priority = db0.index_of(db0.fields_of(Task).priority)
urgent = db0.find(Task, priority.select(1, 2))
ordered = priority.sort(db0.find(Task))Use subscription for dynamic field names:
field_name = "priority"
priority = db0.index_of(db0.fields_of(Task)[field_name])Managed Index Operations
The returned index supports query operations:
select(low=None, high=None, null_first=False)sort(query, desc=False, null_first=False)len(index)
Mutation operations are rejected because dbzero owns the managed index:
add(...)remove(...)clear()flush()
Indexed-field indexes are passive. Join select(...) with a positive predicate,
for example db0.find(Task, priority.select(1, 2)).
Errors
db0.index_of(...) raises an error when:
- the type is not a memo type
- the field name is unknown or not declared with
@db0.indexed_fields(...) - a
FieldRefis combined with a separatefield_name - an explicit
prefixconflicts with a fixed-prefix memo type - the caller does not have read access to a protected field
See Indexed Fields for migration and design guidance.