Skip to content

Commit f44b7fa

Browse files
author
Andrew Brookins
committed
Work on docs, getting started guide
1 parent ad6a323 commit f44b7fa

File tree

10 files changed

+434
-12
lines changed

10 files changed

+434
-12
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- [💻 Installation](#-installation)
2929
- [📚 Documentation](#-documentation)
3030
- [⛏️ Troubleshooting](#-troubleshooting)
31-
- [✨ So how do you get RediSearch and RedisJSON?](#-so-how-do-you-get-redisearch-and-redisjson)
31+
- [✨ So, How Do You Get RediSearch and RedisJSON?](#-so-how-do-you-get-redisearch-and-redisjson)
3232
- [❤️ Contributing](#-contributing)
3333
- [📝 License](#-license)
3434

@@ -111,7 +111,7 @@ Or, continue reading to see how Redis OM makes data validation a snap.
111111

112112
## ✓ Validating Data With Your Model
113113

114-
Redis OM uses [Pydantic](pydantic-url) to validate data based on the type annotations you assign to fields in a model class.
114+
Redis OM uses [Pydantic][pydantic-url] to validate data based on the type annotations you assign to fields in a model class.
115115

116116
This validation ensures that fields like `first_name`, which the `Customer` model marked as a `str`, are always strings. **But every Redis OM model is also a Pydantic model**, so you can use Pydantic validators like `EmailStr`, `Pattern`, and many more for complex validations!
117117

@@ -146,13 +146,15 @@ To learn more, see the [documentation on data validation](docs/validation.md).
146146

147147
Data modeling, validation, and saving models to Redis all work regardless of how you run Redis.
148148

149-
Next, we'll show you the **rich query expressions** and **embedded models** Redis OM provides when the [RediSearch](redisearch-url) and [RedisJSON](redis-json-url) modules are installed in your Redis deployment, or you're using [Redis Enterprise](redis-enterprise-url).
149+
Next, we'll show you the **rich query expressions** and **embedded models** Redis OM provides when the [RediSearch][redisearch-url] and [RedisJSON][redis-json-url] modules are installed in your Redis deployment, or you're using [Redis Enterprise][redis-enterprise-url].
150150

151151
**TIP**: *Wait, what's a Redis module?* If you aren't familiar with Redis modules, review the [So, How Do You Get RediSearch and RedisJSON?](#-so-how-do-you-get-redisearch-and-redisjson) section of this README.
152152

153153
### Querying
154154

155-
Let's make a small change to the `Customer` model we defined earlier to let Redis OM know that we want to query using the `last_name` and `age` fields:
155+
Redis OM comes with a rich query language that allows you to query Redis with Python expressions.
156+
157+
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:
156158

157159
```python
158160
class Customer(HashModel):
@@ -164,7 +166,7 @@ class Customer(HashModel):
164166
bio: Optional[str]
165167
```
166168

167-
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:
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:
168170

169171
```python
170172
# Find all customers with the last name "Brookins"
@@ -184,7 +186,7 @@ These queries -- and more! -- are possible because **Redis OM manages indexes fo
184186

185187
Querying with this index features a rich expression syntax inspired by the Django ORM, SQLAlchemy, and Peewee. We think you'll enjoy it!
186188

187-
To see more example queries, see the [documentation on querying](docs/querying.md).
189+
To learn more about how to query with Redis OM, see the [documentation on querying](docs/querying.md).
188190

189191
### Embedded Models
190192

@@ -259,9 +261,9 @@ hit us up on the [Redis Discord Server](http://discord.gg/redis).
259261

260262
## ✨ So How Do You Get RediSearch and RedisJSON?
261263

262-
Some advanced features of Redis OM rely on core features from two source available Redis modules: [RediSearch](redisearch-url) and [RedisJSON](redis-json-url).
264+
Some advanced features of Redis OM rely on core features from two source available Redis modules: [RediSearch][redisearch-url] and [RedisJSON][redis-json-url].
263265

264-
You can run these modules in your self-hosted Redis deployment, or you can use [Redis Enterprise](redis-enterprise-url), which includes both modules.
266+
You can run these modules in your self-hosted Redis deployment, or you can use [Redis Enterprise][redis-enterprise-url], which includes both modules.
265267

266268
To learn more, read [our documentation](docs/redis_modules.md).
267269

docs/connections.md

Whitespace-only changes.
File renamed without changes.

docs/getting_started.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# Getting Started With Redis OM
2+
3+
## Introduction
4+
5+
This tutorial will walk you through installing Redis OM, creating your first model, and using it to save and validate data.
6+
7+
## Prerequisites
8+
9+
Redis OM requires Python version 3.9 or above and a Redis instance to connect to.
10+
11+
## Python
12+
13+
Make sure you are running **Python version 3.9 or higher**:
14+
15+
```
16+
python --version
17+
Python 3.9.0
18+
```
19+
20+
If you don't have Python installed, you can download it from [Python.org](https://www.python.org/downloads/), use [Pyenv](https://github.com/pyenv/pyenv), or install Python with your operating system's package manager.
21+
22+
## Redis
23+
24+
Redis OM saves data in Redis, so you will need Redis installed and running to complete this tutorial.
25+
26+
### Downloading Redis
27+
28+
The latest version of Redis is available from [Redis.io](https://redis.io/). You can also install Redis with your operating system's package manager.
29+
30+
**NOTE:** This tutorial will guide you through starting Redis locally, but the instructions will also work if Redis is running on a remote server.
31+
32+
### Installing Redis On Windows
33+
34+
Redis doesn't run directly on Windows, but you can use Windows Subsystem for Linux (WSL) to run Redis. See [our video on YouTube](https://youtu.be/_nFwPTHOMIY) for a walk-through.
35+
36+
Windows users can also use Docker. See the next section on running Redis with Docker for more information.
37+
38+
### Running Redis With Docker
39+
40+
Instead of installing Redis manually or with a package manager, you can run Redis with Docker. The official Redis Docker image is hosted on [Docker Hub](https://hub.docker.com/_/redis).
41+
42+
**TIP:** If you plan on using Docker, we recommend the [redismod](https://hub.docker.com/r/redislabs/redismod) image because it includes the RediSearch and RedisJSON modules.
43+
44+
## Recommended: RediSearch and RedisJSON
45+
46+
Redis OM relies on the [RediSearch][redisearch-url] and [RedisJSON][redis-json-url] Redis modules to support [rich queries](querying.md) and [embedded models](embedded_models.md).
47+
48+
You don't need these Redis modules to use Redis OM's data modeling, validation, and persistence features, but we recommend them to get the most out of Redis OM.
49+
50+
The easiest way to run these Redis modules during local development is to use the [redismod](https://hub.docker.com/r/redislabs/redismod) Docker image.
51+
52+
You can quickly start Redis with the redismod Docker image by running the following command:
53+
54+
docker run -d -p 6379:6379 redislabs/redismod
55+
56+
**TIP:** The `-d` option runs Redis in the background.
57+
58+
For other installation methods, follow the "Quick Start" guides on both modules' home pages for alternative installation methods.
59+
60+
## Start Redis
61+
62+
Before you get started with Redis OM, make sure you start Redis.
63+
64+
The command you use to start Redis will depend on how you installed it.
65+
66+
### Ubuntu Linux (Including WSL)
67+
68+
If you installed Redis using `apt`, start it with the `systemctl` command:
69+
70+
sudo systemctl restart redis.service
71+
72+
Otherwise, you can start the server manually:
73+
74+
redis-server start
75+
76+
### macOS with Homebrew
77+
78+
brew services start redis
79+
80+
### Docker
81+
82+
The command to start Redis with Docker depends on the image you've chosen to use.
83+
84+
#### Docker with the redismod image (recommended)
85+
86+
docker run -d -p 6379:6379 redislabs/redismod
87+
88+
### Docker iwth the redis image
89+
90+
docker run -d -p 6379:6379 redis
91+
92+
## Installing Redis OM
93+
94+
You can install Redis OM with `pip` by running the following command:
95+
96+
pip install redis-om
97+
98+
Or, if you're using Poetry, you can install Redis OM with the following command:
99+
100+
poetry install redis-om
101+
102+
With Pipenv, the command is:
103+
104+
pipenv install redis-om
105+
106+
## Setting the Redis URL Environment Variable
107+
108+
We're almost ready to create a Redis OM model! But first, we need to make sure that Redis OM knows how to connect to Redis.
109+
110+
By default, Redis OM tries to connect to Redis on your localhost at port 6379. Most local install methods will result in Redis running at this location, in which case you don't need to do anything special.
111+
112+
However, if you configured Redis to run on a different port, or if you're using a remote Redis server, you'll need to set the `REDIS_URL` environment variable.
113+
114+
The `REDIS_URL` environment variable follows the redis-py URL format:
115+
116+
redis://[[username]:[password]]@localhost:6379/[database number]
117+
118+
The default connection is eqivalent to the following `REDIS_URL` environment variable:
119+
120+
redis://@localhost:6379
121+
122+
**TIP:** Redis databases are numbered, and the default is 0. You can leave off the database number to use the default database.
123+
124+
Other supported prefixes include "rediss" for SSL connections and "unix" for Unix domain sockets:
125+
126+
rediss://[[username]:[password]]@localhost:6379/0
127+
unix://[[username]:[password]]@/path/to/socket.sock?db=0
128+
129+
For more details about how to connect to Redis with Redis OM, see the [connections documentation](connections.md).
130+
131+
### Redis Cluster Support
132+
133+
Redis OM supports connecting to Redis Cluster, but this preview release does not support doing so with the `REDIS_URL` environment variable. However, you can connect by manually creating a connection object.
134+
135+
See the [connections documentation](connections.md) for examples of connecting to Redis Cluster.
136+
137+
Support for connecting to Redis Cluster via `REDIS_URL` will be added in a future release.
138+
139+
## Defining a Model
140+
141+
In this tutorial, we'll create a `Customer` model that validates and saves data. Let's start with a basic definition of the model. We'll add features as we go along.
142+
143+
```python
144+
import datetime
145+
from typing import Optional
146+
147+
from redis_om.model import (
148+
HashModel,
149+
)
150+
151+
152+
class Customer(HashModel):
153+
first_name: str
154+
last_name: str
155+
email: str
156+
join_date: datetime.date
157+
age: int
158+
bio: str
159+
```
160+
161+
There are a few details to note:
162+
163+
1. Our `Customer` model extends the `HashModel` class. This means that it will be saved to Redis as a hash. The other model class that Redis OM provides is `JsonModel`, which we'll discuss later.
164+
2. We've specified the model's fields using Python type annotations.
165+
166+
Let's dig into these two details a bit more.
167+
168+
### The HashModel Class
169+
170+
When you subclass `HashModel`, your subclass is both a Redis OM model, with methods for saving data to Redis, *and* a Pydantic model.
171+
172+
This means that you can use Pydantic field validations with your Redis OM models, which we'll cover later, when we talk about validation. But this also means you can use Redis OM models anywhere you would use a Pydantic model, like in your FastAPI applications. 🤯
173+
174+
### Type Annotations
175+
176+
The type annotations you add to your model fields are used for a few purposes:
177+
178+
* Validating data with Pydantic validators
179+
* Serializing data Redis
180+
* Deserializing data from Redis
181+
182+
We'll see examples of these throughout the course of this tutorial.
183+
184+
An important detail about the `HashModel` class is that it does not support `list`, `set`, or mapping (like `dict`) types. This is because Redis hashes cannot contain lists, sets, or other hashes.
185+
186+
If you want to model fields with a list, set, or mapping type, or another model, you'll need to use the `JsonModel` class, which can support these types, as well as embedded models.
187+
188+
## Creating Models
189+
190+
Let's see what creating a model object looks like:
191+
192+
```python
193+
andrew = Customer(
194+
first_name="Andrew",
195+
last_name="Brookins",
196+
197+
join_date=datetime.date.today(),
198+
age=38,
199+
bio="Python developer, works at Redis, Inc."
200+
)
201+
```
202+
203+
### Optional Fields
204+
205+
What would happen if we left out one of these fields, like `bio`?
206+
207+
```python
208+
Customer(
209+
first_name="Andrew",
210+
last_name="Brookins",
211+
212+
join_date=datetime.date.today(),
213+
age=38)
214+
```
215+
216+
All fields are required because none of the fields are marked `Optional`, so we get a validation error:
217+
218+
```
219+
ValidationError: 1 validation error for Customer
220+
bio
221+
field required (type=value_error.missing)
222+
```
223+
224+
If we want the `bio` field to be optional, we need to change the type annotation:
225+
226+
```python
227+
class Customer(HashModel):
228+
first_name: str
229+
last_name: str
230+
email: str
231+
join_date: datetime.date
232+
age: int
233+
bio: Optional[str]
234+
```
235+
236+
Now we can create `Customer` objects with or without the `bio` field.
237+
238+
### Default Values
239+
240+
Fields can have default values.
241+
242+
```python
243+
class Customer(HashModel):
244+
first_name: str
245+
last_name: str
246+
email: str
247+
join_date: datetime.date
248+
age: int
249+
bio: Optional[str] = "Super dope"
250+
```
251+
252+
Now, if we create a `Customer` object without a `bio` field, it will use the default value.
253+
254+
```python
255+
andrew = Customer(
256+
first_name="Andrew",
257+
last_name="Brookins",
258+
259+
join_date=datetime.date.today(),
260+
age=38)
261+
262+
print(andrew.bio)
263+
'Super Dope'
264+
```
265+
266+
## Saving Models
267+
268+
## Examining Your Data In Redis
269+
270+
## Validating Data
271+
272+
## Next Steps
273+
274+
Now that you know the basics of working with Redis OM, continue on for all the nitty-gritty details about [models and fields](validation.md).
275+
276+
<!-- Links -->
277+
[redisearch-url]: https://oss.redis.com/redisearch/
278+
[redis-json-url]: https://oss.redis.com/redisjson/

docs/models_and_fields.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Models and Fields
2+
3+
## Introduction
4+
5+
## Saving Data As Hashes With HashModel
6+
7+
### What Does Redis Store?
8+
9+
## Saving Data With JSON With JsonModel
10+
11+
### What Does Redis Store?
12+
13+
## Primary Keys
14+
15+
### Why Primary Keys Matter to Redis OM
16+
17+
### Using the Default Primary Key
18+
19+
### Using a Custom Primary Key
20+
21+
## Meta Classes
22+
23+
## Subclassing Models
24+
25+
### Subclassing and Meta Objects
26+
27+
## Saving Models
28+
29+

docs/testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Testing Your Models

docs/validation.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Validation
22

3-
Redis OM uses [Pydantic](pydantic-url) behind the scenes to validate data at runtime, based on the model's type annotations.
3+
Redis OM uses [Pydantic][pydantic-url] behind the scenes to validate data at runtime, based on the model's type annotations.
44

55
## Basic Type Validation
66

@@ -68,4 +68,6 @@ Once again, we get the valiation error:
6868
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
6969
email
7070
value is not a valid email address (type=value_error.email)
71-
```
71+
```
72+
73+
[pydantic-url]: https://github.com/samuelcolvin/pydantic

0 commit comments

Comments
 (0)