Jon Combe logo

Django TinyStore: a tiny, persistent JSON store for Django

April 20222 years ago

TinyStore is another minuscule package I created for Django. It scratched an itch I had with a project I was working on at the time.

TinyStore allows you to keep named blobs of JSON data in a database, and read, update or delete them easily. A super-simple persistent key-value store, where the value can be any valid JSON object. It also allows you to define default values for these blobs in your settings.py file.

Example usage

Setting default values for TinyStore is completely optional, but for the purpose of this example I will start by defining two key-value pairs:

settings.py
TINY_STORE = {
  "discount": {"enabled": False, "percentage": 12 },
  "features": {"free_shipping": False }
}

Reading values

INPUT
TinyStore.get("discount")
OUTPUT
{"enabled": False, "percentage": 12 }

Testing for the existence of a key

INPUT
TinyStore.exists("discount")
OUTPUT
True

List all the available keys

INPUT
TinyStore.keys()
OUTPUT
["discount", "features"]

Update a value

INPUT
TinyStore.set("discount", {"enabled": True, "percentage": 8 })

Reading values (again)

INPUT
TinyStore.get("discount")
OUTPUT
{"enabled": True, "percentage": 8 }

Remove a key

INPUT
TinyStore.remove("discount")

Removing all keys

INPUT
TinyStore.remove_all()

Note that keys with default values set are never removed entirely. Instead, they are just reset to their default values. This is by design: keys that you define defaults for must always exist.

Thanks for reading. You can find installation and usage docs in the GitHub repo. The package is available on PyPI. Happy coding!