Is it possible to create or update a Dynamic Model via REST API?

Hello Jmix team,

I am using Jmix 3.1.0 with the Dynamic Model add-on. I can define entities, attributes and enumerations in the admin UI (Dynamic model settings view) and apply them successfully.

My question: Is it possible to create or update the dynamic model (entities, attributes, enumerations) through the REST API instead of the admin UI?

Thank you.

Hello,

there is no such feature implemented in Jmix at this moment.

Theoretically you can make your export-import service to send YAML definitions over the REST, and then apply the changes.
This is also dangerous.

Are you having a cluster? There is support for the situations like when a change applied to one node is also applied on all other nodes.

Kind regards,
Mladen

Hi @manikandanmariyappan,

As Mladen wrote, there is no standard operation for updating the dynamic model remotely.

However, all the process is encapsulated in just a few bean methods, so you could create your own REST controller like this one (this is AI-generated untested code):

@RestController
@RequestMapping("/api/dynmodel")
public class DynamicModelController {

    @Autowired DynamicModelSettingsService settingsService;
    @Autowired DynamicModelManager modelManager;
    @Autowired DynamicModelDefinitionSerializer serializer;
    @Autowired DataManager dataManager;

    record ModelDto(@Nullable UUID baseId, String content) {}

    @GetMapping
    public ModelDto get() {
        DynamicModelSettings s = settingsService.loadActive();
        // baseId is null when no model was ever saved (loadActive returns a new record then)
        return new ModelDto(entityStates.isNew(s) ? null : s.getId(), s.getContent());
    }

    @PostMapping("/apply")
    public void apply(@RequestBody ModelDto dto) {
        DynamicModelSettings settings = dto.baseId() == null
                ? settingsService.loadActive()   // first-ever save
                : dataManager.load(DynamicModelSettings.class).id(dto.baseId()).one();
        settings.setContent(dto.content());

        DynamicModelDefinition modelDef = serializer.deserialize(dto.content());
        List<DynamicModelMigration> migrations = modelManager.getMigrations(modelDef);
        settingsService.applyAndSave(migrations, settings);
    }
}

Could you describe your use case in more detail, so we would consider adding this as a standard feature?

Regards,
Konstantin

Hi,

Thank you for the suggestion.

I tested the custom REST controller based on your example, and it is working fine.

My use case is a multi-tenant application. I want each tenant administrator to create and manage dynamic entities and fields through REST APIs at runtime.

Could you please suggest the recommended approach for implementing tenant-specific dynamic models in a multi-tenant Jmix application?

Thank you!

Unfortunately, tenant-specific dynamic models are not possible. The dynamic model add-on modifies the metadata of the whole application, which is shared among all tenants. So changes made by one tenant will immediately affect all the others.

Hi Konstantin,

I am considering a database-per-tenant architecture using the Jmix sample below:

https://github.com/jmix-projects/multidb-multitenancy-sample

Since each tenant has its own database, would it be possible to create and maintain tenant-specific Dynamic Models independently, while using the same Jmix application instance?

Would this approach overcome the application-wide metadata limitation, or would it still require customization of the Dynamic Model add-on?

Thank you!

Hi @vimalkumar010

The database-per-tenant approach still assumes a single application with the common metadata. So the Dynamic Model add-on will modify the metadata for all tenants simultaneously.

Regards,
Konstantin

Hi Konstantin,

Thank you for the clarification.

Are there any plans to support tenant-specific metadata in future Jmix releases, allowing each tenant to independently create and modify Dynamic Models within a single application instance?

If not currently planned, would such an architecture be technically feasible through future enhancements to Jmix?

I would appreciate your thoughts on this.

Thank you!

Tenant-specific metadata would not be enough - a lot of internal structures would need to be tenant-specific, e.g. views and menu registries. So it’s a slippery slope.

Looks like your tenants have a lot of differences in the application logic and you don’t mind storing their data in different databases. Then why not use separate deployments? I would rather invest in the standard infrastructure and deployment pipelines than trying to hack everything into a single application.

Regards,
Konstantin