mongoid-dynamic_clients 1.0.0
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 +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +172 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +26 -0
- data/lib/mongoid/dynamic_clients.rb +2 -0
- data/lib/mongoid/dynamic_clients/client_switch.rb +25 -0
- data/lib/mongoid/dynamic_clients/version.rb +5 -0
- data/mongoid-dynamic_clients.gemspec +38 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 872245b9e005a4e7d6c4563530ec18810992c1bc
|
4
|
+
data.tar.gz: 6eb522024736e94b109123f8de288bcbf4e6ca23
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c071a345d114e7a07a6b4296190e7132bd99cc58e67ac5cf56d4116063634f90ebb1efe09785ed96352e987d7b937579ba6f45530451960802eb5062e4e24de
|
7
|
+
data.tar.gz: d50c70de57fc3f4664760d023bad394f8660c5d8b8155830ede03b0d7dd2cf6a7b47152f6863c6653e8a2f2770714fce2c75ec5e9d8b45eadf660b8ff4631c5e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 pavlo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
# Mongoid::DynamicClients
|
2
|
+
|
3
|
+
[](https://semaphoreci.com/pavlikus/mongoid-dynamic-clients)
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
`Mongoid::DynamicClients` helps [MongoId](https://docs.mongodb.com/mongoid/master/#ruby-mongoid-tutorial) enabled
|
8
|
+
apps to talk to multiple MongoDB databases. It is **dynamic** in the sense that you do not necessary have to know the
|
9
|
+
databases you're connecting to beforehand. Instead you provide connection properties (i.e. auth. credentials, hosts etc)
|
10
|
+
at runtime so you can get them gotten from a DB or receive from an other source.
|
11
|
+
|
12
|
+
In the example below the connection properties come from the `config` hash:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
require "mongoid/dynamic_clients"
|
16
|
+
|
17
|
+
# the database to connect to:
|
18
|
+
config = {
|
19
|
+
database: 'my_custom_db',
|
20
|
+
hosts: ['https://foobar.mongo.com:27017'],
|
21
|
+
options: {
|
22
|
+
user: 'default_user',
|
23
|
+
password: 'default_password',
|
24
|
+
auth_source: 'admin'
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
# connect and execute the block against that database:
|
29
|
+
with_mongoid_client(:my_custom_db, config) do
|
30
|
+
Record.create!(foo: "bar")
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
### Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
gem 'mongoid-dynamic_clients'
|
40
|
+
```
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install mongoid-dynamic_clients
|
49
|
+
|
50
|
+
### Documentation
|
51
|
+
|
52
|
+
`Mongoid::DynamicClients` extends [MongoId](https://docs.mongodb.com/mongoid/master/#ruby-mongoid-tutorial) a little bit
|
53
|
+
to make it easier to switch databases at runtime without the need to configure them all in `mongoid.yml` file. This
|
54
|
+
is helpful when you do not know the databases you're connecting to at the build time, say you're developing a multi-tenancy
|
55
|
+
or a white-labeable application where every tenant has its own database and you do not have the ability to enumerate them
|
56
|
+
all in `mongoid.yml`.
|
57
|
+
|
58
|
+
#### Terms: Clients or Databases?
|
59
|
+
|
60
|
+
Let's put it straight - both `clients` and `databases` terms are used interchangeably in this document. It is because
|
61
|
+
MongoId (and the underlying mongodb driver) use the mention of `client` - a thing that is responsible for maintaining
|
62
|
+
connections to the database and perform basic operations against it. Regular people though would call it `database` -
|
63
|
+
i.e. _let it connect to database A and then connect to database B_... So the document uses the two terms in the same meaning
|
64
|
+
so both, experienced and regular developers would understand what's going on here.
|
65
|
+
|
66
|
+
#### The `default` client / database
|
67
|
+
|
68
|
+
When MongoId gem is installed and set up, it generates a `mongoid.yml` config file. The file features a single client
|
69
|
+
(database) that is used by default and has the name of **_default_**:
|
70
|
+
|
71
|
+
```yaml
|
72
|
+
production:
|
73
|
+
# Configure available database clients. (required)
|
74
|
+
clients:
|
75
|
+
# Defines the default client. (required)
|
76
|
+
default:
|
77
|
+
database: default
|
78
|
+
hosts:
|
79
|
+
- 'localhost:27017'
|
80
|
+
options:
|
81
|
+
user: 'default_user'
|
82
|
+
password: 'default_password'
|
83
|
+
auth_source: admin
|
84
|
+
```
|
85
|
+
|
86
|
+
This is default client/database the application will use when it starts up.
|
87
|
+
|
88
|
+
|
89
|
+
#### Switch the clients / databases
|
90
|
+
|
91
|
+
The idea behind the `Mongoid::DynamicClients` is to switch the databases at runtime, for this it provides a single
|
92
|
+
`with_mongoid_client` method that takes care of the job:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
|
96
|
+
# this goes to the `default` database
|
97
|
+
Record.create!()
|
98
|
+
|
99
|
+
# queries within the block go to the `my_custom_db` database
|
100
|
+
with_mongoid_client(:my_custom_db, config) do
|
101
|
+
Record.create!()
|
102
|
+
end
|
103
|
+
|
104
|
+
# this goes back to the `default` database
|
105
|
+
Record.create!()
|
106
|
+
```
|
107
|
+
|
108
|
+
_(the code above assumes that `Record` is a [MongoId document](https://docs.mongodb.com/mongoid/master/tutorials/mongoid-documents/))_
|
109
|
+
|
110
|
+
#### Configuring the client
|
111
|
+
|
112
|
+
The second argument of the `with_mongoid_client` method is a configuration hash. It is supposed to convey connection
|
113
|
+
information for given database. The structure of the config hash is the reflection of the `client` structure seen
|
114
|
+
in `mongoid.yml` file above:
|
115
|
+
|
116
|
+
```yaml
|
117
|
+
|
118
|
+
default:
|
119
|
+
database: default
|
120
|
+
hosts:
|
121
|
+
- 'localhost:27017'
|
122
|
+
options:
|
123
|
+
user: 'default_user'
|
124
|
+
password: 'default_password'
|
125
|
+
auth_source: adminkm
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
so, `config` hash should be a structure like this:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
|
133
|
+
config = {
|
134
|
+
database: 'default',
|
135
|
+
hosts: [ 'localhost:27017' ],
|
136
|
+
options: {
|
137
|
+
user: 'default_user',
|
138
|
+
password: 'default_password',
|
139
|
+
auth_source: 'admin'
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
with_mongoid_client(:my_custom_db, config) do
|
144
|
+
Record.create!()
|
145
|
+
end
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
_Note: any other client options available in `mongoid.yml` can be specified in the configuration hash, so that's how you
|
150
|
+
may configure connection pooling and other options for the client._
|
151
|
+
|
152
|
+
|
153
|
+
### How it works
|
154
|
+
todo: Explain how Core Mongoid Threaded works, where the clients are stored and how that affects code
|
155
|
+
|
156
|
+
#### Integrating with Rails 5
|
157
|
+
todo: Describe puma setup, how threads are managed and how that affects multi database connections
|
158
|
+
|
159
|
+
#### Integrating with Sidekiq
|
160
|
+
todo: Describe the theading model behind sidekiq, provide a code sample of the use of dynamic clients in a sidekiq job
|
161
|
+
|
162
|
+
#### Possible traps
|
163
|
+
|
164
|
+
todo: It is easy to get out of connection pool limits by passing connection pool configuration to `with_mongoid_client` - explain the danger of that.
|
165
|
+
|
166
|
+
## Contributing
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid-dynamic_clients. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
169
|
+
|
170
|
+
## License
|
171
|
+
|
172
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mongoid/dynamic_clients"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/docker-compose.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
version: "3"
|
2
|
+
services:
|
3
|
+
mongo-default:
|
4
|
+
image: mongo:latest
|
5
|
+
environment:
|
6
|
+
MONGO_INITDB_ROOT_USERNAME: "default_user"
|
7
|
+
MONGO_INITDB_ROOT_PASSWORD: "default_password"
|
8
|
+
MONGO_INITDB_DATABASE: "default"
|
9
|
+
ports:
|
10
|
+
- "37017:27017"
|
11
|
+
mongo-client-1:
|
12
|
+
image: mongo:latest
|
13
|
+
environment:
|
14
|
+
MONGO_INITDB_ROOT_USERNAME: "client1_user"
|
15
|
+
MONGO_INITDB_ROOT_PASSWORD: "client1_password"
|
16
|
+
MONGO_INITDB_DATABASE: "client1"
|
17
|
+
ports:
|
18
|
+
- "27018:27017"
|
19
|
+
mongo-client-2:
|
20
|
+
image: mongo:latest
|
21
|
+
environment:
|
22
|
+
MONGO_INITDB_ROOT_USERNAME: "client2_user"
|
23
|
+
MONGO_INITDB_ROOT_PASSWORD: "client2_password"
|
24
|
+
MONGO_INITDB_DATABASE: "client-2"
|
25
|
+
ports:
|
26
|
+
- "27019:27017"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module DynamicClients
|
3
|
+
|
4
|
+
def with_mongoid_client(client_identifier, client_config = {})
|
5
|
+
|
6
|
+
previous_client = Mongoid::Threaded.client_override
|
7
|
+
cid = client_identifier.to_sym
|
8
|
+
|
9
|
+
begin
|
10
|
+
|
11
|
+
unless Mongoid::Config.clients[cid]
|
12
|
+
Mongoid::Config.clients[cid] = client_config
|
13
|
+
#Mongoid::Clients.with_name(cid).reconnect
|
14
|
+
end
|
15
|
+
|
16
|
+
Mongoid.override_client(cid)
|
17
|
+
yield
|
18
|
+
|
19
|
+
ensure
|
20
|
+
Mongoid.override_client(previous_client)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "mongoid/dynamic_clients/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mongoid-dynamic_clients"
|
8
|
+
spec.version = Mongoid::DynamicClients::VERSION
|
9
|
+
spec.authors = ["pavlo"]
|
10
|
+
spec.email = ["pavlikus@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Helps MongoId enabled apps to talk to multiple MongoDB databases}
|
13
|
+
spec.description = %q{The gem makes it easier to switch databases at runtime without the need to configure them all in `mongoid.yml` file. This is helpful when you do not know the databases you're connecting to at the build time, say you're developing a multi-tenancy or a white-labeable application where every tenant has its own database and you do not have the ability to enumerate them all in `mongoid.yml`}
|
14
|
+
spec.homepage = "https://github.com/pavlo/mongoid-dynamic-clients"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency("mongoid", ["~> 6"])
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-dynamic_clients
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pavlo
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongoid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description: The gem makes it easier to switch databases at runtime without the need
|
70
|
+
to configure them all in `mongoid.yml` file. This is helpful when you do not know
|
71
|
+
the databases you're connecting to at the build time, say you're developing a multi-tenancy
|
72
|
+
or a white-labeable application where every tenant has its own database and you
|
73
|
+
do not have the ability to enumerate them all in `mongoid.yml`
|
74
|
+
email:
|
75
|
+
- pavlikus@gmail.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- ".gitignore"
|
81
|
+
- ".travis.yml"
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- docker-compose.yml
|
89
|
+
- lib/mongoid/dynamic_clients.rb
|
90
|
+
- lib/mongoid/dynamic_clients/client_switch.rb
|
91
|
+
- lib/mongoid/dynamic_clients/version.rb
|
92
|
+
- mongoid-dynamic_clients.gemspec
|
93
|
+
homepage: https://github.com/pavlo/mongoid-dynamic-clients
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
allowed_push_host: https://rubygems.org
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.6.11
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Helps MongoId enabled apps to talk to multiple MongoDB databases
|
118
|
+
test_files: []
|