ECOSYSTEM / STORE

Controlled writes

strict=True: outside its methods, a store can only be read.

Ecosystem package janelkit-store Shared application data: stores with actions, controlled writes and change tracking, one instance per application, and persistence. pip install janelkit-store
STORE · PAGE 3 / 8~1 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

Only the store changes the store

In a store declared with @store(strict=True), code outside its methods can only read. cart.items.append(x) from a view, or cart.items[0]["price"] = 9, raises a StoreError that says what to do, and the write does not happen. bind: is only allowed on fields declared with field(..., bind=True); a bind: to another field is logged as a bind: error.

Without strict, which is the default, any code can write; a write that does not go through an action is recorded as having no action.

python
# Ordinary Python: a store works without Qt, which is also how its logic is tested.
from janelkit_store import StoreError, field, store

@store(strict=True)
class Cart:
    items: list = []
    note: str = field("", bind=True)

    def add(self, name):
        self.items.append(name)

cart = Cart()                  # a loose instance, without an application
cart.add("apple")
assert cart.items == ["apple"]

try:
    cart.items.append("pear")  # outside an action
    raise AssertionError("a strict store must refuse this write")
except StoreError:
    pass
assert cart.items == ["apple"]
Copy

How it works

  1. Cart() creates a loose instance, outside any application, which is how a store is tested with plain Python.
  2. add is an action, so its append is allowed.
  3. The append from outside raises StoreError and leaves the list as it was.

Common mistakes

  • Strict mode protects the data, not the rules: an action can still put the store in a wrong state. Test the actions.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.