Code Monkey home page Code Monkey logo

Comments (4)

evanchooly avatar evanchooly commented on July 19, 2024

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.

agiannone avatar agiannone commented on July 19, 2024

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.

evanchooly avatar evanchooly commented on July 19, 2024

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.

agiannone avatar agiannone commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.