emerald_odm 1.0.1 → 1.2.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/.gitignore +2 -0
- data/README.md +107 -26
- data/lib/emerald_odm/version.rb +1 -1
- data/lib/emerald_odm.rb +21 -13
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 801a45bf7c2940c3547f9260d255c47b2b87fc8becad126899ea1e8c078aaf00
|
4
|
+
data.tar.gz: de2e365a5c4e60dc4c79766ff624f8300e84245d09c6937169e7fa98c23771f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40df24b7d653f85b7707d212ef97d661f196e3f3b96cd7dc6a7c735cd74bd44acc2708dc2700e894288f5fc9c12a4d818c7896315e353758b44381797f639f85
|
7
|
+
data.tar.gz: '08e0a9504e115b9d296689b4467147b34f2a74cf8e9d3d7d4ea20788e504de2be38d65f72bd83b835fd893a6adcc89df6197dc7352f2e2a5f6965f50c437ffb2'
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,40 +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
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
4
|
+
The main objective of this gem is primarily to facilitate reading data from a MongoDB database.
|
10
5
|
|
6
|
+
# Installation
|
7
|
+
To install EmeraldODM, simply add it to your Gemfile:
|
11
8
|
```ruby
|
12
|
-
gem '
|
9
|
+
gem 'emerald_odm'
|
13
10
|
```
|
11
|
+
Then run bundle install to install the gem and its dependencies.
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
$ bundle install
|
13
|
+
# Usage
|
14
|
+
Here's a quick example of how to use EmeraldODM to interact with a MongoDB database:
|
18
15
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
## 1. Setup DB connection
|
17
|
+
```ruby
|
18
|
+
require 'emerald_odm'
|
19
|
+
|
20
|
+
# Connect to the MongoDB servers before using EmeraldODM
|
21
|
+
EmeraldODM.databases_settings.merge!(
|
22
|
+
{
|
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
|
+
],
|
43
|
+
}
|
44
|
+
)
|
26
45
|
|
27
|
-
|
46
|
+
```
|
28
47
|
|
29
|
-
|
48
|
+
## 2. Define your model
|
49
|
+
```ruby
|
50
|
+
require 'emerald_odm'
|
51
|
+
# Define a model for the "users" collection
|
52
|
+
class User < EmeraldODM::Collection
|
53
|
+
|
54
|
+
attr_accessor :_id, :name, :email, :posts, :keywords_count
|
55
|
+
|
56
|
+
def self.collection_name
|
57
|
+
:users
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.db_name
|
61
|
+
:blog
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.posts=(posts)
|
65
|
+
@posts = posts.map { |post| Post.new(post)}
|
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
|
+
|
84
|
+
end
|
30
85
|
|
31
|
-
|
86
|
+
```
|
32
87
|
|
33
|
-
##
|
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
|
+
)
|
97
|
+
|
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
|
+
```
|
34
108
|
|
35
|
-
|
109
|
+
# Advanced usage
|
110
|
+
EmeraldODM supports advanced usage such as:
|
36
111
|
|
112
|
+
## Accessing the underlying MongoDB driver
|
113
|
+
```ruby
|
114
|
+
User.collection # returns the underlying MongoDB::Collection object
|
115
|
+
```
|
37
116
|
|
38
|
-
|
117
|
+
# Contributing
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MikaelSantilio/emerald-odm/.
|
39
119
|
|
40
|
-
|
120
|
+
# License
|
121
|
+
The gem is available as open source under the terms of the MIT License.
|
data/lib/emerald_odm/version.rb
CHANGED
data/lib/emerald_odm.rb
CHANGED
@@ -24,8 +24,7 @@ module EmeraldODM
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
class
|
28
|
-
|
27
|
+
class AttrInitializer
|
29
28
|
attr_reader :document
|
30
29
|
|
31
30
|
def initialize(document)
|
@@ -36,14 +35,6 @@ module EmeraldODM
|
|
36
35
|
instance_variable_set('@document', document)
|
37
36
|
end
|
38
37
|
|
39
|
-
# @return [Symbol, nil]
|
40
|
-
def self.db_name
|
41
|
-
nil
|
42
|
-
end
|
43
|
-
# @return [Symbol, nil]
|
44
|
-
def self.collection_name
|
45
|
-
nil
|
46
|
-
end
|
47
38
|
# @return [Array]
|
48
39
|
def self.fields
|
49
40
|
public_instance_methods = self.public_instance_methods(false).grep(/=$/)
|
@@ -57,6 +48,23 @@ module EmeraldODM
|
|
57
48
|
fields
|
58
49
|
end
|
59
50
|
|
51
|
+
def nil?
|
52
|
+
document.nil? || document.empty?
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Collection < AttrInitializer
|
57
|
+
|
58
|
+
# @return [Symbol, nil]
|
59
|
+
def self.db_name
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Symbol, nil]
|
64
|
+
def self.collection_name
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
60
68
|
# @return [Mongo::Collection] The collection
|
61
69
|
def self.collection
|
62
70
|
Connector.database(db_name&.to_sym)[collection_name&.to_sym]
|
@@ -71,7 +79,7 @@ module EmeraldODM
|
|
71
79
|
# @param [Hash] sort The sort
|
72
80
|
# @param [Integer] limit The limit
|
73
81
|
# @return [Array<self>] The documents
|
74
|
-
def find(filter
|
82
|
+
def find(filter, projection: {}, sort: {}, limit: 0)
|
75
83
|
self.class.validate_fields_from_stages(filter, projection, sort)
|
76
84
|
|
77
85
|
if projection.empty?
|
@@ -85,8 +93,8 @@ module EmeraldODM
|
|
85
93
|
query.to_a.map { |document| self.class.new(document) }
|
86
94
|
end
|
87
95
|
|
88
|
-
def self.find(filter
|
89
|
-
new({}).find(filter
|
96
|
+
def self.find(filter, projection: {}, sort: {}, limit: 0)
|
97
|
+
new({}).find(filter, projection: projection, sort: sort, limit: limit)
|
90
98
|
end
|
91
99
|
|
92
100
|
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.2.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-01-
|
11
|
+
date: 2023-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -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.6
|
149
149
|
signing_key:
|
150
150
|
specification_version: 4
|
151
151
|
summary: MongoDB ODM
|