Domain Model Examples
Comprehensive examples for working with Mendix domain models through the Web Extensibility API.
Creating Entities and Associations
This example demonstrates how to create a complete domain model with multiple entities and associations. It creates a customer energy management system with three entities: Customer, EnergyConnection, and MeterReading.
// Access the domain models from the Studio Pro API
const { projects, domainModels, microflows } = studioPro.app.model;
// Load the domain model for a specific module
const domainModels_list = await domainModels.loadAll(
(info) => info.moduleName === 'MySecondModule'
);
const domainModel = domainModels_list[0];
// Verify the domain model was found
if (!domainModel) {
log('❌ Domain model not found');
return;
}
// Check if Customer entity already exists
let customer = domainModel.entities.find(e => e.name === 'Customer');
if (!customer) {
log('Creating Customer entity...');
// Create a new Customer entity with multiple attributes
customer = await domainModel.addEntity({
name: 'Customer',
attributes: [
{ name: 'CustomerNumber', type: 'String' },
{ name: 'Name', type: 'String' },
{ name: 'Email', type: 'String' },
{ name: 'PhoneNumber', type: 'String' },
{ name: 'BillingAddress', type: 'String' },
{ name: 'RegistrationDate', type: 'DateTime' }
]
});
// Set required metadata for the entity
customer.dataStorageGuid = crypto.randomUUID();
customer.location = { x: 100, y: 100 }; // Position in domain model diagram
// Configure each attribute with storage GUIDs and type-specific settings
customer.attributes.forEach(attr => {
attr.dataStorageGuid = crypto.randomUUID();
// Set maximum length for string attributes
if (attr.type.$Type === 'DomainModels$StringAttributeType') {
attr.type.length = 200;
}
});
await domainModels.save(domainModel);
log('✅ Created Customer entity with extended attributes');
} else {
log('✅ Customer entity already exists');
}
// Check if EnergyConnection entity already exists
let energyConnection = domainModel.entities.find(e => e.name === 'EnergyConnection');
if (!energyConnection) {
log('Creating EnergyConnection entity...');
// Create EnergyConnection entity with various attribute types
energyConnection = await domainModel.addEntity({
name: 'EnergyConnection',
attributes: [
{ name: 'ConnectionNumber', type: 'String' },
{ name: 'Address', type: 'String' },
{ name: 'ConnectionType', type: 'String' },
{ name: 'MaxCapacityKW', type: 'Decimal' }, // Decimal type for precise numbers
{ name: 'ActivationDate', type: 'DateTime' },
{ name: 'IsActive', type: 'Boolean' } // Boolean type for true/false values
]
});
// Set entity metadata and diagram position
energyConnection.dataStorageGuid = crypto.randomUUID();
energyConnection.location = { x: 500, y: 100 };
// Configure attributes
energyConnection.attributes.forEach(attr => {
attr.dataStorageGuid = crypto.randomUUID();
if (attr.type.$Type === 'DomainModels$StringAttributeType') {
attr.type.length = 200;
}
});
await domainModels.save(domainModel);
log('✅ Created EnergyConnection entity');
} else {
log('✅ EnergyConnection entity already exists');
}
// Check if MeterReading entity already exists
let meterReading = domainModel.entities.find(e => e.name === 'MeterReading');
if (!meterReading) {
log('Creating MeterReading entity...');
// Create MeterReading entity to track energy usage
meterReading = await domainModel.addEntity({
name: 'MeterReading',
attributes: [
{ name: 'ReadingDate', type: 'DateTime' },
{ name: 'MeterType', type: 'String' },
{ name: 'ReadingValue', type: 'Decimal' },
{ name: 'Unit', type: 'String' },
{ name: 'IsEstimated', type: 'Boolean' }
]
});
// Set entity metadata and diagram position
meterReading.dataStorageGuid = crypto.randomUUID();
meterReading.location = { x: 900, y: 100 };
// Configure attributes
meterReading.attributes.forEach(attr => {
attr.dataStorageGuid = crypto.randomUUID();
if (attr.type.$Type === 'DomainModels$StringAttributeType') {
attr.type.length = 200;
}
});
await domainModels.save(domainModel);
log('✅ Created MeterReading entity');
} else {
log('✅ MeterReading entity already exists');
}
log('Creating associations...');
// Create association between EnergyConnection and Customer
// One customer can have many energy connections
let connectionToCustomer = domainModel.associations.find(
a => a.name === 'EnergyConnection_Customer'
);
if (!connectionToCustomer) {
connectionToCustomer = await domainModel.addAssociation({
name: 'EnergyConnection_Customer',
parentEntity: { $ID: energyConnection.$ID, name: energyConnection.name },
childEntity: { $ID: customer.$ID, name: customer.name },
multiplicity: 'one_to_many' // One customer → many connections
});
// Set association metadata and connection points for diagram
connectionToCustomer.dataStorageGuid = crypto.randomUUID();
connectionToCustomer.parentConnection = { x: 0, y: 0 };
connectionToCustomer.childConnection = { x: 100, y: 0 };
await domainModels.save(domainModel);
log('✅ Created EnergyConnection_Customer association');
} else {
log('✅ EnergyConnection_Customer association already exists');
}
// Create association between MeterReading and EnergyConnection
// One energy connection can have many meter readings
let readingToConnection = domainModel.associations.find(
a => a.name === 'MeterReading_EnergyConnection'
);
if (!readingToConnection) {
readingToConnection = await domainModel.addAssociation({
name: 'MeterReading_EnergyConnection',
parentEntity: { $ID: meterReading.$ID, name: meterReading.name },
childEntity: { $ID: energyConnection.$ID, name: energyConnection.name },
multiplicity: 'one_to_many' // One connection → many readings
});
// Set association metadata and connection points for diagram
readingToConnection.dataStorageGuid = crypto.randomUUID();
readingToConnection.parentConnection = { x: 0, y: 0 };
readingToConnection.childConnection = { x: 100, y: 0 };
await domainModels.save(domainModel);
log('✅ Created MeterReading_EnergyConnection association');
} else {
log('✅ MeterReading_EnergyConnection association already exists');
}
log('🎉 Domain model setup complete!');Key Concepts
Entity Creation:
- Use
domainModel.addEntity()to create new entities - Set
dataStorageGuidusingcrypto.randomUUID()for database mapping - Position entities in the diagram using
locationcoordinates
Attribute Types:
String- Text data (setlengthproperty)DateTime- Date and time valuesDecimal- Precise numeric valuesBoolean- True/false values
Associations:
- Use
domainModel.addAssociation()to create relationships - Specify
parentEntityandchildEntitywith $ID and name - Set
multiplicityto define the relationship type:one_to_many- One parent can have multiple childrenmany_to_one- Many parents to one childone_to_one- One-to-one relationshipmany_to_many- Many-to-many relationship
Best Practices:
- Always check if entities/associations exist before creating
- Save the domain model after each modification
- Use descriptive entity and attribute names
- Set proper attribute types and constraints