Skip to content

Commit 8df9685

Browse files
author
Andrew Brookins
committed
Update Getting Started guide, note WIP docs
1 parent 8a5db64 commit 8df9685

14 files changed

+779
-271
lines changed

Diff for: README.md

+97-54
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
<details>
1919
<summary><strong>Table of contents</strong></summary>
2020

21-
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
21+
span
22+
2223
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
2324

24-
- [💡 Why Redis OM?](#-why-redis-om)
25-
- [📇 Modeling Your Data](#-modeling-your-data)
26-
- [✓ Validating Data With Your Model](#-validating-data-with-your-model)
27-
- [🔎 Rich Queries and Embedded Models](#-rich-queries-and-embedded-models)
28-
- [💻 Installation](#-installation)
29-
- [📚 Documentation](#-documentation)
30-
- [⛏️ Troubleshooting](#-troubleshooting)
31-
- [✨ So, How Do You Get RediSearch and RedisJSON?](#-so-how-do-you-get-redisearch-and-redisjson)
32-
- [❤️ Contributing](#-contributing)
33-
- [📝 License](#-license)
25+
- [💡 Why Redis OM?](#-why-redis-om)
26+
- [📇 Modeling Your Data](#-modeling-your-data)
27+
- [✓ Validating Data With Your Model](#-validating-data-with-your-model)
28+
- [🔎 Rich Queries and Embedded Models](#-rich-queries-and-embedded-models)
29+
- [💻 Installation](#-installation)
30+
- [📚 Documentation](#-documentation)
31+
- [⛏️ Troubleshooting](#-troubleshooting)
32+
- [✨ So, How Do You Get RediSearch and RedisJSON?](#-so-how-do-you-get-redisearch-and-redisjson)
33+
- [❤️ Contributing](#-contributing)
34+
- [📝 License](#-license)
3435

3536
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3637

@@ -58,9 +59,7 @@ from typing import Optional
5859

5960
from pydantic import EmailStr
6061

61-
from redis_om.model import (
62-
HashModel,
63-
)
62+
from redis_om.model import HashModel
6463

6564

6665
class Customer(HashModel):
@@ -74,9 +73,25 @@ class Customer(HashModel):
7473

7574
Now that we have a `Customer` model, let's use it to save customer data to Redis.
7675

77-
First, we create a new `Customer` object:
78-
7976
```python
77+
import datetime
78+
from typing import Optional
79+
80+
from pydantic import EmailStr
81+
82+
from redis_om.model import HashModel
83+
84+
85+
class Customer(HashModel):
86+
first_name: str
87+
last_name: str
88+
email: EmailStr
89+
join_date: datetime.date
90+
age: int
91+
bio: Optional[str]
92+
93+
94+
# First, we create a new `Customer` object:
8095
andrew = Customer(
8196
first_name="Andrew",
8297
last_name="Brookins",
@@ -85,24 +100,17 @@ andrew = Customer(
85100
age=38,
86101
bio="Python developer, works at Redis, Inc."
87102
)
88-
```
89-
The model generates a globally unique primary key automatically without needing to talk to Redis.
90103

91-
```python
104+
# The model generates a globally unique primary key automatically
105+
# without needing to talk to Redis.
92106
print(andrew.pk)
93-
'01FJM6PH661HCNNRC884H6K30C'
94-
```
95-
96-
We can save the model to Redis by calling `save()`:
107+
#> '01FJM6PH661HCNNRC884H6K30C'
97108

98-
```python
109+
# We can save the model to Redis by calling `save()`:
99110
andrew.save()
100-
```
101111

102-
To retrieve this customer with its primary key, we use `Customer.get()`:
103-
104-
```python
105-
other_andrew = Customer.get('01FJM6PH661HCNNRC884H6K30C')
112+
# To retrieve this customer with its primary key, we use `Customer.get()`:
113+
assert Customer.get(andrew.pk) == andrew
106114
```
107115

108116
**Ready to learn more?** Check out the [getting started](docs/getting_started.md) guide.
@@ -118,30 +126,45 @@ This validation ensures that fields like `first_name`, which the `Customer` mode
118126
For example, because we used the `EmailStr` type for the `email` field, we'll get a validation error if we try to create a `Customer` with an invalid email address:
119127

120128
```python
121-
Customer(
122-
first_name="Andrew",
123-
last_name="Brookins",
124-
email="Not an email address!",
125-
join_date=datetime.date.today(),
126-
age=38,
127-
bio="Python developer, works at Redis, Inc."
128-
)
129-
```
129+
import datetime
130+
from typing import Optional
130131

131-
This code generates the following validation error:
132+
from pydantic import EmailStr, ValidationError
132133

133-
```
134-
Traceback:
135-
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
136-
email
137-
value is not a valid email address (type=value_error.email)
134+
from redis_om.model import HashModel
135+
136+
137+
class Customer(HashModel):
138+
first_name: str
139+
last_name: str
140+
email: EmailStr
141+
join_date: datetime.date
142+
age: int
143+
bio: Optional[str]
144+
145+
146+
try:
147+
Customer(
148+
first_name="Andrew",
149+
last_name="Brookins",
150+
email="Not an email address!",
151+
join_date=datetime.date.today(),
152+
age=38,
153+
bio="Python developer, works at Redis, Inc."
154+
)
155+
except ValidationError as e:
156+
print(e)
157+
"""
158+
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
159+
email
160+
value is not a valid email address (type=value_error.email)
161+
"""
138162
```
139163

140164
**Any existing Pydantic validator should work** as a drop-in type annotation with a Redis OM model. You can also write arbitrarily complex custom validations!
141165

142166
To learn more, see the [documentation on data validation](docs/validation.md).
143167

144-
145168
## 🔎 Rich Queries and Embedded Models
146169

147170
Data modeling, validation, and saving models to Redis all work regardless of how you run Redis.
@@ -157,18 +180,35 @@ Redis OM comes with a rich query language that allows you to query Redis with Py
157180
To show how this works, we'll make a small change to the `Customer` model we defined earlier. We'll add `Field(index=True)` to tell Redis OM that we want to index the `last_name` and `age` fields:
158181

159182
```python
183+
import datetime
184+
from typing import Optional
185+
186+
from pydantic import EmailStr
187+
188+
from redis_om.model import (
189+
Field,
190+
HashModel,
191+
Migrator
192+
)
193+
194+
160195
class Customer(HashModel):
161196
first_name: str
162197
last_name: str = Field(index=True)
163198
email: EmailStr
164199
join_date: datetime.date
165200
age: int = Field(index=True)
166201
bio: Optional[str]
167-
```
202+
203+
204+
# Now, if we use this model with a Redis deployment that has the
205+
# RediSearch module installed, we can run queries like the following.
168206

169-
Now, if we use this model with a Redis deployment that has the [RediSearch module][redisearch-url] installed, we can run queries like the following:
207+
# Before running queries, we need to run migrations to set up the
208+
# indexes that Redis OM will use. You can also use the `migrate`
209+
# CLI tool for this!
210+
Migrator().run()
170211

171-
```python
172212
# Find all customers with the last name "Brookins"
173213
Customer.find(Customer.last_name == "Brookins").all()
174214

@@ -202,6 +242,7 @@ from redis_om.model import (
202242
EmbeddedJsonModel,
203243
JsonModel,
204244
Field,
245+
Migrator
205246
)
206247

207248
class Address(EmbeddedJsonModel):
@@ -224,11 +265,15 @@ class Customer(JsonModel):
224265

225266
# Creates an embedded model.
226267
address: Address
227-
```
228268

229-
With these two models and a Redis deployment with the RedisJSON module installed, we can run queries like the following:
269+
# With these two models and a Redis deployment with the RedisJSON
270+
# module installed, we can run queries like the following.
271+
272+
# Before running queries, we need to run migrations to set up the
273+
# indexes that Redis OM will use. You can also use the `migrate`
274+
# CLI tool for this!
275+
Migrator().run()
230276

231-
```python
232277
# Find all customers who live in San Antonio, TX
233278
Customer.find(Customer.address.city == "San Antonio",
234279
Customer.address.state == "TX")
@@ -254,7 +299,7 @@ The Redis OM documentation is available [here](docs/index.md).
254299

255300
## ⛏️ Troubleshooting
256301

257-
If you run into trouble or have any questions, we're here to help!
302+
If you run into trouble or have any questions, we're here to help!
258303

259304
First, check the [FAQ](docs/faq.md). If you don't find the answer there,
260305
hit us up on the [Redis Discord Server](http://discord.gg/redis).
@@ -287,7 +332,6 @@ Redis OM uses the [BSD 3-Clause license][license-url].
287332
[ci-url]: https://github.com/redis-developer/redis-om-python/actions/workflows/ci.yml
288333
[license-image]: http://img.shields.io/badge/license-BSD_3--Clause-green.svg?style=flat-square
289334
[license-url]: LICENSE
290-
291335
<!-- Links -->
292336

293337
[redis-om-website]: https://developer.redis.com
@@ -299,4 +343,3 @@ Redis OM uses the [BSD 3-Clause license][license-url].
299343
[pydantic-url]: https://github.com/samuelcolvin/pydantic
300344
[ulid-url]: https://github.com/ulid/spec
301345
[redis-enterprise-url]: https://redis.com/try-free/
302-

Diff for: docs/connections.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Managing Connections
2+
3+
WIP!

Diff for: docs/embedded_models.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Embedded Models
22

3+
**NOTE:** This documentation is a stub, using the same embedded JSON model example as the README.
4+
35
Redis OM can store and query **nested models** like any document database, with the speed and power you get from Redis. Let's see how this works.
46

57
In the next example, we'll define a new `Address` model and embed it within the `Customer` model.
@@ -12,6 +14,7 @@ from redis_om.model import (
1214
EmbeddedJsonModel,
1315
JsonModel,
1416
Field,
17+
Migrator
1518
)
1619

1720
class Address(EmbeddedJsonModel):
@@ -34,11 +37,15 @@ class Customer(JsonModel):
3437

3538
# Creates an embedded model.
3639
address: Address
37-
```
3840

39-
With these two models and a Redis deployment with the RedisJSON module installed, we can run queries like the following:
41+
# With these two models and a Redis deployment with the RedisJSON
42+
# module installed, we can run queries like the following.
43+
44+
# Before running queries, we need to run migrations to set up the
45+
# indexes that Redis OM will use. You can also use the `migrate`
46+
# CLI tool for this!
47+
Migrator().run()
4048

41-
```python
4249
# Find all customers who live in San Antonio, TX
4350
Customer.find(Customer.address.city == "San Antonio",
4451
Customer.address.state == "TX")

Diff for: docs/faq.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Frequently Asked Questions (FAQ)
2+
3+
WIP!

0 commit comments

Comments
 (0)