Remodel
remodel -- a minimal ORM (object-redis-mapper) in a few hundred lines of ruby
Install / Use
/learn @tlossen/RemodelREADME
remodel your storage layer
use redis instead of mysql to store your application data.
remodel (= redis model) is an ActiveRecord-like mapping layer which offers familiar syntax
like has_many, has_one etc. to build your domain model in ruby.
entities are serialized to json and stored as fields in a redis hash. using different hashes (called 'contexts' in remodel), you can easily separate data belonging to multiple users, for example.
why redis?
redis offers in-memory read and write performance — on the order of 10K to 100K operations per second, comparable to memcached — plus asynchronous persistence to disk. for example, on my macbook (2 ghz):
$ redis-benchmark -d 100 -r 10000 -q
SET: 13864.27 requests per second
GET: 18152.17 requests per second
INCR: 17006.80 requests per second
LPUSH: 17243.99 requests per second
LPOP: 18706.54 requests per second
how to get started
-
install redis and the redis-rb ruby client:
$ brew install redis $ gem install redis -
start redis:
$ ./redis-server -
now the tests should run successfully:
$ rake Started ....................................................................................... Finished in 0.072304 seconds. 87 tests, 138 assertions, 0 failures, 0 errors
example
define your domain model like this:
class Book < Remodel::Entity
has_many :chapters, :class => 'Chapter'
property :title, :short => 't', :class => 'String'
property :year, :class => 'Integer'
property :author, :class => 'String', :default => '(anonymous)'
end
class Chapter < Remodel::Entity
property :title, :class => String
end
now you can do:
>> require './example/book'
=> true
>> context = Remodel.create_context('shelf')
=> #<Remodel::Context(shelf)>
>> book = Book.create context, :title => 'Moby Dick', :year => 1851
=> #<Book(shelf, 1) title: "Moby Dick", year: 1851, author: "(anonymous)">
>> chapter = book.chapters.create :title => 'Ishmael'
=> #<Chapter(shelf, 1) title: "Ishmael">
all entities have been created in the redis hash 'shelf' we have used as context:
>> Remodel.redis.hgetall 'shelf'
=> {"b"=>"1", "b1"=>"{\"t\":\"Moby Dick\",\"year\":1851}", "c"=>"1",
"c1"=>"{\"title\":\"Ishmael\"}", "c1_book"=>"b1", "b1_chapters"=>"[\"c1\"]"}
inspired by
- how to redis — good overview of different mapping options by mattmatt.
- hurl — basically defunkts Hurl::Model is what i started with.
- ohm — object-hash mapping for redis. somewhat similar, but instead of serializing to json, stores each attribute under a separate key.
todo
- better docs
- make serializer (json, messagepack, marshal ...) configurable
status
it has some rough edges, but i have successfully been using remodel in production since summer 2010.
license
Related Skills
node-connect
354.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
112.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
354.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
354.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
