Comments (4)
You can define your own NamingStrategy
and configure Morphia to use that instead. Simply pass the class name for your custom strategy rather than the named type.
from morphia.
We have implemented the NamingStrategy
but it's applied when the EntityModel
is first mapped and cannot be changed after that.
In our case, we have multiple collections for the same object, one for each tenant. Which collection the object is written to depends on which tenant is currently saving the object. For example, we have tenanta.objects
, tenantb.objects
, tenantc.objects
and so on.
The NamingStrategy
allows us to manipulate the objects
part of the collection name, but not to dynamically add the tenant
part.
Before we used to override the getCollectionName
method of the Mapper
which was called when the collection name was needed. However, with the NamingStrategy
that's no longer possible as it's set upfront.
from morphia.
Multitenancy isn't directly supported in 2.x though it's on the roadmap for 3.x. If you need something like that now, your best bet is to create a Datastore
per tenant which is less than ideal, I would agree, but as of now it's the only real option.
from morphia.
I've made the change locally to support this use case.
I've added a new method to the EntityModelBuilder
which exposes the NamingStrategy
. Then in the EntityModel
constructor I store a copy of the NamingStrategy
in the EntityModel
itself. That way the NamingStrategy
can be called directly when getCollectionName
is called.
@NonNull
public String getCollectionName() {
if (namingStrategy == null) {
throw new MappingException(Sofia.noMappedCollection(getType().getName()));
}
return namingStrategy.apply(type.getSimpleName());
}
As NamingStrategy
is called at runtime, we can use it to supply the tenant.
Not sure if this works as a broader solution, but thought it might be worth sharing. Of course, it could be optimised, maybe with an option specifying if the NamingStrategy
should be cached, in which it works as it did previously.
from morphia.
Related Issues (20)
- dev.morphia.Datastore Thread Safety HOT 4
- Prefer to load custom property codec providers before default codec providers.
- why `Query#iterator` return `dev.morphia.query.internal.MorphiaCursor` instead of `dev.morphia.query.MorphiaCursor` HOT 1
- upsert of $inc with map of map throwing ValidationException HOT 1
- jakarta.validation.ValidationException: HV000183: Unable to initialize 'jakarta.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead HOT 1
- Implement aggregation operator $querySettings
- Implement aggregation operator $or
- Implement aggregation operator $size
- Implement aggregation operator $not
- Implement aggregation operator $jsonSchema
- Implement aggregation operator $geometry
- Why are my Morphia Cursors stacking up ? HOT 1
- Implement query operator $or
- Implement query operator $size
- Implement query operator $not
- Implement query operator $jsonSchema
- Implement query operator $geometry
- Morphia Quering is not working as expected. version 2.4 HOT 1
- Update DiscriminatorFunction API to return the value rather than set it directly
- When Adding @Deprecated please add alternatives to use. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morphia.