Reputation: 1
Im having a requirement to create mongodb collections by tenant specific. Eg: I have a collection called audit_logs, but I want to create the following way
tenant : EAD collection : ead_audit_logs tenant : gtc collection : gtc_audit_logs and so on.
somebody please help me to do the configuration in micronaut.
im using micronaut-data and the configuration to be done in the pojo/entity class is not working out.
@Getter
@Setter
@MappedEntity
@Property(name = "#{@ThreadContext.get('tenant')}_audit_log")
public class AuditLog {
@Id
@GeneratedValue
private String id;
}
Upvotes: 0
Views: 230
Reputation: 9071
The code you provided is not valid because the @Property
annotation on the class entity doesn't have any effect. To achieve the desired result, You can provide a custom implementation for the MongoCollectionNameProvider
interface.
Example:
@Primary
@Singleton
public class CustomMongoCollectionNameProvider implements MongoCollectionNameProvider {
@Override
public String provide(PersistentEntity persistentEntity) {
return String.format("%s_%s", ThreadContext.get("tenant"), persistentEntity.getPersistedName());
}
}
In the above example, we have overridden the provide
method in the MongoCollectionNameProvider
interface. You can add your collection naming logic to this method.
And make sure to annotate the class with @Primary
and @Singleton
. This will ensure that the custom implementation overrides the default bean DefaultMongoCollectionNameProvider.
Upvotes: 0