Skip to main content

DB/API Modeling for Redis

Project description

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"}
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

relations_redis-0.2.2.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

relations_redis-0.2.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file relations_redis-0.2.2.tar.gz.

File metadata

  • Download URL: relations_redis-0.2.2.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for relations_redis-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ddd8f480df28b95b2d4c0c8b8e21c6eab3abaab97f79d189d2c57e5aa2cd08e0
MD5 01e4b3f287d1feef03a610a4e990afd6
BLAKE2b-256 31e4cbd2898633ee819da48e0476b86c6b9a5aa0eb9174627b336a72f2af2a04

See more details on using hashes here.

File details

Details for the file relations_redis-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for relations_redis-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 67d275a6301fd4da1703576badc4e2aed552e0297a693dd2bacad38185927ba4
MD5 d67e2be648b1967f769759715b2ec79f
BLAKE2b-256 2daafdeedb482210eb4dbff36c5d34315569116c6c1489dc8388a2d2eb36d0ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page