Getting Started Use Any Data Model Using Multi-Model with Python and Node.js

Using Multi-Model with Python and Node.js

time to complete
45 minutes

This exercise details the process of using the InterSystems IRIS® data platform multi-model capability to create a Node.js application that sends JSON data straight to your database instance without any parsing or mapping. If you have not done so, it is recommended that you also go through the Multi-Model QuickStart.

In this exercise, we will use Python, JavaScript, and InterSystems ObjectScript to interact with the data from different contexts, following these steps:

  1. Use Python to create a table schema using standard SQL statements.
  2. Modify the underlying ObjectScript class for that table to allow it to receive and persist JSON data directly.
  3. Create a simple Node.js application that will send JSON files to the instance of InterSystems IRIS.
  4. Query that database using Python again to see how the same data could be accessed in multiple languages from multiple contexts.

Get set up

  1. First, make sure you have an instance of InterSystems IRIS and an IDE ready to go. Sandbox settings will appear below after you log in and launch a sandbox.
    Need InterSystems IRIS?
    Get a free, online development sandbox here. Log in with your InterSystems login account, or register for one below.
  2. Open the InterSystems IRIS sandbox IDE and clone this repository: git clone -b try-iris
  3. Confirm that the clone was successful. Look in the Explorer pane on the left of the IDE for the multi-model-exercise folder. If it does not appear, open the IDE in your browser’s private/incognito window and try again.
  4. Open the connections.config file in the top-level directory of the multi-model-exercise folder. Update the IP equal to -- cannot display value - please provision a sandbox and change the value of port to -- cannot display value - please provision a sandbox. Then click Save. You’ve now written an ODBC connection configuration that will be used by Python to connect to your InterSystems server.

Create the table schema using Python

  1. Install and configure Python.
    cd /home/project/multi-model-exercise/python
    sudo odbcinst -i -d -f pyodbc_wheel/odbcinst.ini
  2. Open python/ and scroll down to the create_employee function. Below the function declaration, uncomment the following code:


    create_employee = """
    	CREATE TABLE Demo.Employee(
    	    Name varchar(256),
    	    Title varchar(256), 
    	    Department varchar(50)

    Save the modified file. As you can see, this is a standard SQL create statement that will generate an Employee table on your InterSystems IRIS instance.

  3. Run python3 If successful, the terminal will output Created table Demo.Employee successfully.
  4. Confirm that the Demo.Employee table has been created.
    • Open the Management Portal by clicking InterSystems > Management Portal at the top of your sandbox IDE.
    • Navigate to System Explorer > SQL and expand the Tables section. Find Demo.Employee in the list.

Modify the table class using InterSystems ObjectScript

Modifying ObjectScript classes

  1. Click the ObjectScript: Explorer icon  on the left side of your IDE window, then right-click Classes/Demo/Employee.cls > Export. If Employee.cls is not listed, refresh the ObjectScript Explorer by clicking the Refresh button in the top-right corner of the panel.
  2. Navigate to the Explorer pane and open the newly created cls/Demo/Employee.cls file.
  3. At the top of the Demo.Employee class, change Extends %Persistent to Extends (%Persistent, %JSON.Adaptor).
    InterSystems ObjectScript is an object-oriented programming language that supports multiple inheritance. This means that by inheriting the %JSON.Adaptor class, your table is now automatically able to import JSON data into instances. For more information on the %JSON.Adaptor class, read Using the JSON Adaptor.
  4. Because our table includes an auto-incremented primary key, we need to tell the JSON.Adaptor class not to look for that field in incoming JSON files, but to output it as a field when exporting class instances to JSON format. To do this, find the ID property in the Employee class and add (%JSONINCLUDE = "outputonly") after %Library.AutoIncrement.
  5. Before we can run this file, we need to add one small class method to expose the functionality of the %JSON.Adaptor class to the Native API — and, by extension, to our Node.js application. Below the Property, Parameter, and Index declarations in the Demo.Employee class, paste the following code:
    ClassMethod fromJSON(jsonString as %String) As %Status
    	set employee = ..%New() 		        //create a new class instance
    	do employee.%JSONImport(jsonString) 	//call the %JSON.Adapter instance method to import JSON string
    	set employee.ID = 0 			        //this field must be set to 0 for the %Library.AutoIncrement class to increment correctly
    	set status =  employee.%Save() 		 //this persists the instance
    	return status
  6. Make sure to recompile the Demo.Employee class by saving it. If an alert appears about the file version, then you should choose to Overwrite on Server. You have now configured your SQL table class to receive JSON data and automatically create a new record from it.
    Note that the completed ObjectScript Employee class is included in this repository for your reference at ObjectScript/Demo.Employee.cls.

Create a Node.js app to send JSON files to your database

  1. Create a new file in the nodeApp folder called record.json containing the following JSON object:
    	"Name": "JJ Smith",
    	"Title": "Software Engineer",
    	"Department": "Engineering"
  2. Run cd /home/project/multi-model-exercise/nodeApp.
  3. Run npm install --save intersystems-iris-native. This installs the InterSystems IRIS Native API, which enables you to both access the underlying data structures in your database, and to call ObjectScript class methods directly from your code.
  4. Open the app.js file. Navigate to const Iris = connection.createIris() and uncomment the following lines, leaving them indented in the file:
    let record = JSON.parse(fs.readFileSync("./record.json", "utf8"))
        Iris.classMethodValue("Demo.Employee", "fromJSON", JSON.stringify(record))
        console.log(`Created new record in Demo.Employee table.`)

    This code calls a class method using the Native API and passes a JSON string as a parameter. For more information, see Calling ObjectScript Methods and Functions

  5. Make sure to save the file. Then in the terminal, type node app.js. The node application will output that it has created a new record.
    If you get a “Method does not exist” error message, make sure you clicked Save in Visual Studio Code after modifying the Demo.Employee class.
    You have now created a basic node application to communicate with your InterSystems server.

Query the database with Python

  1. cd back into the Python directory  (cd /home/project/multi-model-exercise/python).
  2. Run python You should see the results of the SQL query, which includes the record of JJ Smith that you inserted using Node.js.
Up Next
Globals QuickStart
time to complete
10 minutes