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.
Class Instantiation
app: Pyloid instance (
Pyloid
)"store.json": Path to the database file
Methods
1. get
Description
Returns the value associated with the specified key. If the key does not exist, returns None
.
Parameters
key (
str
): The key to retrieve
Returns
Any: The value stored for the key, or
None
if not found
Example
2. set
Description
Adds or updates a key-value pair in the database. The value must be a JSON-serializable Python object.
Parameters
key (
str
): The key to storevalue (
Any
): The value to store (must be JSON serializable)
Returns
bool: Always returns
True
Example
3. remove
Description
Deletes the value associated with the specified key from the database.
Parameters
key (
str
): The key to remove
Returns
bool: Returns
True
if the key was deleted,False
if the key did not exist
Example
4. all
Description
Returns a list of all keys stored in the database.
Returns
List[str]: A list of all stored keys
Example
5. purge
Description
Deletes all keys and values from the database.
Returns
bool: Always returns
True
Example
6. save
Description
Saves the current state of the database to the file. You can specify orjson serialization options with the option
parameter.
Parameters
option (
Optional[int]
): (Optional) orjson.OPT_* flag
Returns
bool: Returns
True
if saving was successful,False
otherwise
Example
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.
Last updated
Was this helpful?