com.raufferlobo.springboot.multitenant.mongodb:spring-boot-multitenant-mongodb

An useful Mongodb factory which allows applications to choose the database name at runtime.

License

License

Categories

Categories

Spring Boot Container Microservices Ant Build Tools MongoDB Data Databases
GroupId

GroupId

com.raufferlobo.springboot.multitenant.mongodb
ArtifactId

ArtifactId

spring-boot-multitenant-mongodb
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

An useful Mongodb factory which allows applications to choose the database name at runtime.
Project Organization

Project Organization

Pivotal Software, Inc.
Source Code Management

Source Code Management

http://github.com/rauffer/spring-boot-multitenant-mongodb/tree/master

Download spring-boot-multitenant-mongodb

How to add to project

<!-- https://jarcasting.com/artifacts/com.raufferlobo.springboot.multitenant.mongodb/spring-boot-multitenant-mongodb/ -->
<dependency>
    <groupId>com.raufferlobo.springboot.multitenant.mongodb</groupId>
    <artifactId>spring-boot-multitenant-mongodb</artifactId>
    <version>1.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.raufferlobo.springboot.multitenant.mongodb/spring-boot-multitenant-mongodb/
implementation 'com.raufferlobo.springboot.multitenant.mongodb:spring-boot-multitenant-mongodb:1.0.2'
// https://jarcasting.com/artifacts/com.raufferlobo.springboot.multitenant.mongodb/spring-boot-multitenant-mongodb/
implementation ("com.raufferlobo.springboot.multitenant.mongodb:spring-boot-multitenant-mongodb:1.0.2")
'com.raufferlobo.springboot.multitenant.mongodb:spring-boot-multitenant-mongodb:jar:1.0.2'
<dependency org="com.raufferlobo.springboot.multitenant.mongodb" name="spring-boot-multitenant-mongodb" rev="1.0.2">
  <artifact name="spring-boot-multitenant-mongodb" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.raufferlobo.springboot.multitenant.mongodb', module='spring-boot-multitenant-mongodb', version='1.0.2')
)
libraryDependencies += "com.raufferlobo.springboot.multitenant.mongodb" % "spring-boot-multitenant-mongodb" % "1.0.2"
[com.raufferlobo.springboot.multitenant.mongodb/spring-boot-multitenant-mongodb "1.0.2"]

Dependencies

compile (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-data-mongodb jar 1.5.9.RELEASE

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0

test (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.5.9.RELEASE
de.flapdoodle.embed : de.flapdoodle.embed.mongo jar 1.50.5
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

spring-boot-multitenant-mongodb

This project contains a spring-data-mongodb factory extension which is responsible to choose the database name at runtime.

For each request made by a user, Spring data asks the mongodb factory which database may use. This extension uses a TenantProvider interface to decide the database name at runtime.

Setting up your application

Add the Maven dependency:

<dependency>
	<groupId>com.raufferlobo.springboot.multitenant.mongodb</groupId>
	<artifactId>spring-boot-multitenant-mongodb</artifactId>
	<version>1.0.2</version>
</dependency>

Enable Spring boot auto configuration using the@EnableAutoConfiguration annotation:

@SpringBootApplication
@EnableAutoConfiguration
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
    
}

Create a TenantProvider implementation:

@Component
public class MyTenantResolver implements TenantResolver {

    @Override
    public String resolve(HttpServletRequest request) {
        return request.getHeader("X-Tenant"); // implement your own strategy
    }

}

Default behavior

Considering the following application.properties file:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=sample

The default database name is sample, so if the TenantProvider implementation returns tenant1 as result, the MultitenantMongoDbFactory will consider the database name as sample-tenant1.

You can also override the database prefix by providing a tenant prefix as a property:

spring.data.mongodb.tenant-prefix=my-prefix

When MultitenantMongoDbFactory finds this property, the result is my-prefix-tenant1 instead of sample-tenant1.

If you don't provide a TenantProvider implementation or if your TenantProvider return null, the factory will assume the default database name ("sample" in the previous example).

Versions

Version
1.0.2