SQLite storage
from aiogram_magick.sqlite import SqliteStorage
# By default, SqliteStorage is configured to:
# - Commit changes on 30 minute idle and on shutdown;
# - Cache states (up to 20 entries) and data (up to 10 entries);
# - Ignore any exceptions;
# - To avoid file corruptions on shutdown any `sqlite3.OperationalError`s
# are printed using `traceback.print_exception` instead of raised normally.
dp = Dispatcher(storage=SqliteStorage('aiogram.sqlite'))
Configuration
Database path
Commit frequency
By default, SqliteStorage
commits changes (internally saved in __commit
) on:
- the first request of changing a state or data;
- After
idle_to_commit
seconds of idling between eitherset_state()
,set_data()
orupdate_data()
; close()
.
Key factory
SqliteStorage
does not provide an option to configure the key factory that is used for storing keys in the database.
It also does not use aiogram.fsm.storage.base.DefaultKeyFactory
nor bases on it. The reason is simple: DefaultKeyFactory
does not provide a method to convert str
back to StorageKey
that can be used to save data between reloads.
__key_to_sqlite
and __sqlite_to_key
uses this format to work with StorageKey
s:
Warning
thread_id
, business_connection_id
and destiny
fields of StorageKey
are ignored.
API Reference
SQLite-based storage with caching & automatic commits for Aiogram 3.x.
SqliteStorage
Bases: BaseStorage
SQLite FSM storage.
Initalize a SQLite-based storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
Path to the database file |
required |
idle_to_commit |
int
|
Frequency of idling to commit changes (in seconds). It it notable
to mention that regardless of this value all changes will be saved anyway when
|
1800
|
Source code in aiogram_magick/sqlite.py
__connect
async
Source code in aiogram_magick/sqlite.py
__key_to_sqlite
staticmethod
SqliteStorageCache
dataclass
SQLite cache dataclass.
capacity
class-attribute
instance-attribute
add
Add a new cache entry of key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
StorageKey
|
Cache key. |
required |
value |
Union[StateType, Dict[str]]
|
Value |
required |
Source code in aiogram_magick/sqlite.py
get
Get the cache entry of key
if it exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
StorageKey
|
Cache key. |
required |
Returns:
Type | Description |
---|---|
Union[StateType, Dict[str], None]
|
Union[StateType, Dict[str], None]: State, data or |
Source code in aiogram_magick/sqlite.py
update
Update the value for the cache entry of key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
StorageKey
|
Cache key. |
required |
value |
Union[StateType, Dict[str]]
|
New value |
required |