API Reference
indexed_fields

def indexed_fields(*field_names: str) -> Callable[[type], type]

Open SourceStandard

Declare memo fields whose current values should be synchronized into managed indexes.

Use @db0.indexed_fields(...) with @db0.memo when a field should support fast range queries or sorting without maintaining a separate db0.index().

import dbzero as db0
 
@db0.indexed_fields("priority", "due_at")
@db0.memo
class Task:
    def __init__(self, title, priority, due_at=None):
        self.title = title
        self.priority = priority
        self.due_at = due_at

Parameters

  • *field_names str
    Field names to index. Duplicate names are ignored. Passing no names has no effect.

Returns

The decorated class.


Behavior

Each declared field gets one dbzero-managed index. The current field value is the index key and the containing memo instance is the indexed value.

priority = db0.index_of(db0.fields_of(Task).priority)
 
Task("Deploy", 1)
Task("Clean backlog", 5)
 
high_priority = db0.find(Task, priority.select(1, 2))
ordered = priority.sort(db0.find(Task))

dbzero synchronizes the managed index during initialization, assignment, deletion of the field, and deletion of the object. Each field update and index update is atomic.

Managed indexed-field indexes are passive and read-only to application code. Use them with an anchored query such as db0.find(Task, index.select(...)).


Decorator Composition

db0.indexed_fields works before or after @db0.memo:

@db0.indexed_fields("priority")
@db0.memo
class Task:
    pass
 
@db0.memo
@db0.indexed_fields("priority")
class Job:
    pass

Multiple declarations on the same class are merged:

@db0.indexed_fields("due_at")
@db0.memo
@db0.indexed_fields("priority")
class Task:
    pass

This is equivalent to @db0.indexed_fields("priority", "due_at").


Migration

Adding, removing, or renaming indexed-field declarations is a schema change. dbzero can migrate the managed indexes automatically, or you can disable automatic migration with no_auto_migrate=True and apply it with db0.migrate(...).

db0.init("/var/lib/my-app/dbzero", no_auto_migrate=True)
 
try:
    Task("new task", 2)
except db0.MigrateError:
    db0.migrate(Task)

If migration fails, the previous declaration and indexes remain active. A field rename registered with db0.rename_field(...) preserves the indexed-field identity.

See Indexed Fields for the practical guide.