0.0
No commit activity in last 3 years
No release in over 3 years
Multiple databases Mongoid Models. Good for SaaS Apps.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

> 4.0.0
 Project Readme

Mongoid::Tenant

Gem Version Dependency Status Build Status Code Climate Coverage Status

Mongoid::Tenant

Simple multi tenant for mongoid models!

Nice Tenancy for Mongoid Documents

This library is a simple way to split your client data between databases. To mongo it's pretty trivial to use multiple databases. Mongoid helps too.

Supposing you have a Bike SaaS where each Shop stores:

Tenancy

class Shop
  include Mongoid::Document
  include Mongoid::Tenancy

  tenant_key :url
end

Tenant

class Bike
  include Mongoid::Document
  include Mongoid::Tenant
end

And that's all.

Helpers

Codebase is really is small. Check lib/

Tenancy#tenancy!

Helper to set the current namespace.

Shop.first.tenancy!

Tenancy.tenants

Helper to execute something on each tenant namespace. has_many substitute. Eg:

class Shop
   ...
   has_tenant :bikes
end

Or raw:

Shop.tenants { |tenant| puts "#{tenant} have #{Bike.count} bike(s)" }

Rake Tasks

Index creation is the only thing overwriten in Mongoid. You'll need to provide which Tenancy to scope. In our example:

TENANCY=Shop bundle exec rails db:mongoid:create_indexes

ApplicationController

Write your logic:

def app_domain
  @domain ||= Shop.find_by(uri: /^#{request.env["SERVER_NAME"]}/)
  @domain.tenancy!
end

Populate & Spec

Mongoid.purge! won't work. It doesn't know all dbs. A good alternative:

Mongoid.default_client.with(database: db).collections.each(&:drop)

If you don't know every db try Mongoid.default_client.list_databases.

Issues

http://github.com/nofxx/mongoid-tenant