How to create an instance of an existing project with Mendix SDK?

0
Hello, I've followed the instructions from  https://docs.mendix.com/apidocs-mxsdk/mxsdk/creating-your-first-script and I'm able to create a new project using the Mendi SDK. Now I'd like to create an instance of an existing project with Mendix SDK, not a new one. How can I do this?
asked
1 answers
2

Hi Daniel

The article Generate Code from the Model has some sample code that shows how to load an existing project.

Here is the code from that article:

import { ModelSdkClient, IModel, IModelUnit, domainmodels, utils } from "mendixmodelsdk";
import { MendixSdkClient, Project, OnlineWorkingCopy, loadAsPromise } from "mendixplatformsdk";

const username = "{YOUR_USERNAME}";
const apikey = "{YOUR_API_KEY}";
const client = new MendixSdkClient(username, apikey);
// Please change your project Id and name to something you prefer.
let projectName = "{YOUR_PROJECT_NAME}";
let projectId = "{YOUR_PROJECT_ID}";
let moduleName = "MyFirstModule";
let project = new Project(client, projectId, projectName);

async function serializeToJs() {
    const workingCopy = await project.createWorkingCopy();
    const domainModelInterface = workingCopy.model().allDomainModels().filter(dm => dm.containerAsModule.name === moduleName)[0];

    try {
        const domainModel = await loadAsPromise(domainModelInterface);
        console.log(utils.serializeToJs(domainModel)); //print out the generated JavaScript
        console.log("success!")
    } catch (error) {
        console.log(`error: ${error}`);
    }
}

serializeToJs();

Cheers

iain

answered