SDK - How to use the serializeToJs() function?

0
I have heard about the serializeToJs function (https://docs.mendix.com/apidocs-mxsdk/mxsdk/generating-code-from-the-model/). This is a description to get information about the code to generate parts of the domain model. How can I use the serializeToJs function to get the code to create my whole Mendix App? Especially I am interested to see, how to create pages.
asked
1 answers
0

I am not sure if it is possible to generate source code for the entire project. However, you can pass the required model unit(microflow, pages, etc..) to serializeToJs() method that generates the source code for the same.

 

The below code snippet generates source code for the microflow. You can try retrieving the page model unit and give a try.

 

async function serializeToJs() {
    try {
        const client = new MendixPlatformClient();
        const moduleName = "MyFirstModule";
        const mfName = "IVK_TestSDKFlow";
        const app = client.getApp("YOUR-APP-ID");
        const workingCopy = await app.createTemporaryWorkingCopy("trunk");
        const model = await workingCopy.openModel();

        const module = model.findModuleByQualifiedName(moduleName);

        const domainModelInterface = model
            .allDomainModels()
            .filter((dm) => dm.containerAsModule.name === moduleName)[0];

        const domainModel = await domainModelInterface.load();

        const mfInterface = model
            .allMicroflows()
            .filter((mf) => mf.qualifiedName === `${moduleName}.${mfName}`)[0];

        const mf = await mfInterface.load();

        // Redirect generated code to a file.
        fs.writeFile(
            "autogenerated/model-autogenerated.js",
            JavaScriptSerializer.serializeToJs(mf),
            (err) => {
                if (err) {
                    console.error(`Exception while writing to a file. ${err}`);
                }
            }
        );

        console.log(
            `Domain model source code generation for the module ${moduleName} completed.`
        );
    } catch (error) {
        console.log(`error: ${error}`);
    }
}

serializeToJs();

 

answered