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?
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?
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?
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.
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?
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.
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?
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.