Database¶
erpc.database ¶
eRPC database and cache configuration.
Provides dataclasses for configuring eRPC's caching layer, including multiple storage backends (memory, Redis, PostgreSQL, DynamoDB), cache policies with finality awareness, and compression settings.
Examples:
>>> from erpc.database import DatabaseConfig, MemoryConnector, CachePolicy
>>> db = DatabaseConfig(
... connectors=[MemoryConnector(id="mem", max_items=10_000)],
... policies=[CachePolicy(connector="mem", ttl="30s")],
... )
>>> db.to_dict()["connectors"][0]["driver"]
'memory'
Connector ¶
Bases: Protocol
Protocol for database connector types.
Source code in erpc/database.py
TLSConfig
dataclass
¶
TLS certificate configuration for secure connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cert_file
|
str
|
Path to the client certificate file. |
required |
key_file
|
str
|
Path to the client key file. |
required |
ca_file
|
str
|
Path to the CA certificate file. |
required |
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict with camelCase keys matching eRPC YAML schema. |
MemoryConnector
dataclass
¶
In-memory cache connector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique connector identifier referenced by cache policies. |
required |
max_items
|
int
|
Maximum number of items to store in memory. |
required |
Examples:
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with driver and memory configuration. |
RedisConnector
dataclass
¶
Redis cache connector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique connector identifier referenced by cache policies. |
required |
uri
|
str
|
Redis connection URI (e.g., |
required |
tls
|
TLSConfig | None
|
Optional TLS configuration for secure connections. |
None
|
pool_size
|
int | None
|
Optional connection pool size. |
None
|
Examples:
>>> conn = RedisConnector(id="redis1", uri="redis://localhost:6379")
>>> conn.to_dict()["driver"]
'redis'
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with driver and redis configuration. |
Source code in erpc/database.py
PostgresConnector
dataclass
¶
PostgreSQL cache connector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique connector identifier referenced by cache policies. |
required |
uri
|
str
|
PostgreSQL connection URI. |
required |
table
|
str
|
Table name for cache storage. |
required |
Examples:
>>> conn = PostgresConnector(id="pg1", uri="postgres://host/db", table="cache")
>>> conn.to_dict()["driver"]
'postgresql'
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with driver and postgresql configuration. |
Source code in erpc/database.py
DynamoDBConnector
dataclass
¶
AWS DynamoDB cache connector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Unique connector identifier referenced by cache policies. |
required |
table
|
str
|
DynamoDB table name. |
required |
region
|
str
|
AWS region (e.g., |
required |
partition_key_name
|
str
|
Name of the partition key attribute. |
required |
range_key_name
|
str
|
Name of the range/sort key attribute. |
required |
ttl_attribute
|
str
|
Name of the TTL attribute for automatic expiration. |
required |
Examples:
>>> conn = DynamoDBConnector(
... id="ddb1",
... table="cache",
... region="us-east-1",
... partition_key_name="pk",
... range_key_name="sk",
... ttl_attribute="ttl",
... )
>>> conn.to_dict()["driver"]
'dynamodb'
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with driver and dynamodb configuration. |
Source code in erpc/database.py
CompressionConfig
dataclass
¶
Cache compression configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Compression algorithm (e.g., |
'zstd'
|
level
|
str
|
Compression level (e.g., |
'default'
|
threshold
|
int
|
Minimum item size in bytes before compression is applied. |
1024
|
Examples:
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with compression settings. |
CachePolicy
dataclass
¶
Cache policy rule with finality awareness.
Defines caching behavior for specific methods, networks, and finality states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connector
|
str
|
ID of the connector to use for this policy. |
required |
ttl
|
str
|
Time-to-live duration string (e.g., |
required |
network
|
str | None
|
Optional network filter (e.g., |
None
|
method
|
str | None
|
Optional RPC method filter (e.g., |
None
|
finality
|
str | None
|
Optional finality state filter. One of |
None
|
empty
|
str | None
|
Optional empty response handling. One of |
None
|
min_item_size
|
int | None
|
Optional minimum item size in bytes to cache. |
None
|
max_item_size
|
int | None
|
Optional maximum item size in bytes to cache. |
None
|
Examples:
>>> policy = CachePolicy(connector="mem1", ttl="30s", finality="finalized")
>>> policy.to_dict()["finality"]
'finalized'
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with only non-None fields, using camelCase keys. |
Source code in erpc/database.py
DatabaseConfig
dataclass
¶
Top-level database/cache configuration for eRPC.
Composes connectors, cache policies, and optional compression into
the database section of an eRPC config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connectors
|
list[ConnectorType]
|
List of storage backend connectors. |
list()
|
policies
|
list[CachePolicy]
|
List of cache policy rules. |
list()
|
compression
|
CompressionConfig | None
|
Optional compression configuration. |
None
|
Examples:
>>> db = DatabaseConfig(
... connectors=[MemoryConnector(id="mem", max_items=10_000)],
... policies=[CachePolicy(connector="mem", ttl="30s")],
... )
>>> len(db.to_dict()["connectors"])
1
Source code in erpc/database.py
to_dict ¶
Serialize to eRPC config dict.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict suitable for inclusion in eRPC YAML under |
dict[str, Any]
|
the |