Multitenancy with Spring Data and Mongo - spring-data

I am developing a SaaS application with Spring data and Mongo data with multitenancy on the schema level. So, separation is done on each collection. Each collection has tenant_id.
I managed to implement with Mongo template plus query but in that case, I lose repository abstraction.
What I can not find is how to extend repositories and use all the functions from repository API and add for each query type (find, save, update..) tenant_id in the query?

Related

Can I split data into 2 collections in Mongo DB and access the same in SpringBoot

I am using Mongo DB as Database for my springboot application.
I wanted to split the data into 2 collections -primary and auxillary just like how we do it in sql- priamary and secondary table.
Can i do the above and use them in my Springboot application??
Spring boot + Spring data mongo supports multiple collections out of the box.
Just create your POJOs and add the #Document annotation.
You can then define Repository classes (extends MongoRepository) that are then bound to each collection.
Use them to perform nearly any actions on the data.
If you want the data to be in multiple databases, that is also possible, just a bit more complicated.

Is there a support for schema versioning in Spring Data MongoDB?

To evolve the schema of documents or models schema versioning pattern [1] could be a way to go. Does spring data mongodb support this pattern? We use mainly the interface MongoRepository<T, ID> where T is our document.
I think of having MyDocumentV1 and MyDocumentV2 and could imagine to have an interface MyDocument. How to tell the Repository to use either instance based on schemaVersion?
[2] https://www.mongodb.com/docs/manual/tutorial/model-data-for-schema-versioning/

Discriminator based multi-tenancy with Spring Data MongoDB

There is a shared database that has a discriminator field "customer_id" in each collection. That is an approach that was taken for multi-tenancy support.
What would be the recommended approaches on how to implement support of the discriminator field in the latest Spring Data MongoDB?
The most straightforward way I can see at this point is to set "customer_id" in the ThreadLocal per each request (The data is exposed via REST API) and then just pass to each Spring Data method
findByXxxAndCustomerId(x, ThreadLocal.getCustomerId())

How can we achieve MongoDB DBRefs in QUARKUS - MONGODB WITH PANACHE?

I was following the below guidelines to implement MongoDB ORM in my Quarkus server app.
QUARKUS - SIMPLIFIED MONGODB WITH PANACHE
https://quarkus.io/guides/mongodb-panache
How can I achieve document reference with this?
For example: If I have 2 entities like Employee, Organisation then how can I refer Organisation in my Employee entity?
Quarkus MongoDB with Panache didn't offers specific support for dbref.
You may be able to use a field of type com.mongodb.DBRef but we will not automatically load the document referenced by it so you need to do it by yourself.

mongodb persistence patterns for JSON client app, jackson mapper or morphia driver?

I've started a new job where they are using mongodb in a java environment.
They have implemented a pattern using DTOs and factories with the morphia driver, this may be due to a migration onto mongodb from a key value store previously. The client is a JSON client.
It seems to me that the jackson-mongo-mapper would be a better approach because it's just mapping pojos from json to BSON and back, seems like it could do away with all DTO factory facade?
Anyone know any pros and cons with these different approaches?
Spring Data for Mongodb is very nice since you can use even another data store or mix them and repository interface is very helpful.
Kundera is an option through JPA2
http://agilemobiledeveloper.wordpress.com/2013/08/22/working-with-mongodb-using-kundera/
There's a lot of java to mongodb options.
http://www.agilemobiledeveloper.com/2013/01/31/hibernate-ogm-mongodb-vs-kundera-vs-jongo-vs-mongodb-api-vs-morphia-vs-spring-data-mongo-mongodb-drivers-for-java/
Adding your own data layer and making sure you use DI and test it fully is very helpful.
NOSQLUnit is awesome -> https://github.com/lordofthejars/nosql-unit
DTOs are good for keeping a separation between implementation and design, so when they need or want to switch from mongo to some other NoSQL or SQL database it can be done cleanly.