docs.intersystems.com
Home  /  Application Development: Language Bindings and Gateways  /  Using Node.js with InterSystems IRIS  /  Integration with InterSystems IRIS Objects


Using Node.js with InterSystems IRIS
Integration with InterSystems IRIS Objects
[Back] 
InterSystems: The power behind what matters   
Search:  


The iris.node module provides methods to allow Node.js applications to integrate directly with InterSystems IRIS™ objects. (instances of classes defined in the InterSystems IRIS class library).
The examples in this chapter are based on the contrived, but simple, InterSystems IRIS %Persistent class listed below:
Example: Class User.customer
Class User.customer Extends %Persistent
{
   Property number As %Integer;

   Property name As %String;

   ClassMethod MyClassMethod(x As %Integer) As %Integer
   {
      Quit x * 2
   }

   Method MyMethod(x As %Integer) As %Integer
   {
      Quit x * 3
   }
}
NoSQL Based Object-Oriented Development Methodology
The examples shown earlier in this book have illustrated the use of the InterSystems IRIS database as a raw NoSQL engine. The code in this chapter illustrates how the iris.node methods together with the JSON notation in JavaScript can be used to create a more object orientated development methodology.
Example:
var irisobj = require('iris');
var user = new irisobj.Iris();
user.open({
path:"/iris20102/mgr",
username: "_SYSTEM",
password: "SYS",
namespace: "USER"});

var customers = new Customers();
var customer = customers.getCustomer(1);

console.log("customer number 1 is " + customer.data);
customers.setCustomer(9, "Tom Smith");
user.close();

function Customers() {
   this.global = "Customer";
   this.subscripts = new Array();

   this.getCustomer = function(id) {
      this.subscripts[0] = id;
      var person = user.get(this);
      return person;
   }

   this.setCustomer = function(id, name) {
      this.subscripts[0] = id;
      this.data = name;
      var person = user.set(this);
      return;
   }
}
Invoke a Class Method: invoke_classmethod()
A method is an executable element defined by a class. InterSystems IRIS supports two types of methods: instance methods (keyword Method) and class methods (keyword ClassMethod). An instance method is invoked from a specific instance of a class and typically performs some action related to that instance. A class method is a method that can be invoked without reference to any instance; these are called static methods in other languages. The term method usually refers to an instance method. The more specific phrase class method is used to refer to class methods.
Synchronous:
   var result = mydata.invoke_classmethod(class_method);
Asynchronous:
   mydata.invoke_classmethod(class_method, function(error, result){});
Example:
   var result = mydata.invoke_classmethod(
      {class: "User.customer",
       method: "MyClassMethod",
       arguments: [7]}
   );
result:
   {
      "ok": 1,
      "class": "User.customer",
      "method": "MyClassMethod",
      "arguments": ["7"],
      "result": "14"
   }
Create a New InterSystems IRIS Object: create_instance()
Synchronous:
   var instance = mydata.create_instance(class);
Asynchronous:
   mydata.create_instance(class, function(error, instance){});
Example:
   var instance = mydata.create_instance({class: "User.customer"});
instance:
   {
      "ok": 1,
      "class": "User.customer",
      "method": "%New",
      "oref": 1
   }
Open an Existing InterSystems IRIS Object: open_instance()
Synchronous:
   var instance = mydata.open_instance(class);
Asynchronous:
   mydata.open_instance(class, function(error, instance){});
Example:
   var instance = mydata.open_instance({class: "User.customer", arguments: [1]});
instance:
   {
      "ok": 1,
      "class": "User.customer",
      "method": "%OpenId",
      "arguments": ["1"],
      "oref": 1
   }
Set the Value for a Property: set_property()
Synchronous:
   var result = mydata.set_property(instance, property_value);
Asynchronous:
   mydata.set_property(instance, property_value, function(error, result){});
Example 1:
   var result = mydata.set_property(
      instance,
      {property: "number",
       value: 100000}
    );
result:
   {
      "ok": 1,
      "oref": 1,
      "property": "number",
      "value": 100000
   }
Example 2:
   var result = mydata.set_property(
      instance,
      {property: "name",
      value: "Chris Munt"}
   );
result:
   {
      "ok": 1,
      "oref": 1,
      "property": "name",
      "value": "Chris Munt"
   }
Get the Value for a Property: get_property()
Synchronous:
   var result = mydata.get_property(instance, property);
Asynchronous:
   mydata.get_property(instance, property, function(error, result){});
Example:
   var get_property = mydata.get_property(instance, {property: "name"});
get_property:
   {
      "ok": 1,
      "oref": 1,
      "property": "name",
      "value": "Chris Munt"
   }
Invoke an Instance Method: invoke_method()
Synchronous:
   result = mydata.invoke_method(instance, method);
Asynchronous:
   mydata.invoke_method(instance, method, function(error, result){});
Example:
   var result = mydata.invoke_method(instance, {method: "MyMethod", arguments: [3]});
result:
   {
      "ok": 1,
      "oref": 1,
      "method": "MyMethod",
      "arguments": ["3"],
      "result": "9"
   }
Save an InterSystems IRIS Object: save_instance()
Synchronous:
   var result = mydata.save_instance(instance);
Asynchronous:
   mydata.save_instance(instance, function(error, result){});
Example:
   var result = mydata.save_instance(instance);
result:
   {
      "ok": 1,
      "oref": 1
   }
Close an InterSystems IRIS Object: close_instance()
Synchronous:
   var result = mydata.close_instance(instance);
Asynchronous:
   mydata.close_instance(instance, function(error, result){});
Example:
   var result = mydata.close_instance(instance);
result:
   {
      "ok": 1,
      "oref": 1
   }
Putting it All Together
Adopting an object oriented approach based on InterSystems IRIS objects.
Example:
var new_customer = new customer();

new_customer.create();
new_customer.number = 100000;
new_customer.name = "Chris Munt";
new_customer.save();
new_customer.close();


function customer() {
   this.class = "User.Customer";
   this.instance = {};
   this.number = 0;
   this.name = "";

   this.create = function() {
      this.instance = user.create_instance({class: this.class});
      return this.instance;
   }

   this.save = function() {
      var set_number = user.set_property(this.instance,
                        {property: "number", value: this.number});
      var set_name = user.set_property(this.instance,
                        {property: "name", value: this.name});
      var save = user.save_instance(this.instance);
      return save;
   }

   this.close = function() {
      var close = user.close_instance(this.instance);
      return close;
   }
}