This blog is the second in series of NodeJS REST service blogs. My previous blog (link below) covered the setting up of development environment for Nodejs development for simple static JSON based REST service development. It also covered the usage of Oracle Developer Cloud to Develop, Build and Deploy the Node code on Oracle Application Container Cloud Service.

This blog goes a step further to introduce the usage of Oracle Database Cloud Service (DBCS ) with Nodejs for REST service development. Also shows how Developer Cloud Service could be used for build, deployments for Database Cloud Service.

This blog also shows how the entire application lifecycle can be managed for a Microservice using Developer Cloud for the mentioned technology stack.

 

https://community.oracle.com/community/cloud_computing/platform-as-a-service-paas/oracle-developer-cloud-service/blog/2016/09/07/oracle-developer-cloud-to-build-and-deploy-nodejs-rest-project-on-application-container-cloud

 

Tech Stack Usage

Eclipse: IDE for Nodejs development

Jdeveloper 12.1.3: For Database Development

Expressjs: Nodejs module for REST web service development.

Grunt: Tool for building the Nodejs code archive for deployment.

Oracle Developer Cloud: For managing the application lifecycle of the Project.

Oracle Application Container Cloud: For deployment of the Nodejs web service on container hosted by the cloud.

Oracle Database Cloud Service: As the database for building the REST service.

 

Note: For the purpose of this blog the development environment was setup on Microsoft Windows 7.

 

Database Development

Let us now start with the Database Development first. For this blog we will be developing very simple DDL script which will just focus on getting database table ready for the service consumption.

Steps:

  • Setup the Oracle Database Cloud service for usage and note the SYS user credentials for future usage.
  • As part of the development environment setup, first install a Jdeveloper 12.1.3 on your machine.
  • Create a Database Application in Jdeveloper using the below shown template.
  • Create a sql script file called employee.sql and also generate an Ant build file using the option Empty Build File (Ant) option from the New Gallery.

 

Please refer the screenshots below to see the project structure for the database application. Also attached are the files for the sql script and ant build.xml.

 

2016-11-21 16_41_04-Oracle JDeveloper 12c - EmployeeData.jws _ DatabaseModel.jpr _ D__WorkSpaces_JDE.png2016-11-21 16_50_38-Oracle JDeveloper 12c - EmployeeData.jws _ DatabaseModel.jpr.png

.      

 

We would also need the ojdbc7.jar for the Ant task to run. You can download this jar using the link http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html. Create a “lib” folder in the DatabaseModel project of your workspace and place the downloaded jar there. Also include this jar in the project classpath using the libraries and classpath option for project in Jdeveloper.

Node that the build.xml file should have the DBCS user id as “SYS as SYDBA” if SYS admin user is getting used Below are the screenshots for the sql file and build.xml. Also point to the lib folder where you have placed your ojdbc7.jar. Keep it contextual to make it work on cloud as well.

 

2016-11-21 17_40_56-Oracle JDeveloper 12c - EmployeeData.jws _ DatabaseModel.jpr.png  2016-11-22 11_14_26-C__Users_abhshrof_Desktop_attachments_build.xml - Notepad++.png

   

Now create the Project on Oracle Developer Cloud Service called NodeOracleDB (you can keep a name of your choice). After creation of the project, Create/Initialize two empty repositories as shown in the screenshot below.

DatabaseScripts -   This Git repository will contain the database project code we just created using Jdeveloper.

EmployeeService -  This will contain the Node code we are about to develop for the REST service.

 

2016-11-21 16_55_02-Home _ NODEOracleDB - Oracle Developer Cloud Service.png

For understading the usage Jdeveloper’s Team plugin with Oracle Developer Cloud for Git repository operations please refer to the blog link below.

https://blogs.oracle.com/shay/entry/using_the_oracle_developer_cloud

 

Once the code is uploaded to the DatabaseScripts Git repository, you would have to configure the build as shown in the screen shots below.

Note: in the source control point to the databaseScripts.git repository.

 

2016-11-21 19_33_35-Build_ NODEOracleDB - Oracle Developer Cloud Service.png

 

On executing the build the Ant task will use the ojdbc7.jar to execute the sql statement in the sql file and create the table/insert test data on the DBCS.

Note: Grant permissions to the table and tablespace as per your need. Please visit the Oracle database documentation for help if needed.

 

Nodejs REST service Development

 

In my previous blog on Nodejs REST development, I had covered Eclipse setup for Nodejs, setup of Oracle cloud plugin and usage of the same for Oracle Developer Cloud service. Please refer the link below for the sections: Setting up Nodejs and Oracle Cloud on Eclipse, REST using Expressjs, Files that need to be created and Code Check-in

 

https://community.oracle.com/community/cloud_computing/platform-as-a-service-paas/oracle-developer-cloud-service/blog/2016/09/07/oracle-developer-cloud-to-build-and-deploy-nodejs-rest-project-on-application-container-cloud

