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:
git clone -b try-iris https://github.com/intersystems/multi-model-exercise
.connections.config
file in the top-level directory and update the IP equal to Please launch the sandbox before continuing! and change the value of port to Please launch the sandbox before continuing!. Then click Save.cd ~/multi-model-exercise/python
.sudo odbcinst -i -d -f pyodbc_wheel/odbcinst.ini
.python/createSchema.py
and scroll down to the create_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.
python createSchema.py
. If successful, the terminal will output Created table Demo.Employee successfully
.Demo.Employee
in the list.InterSystems ObjectScript
extension.{ "objectscript.conn": { "active": true, "label": "LOCAL", "host": "Please launch the sandbox before continuing!" "port": Please launch the sandbox before continuing! "username": "tech", "password": "demo", "ns": "USER", // the namespace you wish to connect to "https": false } }
Classes/Demo/Employee.cls
and select Export.src/Demo/Employee.cls
file.Demo.Employee
class, change Extends %Persistent
to Extends (%Persistent, %JSON.Adaptor)
.%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.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
.%JSON.Adaptor
class to the Native API — and, by extension, to our Node.js application. Below the Property
and Parameter
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 }
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.Employee
class is included in this repository for your reference at ObjectScript/Demo.Employee.cls
.cd ~/multi-model-exercise/nodeApp
.{ "Name": "JJ Smith", "Title": "Software Engineer", "Department": "Engineering" }
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.const 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
node app.js
. The node application will output that it has created a new record.Demo.Employee
class.cd
back in the Python directory (cd ~/multi-model-exercise/python
).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.To give you the best possible experience, this site uses cookies and by continuing to use the site you agree that we can save them on your device.