Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
pallab_haldar
Active Participant
763

--------------------------------------------------------------------------------------------------------------
Scenario when use odata and XSJS is preferable
-------------------------------------------------------------------------------------------------------------


When required this two use oData -
1. Select data from the database and Expose as a service.

2. Do n't used to perform CRUD operations (CREATE, READ, UPDATE and DELETE).

When required this two use XSJS -

1.Crud Operation is needed.

2.Sending data to another server, one way may be by providing a web service to the second server.

XSJS can make Web Request to a configured destination so you can POST data to your server.To check for any GET or POST request & request parameters, you can use $.request. With these you can perform your CRUD operations




************** Sample Syntax to Send data to different sever********************************************
var dest = $.net.http.readDestination("<package path to destination file>", "<destination file name>");
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.POST, "/");
req.contentType = "application/json";
req.setBody("My data");
client.request(req, dest);
var response = client.getResponse();

********************************************************************************************************

 

------------------------------------------------------------------------------------------------------------------
3 Scenario to expose Database table as a Service using Pure Node.JS,XSJS and OData
------------------------------------------------------------------------------------------------------------------


1. Pure Node.JS : 

 
-----------------------------------------------------------------------------------------------
Pure Node.JS
-----------------------------------------------------------------------------------------------
var hana = require("@sap/hana-client");

var conn = {
serverNode: "f5b68294-bd42-4e68-a1c9-4bfbf5a2f32d.hana.trial-us10.hanacloud.ondemand.com:443",
encrypt: "true",
sslValidateCertificate: "false",
uid: "DBADMIN",
pwd: "**********",
};

var hanaConnect = hana.createConnection();

hanaConnect.connect(conn, function (err) {
if(err){
console.log(err);
}
else{
hanaConnect.exec("SELECT * FROM EMPLOYEE",
function (err, result) {
if (err) throw err;
console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
hanaConnect.disconnect();
}
);
}
});
--------------------------------------------------------------------------------------------------

2. Expose as XSJS -

 
---------------------------------------------------------------------------------------------------
XSJS Expose
---------------------------------------------------------------------------------------------------
var employee = [];
var stmnt = null;
var rs = null;
var conn = $.db.getConnection();
var stmnt = conn.prepareStatement( "select * from "DB_PLB.OFFICE"::"CV_EMPLOYEE" );
var rs = stmnt.executeQuery();

if (!rs.next()) {
$.response.setBody( "Unable to get result" );
$.response.status = $.net.http.INTERNAL_SERVER_ERROR;
} else {
emp = {};
emp.emp_id = resultSet.getString(1);
emp.emp_name = resultSet.getString(2);
emp.emp_mail_id = resultSet.getString(3);
emp.address = resultSet.getString(4);
employee.push(emp);
}
rs.close();
pstmt.close();
conn.close();

try{
$.response.contentType = "application/json";
$.response.setBody(JSON.stringify(employee));
}
catch(err){
$.response.contentType = "text/plain";
$.response.setBody("Got error : [" + err.message + "]");
$.response.returnCode = 400;
}

 

3. Odata Expose -
--------------------------------------------------------------------------------------------------------
Odata Expose
--------------------------------------------------------------------------------------------------------
Service{
"PLB_PROJECT.DB_PLB::CV_EMPLOYEE " AS "EMPLOYEE" Key ("EMP_ID")
}


 

4.

--------------------------------------------------------------------------------------------------
Web module include XSJS service and odata
--------------------------------------------------------------------------------------------------
1. Add UAA-service to the dependent node.js module for dependency service
2. configuration to the xss-app.json file to change the route to xsjs -
"routes": [{
"source": "(.*)(.xsjs)",
"destination": "demo_dest",
"csrfProtection": false,
"authenticationType": "xsuaa"
}, {
"source": "(.*)(.xsodata)",
"destination": "demo_dest",
"authenticationType": "xsuaa"

}]

3. Change the Servery.js file and comment // anonymous : true to activate.
4. Change the Node version to “10.X” in the package. Json file.
5. Execute the HTML5 app and copy the url and replace the index.html file –

 

Output Url -

http://hcpeur10.hcs.cloud.sap:8000/employee. Xsodata?$format=json

http://hcpeur10.hcs.cloud.sap:8000/employee. Xsodata/$metadata
http://hcpeur10.hcs.cloud.sap:8000/employee. Xsodata?$format=json