multi tenancy with arangodb spring data

300 views
Skip to first unread message

bdardik

unread,
Jun 11, 2018, 6:39:38 AM6/11/18
to ArangoDB
Hey guys, I'm currently trying to hack my way through implementing multi tenancy with arangodb spring data.
the plan is to have multiple ArangoRepositories that each reference different arangodb db instance.
after some reading online and deep diving into the code i came up with this example
Map<String, ArangoRepository<ArangoDocument>> repos = (Map<String, ArangoRepository<ArangoDocument>>) applicationContext.getBeansOfType(typeRef.getClass());
final ArangoOperations operations = arangoConfig.arangoTemplate();
ArangoRepositoryFactory arangoRepoFactory = new ArangoRepositoryFactory(operations); 
arangoRepoFactory.setQueryLookupStrategyKey(Key.CREATE_IF_NOT_FOUND);
repos.entrySet().stream().forEach(entry -> {
ArangoRepositoryFactoryBean<? extends ArangoRepository, String> repoBean = new ArangoRepositoryFactoryBean<>(entry.getValue().getClass());
repoBean.setArangoOperations(operations);
});
ArangoDiscoveryRepository discovery = arangoRepoFactory.getRepository(ArangoDiscoveryRepository.class);

quick review over the code:
im creating manually a ArangoOperation object for each db 
instantiate a ArangoRepositoryFactory with the correct ArangoOperation
set the default lookup strategy
register the correct repositories 
and then use the factory to obtain an instance of the repository

My questions are :
I didnt find any by design support in arangodb spring data for multi tenancy, did i miss it?
is there any things i need to take into account with the way im doing it?

 Note: its just play around code 
Any advice will be extremely appreciated 

mpv1989

unread,
Jul 25, 2018, 5:46:27 AM7/25/18
to ArangoDB
Hi bdardik,

Do you mean with "different arangodb db instance" separate ArangoDB servers or one ArangoDB server with multiple databases in addition to the _system database?

For Multi-tenancy on server level, we currently do not have any convenient solution, but if this is something you need, feel free to open a feature request on https://github.com/arangodb/spring-data/issues.
For Multi-tenancy on database and collection level, we already have a solution using SpEL (Spring Expression Language). It is currently not part of a release. But you can already test it with arangodb-spring-data 3.0.0-SNAPSHOT.

Example - Multi-tenancy on database level

You can use a database name pre- or postfix using SpEL. For this you can use a Spring bean to expose the tenant prefix into a ThreadLocal.

@Component
public class TenantProvider {


 
private final ThreadLocal<String> id;


 
public TenantProvider() {
   
super();
    id
= new ThreadLocal<>();
 
}


 
public String getId() {
   
return id.get();
 
}


 
public void setId(final String id) {
   
this.id.set(id);
 
}


}


Then use the SpEL expression in your configuration class in `database()`

@Configuration
public class MyConfiguration extends AbstractArangoConfiguration {


 
@Override
 
public ArangoDB.Builder arango() {
   
return new ArangoDB.Builder();
 
}


 
@Override
 
public String database() {
   
return "#{tenantProvider.getId()}_db";
 
}


}

Now you can set the tenant id/prefix before making a database call and automatically talk with the tenant associated database.

Example - Multi-tenancy on collection level

Same as for database level you need a Spring bean which stores your tenant id/prefix. This time you use your SpEL expression in your domain types annotations `@Document`/`@Edge`.

@Document("#{tenantProvider.getId()}_entity")
public class MyEntity {


}

This creates you a pre-/postfixed collection for every of your tenants.

best
Mark
Reply all
Reply to author
Forward
0 new messages