You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7.0 KiB

Redis Velvet

Objecting mapping and more, for Redis.


Version License Build Status

Redis Velvet is a library that helps you build modern Python applications with Redis.

Redis Velvet Python | Redis Velvet Node.js | Redis Velvet Spring | Redis Velvet .NET

Table of contents

➡ Why Redis Velvet?

Redis Velvet is a library of high-level tools that help you build modern Python applications with Redis.

This preview release includes our first major component: a declarative model class backed by Redis.

🏁 Getting started

Object Mapping

With Redis Velvet, you get powerful data modeling, validation, and query expressions with a small amount of code. Check out this example:

import datetime
from typing import Optional

from redis_developer.model import (
    EmbeddedJsonModel,
    JsonModel,
    Field,
)

class Address(EmbeddedJsonModel):
    address_line_1: str
    address_line_2: Optional[str]
    city: str = Field(index=True)
    state: str = Field(index=True)
    country: str
    postal_code: str = Field(index=True)


class Customer(JsonModel):
    first_name: str = Field(index=True)
    last_name: str = Field(index=True)
    email: str = Field(index=True)
    join_date: datetime.date
    age: int = Field(index=True)
    bio: Optional[str] = Field(index=True, full_text_search=True,
                               default="")

    # Creates an embedded model.
    address: Address

The example code defines Address and Customer models for use with a Redis database with the RedisJSON module installed.

With these two classes defined, you can now:

  • Validate data based on the model's type annotations using Pydantic
  • Persist model instances to Redis as JSON
  • Instantiate model instances from Redis by primary key (a client-generated ULID)
  • Query on any indexed fields in the models

Querying

Querying uses a rich expression syntax inspired by the Django ORM, SQLAlchemy, and Peewee.

Here are a few example queries that use the models we defined earlier:

# Find all customers with the last name "Brookins"
Customer.find(Customer.last_name == "Brookins").all()

# Find all customers that do NOT have the last name "Brookins"
Customer.find(Customer.last_name != "Brookins").all()
 
# Find all customers whose last name is "Brookins" OR whose age is 
# 100 AND whose last name is "Smith"
Customer.find((Customer.last_name == "Brookins") | (
    Customer.age == 100
) & (Customer.last_name == "Smith")).all()

# Find all customers who live in San Antonio, TX
Customer.find(Customer.address.city == "San Antonio",
              Customer.address.state == "TX")

Ready to learn more? Read the getting started guide or check out how to add Redis Velvet to your FastAPI project.

RediSearch and RediJSON

Redis Velvet relies on core features from two source available Redis modules: RediSearch and RedisJSON.

RediSearch is a module that adds querying and full-text search to Redis, while RedisJSON adds support for the JSON data type to Redis.

Why this is important

Without RediSearch or RedisJSON installed, you can still use Redis Velvet to create declarative models backed by Redis. We'll store your model data in Redis as Hashes, and you can retrieve models using their primary keys. You'll also get all the validation features from Pydantic.

So, what won't work without these modules?

  1. Without RedisJSON, you won't be able to nest models inside each other, like we did with the example model of a Customer model that has an Address embedded inside it. This is because Redis Velvet will store your models in Redis as Hashes, which can't contain other container types like Lists or Hashes.
  2. Without RediSearch, you won't be able to use our expressive queries to find models -- just the primary key.

So how do you get RediSearch and RedisJSON?

You can use RediSearch and RedisJSON with your self-hosted Redis deployment. Just follow the instructions on installing the binary versions of the modules in their Quick Start Guides:

RediSearch and RedisJSON are also available on all Redis Cloud managed services. Get started here.

💻 Installation

Installation is simple with pip, Poetry, or Pipenv.

$ pip install redis-velvet

# Or, using Poetry
$ poetry add redis-velvet

📚 Documentation

Documentation is available here.

⛏️ Troubleshooting

If you run into trouble or have any questions, we're here to help!

First, check the FAQ. If you don't find the answer there, hit us up on the Redis Discord Server.

❤️ Contributing

We'd love your contributions!

Bug reports are especially helpful at this stage of the project. You can open a big 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 Velvet is MIT licensed.