This exercise details the process of using 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.
In this exercise, we will use Python, JavaScript, and InterSystems ObjectScript to interact with data from different contexts, following these steps:
- Use Python to create a table schema using standard SQL statements.
- Modify the underlying ObjectScript class for that table to allow it to receive and persist JSON data directly.
- Create a simple Node.js application that will send JSON files to the instance of InterSystems IRIS.
- Query that database using Python again to see how the same data could be accessed in multiple languages from multiple contexts.
It is recommended that you use the InterSystems IRIS Sandbox to run this exercise. If you do so, skip the first installation step and begin the exercise with step # 2.
-
This exercise requires the 64-bit version of Python 3.
-
If you already have Python installed, be sure to check what bit version you are using by launching the python shell by typing
python
. If the version is 2, try quitting the shell (control-z + enter
on Windows, orcontrol-d
on macOS) and typingpython3
. -
Install Python by going here https://www.python.org/downloads/ (be sure to check off 'Add Python to environment variables' in the 'Advanced Options' section of the installation.
-
Note: do not click the 'Download Python 3.7.4' button directly on that site as it might download the 32 bit version of python, which will not work with the exercise. Select the link to your operating system and download the 64 bit Python file.
-
You may need to restart your terminal or even add python to the PATH environment variable if the python command does not work after installing python.
-
-
Open the InterSystems IRIS sandbox IDE. If you are completing this exercise on your local machine, open Visual Studio Code, which can be downloaded here.
-
Begin by cloning this repository:
git clone https://github.com/intersystems/multi-model-exercise
. -
Open the connections.config file in the top-level directory. Enter the InterSystems IP and Port listed for your InterSystems IRIS instance and click Save. If you are using the InterSystems IRIS sandbox instance, update the IP and Port fields to match the ‘Server IP Address’ and 'Server Port' fields found in your sandbox. If you are using the InterSystems InterSystems IRIS community edition through Docker, you will need to follow a few extra steps:
- Install Docker
- Run
docker run --name my-iris2 -d -p 52773:52773 -p 51773:51773 store/intersystems/iris-community:2020.1.0.215.0
- Navigate to
http://localhost:52773/csp/sys/%25CSP.Portal.Home.zen
and update your password. If necessary, replace 'localhost' with your computer's IP address - Change your password in the connections.config file to the one you chose. Change the port value to
51773
and change the IP to 'localhost' or your computer's IP address.
-
Install and configure Python
- Run
cd ./python
- On the sandbox:
- Run
odbcinst -i -d -f pyodbc_wheel/linux/odbcinst.ini
- Run
- On macOS:
- Install homebrew
- Run
brew install unixodbc
- Run
odbcinst -i -d -f pyodbc_wheel/mac/odbcinst.ini
- Run
pip install pip==7.1.2
- Run
pip install --upgrade --global-option=build_ext --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib" --allow-external pyodbc --allow-unverified pyodbc pyodbc
- On Windows:
- Run
./pyodbc_wheel/ODBC-2019.1.0.510.0-win_x64.exe
- Run
pip install pyodbc
- If the
pip
command is not recognized, you can also usepy -m pip install
for anypip
installation command.
- If the
- Run
- Run
-
In the IDE provided with your sandbox or Visual Studio Code, open
python/createSchema.py
and scroll down to thecreate_employee
function. Below the function declaration, uncomment the following code:create_employee = """ CREATE TABLE Demo.Employee( ID Integer PRIMARY KEY AUTO_INCREMENT, Name varchar(256), Title varchar(256), Department varchar(50) ) """
As you can see, this is a standard SQL create statement that will generate an Employee table on your InterSystems IRIS instance.
Note: if you are using python 2 or earlier, follow the commented instructions in
createschema.py
in theconnect_to_iris()
function to configure the connection properly. -
Run
python createSchema.py
. If successful, the terminal will outputCreated table Demo.Employee successfully
.- Note: This exercise is configured for Python 3. For some users, you may need to run
python3 createSchema.py
if thepython
command defaults to Python 2.
- Note: This exercise is configured for Python 3. For some users, you may need to run
-
Confirm that the
Demo.Employee
table has been created.- Open the Management Portal by following the link provided when you created your instance of the InterSystems IRIS sandbox. If you are using the Docker container, go to http://localhost:52773/csp/sys/%25CSP.Portal.Home.zen.
- Navigate to System Explorer > SQL and expand the Tables section. Find
Demo.Employee
in the list.
-
Install InterSystems Visual Studio Code, if you have not already done so.
-
Open Visual Studio code. In the Extensions Manager, search for and install the
InterSystems ObjectScript
extension. -
Select a folder for your workspace by selecting Open Folder in the File Explorer, or File > Open. Select or create a folder on your local machine to store settings specific to your workspace.
-
Open your Visual Studio Code settings (File (or Code if on macOS) > Preferences > Settings > InterSystems ObjectScript and select Edit in settings.json. Paste the following JSON, filling in the settings for your InterSystems IRIS instance connection:
{ "objectscript.conn": { "active": true, "label": "LOCAL", "host": "<ip for your instance>", // If using Sandbox, use the Atelier Server Address provided. "port": 80, // If using Sandbox, the Atelier Server Port provided. "username": "tech", "password": "demo", "ns": "USER", // this is the namespace you wish to connect to "https": false } }
-
Navigate to the ObjectScript extension in your Visual Studio Code, right click
Classes/Demo/Employee.cls
and select Export. -
Navigate to the File Explorer and open the newly created
src/Demo/Employee.cls
file.
- At the top of the Demo.Employee class, change
Extends %Persistent
toExtends (%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.
-
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
. -
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 and Parameter declarations in theDemo.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
}
- Make sure to recompile the
Demo.Employee
class by saving it. You have now configured your SQL table class to receive JSON data and automatically create a new record from it.
Note: The completed ObjectScript Employee
class is included in this repository for your reference at ObjectScript/Demo.Employee.cls
.
-
If you do not have Node.js installed locally, download and install it here. If you are using the InterSystems IRIS sandbox, you can skip this step.
- Note: once Node.js is installed, you may need to restart your terminal in order for it to recognize
node
commands.
- Note: once Node.js is installed, you may need to restart your terminal in order for it to recognize
-
Run
cd ../nodeApp
-
Create a new file called
record.json
containing the following JSON object:{ "Name": "JJ Smith", "Title": "Software Engineer", "Department": "Engineering" }
-
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. -
Open the
app.js
file. Navigate toconst Iris = connection.createIris()
and uncomment the following lines: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
-
In the terminal, type
node app.js
. The node application will output that it has created a new record.
-
cd
back into the Python directory (cd ../python
) -
Run
python query.py
. You should see the results of the SQL query, which includes the record of JJ Smith that you inserted using Node.js.
Problem | Likely Solution |
---|---|
I get a 'Data source name not found' error when I run python createSchema.py |
You may have the 32-bit version of Python installed on your computer instead of the 64-bit. |
My node.js app quits unexpectedly when I click Submit. | Make sure that you click Save in Visual Studio Code and that the class compiled successfully. |
I'm on a Windows and the python command is not recognized. |
Be sure to add python to your environment variables. |
-
Visit GettingStarted.InterSystems.com to learn more about InterSystems IRIS data platform.
-
The Multi-Model QuickStart provides a quick introduction to how to use the multi-model capabilities of InterSystems IRIS in the language of your choosing.
-
Try the (Globals QuickStart)[https://gettingstarted.intersystems.com/multimodel-overview/globals-quickstart/] to learn about the proprietary data structure that comes with InterSystems IRIS data platform.