"""Pagination documents matching the filter.
Pagination is used to separate long sets of data so
that it is easier for a user to consume information.
The search is based on the effect of a quantum loop.
The search effectiveness depends on the number of processor threads.
"""
import anyio
from typing import Annotated
from pydantic import Field
from scruby import Scruby, ScrubyModel
from pprint import pprint as pp
class Car(ScrubyModel):
"""Car model."""
brand: Annotated[str, Field(frozen=True)]
model: Annotated[str, Field(frozen=True)]
year: int
power_reserve: int
# key is always at bottom
key: Annotated[
str,
Field(
frozen=True,
default_factory=lambda data: f"{data['brand']}:{data['model']}",
),
]
async def main() -> None:
"""Example."""
# Activate database.
Scruby.run()
# Get collection `Car`.
car_coll = Scruby(Car)
# Create cars.
for num in range(1, 10):
car = Car(
brand="Mazda",
model=f"EZ-6 {num}",
year=2025,
power_reserve=600,
)
await car_coll.add_doc(car)
# Pagination.
car_list: list[Car] | None = car_coll.find_many(
filter_fn=lambda doc: doc.brand == "Mazda",
limit_docs=5,
page_number=2,
)
if car_list is not None:
pp(car_list)
else:
print("No cars!")
# Full database deletion.
# Hint: The main purpose is tests.
Scruby.napalm()
if __name__ == "__main__":
anyio.run(main)