redijson_models 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +6 -0
- data/Readme.md +81 -0
- data/lib/env.rb +14 -0
- data/lib/redijson_models.rb +101 -0
- data/lib/version.rb +3 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: da409841ddb0e2d3251b356e3c8ec4323fde45d72ff59616c8b45a66321fec1f
|
4
|
+
data.tar.gz: f021b1e4ad6e756bc5901b81dff85f6eb58a73ff44b1fc0281faae37332ef750
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9754b0ba903cf06f031c59868eacdece3edd63a83b4715b4f91f2aec5692efae336cd40c9564a8be4df9794932c085c673abe1047daaf6c883d74fbd030a1ddb
|
7
|
+
data.tar.gz: b69e219eb00f1e5172a132a56ff208c987bd606c2728a57ab40ff4d406b7a4cadd7809694c538c3073afc2e3b8f040dc44af48d6131e9fb4e784db4da6b3560c
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# RediJsonModels
|
2
|
+
|
3
|
+
Document-like ORM based on Redis key value store - uses Redis Re-JSON for full json manipulation
|
4
|
+
|
5
|
+
### Requirements
|
6
|
+
|
7
|
+
- Redis (v4+)
|
8
|
+
- Re-JSON module installed
|
9
|
+
|
10
|
+
### DB API
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
Model.get 1 #=> returns an object instance (gets it from redis)
|
14
|
+
|
15
|
+
Model.all #=> returns an array of Model object instances
|
16
|
+
|
17
|
+
Model.create({}) #=> creates an entry in redis
|
18
|
+
|
19
|
+
Model.update(1, {}) #=> updates a record in redis
|
20
|
+
```
|
21
|
+
|
22
|
+
### Model Definition and Instance API by Virtus
|
23
|
+
|
24
|
+
#### Definition:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Document
|
28
|
+
extend RediJsonModelLib
|
29
|
+
include RediJsonModelMixin
|
30
|
+
include Virtus.model
|
31
|
+
|
32
|
+
attribute :id, Integer
|
33
|
+
attribute :name, String
|
34
|
+
attribute :contents, String
|
35
|
+
|
36
|
+
def antani
|
37
|
+
# "..."
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Instance API:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
doc = Document.new name: "Foo", contents: "Bar123"
|
46
|
+
puts doc.name #=> "foo"
|
47
|
+
puts doc.antani #=> "..."
|
48
|
+
doc.save # saves the record, gives an auto increment id (example: 1)
|
49
|
+
|
50
|
+
doc = Document.get doc.id
|
51
|
+
puts doc.name #=> "foo"
|
52
|
+
puts doc.antani #=> "..."
|
53
|
+
```
|
54
|
+
|
55
|
+
### Run local Redis (+ Re-JSON module) locally via Docker:
|
56
|
+
|
57
|
+
in this repo run:
|
58
|
+
|
59
|
+
```
|
60
|
+
docker-compose up
|
61
|
+
```
|
62
|
+
|
63
|
+
Which use the docker-compose.yml to start your redis.
|
64
|
+
|
65
|
+
|
66
|
+
### Run specs
|
67
|
+
|
68
|
+
(you need to run a local redis + rejson service - explained above)
|
69
|
+
|
70
|
+
Run:
|
71
|
+
|
72
|
+
```
|
73
|
+
rake spec
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
Enjoy!
|
80
|
+
|
81
|
+
@makevoid
|
data/lib/env.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# require 'bundler/setup'
|
2
|
+
# Bundler.require :default
|
3
|
+
require 'redis'
|
4
|
+
require 'virtus'
|
5
|
+
require 'oj'
|
6
|
+
require 'dry-inflector'
|
7
|
+
# require 'redis_rejson'
|
8
|
+
require 'rejson'
|
9
|
+
|
10
|
+
module RediJsonModels
|
11
|
+
REDIS_DB = ENV["REDIS_DB"]
|
12
|
+
|
13
|
+
R = Redis.new db: REDIS_DB || 0
|
14
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative 'env'
|
2
|
+
|
3
|
+
module RediJsonModels
|
4
|
+
|
5
|
+
# low level API
|
6
|
+
class RJ
|
7
|
+
def self.configure(redis:)
|
8
|
+
@@redis = redis
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.redis
|
12
|
+
@@redis
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.[](key)
|
16
|
+
redis.json_get key, Rejson::Path.root_path
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.[]=(key, val)
|
20
|
+
redis.json_set key, Rejson::Path.root_path, val
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module RediJsonModelLib
|
25
|
+
INFLECTOR = Dry::Inflector.new
|
26
|
+
|
27
|
+
def all
|
28
|
+
1.upto(count).map do |entry_id|
|
29
|
+
get entry_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def count
|
34
|
+
( RJ["#{resource}:count"] || 0 ).to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
def get(id)
|
38
|
+
data = RJ["#{resource}:#{id}"]
|
39
|
+
return unless data
|
40
|
+
data = Oj.load data
|
41
|
+
new data
|
42
|
+
end
|
43
|
+
|
44
|
+
def create(attrs)
|
45
|
+
id = incr
|
46
|
+
attrs.merge! id: id
|
47
|
+
obj = new attrs
|
48
|
+
data = Oj.dump obj.attributes
|
49
|
+
RJ["#{resource}:#{id}"] = data
|
50
|
+
obj
|
51
|
+
end
|
52
|
+
|
53
|
+
def update(id, attrs)
|
54
|
+
resource = get id
|
55
|
+
resource.update attrs
|
56
|
+
end
|
57
|
+
|
58
|
+
def resource
|
59
|
+
INFLECTOR.pluralize model_name
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# TODO: use incr
|
65
|
+
def incr
|
66
|
+
RJ["#{resource}:count"] = count + 1
|
67
|
+
end
|
68
|
+
|
69
|
+
def model_name
|
70
|
+
self.name.downcase
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
module RediJsonModelMixin
|
76
|
+
|
77
|
+
def update(attrs_new)
|
78
|
+
raise "Can't update a resource without an `id`" unless id
|
79
|
+
klass = self.class
|
80
|
+
model = klass.get id
|
81
|
+
attrs = model.attributes
|
82
|
+
attrs.merge! attrs_new
|
83
|
+
obj = klass.new attrs
|
84
|
+
data = Oj.dump obj.attributes
|
85
|
+
RJ["#{self.class.resource}:#{id}"] = data
|
86
|
+
obj
|
87
|
+
end
|
88
|
+
|
89
|
+
def save
|
90
|
+
klass = self.class
|
91
|
+
id = klass.send :incr
|
92
|
+
attrs = attributes
|
93
|
+
attrs.merge! id: id
|
94
|
+
data = Oj.dump attrs
|
95
|
+
RJ["#{self.class.resource}:#{id}"] = data
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redijson_models
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- makevoid
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: virtus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.5
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.0.5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oj
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.10.6
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.10.6
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dry-inflector
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.2.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rejson-rb
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.0.0
|
83
|
+
description: Document-like ORM based on Redis key value store - uses Redis Re-JSON
|
84
|
+
for full json manipulation
|
85
|
+
email:
|
86
|
+
- makevoid@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- Rakefile
|
92
|
+
- Readme.md
|
93
|
+
- lib/env.rb
|
94
|
+
- lib/redijson_models.rb
|
95
|
+
- lib/version.rb
|
96
|
+
homepage: http://makevoid.com
|
97
|
+
licenses:
|
98
|
+
- Unlicense
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubygems_version: 3.0.3
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Document-like ORM based on Redis key value store
|
119
|
+
test_files: []
|