{"id":18492293,"url":"https://github.com/makevoid/redis_rejson_models","last_synced_at":"2025-04-08T21:31:15.552Z","repository":{"id":45370438,"uuid":"91628167","full_name":"makevoid/redis_rejson_models","owner":"makevoid","description":"Document-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation ","archived":false,"fork":false,"pushed_at":"2021-12-21T20:17:15.000Z","size":108,"stargazers_count":3,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T18:37:44.989Z","etag":null,"topics":["json","key-value-store","orm","re-json","redis","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/makevoid.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-17T23:16:46.000Z","updated_at":"2021-09-24T05:10:53.000Z","dependencies_parsed_at":"2022-09-06T17:51:07.854Z","dependency_job_id":null,"html_url":"https://github.com/makevoid/redis_rejson_models","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makevoid%2Fredis_rejson_models","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makevoid%2Fredis_rejson_models/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makevoid%2Fredis_rejson_models/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makevoid%2Fredis_rejson_models/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/makevoid","download_url":"https://codeload.github.com/makevoid/redis_rejson_models/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247931276,"owners_count":21020203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","key-value-store","orm","re-json","redis","ruby"],"created_at":"2024-11-06T13:06:57.531Z","updated_at":"2025-04-08T21:31:10.542Z","avatar_url":"https://github.com/makevoid.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RedisRejsonModels\n\nDocument-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation\n\n### Install\n\nAdd to your gemfile:\n\n```rb\ngem 'redis_rejson_models', '~\u003e 0.9.5'\n```\n\nOr without bundler, run in your shell:\n\n    gem i redis_rejson_models\n\n\n### Requirements\n\n- Redis (v4+)\n- Re-JSON module installed\n\n\n### Instance API:\n\nHere's a brief walktrough of how the Instance API works\n\nIn the code that folows, we defined a `Document` model, we did that by including the `ShallowAttributes` and the `redis_rejson_models` mixins and by defining some properties/attributes.\n\nHere's an example code that you should look at first, RedisRejsonModel's usage.\n\nIn this first code sample we have an object instantiated via the `Document` class that has 2 attribute accessors: `.name` and `.content \n\nWe call this object `doc`, an object which not only has the properties `name` and `contents`, it has also a `.save` method.\n\n`.save()` saves the object as a  json object in redis using the rejson indexing, here's the json object saved in redis:\n\n```rb\n{ id: 234, name: \"Foo\", contents: \"Bar123\" }\n```\n\nin JSON format:\n\n```js\n{ \"id\": 234, \"name\": \"Foo\", \"contents\": \"Bar123\" }\n```\n\nHere's the first sample code showing the instance api in all his glory:\n\n```ruby\ndoc = Document.new name: \"Foo\", contents: \"Bar123\"\nputs doc.name      #=\u003e \"foo\"\nputs doc.antani    #=\u003e \"...\"\ndoc.save # saves the record, gives an auto increment id (example: 1)\n\ndoc = Document.get doc.id\nputs doc.name      #=\u003e \"foo\"\nputs doc.antani    #=\u003e \"...\"\n```\n\nNote: We use the `shallow_attributes` gem (already required by `redis_rejson_models` that provides the main ORM Model-like familiar ruby API that most ruby ORMs have / used to have)\n\n\n### Query API:\n\nRedisRejsonModels not only has the `name` has also an instance api\n\n```rb\nDocument.get 123 #=\u003e returns a single redis rejson object as a whole { id: 123, name: \"Foo\", contents: \"...\" }\n\nDocument.get_attr 123, \"name\" #=\u003e \"name\" # this is the fastest method to read an attribute, even if your json gets big, by using `Model.get_attr` you can be sure that you've got a very quick access and retrieval of that data\n\nDocument.all #=\u003e returns a list of hashes querying all the entries from redis [{ id: 123, name: ... }, { id: 234, name: ... }, ...]\n\n```\n\n### Create / Update\n\nIf you don't like setting properties and invoking `.save()` you should check out how RedisRejsonModels handles with a straightforward class-method-based api the creation and update of json records in redis. \n\n```rb\n# Document.create attrs\ndoc = Document.create name: \"Foo\", contents: \"...\" \ndoc.id #=\u003e has the new auto increment id (e.g. `123`)\n\n# Document.update id, attrs\nDocument.update 123, name: \"Foo2\" \n\ndoc = Document.get 123 \ndoc.name #=\u003e \"Foo2\"\n```\n\n\n### Model Definition:\n\nFinally by looking at the definition we can clearly see that it's very sensible, we use ShallowAttributes attributes and RedisRejsonModels gives us all the extra method for persisting the model data.\n\n```ruby\nclass Document\n  extend  RedisRejsonModelLib\n  include RedisRejsonModelMixin\n  include ShallowAttributes\n\n  attribute :id,        Integer\n  attribute :name,      String\n  attribute :contents,  String\n\n  def antani\n    \"hi #{name}, what do you think about #{contents} ?\"\n  end\nend\n```\n\ntreat your models as a normal ruby object (not even a PORO, just a normal object :D) and you'll go long way, maximum testability, crazy redis speeds and it's just json data, treated as a document-based database. \n\n\n### Run local Redis (+ Re-JSON module) locally via Docker:\n\nin this repo run:\n\n```\ndocker-compose up\n```\n\nWhich use the docker-compose.yml to start your redis.\n\n\n### Set up your Redis and RJSON\n\n```rb\n# sample environment setup\nrequire 'bundler'\nBundler.require :default\n\n# setup redis\nR = Redis.new\n\n# load and configure redis with rejson models\ninclude RedisRejsonModels\nRJ.configure redis: R\n\nclass Document\n  # ...\nend\n```\n\nCheck a sample project contained in the `example` dir of this repo: https://github.com/makevoid/redis_rejson_models/tree/master/example/default\n\n\n### Run specs\n\n(you need to run a local redis + rejson service - explained above)\n\nRun:\n\n```\nrake spec\n```\n\n\n---\n\nEnjoy!\n\n@makevoid\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakevoid%2Fredis_rejson_models","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmakevoid%2Fredis_rejson_models","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakevoid%2Fredis_rejson_models/lists"}