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