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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5730dcce3f54aca15736e052c5a98a758d0d691b243e544f406de15c7d1cbbb4
4
- data.tar.gz: 3814ee64675fe80d44e96a15ff17e05d849c71f44477c607a388a7c3f9863a53
3
+ metadata.gz: 801a45bf7c2940c3547f9260d255c47b2b87fc8becad126899ea1e8c078aaf00
4
+ data.tar.gz: de2e365a5c4e60dc4c79766ff624f8300e84245d09c6937169e7fa98c23771f3
5
5
  SHA512:
6
- metadata.gz: b6b884d39e9b34f8a13237413753fbfe5265e51f463a93befe6c4b848b8bce379f37cfb84103f425788042b6480a138f4a51e87fffb039d77a6ae01b5d8f9bcf
7
- data.tar.gz: 751d69add77372aa90f657d4a6802f1794391f7155ff9451a019750704e6cf84c125212a9c97d41e5049ce9bed26e9e379ccf1d693ddce3797c37525321f4b6c
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
- EmeraldODM is an simple ODM (Object-Document Mapper) framework for MongoDB in Ruby.
4
+ The main objective of this gem is primarily to facilitate reading data from a MongoDB database.
4
5
 
5
-
6
-
7
- ## Installation
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
- ## Usage
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
- # 1. Setup DB connection
20
+ # Connect to the MongoDB servers before using EmeraldODM
19
21
  EmeraldODM.databases_settings.merge!(
20
22
  {
21
- test: [
22
- [ ENV['IP_MONGO_DATABASE'] ],
23
- {
24
- database: 'test',
25
- user: ENV['DB_LOGIN'],
26
- password: ENV['DB_PASSWD'],
27
- auth_source: ENV['auth_source'],
28
- max_pool_size: 20,
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
- # 2. Define your model
35
- class Users < EmeraldODM::Collection
36
- attr_accessor :_id, :name, :email, :age
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
- :test
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
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/MikaelSantilio/emerald-odm.
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
- ## License
117
+ # Contributing
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MikaelSantilio/emerald-odm/.
59
119
 
60
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
120
+ # License
121
+ The gem is available as open source under the terms of the MIT License.
@@ -1,3 +1,3 @@
1
1
  module EmeraldODM
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.1'
3
3
  end
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: {}, projection: {}, sort: {}, limit: 0)
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: {}, projection: {}, sort: {}, limit: 0)
97
- new({}).find(filter: filter, projection: projection, sort: sort, limit: limit)
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.1.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-21 00:00:00.000000000 Z
11
+ date: 2023-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler