Recto is an Elixir library for defining schemas and interacting with Redis, similar to Ecto for databases. It provides a straightforward API to manage data stored in Redis, including support for lists, sets, and sorted sets.
- Define schemas for your Redis data structures
- Easy-to-use API for common Redis operations
- Support for lists, sets, and sorted sets
- Flexible configuration using a Supervisor-based application
If available in Hex, the package can be installed
by adding recto to your list of dependencies in mix.exs:
def deps do
[
{:recto, "~> 0.1.0"}
]
endThen, run mix deps.get to fetch the dependencies.
To define a schema, use Recto.Schema in your module. You can define fields with different types and specify default values.
defmodule MyApp.MySchema do
use Recto.Schema
schema "my_schema" do
field :id, :integer
field :name, :string, default: "unknown"
field :age, :integer
field :tags, {:array, :string}
field :attributes, {:map, :string}
timestamps()
end
endRecto.Repo provides an interface to interact with Redis. Below is an example of how to use it.
defmodule MyApp.Repo do
use Recto.Repo, otp_app: :my_app, adapter: MyApp.RedisAdapter
endschema = %MyApp.MySchema{id: 1, name: "Alice", age: 30, tags: ["elixir", "redis"]}
MyApp.Repo.set("user:1", schema)
{:ok, retrieved_schema} = MyApp.Repo.get("user:1")MyApp.Repo.rpush("my_list", schema)
{:ok, first_item} = MyApp.Repo.lpop("my_list")MyApp.Repo.sadd("my_set", schema)
{:ok, members} = MyApp.Repo.smembers("my_set")MyApp.Repo.zadd("my_sorted_set", 1, schema)
{:ok, rank} = MyApp.Repo.zrank("my_sorted_set", schema)Configure Recto in your application’s configuration file (config/config.exs):
config :my_app, MyApp.Repo,
adapter: MyApp.RedisAdapter,
host: "localhost",
port: 6379Add the Recto.Application to your supervision tree:
defmodule MyApp.Application do
use Application
def start(_type, _args) do
children = [
MyApp.Repo,
# other children
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
endHere is an example test using ExUnit:
defmodule MyApp.MySchemaTest do
use ExUnit.Case
alias MyApp.{Repo, MySchema}
test "can set and get schema data" do
schema = %MySchema{id: 1, name: "Test"}
assert {:ok, "OK"} = Repo.set("test_key", schema)
assert {:ok, ^schema} = Repo.get("test_key")
end
endThis library is licensed under the MIT License. See the LICENSE.txt file for details.