Object mapping, and more, for Redis and Python
Redis OM Python makes it easy to model Redis data in your Python applications.
Redis OM .NET | Redis OM Node.js | Redis OM Spring | Redis OM Python
Table of contents
span - [💡 Why Redis OM?](..#-why-redis-om) - [📇 Modeling Your Data](..#-modeling-your-data) - [✓ Validating Data With Your Model](..#-validating-data-with-your-model) - [🔎 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) - [✨ So How Do You Get RediSearch and RedisJSON?](..#-so-how-do-you-get-redisearch-and-redisjson) - [❤️ Contributing](..#️-contributing) - [📝 License](..#-license)💡 Why Redis OM?¶
Redis OM provides high-level abstractions that make it easy to model and query data in Redis with modern Python applications.
This preview release contains the following features:
- Declarative object mapping for Redis objects
- Declarative secondary-index generation
- Fluent APIs for querying Redis
📇 Modeling Your Data¶
Redis OM contains powerful declarative models that give you data validation, serialization, and persistence to Redis.
Check out this example of modeling customer data with Redis OM. First, we create a Customer model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Now that we have a Customer model, let's use it to save customer data to Redis.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
Ready to learn more? Check out the getting started guide.
Or, continue reading to see how Redis OM makes data validation a snap.
✓ Validating Data With Your Model¶
Redis OM uses Pydantic to validate data based on the type annotations you assign to fields in a model class.
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!
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
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!
To learn more, see the documentation on data validation.
🔎 Rich Queries and Embedded Models¶
Data modeling, validation, and saving models to Redis all work regardless of how you run Redis.
Next, we'll show you the rich query expressions and embedded models Redis OM provides when the RediSearch and RedisJSON modules are installed in your Redis deployment, or you're using Redis Enterprise.
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? section of this README.
Querying¶
Redis OM comes with a rich query language that allows you to query Redis with Python expressions.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
These queries -- and more! -- are possible because Redis OM manages indexes for you automatically.
Querying with this index features a rich expression syntax inspired by the Django ORM, SQLAlchemy, and Peewee. We think you'll enjoy it!
Note: Indexing only works for data stored in Redis logical database 0. If you are using a different database number when connecting to Redis, you can expect the code to raise a MigrationError when you run the migrator.
Embedded Models¶
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.
In the next example, we'll define a new Address model and embed it within the Customer model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The parameters expected by each command function are those documented on the command's page on redis.io.
If you don't want to get a Redis connection from a model class, you can also use get_redis_connection:
1 2 3 4 | |
💻 Installation¶
Installation is simple with pip, Poetry, or Pipenv.
1 2 3 4 5 | |
📚 Documentation¶
The Redis OM documentation is available here.
⛏️ Troubleshooting¶
If you run into trouble or have any questions, we're here to help!
Hit us up on the Redis Discord Server or open an issue on GitHub.
✨ So How Do You Get RediSearch and RedisJSON?¶
Some advanced features of Redis OM rely on core features from two source available Redis modules: RediSearch and RedisJSON.
You can run these modules in your self-hosted Redis deployment, or you can use Redis Enterprise, which includes both modules.
To learn more, read our documentation.
❤️ Contributing¶
We'd love your contributions!
Bug reports are especially helpful at this stage of the project. You can open a bug report on GitHub.
You can also contribute documentation -- or just let us know if something needs more detail. Open an issue on GitHub to get started.
📝 License¶
Redis OM uses the MIT license.