You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63-29
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
<h1align="center">Redis OM</h1>
2
2
<palign="center">
3
3
<p align="center">
4
-
Objecting mapping and more, for Redis.
4
+
Objecting mapping, and more, for Redis.
5
5
</p>
6
6
</p>
7
7
@@ -11,7 +11,7 @@
11
11
[![License][license-image]][license-url]
12
12
[![Build Status][ci-svg]][ci-url]
13
13
14
-
Redis OM is a library that helps you build modern Python applications with Redis.
14
+
**Redis OM Python** makes it easy to model Redis data in your Python applications.
15
15
16
16
**Redis OM Python** | [Redis OM Node.js][redis-om-js] | [Redis OM Spring][redis-om-spring] | [Redis OM .NET][redis-om-dotnet]
17
17
@@ -21,32 +21,41 @@ Redis OM is a library that helps you build modern Python applications with Redis
21
21
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
22
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
23
23
24
-
25
-
-[Why Redis OM?](#why)
26
-
-[Getting started](#getting-started)
27
-
-[Installation](#installation)
28
-
-[Documentation](#documentation)
29
-
-[Troubleshooting](#troubleshooting)
30
-
-[Contributing](#contributing)
31
-
-[License](#license)
24
+
-[💡 Why Redis OM?](#-why-redis-om)
25
+
-[Starting Redis](#starting-redis)
26
+
-[📇 Modeling your domain (and indexing it!)](#-modeling-your-domain-and-indexing-it)
27
+
-[🔎 Querying](#-querying)
28
+
-[Why this is important](#why-this-is-important)
29
+
-[So how do you get RediSearch and RedisJSON?](#so-how-do-you-get-redisearch-and-redisjson)
30
+
-[Why Redis OM?](#why)
31
+
-[Getting started](#getting-started)
32
+
-[Installation](#installation)
33
+
-[Documentation](#documentation)
34
+
-[Troubleshooting](#troubleshooting)
35
+
-[Contributing](#contributing)
36
+
-[License](#license)
32
37
33
38
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
34
39
35
40
</details>
36
41
37
-
## ➡ Why Redis OM?
42
+
## 💡 Why Redis OM?
43
+
44
+
Redis OM provides high-level abstractions for using Redis in Python, making it easy to model and query your Redis domain objects.
38
45
39
-
Redis OM is a library of high-level tools that help you build modern Python applications with Redis.
46
+
This **preview** release contains the following features:
40
47
41
-
This *preview release* includes our first major component: a **declarative model class** backed by Redis.
48
+
* Declarative object mapping for Redis objects
49
+
* Declarative secondary-index generation
50
+
* Fluent APIs for querying Redis
42
51
43
52
## 🏁 Getting started
44
53
45
54
### Object Mapping
46
55
47
-
With Redis OM, you get powerful data modeling, extensible data validation with [Pydantic](pydantic-url), and rich query expressions with a small amount of code.
56
+
With Redis OM, you get powerful data modeling, extensible data validation with [Pydantic](pydantic-url), and rich query expressions.
48
57
49
-
Check out this example of data modeling and validation. First, we're going to create a `Customer` model that we can use to save data to Redis.
58
+
Check out this example of how we'd model customer data with Redis OM. First, we're going to create a `Customer` model:
50
59
51
60
```python
52
61
import datetime
@@ -68,12 +77,13 @@ class Customer(HashModel):
68
77
bio: Optional[str]
69
78
```
70
79
71
-
Here, we've defined a `Customer` model with the `HashModel` class from redis-om. This model will save data in Redis as a [Redis Hash](https://redis.io/topics/data-types).
80
+
**NOTE**: Redis OM uses Python type annotations for data validation. See the _Data Validation_ section of this README for more details.
72
81
73
-
Next, let's see how Redis OM makes it easy to save and retrieve `Customer` data in Redis.
82
+
Now that we have a `Customer` model, let's use it to save customer data to Redis.
83
+
84
+
First, we create a new `Customer` object:
74
85
75
86
```python
76
-
# We can create a new Customer object:
77
87
andrew = Customer(
78
88
first_name="Andrew",
79
89
last_name="Brookins",
@@ -82,25 +92,47 @@ andrew = Customer(
82
92
age=38,
83
93
bio="Python developer, works at Redis, Inc."
84
94
)
95
+
```
96
+
The model generates a globally unique primary key automatically without needing to talk to Redis.
85
97
86
-
# The model generates a globally unique primary key automatically without
87
-
# needing to talk to Redis.
98
+
```python
88
99
print(andrew.pk)
89
-
# '01FJM6PH661HCNNRC884H6K30C'
100
+
'01FJM6PH661HCNNRC884H6K30C'
101
+
```
102
+
103
+
We can save the model to Redis by calling `save()`:
90
104
91
-
# We can save the model to Redis.
105
+
```python
92
106
andrew.save()
107
+
```
108
+
109
+
To retrieve this customer with its primary key, we use `Customer.get()`:
93
110
94
-
# Now, we can retrieve this customer with its primary key:
Now let's see how Redis OM makes data validation a snap, thanks to [Pydantic](https://pydantic-docs.helpmanual.io/).
96
116
97
-
# The original model and this one pass an equality check.
98
-
assert other_andrew == andrew
117
+
### Data Validation
118
+
119
+
When you create a model with Redis OM, you define fields and give them type annotations. As a refresher, take a look at the `Customer` model we already built:
120
+
121
+
```python
122
+
classCustomer(HashModel):
123
+
first_name: str
124
+
last_name: str
125
+
email: EmailStr
126
+
join_date: datetime.date
127
+
age: int
128
+
bio: Optional[str]
99
129
```
100
130
101
-
Now, let's talk about **validation**. Did you notice the type annotation for the `email` field was `EmailStr`?
131
+
Redis OM uses [Pydantic](pydantic-url) behind the scenes to validate data at runtime, based on the model's type annotations.
132
+
133
+
This validation works for basic cases, like ensuring that `first_name` is always a string. But every Redis OM model is also a Pydantic model, so you can use existing Pydantic validators like `EmailStr`, `Pattern`, and many more for complex validation!
102
134
103
-
`EmailStr` is a [Pydantic field validator](https://pydantic-docs.helpmanual.io/usage/types/). Because every Redis OM model is also a Pydantic model, you can use Pydantic validators like `EmailStr`, `Pattern`, and many more!
135
+
#### A Demo
104
136
105
137
Let's see what happens if we try to instantiate our `Customer` class with an invalid email address.
106
138
@@ -140,9 +172,11 @@ andrew.save()
140
172
# value is not a valid email address (type=value_error.email)
141
173
```
142
174
143
-
Data modeling, validation, and persistent to Redis all work regardless of where you run Redis. But can we do more?
175
+
Data modeling, validation, and persisting to Redis all work regardless of how you run Redis.
176
+
177
+
However, Redis OM will take your Python applications to the next level if you're using the RediSearch and RedisJSON modules in your Redis deployment. Next, we'll talk about the **rich query expressions** and **embedded models** that Redis OM gives you with those Redis modules.
144
178
145
-
Yes, we can! Next, we'll talk about the **rich query expressions** and **embedded models** that Redis OM gives you when you're using the RediSearch and RedisJSON Redis modules.
179
+
**TIP**: *Wait, what's a Redis module?* If you aren't familiar with Redis modules, review the *RediSearch and RedisJSON* section of this README.
146
180
147
181
### Querying
148
182
Querying uses a rich expression syntax inspired by the Django ORM, SQLAlchemy, and Peewee.
0 commit comments