emerald_odm 1.1.1 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +92 -31
- data/lib/emerald_odm/version.rb +1 -1
- data/lib/emerald_odm.rb +3 -3
- metadata +2 -2
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/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/lib/emerald_odm/version.rb
CHANGED
data/lib/emerald_odm.rb
CHANGED
@@ -79,7 +79,7 @@ module EmeraldODM
|
|
79
79
|
# @param [Hash] sort The sort
|
80
80
|
# @param [Integer] limit The limit
|
81
81
|
# @return [Array<self>] The documents
|
82
|
-
def find(filter
|
82
|
+
def find(filter, projection: {}, sort: {}, limit: 0)
|
83
83
|
self.class.validate_fields_from_stages(filter, projection, sort)
|
84
84
|
|
85
85
|
if projection.empty?
|
@@ -93,8 +93,8 @@ module EmeraldODM
|
|
93
93
|
query.to_a.map { |document| self.class.new(document) }
|
94
94
|
end
|
95
95
|
|
96
|
-
def self.find(filter
|
97
|
-
new({}).find(filter
|
96
|
+
def self.find(filter, projection: {}, sort: {}, limit: 0)
|
97
|
+
new({}).find(filter, projection: projection, sort: sort, limit: limit)
|
98
98
|
end
|
99
99
|
|
100
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
|