Skip to content

Commit 0d74f4e

Browse files
author
Andrew Brookins
committed
Add connection docs
1 parent 6dbeb5e commit 0d74f4e

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

docs/connections.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Connecting to Redis
2+
3+
You can control how Redis OM connects to Redis with the `REDIS_OM_URL` environment variable, or by manually constructing Redis client objects.
4+
5+
## Environment Variable
6+
7+
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 for Redis OM to connect to Redis.
8+
9+
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_OM_URL` environment variable.
10+
11+
The `REDIS_OM_URL` environment variable follows the redis-py URL format:
12+
13+
redis://[[username]:[password]]@localhost:6379/[database number]
14+
15+
**NOTE:** The square brackets indicate an optional value and are not part of the URL format.
16+
17+
The default connection is equivalent to the following `REDIS_OM_URL` environment variable:
18+
19+
redis://localhost:6379
20+
21+
### Passwords and Usernames
22+
23+
Redis can be configured with password protection and a "default" user, in which case you might connect using only the password.
24+
25+
You can do so with Redis OM like this:
26+
27+
redis://:your-password@localhost:6379
28+
29+
If your Redis instance requires both a username and a password, you would include both in the URL:
30+
31+
redis://your-username:your-password@localhost:6379
32+
33+
### Database Number
34+
35+
Redis databases are numbered, and the default is 0. You can leave off the database number to use the default database, or specify it.
36+
37+
### SSL Connections
38+
39+
Use the "rediss" prefix for SSL connections:
40+
41+
rediss://[[username]:[password]]@localhost:6379/0
42+
43+
### Unix Domain Sockets
44+
45+
Use the "unix" prefix to connect to Redis over Unix domain sockets:
46+
47+
unix://[[username]:[password]]@/path/to/socket.sock?db=0
48+
49+
### To Learn More
50+
51+
To learn more about the URL format that Redis OM Python uses, consult [redis-py's URL documentation](https://redis-py.readthedocs.io/en/stable/#redis.Redis.from_url).
52+
53+
**TIP:** The URL format is the same if you're using async or sync mode with Redis OM (i.e., importing `aredis_om` for async or `redis_om` for sync).
54+
55+
## Connection Objects
56+
57+
Aside from controlling connections via the `REDIS_OM_URL` environment variable, you can manually construct Redis client connections per Redis OM model.
58+
59+
This method takes precedence over the `REDIS_OM_URL` environment variable.
60+
61+
You can control the connection a specific model class should use by assigning an object to the *database* field of a model's _Meta_ object, like so:
62+
63+
```python
64+
from redis_om import HashModel, get_redis_connection
65+
66+
67+
redis = get_redis_connection(port=6378)
68+
69+
70+
class Customer(HashModel):
71+
first_name: str
72+
last_name: str
73+
age: int
74+
75+
class Meta:
76+
database = redis
77+
```
78+
79+
The `get_redis_connection()` function is a Redis OM helper that passes keyword arguments to either `aioredis.Redis.from_url()` or `redis.Redis.from_url()`, depending on whether you are using Redis OM in async or sync mode.
80+
81+
You can also manually construct a client object:
82+
83+
```python
84+
from redis import Redis
85+
86+
from redis_om import HashModel
87+
88+
89+
class Customer(HashModel):
90+
first_name: str
91+
last_name: str
92+
age: int
93+
94+
class Meta:
95+
database = Redis(port=6378)
96+
```

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Welcome! This is the index of documentation for redis-om-python.
88

99
Read the Getting Started tutorial at [getting_started.md](getting_started.md).
1010

11+
## Connecting to Redis
12+
13+
Read about connecting to Redis at [connections.md](connections.md).
14+
1115
## Validating Data
1216

1317
Read about how to use Redis OM models to validate data at [validation.md](validation.md)

0 commit comments

Comments
 (0)