Skip to content

Commit

Permalink
Merge pull request #169 from redis/document-using-redis-commands-#81
Browse files Browse the repository at this point in the history
Document using redis commands #81
  • Loading branch information
Simon Prickett authored Mar 29, 2022
2 parents 8a8c94f + a9abb92 commit a538dd6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ span
- [🔎 Rich Queries and Embedded Models](#-rich-queries-and-embedded-models)
- [Querying](#querying)
- [Embedded Models](#embedded-models)
- [Calling Other Redis Commands](#calling-other-redis-commands)
- [💻 Installation](#-installation)
- [📚 Documentation](#-documentation)
- [⛏️ Troubleshooting](#️-troubleshooting)
Expand Down Expand Up @@ -288,6 +289,38 @@ Customer.find(Customer.address.city == "San Antonio",
Customer.address.state == "TX")
```

## Calling Other Redis Commands

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:

```python
from redis_om import HashModel

class Demo(HashModel):
some_field: str

redis_conn = Demo.db()

redis_conn.sadd("myset", "a", "b", "c", "d")

# Prints False
print(redis_conn.sismember("myset", "e"))

# Prints True
print(redis_conn.sismember("myset", "b"))
```

The parameters expected by each command function are those documented on the command's page on [redis.io](https://redis.io/commands/).

If you don't want to get a Redis connection from a model class, you can also use `get_redis_connection`:

```python
from redis_om import get_redis_connection

redis_conn = get_redis_conection()
redis_conn.set("hello", "world")
```

## 💻 Installation

Installation is simple with `pip`, Poetry, or Pipenv.
Expand Down
32 changes: 32 additions & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,38 @@ Customer.find((Customer.last_name == "Brookins") | (
) & (Customer.last_name == "Smith")).all()
```

## Calling Other Redis Commands

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:

```python
from redis_om import HashModel

class Demo(HashModel):
some_field: str

redis_conn = Demo.db()

redis_conn.sadd("myset", "a", "b", "c", "d")

# Prints False
print(redis_conn.sismember("myset", "e"))

# Prints True
print(redis_conn.sismember("myset", "b"))
```

The parameters expected by each command function are those documented on the command's page on [redis.io](https://redis.io/commands/).

If you don't want to get a Redis connection from a model class, you can also use `get_redis_connection`:

```python
from redis_om import get_redis_connection

redis_conn = get_redis_conection()
redis_conn.set("hello", "world")
```

## Next Steps

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

0 comments on commit a538dd6

Please sign in to comment.