mongoid-tenant 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +3 -0
- data/.travis.yml +26 -0
- data/Gemfile +13 -0
- data/Guardfile +22 -0
- data/MIT-LICENSE.txt +22 -0
- data/README.md +71 -0
- data/Rakefile +2 -0
- data/benchmarks/benchmark.rb +3 -0
- data/lib/mongoid/tenancy.rb +24 -0
- data/lib/mongoid/tenant.rb +14 -0
- data/lib/mongoid/tenant/railtie.rb +10 -0
- data/lib/mongoid/tenant/tasks/tenant.rake +29 -0
- data/lib/mongoid/tenant/version.rb +6 -0
- data/mongoid-tenant.gemspec +22 -0
- data/spec/mongoid/tenant_spec.rb +4 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/support/models.rb +14 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d98d3fcee576f2aa4fd6d1c72225b0491b614694
|
4
|
+
data.tar.gz: 600205308c2e42bc4c4b8ea8c855b89af2402797
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a04ab62aa4848479701228f93b75f2fad985c489284ac95d5c0922d0b697baccfb974424678374d030a9274e80a344b6c19e35b94fd5e78a4bde30124870f3a4
|
7
|
+
data.tar.gz: 4c44fd1d7bc9b70dc58cad1720ea93e094574903656ecdb1e6eda79590b18f3fb3ca97c391d62bad61c734949612c743fe8a74a17b1331fbae9b4d5d2aeac645
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 2.1.1
|
5
|
+
- 2.2.2
|
6
|
+
- ruby-head
|
7
|
+
- jruby-head
|
8
|
+
- rbx-2
|
9
|
+
|
10
|
+
env: CI="travis" JRUBY_OPTS="--2.0 --server -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -J-Xms512m -J-Xmx1024m"
|
11
|
+
|
12
|
+
matrix:
|
13
|
+
allow_failures:
|
14
|
+
- rvm: ruby-head
|
15
|
+
- rvm: jruby-head
|
16
|
+
- rvm: rbx-2
|
17
|
+
|
18
|
+
gemfile:
|
19
|
+
- Gemfile
|
20
|
+
|
21
|
+
services:
|
22
|
+
- mongodb
|
23
|
+
|
24
|
+
env: CI="travis"
|
25
|
+
|
26
|
+
script: "bundle exec rspec"
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'guard'
|
7
|
+
gem 'guard-rspec'
|
8
|
+
gem 'guard-rubocop'
|
9
|
+
gem 'rspec', '>= 3.2.0'
|
10
|
+
gem 'coveralls', require: false
|
11
|
+
gem 'codeclimate-test-reporter', require: nil
|
12
|
+
gem 'mongoid-rspec', git: 'https://github.com/nofxx/mongoid-rspec'
|
13
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
2
|
+
# rspec may be run, below are examples of the most common uses.
|
3
|
+
# * bundler: 'bundle exec rspec'
|
4
|
+
# * bundler binstubs: 'bin/rspec'
|
5
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
6
|
+
# installed the spring binstubs per the docs)
|
7
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
8
|
+
# * 'just' rspec: 'rspec'
|
9
|
+
|
10
|
+
# guard :rubocop do
|
11
|
+
guard :rubocop, all_on_start: false, keep_failed: false, cli: ['-D'] do
|
12
|
+
watch(%r{.+\.rb$})
|
13
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
14
|
+
end
|
15
|
+
|
16
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
17
|
+
watch(%r{^spec/.+_spec\.rb$})
|
18
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
+
watch(/^generators\/(.+)\.rb$/) { |_m| 'spec/schemaless/worker_spec' }
|
20
|
+
|
21
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
22
|
+
end
|
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Marcos Piccinini
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
Mongoid::Tenant
|
2
|
+
===============
|
3
|
+
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/mongoid-tenant.png)](http://badge.fury.io/rb/mongoid-tenant)
|
5
|
+
[![Dependency Status](https://gemnasium.com/nofxx/mongoid-tenant.svg)](https://gemnasium.com/nofxx/mongoid-tenant)
|
6
|
+
[![Build Status](https://secure.travis-ci.org/nofxx/mongoid-tenant.png)](http://travis-ci.org/nofxx/mongoid-tenant)
|
7
|
+
|
8
|
+
## Mongoid::Tenant
|
9
|
+
|
10
|
+
Simple multi tenant for mongoid models!
|
11
|
+
|
12
|
+
|
13
|
+
## Nice Tenancy for Mongoid Documents
|
14
|
+
|
15
|
+
This library is a simple way to split your client data between databases.
|
16
|
+
To mongo it's pretty trivial to use multiple databases. Mongoid helps too.
|
17
|
+
|
18
|
+
## Tenant
|
19
|
+
|
20
|
+
Models to be splitted, supposing you have a Bike SaaS where each Shop stores
|
21
|
+
their bikes:
|
22
|
+
|
23
|
+
```
|
24
|
+
class Bike
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::Tenant
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
And that's all. But we need a tenancy:
|
31
|
+
|
32
|
+
## Tenancy
|
33
|
+
|
34
|
+
```
|
35
|
+
class Shop
|
36
|
+
include Mongoid::Document
|
37
|
+
include Mongoid::Tenancy
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
## Helpers
|
43
|
+
|
44
|
+
|
45
|
+
Codebase is really is small. Check lib/
|
46
|
+
|
47
|
+
### Tenancy#tenancy!
|
48
|
+
|
49
|
+
Helper to set the current namespace.
|
50
|
+
|
51
|
+
```
|
52
|
+
Shop.first.tenancy!
|
53
|
+
```
|
54
|
+
|
55
|
+
### Tenancy.tenants
|
56
|
+
|
57
|
+
Helper to execute something on each tenant namespace. Eg:
|
58
|
+
|
59
|
+
```
|
60
|
+
Shop.tenants { |t| puts "#{t} has #{Bike.count}" }
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
### Rake Tasks
|
65
|
+
|
66
|
+
Index creation is the only thing overwriten in Mongoid.
|
67
|
+
You'll need to provide which Tenancy to scope. In our example:
|
68
|
+
|
69
|
+
```
|
70
|
+
TENANCY=Shop bundle exec rails db:mongoid:create_indexes
|
71
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Tenancy
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
field :uri, type: String
|
7
|
+
|
8
|
+
validates :uri, uniqueness: true
|
9
|
+
|
10
|
+
index({ uri: 1 }, unique: true)
|
11
|
+
|
12
|
+
def self.tenants
|
13
|
+
all.each do |t|
|
14
|
+
t.tenancy!
|
15
|
+
yield t
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def tenancy!
|
21
|
+
Thread.current[:mongodb] = _id.to_s
|
22
|
+
end
|
23
|
+
end # Tenancy
|
24
|
+
end # Mongoid
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require_relative 'tenancy'
|
3
|
+
|
4
|
+
require 'mongoid/tenant/railtie' if defined?(Rails)
|
5
|
+
|
6
|
+
module Mongoid
|
7
|
+
module Tenant
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
store_in database: -> { Thread.current[:mongodb] }
|
12
|
+
end
|
13
|
+
end # Tenant
|
14
|
+
end # Mongoid
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Rake::Task["db:mongoid:create_indexes"].clear
|
2
|
+
Rake::Task["db:mongoid:remove_undefined_indexes"].clear
|
3
|
+
|
4
|
+
namespace :db do
|
5
|
+
namespace :mongoid do
|
6
|
+
|
7
|
+
def get_tenancy
|
8
|
+
ENV['TENANCY'] ||
|
9
|
+
fail("Provide a tenancy model: `TENANCY=Foo #{ARGV.join}`")
|
10
|
+
end
|
11
|
+
|
12
|
+
task :create_indexes => [:environment, :load_models] do
|
13
|
+
Rake::Task["db:mongoid:create_indexes"].clear
|
14
|
+
Object.const_get(get_tenancy).all.each do |t|
|
15
|
+
puts "Tenant #{t}"
|
16
|
+
t.tenancy!
|
17
|
+
::Mongoid::Tasks::Database.create_indexes
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
task :remove_undefined_indexes => [:environment, :load_models] do
|
22
|
+
Object.const_get(get_tenancy).all.each do |t|
|
23
|
+
puts "Tenant #{t}"
|
24
|
+
t.tenancy!
|
25
|
+
::Mongoid::Tasks::Database.remove_undefined_indexes
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'mongoid/tenant/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'mongoid-tenant'
|
7
|
+
s.version = Mongoid::Tenant::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Marcos Piccinini']
|
10
|
+
s.homepage = 'http://github.com/nofxx/mongoid-tenant'
|
11
|
+
s.summary = 'Multi Database/Tenants models for Mongoid documents.'
|
12
|
+
s.description = 'Mongoid Tenant with multiple database per client.'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.rubyforge_project = 'mongoid-tenant'
|
16
|
+
s.add_dependency 'mongoid', '> 4.0.0'
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# require 'codeclimate-test-reporter'
|
2
|
+
# CodeClimate::TestReporter.start
|
3
|
+
if ENV['CI']
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
end
|
7
|
+
|
8
|
+
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
9
|
+
|
10
|
+
# require 'pry'
|
11
|
+
# require 'database_cleaner'
|
12
|
+
require 'mongoid'
|
13
|
+
require 'mongoid-rspec'
|
14
|
+
|
15
|
+
require 'mongoid/tenant'
|
16
|
+
|
17
|
+
ENV['MONGOID_ENV'] = 'test'
|
18
|
+
|
19
|
+
db_config = {
|
20
|
+
default: {
|
21
|
+
database: 'mongoid_tenant_test',
|
22
|
+
hosts: ["localhost: #{ENV['MONGODB_PORT'] || 27_017}"],
|
23
|
+
options: {}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
Mongoid.configure do |config|
|
28
|
+
config.load_configuration(
|
29
|
+
if Mongoid::VERSION >= '5'
|
30
|
+
{ clients: db_config }
|
31
|
+
else
|
32
|
+
{ sessions: db_config }
|
33
|
+
end
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'support/models'
|
38
|
+
|
39
|
+
Mongo::Logger.logger.level = Logger::INFO if Mongoid::VERSION >= '5'
|
40
|
+
|
41
|
+
RSpec.configure do |config|
|
42
|
+
config.include Mongoid::Matchers
|
43
|
+
|
44
|
+
config.before(:each) do
|
45
|
+
Mongoid.purge!
|
46
|
+
end
|
47
|
+
|
48
|
+
config.after(:suite) do
|
49
|
+
puts "\n# With Mongoid v#{Mongoid::VERSION}"
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid-tenant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcos Piccinini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-18 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: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
description: Mongoid Tenant with multiple database per client.
|
28
|
+
email:
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- ".rspec"
|
35
|
+
- ".travis.yml"
|
36
|
+
- Gemfile
|
37
|
+
- Guardfile
|
38
|
+
- MIT-LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- benchmarks/benchmark.rb
|
42
|
+
- lib/mongoid/tenancy.rb
|
43
|
+
- lib/mongoid/tenant.rb
|
44
|
+
- lib/mongoid/tenant/railtie.rb
|
45
|
+
- lib/mongoid/tenant/tasks/tenant.rake
|
46
|
+
- lib/mongoid/tenant/version.rb
|
47
|
+
- mongoid-tenant.gemspec
|
48
|
+
- spec/mongoid/tenant_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- spec/support/models.rb
|
51
|
+
homepage: http://github.com/nofxx/mongoid-tenant
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata: {}
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project: mongoid-tenant
|
71
|
+
rubygems_version: 2.4.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Multi Database/Tenants models for Mongoid documents.
|
75
|
+
test_files:
|
76
|
+
- spec/mongoid/tenant_spec.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
- spec/support/models.rb
|