Store
The Store class is a lightweight key-value database used by creating it via app.store("store.json") from a Pyloid app instance. You can store any Python object that is JSON serializable, and the data is persistently managed as a database file.
Creating a Store
store
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")Parameters
Prop
Type
Returns
StoreStore instance for key-value storage.Data Operations
get
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("user", {"name": "Hong Gil-dong", "age": 30})
user = store.get("user")
print(user) # {'name': 'Hong Gil-dong', 'age': 30}
print(store.get("nonexistent_key")) # NoneReturns the value associated with the specified key. If the key does not exist, returns default.
Parameters
Prop
Type
Returns
AnyThe value stored for the key, or default if not found.set
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("settings", {"theme": "dark", "notifications": True})
store.set("counter", 42)
store.set("items", ["apple", "banana", "orange"])Adds or updates a key-value pair in the database. The value must be a JSON-serializable Python object.
Parameters
Prop
Type
Returns
boolAlways returns True.remove
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("temp", "temporary data")
store.remove("temp") # True
store.remove("nonexistent_key") # FalseDeletes the value associated with the specified key from the database.
Parameters
Prop
Type
Returns
boolReturns True if the key was deleted, False if the key did not exist.Bulk Operations
all
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("key1", "value1")
store.set("key2", "value2")
keys = store.all()
print(keys) # ['key1', 'key2']Returns a list of all keys stored in the database.
Returns
List[str]A list of all stored keys.purge
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("key1", "value1")
store.set("key2", "value2")
store.purge() # True
print(store.all()) # []Deletes all keys and values from the database.
Returns
boolAlways returns True.Persistence
save
from pyloid import Pyloid
app = Pyloid(app_name="MyApp", single_instance=True)
store = app.store("store.json")
store.set("key", "value")
store.save() # TrueSaves the current state of the database to the file. You can specify orjson serialization options with the option parameter.
Parameters
Prop
Type
Returns
boolReturns True if saving was successful, False otherwise.Summary
- Store is created from a Pyloid app instance using
app.store("store.json"). - You can store any Python object that is JSON serializable, such as strings, numbers, lists, and dictionaries.
- Main methods:
get,set,remove,all,purge,save - Data is persistently managed as a database file.