Reference
pydantic_redis
A simple declarative ORM for redis based on pydantic.
Provides:
- A subclass-able
Model
class to create Object Relational Mapping to redis hashes - A redis
Store
class to mutate and queryModel
's registered in it - A
RedisConfig
class to pass to theStore
constructor to connect to a redis instance - A synchronous
syncio
and an asynchronousasyncio
interface to the above classes - Parent-child relationships allowing for nesting models within models.
pydantic_redis.syncio
Synchronous API for pydantic-redis ORM.
Typical usage example:
# from pydantic_redis import Store, Model, RedisConfig
from pydantic_redis.syncio import Store, Model, RedisConfig
class Book(Model):
_primary_key_field = 'title'
title: str
if __name__ == '__main__':
store = Store(name="sample", redis_config=RedisConfig())
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.update(
_id="Oliver Twist", data={"author": "Jane Austen"}, life_span_seconds=3600
)
results = Book.select()
Book.delete(ids=["Oliver Twist", "Great Expectations"])
Model
Bases: AbstractModel
The Base class for all Synchronous models.
Inherit this class when creating a new model.
The new model should have _primary_key_field
defined.
Any interaction with redis is done through Model
's.
Source code in pydantic_redis/syncio/model.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
delete(ids)
classmethod
Removes a list of this Model's records from redis
Removes all the records for the current Model whose primary keys
have been included in the ids
passed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
Union[Any, List[Any]]
|
list of primary keys of the records to remove |
required |
Source code in pydantic_redis/syncio/model.py
insert(data, life_span_seconds=None)
classmethod
Inserts a given record or list of records into the redis.
Can add a single record or multiple records into redis.
The records must be instances of this class. i.e. a Book
model can only insert Book
instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[List[AbstractModel], AbstractModel]
|
a model instance or list of model instances to put into the redis store |
required |
life_span_seconds |
Optional[float]
|
the time-to-live in seconds of the records
to be inserted. If not specified, it defaults to the |
None
|
Source code in pydantic_redis/syncio/model.py
select(columns=None, ids=None, skip=0, limit=None, **kwargs)
classmethod
Retrieves records of this Model from redis.
Retrieves the records for this Model from redis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
Optional[List[str]]
|
the fields to return for each record |
None
|
ids |
Optional[List[Any]]
|
the primary keys of the records to returns |
None
|
skip |
int
|
the number of records to skip. (default: 0) |
0
|
limit |
Optional[int]
|
the maximum number of records to return |
None
|
Returns:
Type | Description |
---|---|
Union[Model, Dict[str, Any]]
|
By default, it returns all records that belong to current Model. |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
have been listed in |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
all records are returned. |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
the fields specified in |
Union[Model, Dict[str, Any]]
|
of the current Model are returned. |
Source code in pydantic_redis/syncio/model.py
update(_id, data, life_span_seconds=None)
classmethod
Updates the record whose primary key is _id
.
Updates the record of this Model in redis whose primary key is equal to the _id
provided.
The record is partially updated from the data
.
If life_span_seconds
is provided, it will also update the time-to-live of
the record.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_id |
Any
|
the primary key of record to be updated. |
required |
data |
Dict[str, Any]
|
the new changes |
required |
life_span_seconds |
Optional[float]
|
the new time-to-live for the record |
None
|
Source code in pydantic_redis/syncio/model.py
RedisConfig
Bases: BaseModel
Configuration for connecting to redis database.
Inorder to connect to a redis database, there are a number of
configurations that are needed including the server's host address
and port. RedisConfig
computes a redis-url similar to
redis://:password@host:self.port/db
Attributes:
Name | Type | Description |
---|---|---|
host |
str
|
the host address where the redis server is found (default: 'localhost'). |
port |
int
|
the port on which the redis server is running (default: 6379). |
db |
int
|
the redis database identifier (default: 0). |
password |
Optional[int]
|
the password for connecting to the redis server (default: None). |
ssl |
bool
|
whether the connection to the redis server is to be via TLS (default: False) |
encoding |
Optional[str]
|
(Optional[str]): the string encoding used with the redis database (default: utf-8) |
Source code in pydantic_redis/config.py
redis_url: str
property
a redis URL of form redis://:password@host:port/db
. (rediss://..
if TLS).
Store
Bases: AbstractStore
Manages a collection of Model's, connecting them to a redis database
A Model can only interact with a redis database when it is registered
with a Store
that is connected to that database.
Attributes:
Name | Type | Description |
---|---|---|
models |
Dict[str, Type[Model]]
|
a mapping of registered |
name |
str
|
the name of this Store |
redis_config |
RedisConfig
|
the configuration for connecting to a redis database |
life_span_in_seconds |
Optional[int]
|
the default time-to-live for the records inserted in this store (default: None) |
Source code in pydantic_redis/syncio/store.py
pydantic_redis.asyncio
Asynchronous API for pydantic-redis ORM.
Typical usage example:
import asyncio
from pydantic_redis.asyncio import Store, Model, RedisConfig
class Book(Model):
_primary_key_field = 'title'
title: str
async def main():
store = Store(name="sample", redis_config=RedisConfig())
store.register_model(Book)
await Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
await Book.update(
_id="Oliver Twist", data={"author": "Jane Austen"}, life_span_seconds=3600
)
results = await Book.select()
await Book.delete(ids=["Oliver Twist", "Great Expectations"])
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Model
Bases: AbstractModel
The Base class for all Asynchronous models.
Inherit this class when creating a new model.
The new model should have _primary_key_field
defined.
Any interaction with redis is done through Model
's.
Source code in pydantic_redis/asyncio/model.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
delete(ids)
async
classmethod
Removes a list of this Model's records from redis
Removes all the records for the current Model whose primary keys
have been included in the ids
passed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
Union[Any, List[Any]]
|
list of primary keys of the records to remove |
required |
Source code in pydantic_redis/asyncio/model.py
insert(data, life_span_seconds=None)
async
classmethod
Inserts a given record or list of records into the redis.
Can add a single record or multiple records into redis.
The records must be instances of this class. i.e. a Book
model can only insert Book
instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[List[AbstractModel], AbstractModel]
|
a model instance or list of model instances to put into the redis store |
required |
life_span_seconds |
Optional[float]
|
the time-to-live in seconds of the records
to be inserted. If not specified, it defaults to the |
None
|
Source code in pydantic_redis/asyncio/model.py
select(columns=None, ids=None, skip=0, limit=None, **kwargs)
async
classmethod
Retrieves records of this Model from redis.
Retrieves the records for this Model from redis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
columns |
Optional[List[str]]
|
the fields to return for each record |
None
|
ids |
Optional[List[Any]]
|
the primary keys of the records to returns |
None
|
skip |
int
|
the number of records to skip. (default: 0) |
0
|
limit |
Optional[int]
|
the maximum number of records to return |
None
|
Returns:
Type | Description |
---|---|
Union[Model, Dict[str, Any]]
|
By default, it returns all records that belong to current Model. |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
have been listed in |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
all records are returned. |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
If |
Union[Model, Dict[str, Any]]
|
the fields specified in |
Union[Model, Dict[str, Any]]
|
of the current Model are returned. |
Source code in pydantic_redis/asyncio/model.py
update(_id, data, life_span_seconds=None)
async
classmethod
Updates the record whose primary key is _id
.
Updates the record of this Model in redis whose primary key is equal to the _id
provided.
The record is partially updated from the data
.
If life_span_seconds
is provided, it will also update the time-to-live of
the record.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
_id |
Any
|
the primary key of record to be updated. |
required |
data |
Dict[str, Any]
|
the new changes |
required |
life_span_seconds |
Optional[float]
|
the new time-to-live for the record |
None
|
Source code in pydantic_redis/asyncio/model.py
RedisConfig
Bases: BaseModel
Configuration for connecting to redis database.
Inorder to connect to a redis database, there are a number of
configurations that are needed including the server's host address
and port. RedisConfig
computes a redis-url similar to
redis://:password@host:self.port/db
Attributes:
Name | Type | Description |
---|---|---|
host |
str
|
the host address where the redis server is found (default: 'localhost'). |
port |
int
|
the port on which the redis server is running (default: 6379). |
db |
int
|
the redis database identifier (default: 0). |
password |
Optional[int]
|
the password for connecting to the redis server (default: None). |
ssl |
bool
|
whether the connection to the redis server is to be via TLS (default: False) |
encoding |
Optional[str]
|
(Optional[str]): the string encoding used with the redis database (default: utf-8) |
Source code in pydantic_redis/config.py
redis_url: str
property
a redis URL of form redis://:password@host:port/db
. (rediss://..
if TLS).
Store
Bases: AbstractStore
Manages a collection of Model's, connecting them to a redis database
A Model can only interact with a redis database when it is registered
with a Store
that is connected to that database.
Attributes:
Name | Type | Description |
---|---|---|
models |
Dict[str, Type[Model]]
|
a mapping of registered |
name |
str
|
the name of this Store |
redis_config |
RedisConfig
|
the configuration for connecting to a redis database |
life_span_in_seconds |
Optional[int]
|
the default time-to-live for the records inserted in this store (default: None) |