Reference / KV API for Python
KV API for Python
Every public class, function and type of the KV engine in tinystore, generated from its source. The KV guide explains how to use them, and the Bun and Node, Go and Rust pages list the same API.
IfVersion
class IfVersion(TypedDict):
if_version: NotRequired[Version]Writes only while the key is at this version: ConflictError otherwise.
It takes a version and never None, so that an absent key never writes without its check.
Entry
@dataclass(frozen=True, slots=True)
class Entry[T]:
key: str
value: T
version: Version # compared only for equality: give it back to if_version
expires_at: datetime | None # None for a key that never expiresA key's value with its version and expiry.
Page
@dataclass(frozen=True, slots=True)
class Page[T]:
entries: list[Entry[T]]
next: str | NoneA page of a branch's keys, and where the next starts when there is one.
Bucket
class Bucket[T]:
name: strA bucket's values under one branch of owners, the bucket's own when there are none.
Buckets come from store.bucket, db.bucket and their under;
inside a transaction, from tx.with_(bucket), whose calls go through
the transaction.
Bucket.parts
@property
def parts() -> Parts: ...Bucket.under
def under(*owners: Key) -> Bucket[T]: ...The branch the owners name below this one: whose keys these are.
Bucket.get
async def get(key: Key) -> T | None: ...The key's value, None when it holds none.
Bucket.entry
async def entry(key: Key) -> Entry[T] | None: ...The key's value with its version and expiry, None when it holds none.
Bucket.has
async def has(key: Key) -> bool: ...Bucket.set
async def set(
key: Key,
value: T,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
**check: Unpack[IfVersion],
) -> None: ...Writes the value; a key that exists keeps its expiry unless the call gives one.
Bucket.create
async def create(
key: Key,
value: T,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
) -> bool: ...Writes the value only where no live key is, and says whether it did.
Bucket.update
async def update(key: Key, change: Callable[[T | None], T | None]) -> T | None: ...Writes what change makes of the key's value without losing a write made meanwhile.
It reads the key, calls change with its value, or None where it holds none, and writes what change returns at the version it read, or creates the key. When another write came first it reads again and calls change again, eight times at most, then raises ConflictError: change may run more than once, so it must do nothing else. A change that returns None, or the very value it was given, writes nothing. It returns the value written, or the one there when nothing was. A key that is there keeps its expiry, and one it creates takes the bucket's, as set and create do:
await pinned.update(student_id, lambda courses: [*(courses or []), 2518])Bucket.add
async def add(
key: Key,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
) -> bool: ...Puts the key in a bucket used as a set; False when it was there.
Bucket.take
async def take(key: Key, **check: Unpack[IfVersion]) -> T | None: ...Reads the value and removes the key in one commit: of two callers taking a code at once, one gets it.
Bucket.delete
async def delete(key: Key, **check: Unpack[IfVersion]) -> bool: ...Removes the key, and says whether it was there.
Bucket.expire
async def expire(
key: Key,
when: Duration | datetime,
**check: Unpack[IfVersion],
) -> bool: ...Gives a live key a new expiry, a span from now or a time, keeping its value; False when it holds none.
Bucket.clear
async def clear() -> None: ...Removes this branch's keys and every branch under it, at once however many.
Bucket.reseal
async def reseal() -> int: ...Seals again with the store's sealing key what an older key sealed, and says how many.
Every value of this branch, and of the branches under it, that an older key sealed: once every value of the bucket is, the older key can leave the store's options. A value sealed with a key the store does not hold stops it with an InvalidError that names the key, and a reseal after the key is given goes on.
Bucket.count
async def count() -> int: ...How many keys all would walk now: this branch's own, not those of the branches under it.
An expired key not yet swept and a cleared one are not counted, and an idle key is counted without being renewed. A large branch is counted in several reads, so a key written meanwhile may or may not be.
Bucket.list
async def list(*, limit: int | None = None, after: Key | None = None) -> Page[T]: ...One page of this branch's own keys, in the byte order of their text: "10" before "9".
Bucket.all
async def all(*, limit: int | None = None) -> AsyncIterator[Entry[T]]: ...Walks this branch's own keys a page at a time, holding nothing between pages.
A key written during the walk may or may not be met. It renews no idle key.
Counters
class Counters:
name: strNumbers by key that only add up: 0 for one never added to, or expired. They come from store.counters.
Counters.parts
@property
def parts() -> Parts: ...Counters.under
def under(*owners: Key) -> Counters: ...Counters.add
async def add(key: Key, n: int = 1) -> int: ...Adds n, 1 unless given, and gives the new count.
Counters.get
async def get(key: Key) -> int: ...Counters.delete
async def delete(key: Key) -> bool: ...Removes the counter, and says whether it was there.
Counters.clear
async def clear() -> None: ...Removes this branch's counters and every branch's under it.
WindowUse
@dataclass(frozen=True, slots=True)
class WindowUse:
used: int
limit: int
left: int
resets_at: datetime | None # when the window resets; None for one not startedOne window of a quota as a key stands in it.
Allowance
@dataclass(frozen=True, slots=True)
class Allowance:
ok: bool
left: int # how many more would pass now
retry_at: datetime | None # when a refused request may try again; None when it passed
windows: dict[str, WindowUse] = field(default_factory=dict[str, WindowUse]) # a quota's windows by name; none for a rate limitWhat a rate limit or a quota answers a request.
RateLimit
class RateLimit:
name: strA rate of requests a key: open it with store.rate_limit(name, rate=...).
RateLimit.under
def under(*owners: Key) -> RateLimit: ...The rate limit of a branch, each key apart from the same key elsewhere.
RateLimit.allow
async def allow(key: Key, n: int = 1) -> Allowance: ...Asks for n requests of key, one unless given: all pass or none; more than the burst is InvalidError.
RateLimit.peek
async def peek(key: Key) -> Allowance: ...The answer one request would get, using nothing.
RateLimit.reset
async def reset(key: Key) -> None: ...Forgets key, so that its next request starts afresh.
Quota
class Quota:
name: strUses by key in named windows: open it with store.quota(name, daily="100/1d").
Quota.under
def under(*owners: Key) -> Quota: ...Quota.allow
async def allow(key: Key, n: int = 1) -> Allowance: ...Uses n of key's every window, one unless given, or none when one has no room.
More than a window's limit is InvalidError.
Quota.peek
async def peek(key: Key) -> Allowance: ...Quota.reset
async def reset(key: Key) -> None: ...Quota.refund
async def refund(key: Key, n: int = 1) -> None: ...Gives n uses back to every window of key, one unless given.
Fetched
@dataclass(frozen=True, slots=True)
class Fetched[T]:
value: T
age: timedelta
stale: boolAn answer with how old it is: stale once older than the fresh its answers opened with.
Once
class Once[T]:
name: strAnswers kept once a key: open them with store.once(name, type).
Once.under
def under(*owners: Key) -> Once[T]: ...The answers of a branch, each key apart from the same key elsewhere.
Once.run
async def run(key: Key, fn: Callable[[], Awaitable[T] | T]) -> T: ...The answer kept for key, or fn's, which is kept.
A run of a key that another runs, here or in another process, waits for it and gets its answer; an exception of fn keeps nothing and is raised, so the next run runs fn again. A connection lost after fn returned and before its answer was kept is OutcomeUnknownError.
Once.fetch
async def fetch(key: Key, fn: Callable[[], Awaitable[T] | T]) -> Fetched[T]: ...run's answer with how old it is.
Opened with fresh, a kept answer older than it comes back at once,
stale, and fn may make the next one in the background: its answer
replaces the kept one, and its exception, which no caller sees, keeps it
until keep ends.
Once.get
async def get(key: Key) -> T | None: ...The answer kept for key, None when none is.
Once.delete
async def delete(key: Key) -> bool: ...Forgets the answer kept for key, so that its next run runs fn again.
Once.clear
async def clear() -> None: ...Forgets every answer of this branch and of the branches under it, in one commit.
A run under way meanwhile keeps its answer once it ends.
Once.reseal
async def reseal() -> int: ...Seals again with the store's sealing key what an older key sealed, and says how many.
Every answer of this branch, and of the branches under it, as an
encrypted bucket's reseal does; answers opened without
encrypted are an InvalidError.
Tx
class Tx:
...The calls of a transaction, each through tx.with_(handle).
Tx.with_
@overload
def with_[T](handle: Bucket[T]) -> TxBucket[T]: ...
@overload
def with_(handle: Counters) -> TxCounters: ...
@overload
def with_[T](handle: Queue[T]) -> StoreTxQueue[T]: ...
@overload
def with_[T](handle: Channel[T]) -> TxChannel[T]: ...
def with_(
handle: Bucket[Any] | Counters | Queue[Any] | Channel[Any],
) -> TxBucket[Any] | TxCounters | StoreTxQueue[Any] | TxChannel[Any]: ...Takes a handle into the transaction: its calls then go through it.
Tx.takes
def takes(parts: Parts, file: Literal['kv.db', 'jobs.db'], what: str) -> None: ...Tx.read
async def read(parts: Parts, key: Key) -> dict[str, Any]: ...What the key holds as this transaction sees it: its own writes over what the store held.
Tx.write
def write(
method: str,
parts: Parts,
fields: dict[str, Any],
found: dict[str, Any] | None = None,
) -> None: ...Keeps a write for the commit; found is what a read of its key answers after it.
Tx.clear
def clear(parts: Parts) -> None: ...Tx.after
def after(fn: Callable[[], Awaitable[None]]) -> None: ...What the commit does once it answered: a message published inside is handed over then.
Tx.commit
async def commit() -> None: ...Commits the writes, then does what waited for the commit.
TxBucket
class TxBucket[T]:
...A bucket's calls inside a transaction: reads answer at once, writes wait for its commit.
TxBucket.under
def under(*owners: Key) -> TxBucket[T]: ...TxBucket.get
async def get(key: Key) -> T | None: ...TxBucket.entry
async def entry(key: Key) -> Entry[T] | None: ...TxBucket.has
async def has(key: Key) -> bool: ...TxBucket.set
async def set(
key: Key,
value: T,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
**check: Unpack[IfVersion],
) -> None: ...TxBucket.create
async def create(
key: Key,
value: T,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
) -> bool: ...Writes the value only where no live key is, and says whether it will.
TxBucket.add
async def add(
key: Key,
*,
ttl: Duration | None = None,
expires_at: datetime | None = None,
) -> bool: ...TxBucket.take
async def take(key: Key, **check: Unpack[IfVersion]) -> T | None: ...TxBucket.delete
async def delete(key: Key, **check: Unpack[IfVersion]) -> bool: ...TxBucket.expire
async def expire(
key: Key,
when: Duration | datetime,
**check: Unpack[IfVersion],
) -> bool: ...TxBucket.clear
async def clear() -> None: ...Removes this branch's keys and every branch's under it, with the commit.
TxCounters
class TxCounters:
...Counters inside a transaction: an add commits with its writes.
TxCounters.under
def under(*owners: Key) -> TxCounters: ...TxCounters.add
async def add(key: Key, n: int = 1) -> None: ...Adds n, 1 unless given, with the commit; the count is known once it has.