Note:  While following the instructions in “Files that need to be created” section, please do not copy the code for service,js and Gruntfile.js as these files need to go through some changes.

 

Apart from the setup done following the blog link given above, we would have to do some more additions and changes to the Nodejs REST service project.

 

After the Nodejs EmployeeService project is created and the changes are done as per the previous blog. Open the command prompt and change to the EmployeeService directory. And then start preparation for adding the oracledb node module.

 

Preparing for the Database consumption in Nodejs

 

To consume Oracle Database in Nodejs, you would need to have a compiled node driver. It will be a node module which will be referenced by your code to perform database operation from the Nodejs code.

I would recommend you to read and follow the document link below, to enable you to download the oracledb node module code from GitHub and then compile it. This document lists down steps for various operating systems where you can compile the code.

https://github.com/oracle/node-oracledb/blob/master/INSTALL.md

 

Advice: Try compiling the node-module with npm directly in your Nodejs REST service project to avoid any issues.

 

Note: You can also follow this blog https://blogs.oracle.com/opal/entry/introducing_node_oracledb_a_node,

to know more about the Oracle Database driver for Nodejs.

 

Below is the list of files that need to be there in the EmployeeService project:

  • dbconfig.js
  • Gruntfile.js
  • service.js
  • package.json
  • manifest.json

 

The README.md is optional, but good to have for documentation sake.

 

In the node_modules folder below are the modules required:

  • express
  • grunt
  • grunt-contrib-compress
  • grunt-hook
  • load-grunt-tasks
  • oracledb

 

  2016-11-22 00_35_42-Node - EmployeeService_dbconfig.js - Eclipse.png

 

 

Files that need to be added new or changed:

Note: Code to all the files with placeholder will be attached to the blog.

 

  • dbconfig.js
  • Gruntfile.js
  • service.js
  • package.json

 

Gruntfile.js

The structure of the Gruntfile remains the same as the one in my previous blog, only thing that changes is the src array. Where instead of giving wildcard to include all the files in the package, we would specifically give the names of the files and folders that we would like to include in the archive. This is being done to ensure that oracledb node module folder does not get included in the archive. Below is the snippet of the changed piece of code.

 

src: ['./dbconfig.js','./Gruntfile.js', './service.js','./manifest.json','./package.json','./node_modules/express/**/*','./node_modules/grunt/**/*','./node_modules/grunt-hook/**/*','./node_modules/grunt-contrib-compress/**/*','./node_modules/load-grunt-tasks/**/*']

 

Please refer the attached file for complete code.

 

dbconfig.js

This file contains the database related configurations and has been created for the maintainability and modularity sake. Although this file is being used mostly for the local testing of the Nodejs code, as on deployment on Application Container Cloud, it will have to access the database using database service binding. I will be covering this in a while.

Please refer the attached file for complete code.

 

package.json

This file would have only an addition of oracledb node module in the dependencies. Below is the configuration which gets added in addition to package.json of EmployeeService project from previous blog(link as given in the starting).

 

"oracledb": "1.11"

Please refer the attached file for complete code.

 

service.js

This is the main service code file. This goes through a radical change, as it no longer uses the static json but now retrieves the data from the database. The code now, executes a query using the oracledb node module(Oracle Database Driver) and then processes it to construct a json object which is then send as a response to the service calls. I have used a select query with metadata based dynamic result set processing.

The database access code for local, which access the dbconfig while on Application Container Cloud it uses the service binding is:

 

2016-11-22 10_27_53-Node - EmployeeService_service.js - Eclipse.png

Similarly for the listening port we could use the below snippet for the ease of local testing:

           process.env.PORT || 8089

 

Please refer the attached file for complete code.

 

Build Configuration

It very much remains the same as the previous blog. Please refer the section “Configuring the Build Process” for details.

https://community.oracle.com/community/cloud_computing/platform-as-a-service-paas/oracle-developer-cloud-service/blog/2016/09/07/oracle-developer-cloud-to-build-and-deploy-nodejs-rest-project-on-application-container-cloud

 

Deployment Configuration

It very much remains the same as the previous blog. Please refer the section “Deployment of the Archive on Oracle Application Container Cloud Service” for details, using the link given above.

Post completion of all the configurations, execute the build. If you have configured post build deployment (as shown in the previous blog), then the build process itself would deploy and start the application on the Application Container cloud.

 

Database Service Binding

This is a configuration for database access for the Nodejs code deployed on Application Container Cloud. Ensure that you provide the schema user and password for use. This configuration needs to be done from the Application Container Cloud service console. This configuration can be done post the application deployment is completed by navigating to the application detail. Please refer the screenshot below:

 

2016-11-22 10_40_24-.png

 

You will be able to access the service consuming database once the application starts.

2016-11-22 10_59_44-https___employeeservice-paas124.apaas.em2.oraclecloud.com_getUsers.png

  

**The views expressed in this post are my own and do not necessarily reflect the views of Oracle.