Skip to content

Commit a538dd6

Browse files
author
Simon Prickett
authored
Merge pull request #169 from redis/document-using-redis-commands-#81
Document using redis commands #81
2 parents 8a8c94f + a9abb92 commit a538dd6

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ span
3535
- [🔎 Rich Queries and Embedded Models](#-rich-queries-and-embedded-models)
3636
- [Querying](#querying)
3737
- [Embedded Models](#embedded-models)
38+
- [Calling Other Redis Commands](#calling-other-redis-commands)
3839
- [💻 Installation](#-installation)
3940
- [📚 Documentation](#-documentation)
4041
- [⛏️ Troubleshooting](#️-troubleshooting)
@@ -288,6 +289,38 @@ Customer.find(Customer.address.city == "San Antonio",
288289
Customer.address.state == "TX")
289290
```
290291

292+
## Calling Other Redis Commands
293+
294+
Sometimes you'll need to run a Redis command directly. Redis OM supports this through the `db` method on your model's class. This returns a connected Redis client instance which exposes a function named for each Redis command. For example, let's perform some basic set operations:
295+
296+
```python
297+
from redis_om import HashModel
298+
299+
class Demo(HashModel):
300+
some_field: str
301+
302+
redis_conn = Demo.db()
303+
304+
redis_conn.sadd("myset", "a", "b", "c", "d")
305+
306+
# Prints False
307+
print(redis_conn.sismember("myset", "e"))
308+
309+
# Prints True
310+
print(redis_conn.sismember("myset", "b"))
311+
```
312+
313+
The parameters expected by each command function are those documented on the command's page on [redis.io](https://redis.io/commands/).
314+
315+
If you don't want to get a Redis connection from a model class, you can also use `get_redis_connection`:
316+
317+
```python
318+
from redis_om import get_redis_connection
319+
320+
redis_conn = get_redis_conection()
321+
redis_conn.set("hello", "world")
322+
```
323+
291324
## 💻 Installation
292325

293326
Installation is simple with `pip`, Poetry, or Pipenv.

docs/getting_started.md

+32
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,38 @@ Customer.find((Customer.last_name == "Brookins") | (
693693
) & (Customer.last_name == "Smith")).all()
694694
```
695695

696+
## Calling Other Redis Commands
697+
698+
Sometimes you'll need to run a Redis command directly. Redis OM supports this through the `db` method on your model's class. This returns a connected Redis client instance which exposes a function named for each Redis command. For example, let's perform some basic set operations:
699+
700+
```python
701+
from redis_om import HashModel
702+
703+
class Demo(HashModel):
704+
some_field: str
705+
706+
redis_conn = Demo.db()
707+
708+
redis_conn.sadd("myset", "a", "b", "c", "d")
709+
710+
# Prints False
711+
print(redis_conn.sismember("myset", "e"))
712+
713+
# Prints True
714+
print(redis_conn.sismember("myset", "b"))
715+
```
716+
717+
The parameters expected by each command function are those documented on the command's page on [redis.io](https://redis.io/commands/).
718+
719+
If you don't want to get a Redis connection from a model class, you can also use `get_redis_connection`:
720+
721+
```python
722+
from redis_om import get_redis_connection
723+
724+
redis_conn = get_redis_conection()
725+
redis_conn.set("hello", "world")
726+
```
727+
696728
## Next Steps
697729

698730
Now that you know the basics of working with Redis OM, start playing around with it in your project!

0 commit comments

Comments
 (0)