Giter Site home page Giter Site logo

gorm-multitenancy-additional-tenant-query's Introduction

Grails Multi-Tenancy Example

The cache branch of this repository contains logic showing how to use Grails Cache Plugin with the tenant Id as part of the cache key.

The sample code

When the cache is not hit you will see a log line:

INFO --- [nio-8080-exec-2] example.BookListService : not cached

The index page of the application http://localhost:8080 contains links to evict the cache.


This Grails application uses GORM for Hibernate.

It uses DISCRIMINATOR multi-tenancy mode by setting at grails-app/conf/application.yml the following:

grails:
    gorm:
        multiTenancy:
            mode: DISCRIMINATOR

It has a domain class Book which uses a custom field tenant as the tenancy discriminator.

package example

import grails.compiler.GrailsCompileStatic
import grails.gorm.MultiTenant

@GrailsCompileStatic
class Book implements MultiTenant<Book> {
    ...
    String tenant
    static constraints = {
...
    static mapping = {
        tenantId name:'tenant'
    }
}

In grails-app/init/BootStrap.groovy multiple Books are saved. To select the tenant, for which the books are being saved, the method MultiTenant::withTenant(Serializable, Closure) is used.

  • For tenant groovy, the application saves 5 books
  • For tenant grails, the application saves 8 books
  • For tenant micronaut, the application saves 1 books

To save the books, Boostrap injects BookService a GORM Data Service annotaed with @CurrentTenant, an annotation which resolves the current tenant for the context of a class or method. It uses the method BookService::save(String, String, String, String, String)

The application exposes following endpoints:

  • GET /books/groovy returns books of current tenant groovy
  • GET /books/grails returns books of current tenant grails
  • GET /books/micronaut returns books of current tenant micronaut
  • GET /books/groovygrails returns books of current tenant groovy and books of an extra tenant grails

To allow queries which fetch current tenant items plus items belonging to other tenant BookService extends an abstractclass DataService

@CompileStatic
@CurrentTenant
@Service(Book)
abstract class BookService extends DataService<Book> {

    @Autowired
    HibernateDatastore hibernateDatastore

    @Override
    HibernateDatastore getHibernateDataStore() {
        return this.hibernateDatastore
    }

    @Override
    DetachedCriteria<Book> getDetachedCriteria() {
        Book.where {}
    }
    ...

To allow fetching information coming from multiple tenants DataService<T> exposes a method:

DetachedCriteria<T> criteriaInTenants(String... additionalTenant = null)

DetachedCriteria allows for dynamic constructions of queries. If you build the criteria with criteriaInTenant execute the query within the Closure of Tenants.withoutId(Closure). Example:

    ...
    Number count(String additionalTenant = null) {
        DetachedCriteria<Book> c = criteriaInTenants(additionalTenant)
        Tenants.withoutId {
            c.count()
        }
    }
    ...

gorm-multitenancy-additional-tenant-query's People

Contributors

sdelamo avatar

Stargazers

Ben Jones avatar

Watchers

James Cloos avatar  avatar Ben Jones avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.