emerald_odm 1.1.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +92 -31
- data/emerald_odm.gemspec +1 -1
- data/lib/emerald_odm/version.rb +1 -1
- data/lib/emerald_odm.rb +41 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58be3d671b46d8703d0308a6921e515a6b68e38da855a766c25bc01c9435c4c4
|
4
|
+
data.tar.gz: 8d6aa735ce20df9d627408d79d527774bd9dab8b40df5c6477053a4503b15c3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 380ad74efa2cdd7eeca559c3a1fcd66fa3546b2abfa3812d4ea6e82621d90c73bc019551506a4b5aae6a01a8ef8fb3e8b6381cb8213bb450e289b2487eddf440
|
7
|
+
data.tar.gz: 01047b0d65a1c6067c24d967f6b300bbda7b7550915da8c96138da875ea23919caad7f59e596a7aa1fed9d02c1c1a98aedb8f55acddc9ddeebc7f03e2c8a5243
|
data/README.md
CHANGED
@@ -1,60 +1,121 @@
|
|
1
1
|
# EmeraldODM
|
2
|
+
EmeraldODM is an Object-Document Mapper (ODM) for Ruby that allows you to interact with MongoDB databases in a simple, Ruby-like way. It provides a high-level, easy-to-use interface for working with MongoDB documents, while abstracting away the low-level details of the MongoDB driver.
|
2
3
|
|
3
|
-
|
4
|
+
The main objective of this gem is primarily to facilitate reading data from a MongoDB database.
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
```bash
|
10
|
-
gem install emerald_odm
|
6
|
+
# Installation
|
7
|
+
To install EmeraldODM, simply add it to your Gemfile:
|
8
|
+
```ruby
|
9
|
+
gem 'emerald_odm'
|
11
10
|
```
|
11
|
+
Then run bundle install to install the gem and its dependencies.
|
12
12
|
|
13
|
-
|
13
|
+
# Usage
|
14
|
+
Here's a quick example of how to use EmeraldODM to interact with a MongoDB database:
|
14
15
|
|
16
|
+
## 1. Setup DB connection
|
15
17
|
```ruby
|
16
18
|
require 'emerald_odm'
|
17
19
|
|
18
|
-
#
|
20
|
+
# Connect to the MongoDB servers before using EmeraldODM
|
19
21
|
EmeraldODM.databases_settings.merge!(
|
20
22
|
{
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
]
|
23
|
+
blog: [
|
24
|
+
[ '192.168.0.1:27017', '192.168.1.1:27017'],
|
25
|
+
{
|
26
|
+
database: 'blog',
|
27
|
+
user: ENV['MONGO_LOGIN'],
|
28
|
+
password: ENV['MONGO_PASSWD'],
|
29
|
+
auth_source: ENV['auth_source'],
|
30
|
+
max_pool_size: 20,
|
31
|
+
}
|
32
|
+
],
|
33
|
+
ecommerce: [
|
34
|
+
[ '193.168.0.1:27017', '193.168.1.1:27017'],
|
35
|
+
{
|
36
|
+
database: 'ecommerce',
|
37
|
+
user: ENV['MONGO_LOGIN'],
|
38
|
+
password: ENV['MONGO_PASSWD'],
|
39
|
+
auth_source: ENV['auth_source'],
|
40
|
+
max_pool_size: 20,
|
41
|
+
}
|
42
|
+
],
|
31
43
|
}
|
32
44
|
)
|
33
45
|
|
34
|
-
|
35
|
-
|
36
|
-
|
46
|
+
```
|
47
|
+
|
48
|
+
## 2. Define your model
|
49
|
+
```ruby
|
50
|
+
require 'emerald_odm'
|
51
|
+
# Define a model for the "users" collection
|
52
|
+
class User < EmeraldODM::Collection
|
37
53
|
|
54
|
+
attr_accessor :_id, :name, :email, :posts, :keywords_count
|
55
|
+
|
38
56
|
def self.collection_name
|
39
57
|
:users
|
40
58
|
end
|
41
59
|
|
42
60
|
def self.db_name
|
43
|
-
:
|
61
|
+
:blog
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.posts=(posts)
|
65
|
+
@posts = posts.map { |post| Post.new(post)}
|
44
66
|
end
|
67
|
+
|
68
|
+
class Post < EmeraldODM::AttrInitializer
|
69
|
+
attr_accessor :id, :title, :body, :created_at, :updated_at
|
70
|
+
|
71
|
+
def created_at
|
72
|
+
Time.parse(@created_at)
|
73
|
+
end
|
74
|
+
|
75
|
+
def updated_at
|
76
|
+
Time.parse(@updated_at)
|
77
|
+
end
|
78
|
+
|
79
|
+
def keywords
|
80
|
+
body.scan(/\w+/)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
45
84
|
end
|
46
85
|
|
47
|
-
# 3. Use it
|
48
|
-
Users.find(filter: {_id: '5c9b9b9b9b9b9b9b9b9b9b9b'}).first
|
49
|
-
|
50
|
-
Users.update_one(filter: {_id: '5c9b9b9b9b9b9b9b9b9b9b9b'}, set: {name: 'John Doe'}, unset: {age: 1})
|
51
86
|
```
|
52
87
|
|
53
|
-
##
|
88
|
+
## 3. Use it
|
89
|
+
```ruby
|
90
|
+
# Find users using a query
|
91
|
+
users = User.find(
|
92
|
+
{name: 'John Doe'}, # filter query is required
|
93
|
+
projection: {name: 1, email: 1, posts: 1, keywords_count: 1}, # optional, the default is to return all fields defined in the model
|
94
|
+
limit: 10, # optional, the default is to return all documents
|
95
|
+
sort: {name: 1} # optional
|
96
|
+
)
|
54
97
|
|
55
|
-
|
98
|
+
# users is an array of User objects like Array<User>
|
99
|
+
users.each do |user|
|
100
|
+
posts = user.posts
|
101
|
+
all_user_keywords = posts.map(&:keywords).flatten.uniq
|
102
|
+
User.update_one(
|
103
|
+
{_id: user._id},
|
104
|
+
set: {keywords_count: all_user_keywords.count}
|
105
|
+
)
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
# Advanced usage
|
110
|
+
EmeraldODM supports advanced usage such as:
|
56
111
|
|
112
|
+
## Accessing the underlying MongoDB driver
|
113
|
+
```ruby
|
114
|
+
User.collection # returns the underlying MongoDB::Collection object
|
115
|
+
```
|
57
116
|
|
58
|
-
|
117
|
+
# Contributing
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MikaelSantilio/emerald-odm/.
|
59
119
|
|
60
|
-
|
120
|
+
# License
|
121
|
+
The gem is available as open source under the terms of the MIT License.
|
data/emerald_odm.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.email = ['mikael.santilio@gmail.com']
|
12
12
|
|
13
13
|
spec.summary = 'MongoDB ODM'
|
14
|
-
spec.description = '
|
14
|
+
spec.description = 'A simple MongoDB ODM without Active Record'
|
15
15
|
spec.homepage = 'https://github.com/MikaelSantilio/emerald-odm'
|
16
16
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
17
17
|
|
data/lib/emerald_odm/version.rb
CHANGED
data/lib/emerald_odm.rb
CHANGED
@@ -25,20 +25,20 @@ module EmeraldODM
|
|
25
25
|
end
|
26
26
|
|
27
27
|
class AttrInitializer
|
28
|
-
attr_reader :
|
28
|
+
attr_reader :_document
|
29
29
|
|
30
30
|
def initialize(document)
|
31
31
|
self.class.fields.each do |field|
|
32
32
|
document_value = document[field]
|
33
33
|
send("#{field}=", document_value) unless document_value.nil?
|
34
34
|
end
|
35
|
-
instance_variable_set('@
|
35
|
+
instance_variable_set('@_document', document)
|
36
36
|
end
|
37
37
|
|
38
38
|
# @return [Array]
|
39
39
|
def self.fields
|
40
40
|
public_instance_methods = self.public_instance_methods(false).grep(/=$/)
|
41
|
-
rejected_attr_names = %w[
|
41
|
+
rejected_attr_names = %w[_document]
|
42
42
|
fields = []
|
43
43
|
public_instance_methods.each do |attr|
|
44
44
|
attr_name = attr.to_s.gsub('=', '')
|
@@ -51,6 +51,41 @@ module EmeraldODM
|
|
51
51
|
def nil?
|
52
52
|
document.nil? || document.empty?
|
53
53
|
end
|
54
|
+
|
55
|
+
def self.array_to_h(array)
|
56
|
+
array.map do |item|
|
57
|
+
if item.is_a?(AttrInitializer)
|
58
|
+
item.to_h
|
59
|
+
elsif item.is_a?(Array)
|
60
|
+
array_to_h(item)
|
61
|
+
else
|
62
|
+
item
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_h
|
68
|
+
hash = {}
|
69
|
+
known_fields = self.class.fields
|
70
|
+
unknown_fields = document.keys - known_fields
|
71
|
+
unknown_fields.reject! { |field| field.start_with?('_') }
|
72
|
+
known_fields.each do |field|
|
73
|
+
value = send(field)
|
74
|
+
if value.is_a?(AttrInitializer)
|
75
|
+
hash[field] = value.to_h
|
76
|
+
elsif value.is_a?(Array)
|
77
|
+
hash[field] = self.class.array_to_h(value)
|
78
|
+
else
|
79
|
+
hash[field] = value
|
80
|
+
end
|
81
|
+
end
|
82
|
+
unknown_fields.each do |field|
|
83
|
+
value = document[field]
|
84
|
+
hash[field] = value
|
85
|
+
end
|
86
|
+
|
87
|
+
hash
|
88
|
+
end
|
54
89
|
end
|
55
90
|
|
56
91
|
class Collection < AttrInitializer
|
@@ -79,7 +114,7 @@ module EmeraldODM
|
|
79
114
|
# @param [Hash] sort The sort
|
80
115
|
# @param [Integer] limit The limit
|
81
116
|
# @return [Array<self>] The documents
|
82
|
-
def find(filter
|
117
|
+
def find(filter, projection: {}, sort: {}, limit: 0)
|
83
118
|
self.class.validate_fields_from_stages(filter, projection, sort)
|
84
119
|
|
85
120
|
if projection.empty?
|
@@ -93,8 +128,8 @@ module EmeraldODM
|
|
93
128
|
query.to_a.map { |document| self.class.new(document) }
|
94
129
|
end
|
95
130
|
|
96
|
-
def self.find(filter
|
97
|
-
new({}).find(filter
|
131
|
+
def self.find(filter, projection: {}, sort: {}, limit: 0)
|
132
|
+
new({}).find(filter, projection: projection, sort: sort, limit: limit)
|
98
133
|
end
|
99
134
|
|
100
135
|
def self.update(type, filter, set: {}, unset: {})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emerald_odm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mikael
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,7 +108,7 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
-
description:
|
111
|
+
description: A simple MongoDB ODM without Active Record
|
112
112
|
email:
|
113
113
|
- mikael.santilio@gmail.com
|
114
114
|
executables: []
|
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
145
|
version: '0'
|
146
146
|
requirements: []
|
147
147
|
rubyforge_project:
|
148
|
-
rubygems_version: 2.7.
|
148
|
+
rubygems_version: 2.7.7
|
149
149
|
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: MongoDB ODM
|