Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ModelAPI Entry Point

The api package provides a high-level fluent API inspired by the Mendix Web Extensibility Model API. It wraps the lower-level reader/writer with builder patterns for common operations.

Creating the ModelAPI

import (
    "github.com/mendixlabs/mxcli/api"
    "github.com/mendixlabs/mxcli/sdk/mpr"
)

writer, _ := mpr.OpenForWriting("/path/to/MyApp.mpr")
defer writer.Close()

modelAPI := api.New(writer)

Setting the Module Context

Most operations require a module context. Set it before calling builders:

module, _ := modelAPI.Modules.GetModule("MyModule")
modelAPI.SetModule(module)

All subsequent Create* calls will create elements in this module.

Namespace Accessors

The ModelAPI struct provides access to domain-specific APIs through namespace accessors:

NamespaceAccessorDescription
DomainModelsmodelAPI.DomainModelsCreate/modify entities, attributes, associations
EnumerationsmodelAPI.EnumerationsCreate/modify enumerations and their values
MicroflowsmodelAPI.MicroflowsCreate microflows with parameters and return types
PagesmodelAPI.PagesCreate pages with widgets
ModulesmodelAPI.ModulesList and retrieve modules

DomainModels Namespace

// Create an entity
entity, _ := modelAPI.DomainModels.CreateEntity("Customer").
    Persistent().
    WithStringAttribute("Name", 200).
    Build()

// Create an association
assoc, _ := modelAPI.DomainModels.CreateAssociation("Customer_Orders").
    From("Customer").
    To("Order").
    OneToMany().
    Build()

Enumerations Namespace

enum, _ := modelAPI.Enumerations.CreateEnumeration("OrderStatus").
    WithValue("Draft", "Draft").
    WithValue("Active", "Active").
    WithValue("Closed", "Closed").
    Build()

Microflows Namespace

mf, _ := modelAPI.Microflows.CreateMicroflow("ACT_ProcessOrder").
    WithParameter("Order", "MyModule.Order").
    WithStringParameter("Note").
    ReturnsBoolean().
    Build()

Pages Namespace

page, _ := modelAPI.Pages.CreatePage("CustomerOverview").
    WithTitle("Customer Overview").
    Build()

Modules Namespace

// List all modules
modules, _ := modelAPI.Modules.ListModules()

// Get a specific module
module, _ := modelAPI.Modules.GetModule("MyModule")

MDL to Fluent API Mapping

MDL StatementFluent API Method
CREATE PERSISTENT ENTITYDomainModels.CreateEntity().Persistent().Build()
CREATE NON-PERSISTENT ENTITYDomainModels.CreateEntity().NonPersistent().Build()
CREATE ASSOCIATIONDomainModels.CreateAssociation().Build()
CREATE ENUMERATIONEnumerations.CreateEnumeration().Build()
CREATE MICROFLOWMicroflows.CreateMicroflow().Build()
CREATE PAGEPages.CreatePage().Build()

When to Use the Fluent API vs Direct SDK

Use CaseRecommended Approach
Simple CRUD operationsFluent API (less boilerplate)
Complex microflow constructionDirect SDK types (more control)
Bulk operationsDirect writer methods (batch efficiency)
Integration testingFluent API (readable test code)