Using Node.js with Caché
Integration with Caché Objects
[Home] [Back] 
InterSystems: The power behind what matters   
Class Reference   
Search:    

The cache.node module provides methods to allow Node.js applications to integrate directly with Caché objects. (instances of classes defined in the Caché class library).
The examples in this chapter are based on the contrived, but simple, Caché %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
   }
}
Towards an Object Oriented Development Methodology
The examples shown earlier in this book have illustrated the use of the Caché database as a raw NoSQL engine. The code in this chapter illustrates how the cache.node methods together with the JSON notation in JavaScript can be used to create a more object orientated development methodology.
Example:
var cachedb = require('cache');
var user = new cachedb.Cache();
user.open({	path:"/cache20102/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. Caché 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 Caché 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 Caché 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 a Caché 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 a Caché 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 Caché 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;
   }
}