NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

About multi-tenant model and common database #252

Closed extem closed 5 years ago

extem commented 5 years ago

I have a question about designing a multi-tenant model. Create the following DB and table.

common_db.users ... Common DB user1_db.foo_tables ... DB of each tenant user2_db.foo_tables ... user3_db.foo_tables ... user4_db.foo_tables ...

Create the following model.

app/modes common_db.rb foo_tables.rb

rails starts for each tenant.

NoBrainer.sync_schema will create a users table for each tenant. Is there a way to prevent this? Access to common_db is NoBrainer.run_with (: db => 'common_db') {...}

In the previous version, it seems that the model had a function to specify DB with sotre_in.

I would appreciate it if you could tell me. best regards

nviennot commented 5 years ago

The "Model Specific Behavior" section of http://nobrainer.io/docs/multi_tenancy/ explains how to do what you wish

extem commented 5 years ago

Thank you for your reply. "table_config" can specify the table name, but it does not seem to be able to specify the database name. I want to create a model that accesses one shared database with multiple tenant applications.

Temporarily, Specify models to exclude without using NoBrainer.sync_schema.

  def custom_sync_schema(commonModels, is_commondb, options = {})
    if is_commondb
      models = commonModels
    else 
      models = NoBrainer::Document.all(types: %i[user nobrainer])
      models.delete_if {|model| commonModels.include?(model)}
    end
    NoBrainer::Document::TableConfig::Synchronizer.new(models).sync_table_config(options)

    if is_commondb
      models = commonModels
    else 
      models = NoBrainer::Document.all(types: [:user])
      models.delete_if {|model| commonModels.include?(model)}
    end
    NoBrainer::Document::Index::Synchronizer.new(models).sync_indexes(options)
  end

However, if you accidentally run NoBrainer.sync_schema, unnecessary tables will be created.