grails.gorm.MultiTenant.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grails-datastore-gorm Show documentation
Show all versions of grails-datastore-gorm Show documentation
GORM - Grails Data Access Framework
package grails.gorm
import grails.gorm.api.GormAllOperations
import groovy.transform.CompileStatic
import org.grails.datastore.gorm.GormEnhancer
import org.grails.datastore.mapping.core.connections.ConnectionSource
/**
* A trait for domain classes to implement that should be treated as multi tenant
*
* @author Graeme Rocher
* @since 6.0
*/
@CompileStatic
trait MultiTenant extends Entity {
/**
* Execute the closure with the given tenantId
*
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*/
static T withTenant(Serializable tenantId, Closure callable) {
GormEnhancer.findStaticApi(this).withTenant tenantId, callable
}
/**
* Execute the closure for each tenant
*
* @param callable The closure
* @return The result of the closure
*/
static GormAllOperations eachTenant(Closure callable) {
GormEnhancer.findStaticApi(this, ConnectionSource.DEFAULT).eachTenant callable
}
/**
* Return the {@link GormAllOperations} for the given tenant id
*
* @param tenantId The tenant id
* @return The operations
*/
static GormAllOperations withTenant(Serializable tenantId) {
(GormAllOperations)GormEnhancer.findStaticApi(this).withTenant(tenantId)
}
}