Skip to main content

relations-redis

DB/API Modeling for Redis

Relations overall is designed to be a simple, straight forward, flexible DIL (data interface layer).

Quite different from other DIL's, it has the singular, microservice based purpose to:

  • Create models with very little code, independent of backends
  • Create CRUD API with a database backend from those models with very little code
  • Create microservices to use those same models but with that CRUD API as the backend

Ya, that last one is kinda new I guess.

Say we create a service, composed of microservices, which in turn is to be consumed by other services made of microservices.

You should only need to define the model once. Your conceptual structure is the same, to the DB, the API, and anything using that API. You shouldn't have say that structure over and over. You shouldn't have to define CRUD endpoints over and over. That's so boring, tedious, and unnecessary.

Furthermore, the conceptual structure is based not the backend of what you've going to use at that moment of time (scaling matters) but on the relations, how the pieces interact. If you know the structure of the data, that's all you need to interact with the data.

So with Relations, Models and Fields are defined independent of any backend, which instead is set at runtime. So the API will use a DB, everything else will use that API.

This is just the Redis backend of models and what not.

Unlike the SQL backends, Redis is a key-value store with no query engine. Each record is stored as one JSON string keyed by <prefix>:<model>:<id>, with an atomic INCR counter allocating ids. Id-based reads/writes/deletes are direct GET/SET/DEL; any other filter is resolved client-side by scanning the model's keys and matching in Python (the same record matcher every backend uses) — so the model-facing API is identical, but a non-id filter is a full scan rather than an indexed query.

Don't have great docs yet so I've included some of the unittests to show what's possible.

Example

define

import relations
import relations_redis

# The source is a string, the backend of which is defined at runtime

class SourceModel(relations.Model):
    SOURCE = "RedisSource"

class Simple(SourceModel):
    id = int
    name = str

class Plain(SourceModel):
    ID = None # This table has no primary id field
    simple_id = int
    name = str

# This makes Simple a parent of Plain

relations.OneToMany(Simple, Plain)

# With this statement, all the above models now have this Redis as a backend.
# Redis is schemaless, so there's no define()/migrate() step — just connect.

self.source = relations_redis.Source("RedisSource", host="localhost", port=6379)

create

simple = Simple("sure")
simple.plain.add("fine")

simple.create()

self.assertEqual(simple.id, 1)
self.assertEqual(simple._action, "update")
self.assertEqual(simple._record._action, "update")
self.assertEqual(simple.plain[0].simple_id, 1)
self.assertEqual(simple.plain._action, "update")
self.assertEqual(simple.plain[0]._record._action, "update")

# Each record is one JSON string keyed by <prefix>:<model>:<id>

self.assertEqual(
    json.loads(self.source.connection.get("simple:1")),
    {"id": 1, "name": "sure"}
)

simples = Simple.bulk().add("ya").create()
self.assertEqual(simples._models, [])

self.assertEqual(
    json.loads(self.source.connection.get("simple:2")),
    {"id": 2, "name": "ya"}
)

self.assertEqual(
    json.loads(self.source.connection.get("plain:1")),
    {"simple_id": 1, "name": "fine"}
)

Metadata

Release files for relations-redis 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for relations-redis 0.2.5
File Size Uploaded Provenance
relations_redis-0.2.5.tar.gz 11.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for relations-redis 0.2.5
File Interpreter ABI Platform
relations_redis-0.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / relations_redis-0.2.5.tar.gz

Download URL relations_redis-0.2.5.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
44c99ff098c086c3950bf5c66b3e8fb3afd4c374f24f9c34c7611d86df652325
BLAKE2b-256 checksum
How to use checksums
5482d88b4cb61feebd258dcfd3be4073732341576a99632deb6b6fadad347762
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / relations_redis-0.2.5-py3-none-any.whl

Download URL relations_redis-0.2.5-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
483fa68bf3d1604a0e8dbe35ab4e56a5eb6a11ee5d5b08c339a7944107343a69
BLAKE2b-256 checksum
How to use checksums
6250f6b03d9c7e3b8219db8b8959222f503719897c18216bbd8df57e31bda1c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for relations-redis 0.2.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page