Unpacking the Mendix Software Development Kit

In this post, we’ll cover the basic structure, core functionalities, and how the SDK fits into the Mendix ecosystem. While we won’t go step-by-step through the setup process, I have included direct links to the most relevant official resources to help you get started quickly and accurately.
Prerequisites:
1. Set Up Your Development Environment
2. Set Up Your Personal Access Token (PAT)
3. Recognising Where and How to Run Code I suggest reading the Mendix manual called Create Your First Script. to get insight into how to run code efficiently to get the intended outcomes. You will gain a better knowledge from this material.
Developing a Novel Mendix Application with Entities, Characteristics, Connections, and Their Actions
Setting up a new Mendix application, defining entities, adding characteristics of different types, establishing relationships between them, and configuring association types and associated delete actions are all covered in this part.
1. Configuring the Mendix App
We start by creating a new app and connecting to the Mendix platform. Git will be used as the repository type for version control when the application is first launched.
import { domainmodels } from "mendixmodelsdk";
import { MendixPlatformClient } from "mendixplatformsdk";
async function main() {
const client = new MendixPlatformClient();
// Create a new app
const app = await client.createNewApp(`SDKDemo`, {
repositoryType: "git"
});
console.log("New app created successfully!");
}
Here, we communicate with the platform using "MendixPlatformClient." The "createNewApp" function uses a Git-based repository to store project versions and generates a new application called SDKDemo.
2. Creating a Temporary Working Copy
We build a temporary functioning duplicate of the app after it is developed. Before submitting modifications to the repository, developers can work on the Mendix model locally using a working copy.
// Create a temporary working copy
const workingCopy = await app.createTemporaryWorkingCopy("main");
console.log("Temporary working copy created successfully.");
3. Opening the Domain Model
We load the domain model from a particular app module after making the working copy. The domain model is loaded from MyFirstModule in this example.
// Open the model
const model = await workingCopy.openModel();
console.log("Model opened successfully.");
// Load the domain model from the specified module
const domainModelInterface = model.allDomainModels().filter(dm => dm.containerAsModule.name === "MyFirstModule")[0];
const domainModel = await domainModelInterface.load();
console.log("Domain model loaded successfully.");
Here, “allDomainModels()” retrieves all domain models in the application, and we filter to get the one belonging to “MyFirstModule”.
4. Creating Entities and Attributes
Now, we can create entities and attributes. We construct two entities, Observation and Checkpoint, each of which has a set of properties.
// Create the first entity
const entity0 = domainmodels.Entity.createIn(domainModel);
entity0.name = `Checkpoint`;
console.log("Entity 'Checkpoint' created successfully.");
// Create an attribute for the first entity
const attribute = domainmodels.Attribute.createIn(entity0);
attribute.name = "NumberOfObservation";
attribute.type = domainmodels.IntegerAttributeType.create(model);
console.log("Attribute 'NumberOfObservation' created successfully.");
An integer attribute called NumberOfObservation is added to the first entity, which is called Checkpoint. Likewise, a string property called Name is used to form the second entity, Observation.
// Create the second entity
const entity1 = domainmodels.Entity.createIn(domainModel);
entity1.name = `Observation`;
console.log("Entity 'Observation' created successfully.");
// Create an attribute for the second entity
const attribute1 = domainmodels.Attribute.createIn(entity1);
attribute1.name = "Name";
attribute1.type = domainmodels.StringAttributeType.create(model);
console.log("Attribute 'Name' created successfully.");
5. Setting Up Associations
We then establish a connection between Checkpoint and Observation. This establishes a parent-child bond between the two things.
// Create an association between the two entities
const association = domainmodels.Association.createIn(domainModel);
association.name = "Observation_Checkpoint";
association.parent = entity1;
association.child = entity0;
console.log("Association 'Observation_Checkpoint' created successfully.");
The direction of the association is specified by the parent and child parameters. Here, Checkpoint is the child and Observation is the parent.
6. Configuring Delete Behavior
Mendix provides delete behaviors to manage how deletions of associated entities should be handled. For example, when an Observation is deleted, we may want to delete its associated Checkpoint as well.
// Set the delete behavior for the association
association.deleteBehavior = domainmodels.AssociationDeleteBehavior.createIn(association);
association.deleteBehavior.parentDeleteBehavior = domainmodels.DeletingBehavior.DeleteMeButKeepReferences;
association.deleteBehavior.childDeleteBehavior = domainmodels.DeletingBehavior.DeleteMeAndReferences;
console.log("Delete behavior set successfully.");
Here, we specify that while deleting an Observation would not impact the Checkpoint, deleting a Checkpoint will erase the Observation.
7. Committing the Changes
We flush and commit the modifications to the repository after they are finished.flushChanges() sends all staged (in-memory) changes made to the Mendix model to the local working copy. Until flushed, changes are not fully registered in the model and won’t be available for committing.
// Flush changes to the model
await model.flushChanges();
console.log("Changes flushed successfully.");
// Commit the changes to the repository
await workingCopy.commitToRepository("main", {
commitMessage: "Created application, entity, attributes and association with association type and delete behaviour of an association."
});
console.log("Changes committed successfully.");
main().catch(error => {
console.error("An error occurred:", error);
});
I urge you to compile all the code snippets from this blog into a single file and run it as we end. Upon successful execution of the script, the newly generated application ought to be visible on the Mendix Sprintr page. To confirm that everything was produced as planned, you may then launch the application in Studio Pro.