Skip to main content
Previous section   

Integration with Caché Objects

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
   }
}
Copy code to clipboard

NoSQL Based 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;
   }
}
Copy code to clipboard

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);
Copy code to clipboard

Asynchronous:

   mydata.invoke_classmethod(class_method, function(error, result){});
Copy code to clipboard
Example:
   var result = mydata.invoke_classmethod(
      {class: "User.customer",
       method: "MyClassMethod",
       arguments: [7]}
   );
Copy code to clipboard

result:

   {
      "ok": 1,
      "class": "User.customer",
      "method": "MyClassMethod",
      "arguments": ["7"],
      "result": "14"
   }
Copy code to clipboard

Create a New Caché Object: create_instance()

Synchronous:

   var instance = mydata.create_instance(class);
Copy code to clipboard

Asynchronous:

   mydata.create_instance(class, function(error, instance){});
Copy code to clipboard
Example:
   var instance = mydata.create_instance({class: "User.customer"});
Copy code to clipboard

instance:

   {
      "ok": 1,
      "class": "User.customer",
      "method": "%New",
      "oref": 1
   }
Copy code to clipboard

Open an Existing Caché Object: open_instance()

Synchronous:

   var instance = mydata.open_instance(class);
Copy code to clipboard

Asynchronous:

   mydata.open_instance(class, function(error, instance){});
Copy code to clipboard
Example:
   var instance = mydata.open_instance({class: "User.customer", arguments: [1]});
Copy code to clipboard

instance:

   {
      "ok": 1,
      "class": "User.customer",
      "method": "%OpenId",
      "arguments": ["1"],
      "oref": 1
   }
Copy code to clipboard

Set the Value for a Property: set_property()

Synchronous:

   var result = mydata.set_property(instance, property_value);
Copy code to clipboard

Asynchronous:

   mydata.set_property(instance, property_value, function(error, result){});
Copy code to clipboard
Example 1:
   var result = mydata.set_property(
      instance,
      {property: "number",
       value: 100000}
    );
Copy code to clipboard

result:

   {
      "ok": 1,
      "oref": 1,
      "property": "number",
      "value": 100000
   }
Copy code to clipboard
Example 2:
   var result = mydata.set_property(
      instance,
      {property: "name",
      value: "Chris Munt"}
   );
Copy code to clipboard

result:

   {
      "ok": 1,
      "oref": 1,
      "property": "name",
      "value": "Chris Munt"
   }
Copy code to clipboard

Get the Value for a Property: get_property()

Synchronous:

   var result = mydata.get_property(instance, property);
Copy code to clipboard

Asynchronous:

   mydata.get_property(instance, property, function(error, result){});
Copy code to clipboard
Example:
   var get_property = mydata.get_property(instance, {property: "name"});
Copy code to clipboard

get_property:

   {
      "ok": 1,
      "oref": 1,
      "property": "name",
      "value": "Chris Munt"
   }
Copy code to clipboard

Invoke an Instance Method: invoke_method()

Synchronous:

   result = mydata.invoke_method(instance, method);
Copy code to clipboard

Asynchronous:

   mydata.invoke_method(instance, method, function(error, result){});
Copy code to clipboard
Example:
   var result = mydata.invoke_method(instance, {method: "MyMethod", arguments: [3]});
Copy code to clipboard

result:

   {
      "ok": 1,
      "oref": 1,
      "method": "MyMethod",
      "arguments": ["3"],
      "result": "9"
   }
Copy code to clipboard

Save a Caché Object: save_instance()

Synchronous:

   var result = mydata.save_instance(instance);
Copy code to clipboard

Asynchronous:

   mydata.save_instance(instance, function(error, result){});
Copy code to clipboard
Example:
   var result = mydata.save_instance(instance);
Copy code to clipboard

result:

   {
      "ok": 1,
      "oref": 1
   }
Copy code to clipboard

Close a Caché Object: close_instance()

Synchronous:

   var result = mydata.close_instance(instance);
Copy code to clipboard

Asynchronous:

   mydata.close_instance(instance, function(error, result){});
Copy code to clipboard
Example:
   var result = mydata.close_instance(instance);
Copy code to clipboard

result:

   {
      "ok": 1,
      "oref": 1
   }
Copy code to clipboard

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;
   }
}
Copy code to clipboard