Monday, November 21, 2016

Create Micro CRUD services for Oracle Database Cloud using NodeJS

I will try to explain, how you can use NodeJS to create mirco services for the tables in your Oracle Database Cloud or on-premise Database.

Complete Github project :
You need to do "npm install" to download the node_modules.

Step by Step guide :

1. NodeJS : either 32 or 64 bit.

If you already have NodeJS installed, please check whether it is 64 or 32. Use below command to figure that out :
> require('os').arch()

If you get : 'ia32' , then it is 32 bit installation.

2. Install oracle-db node module

This was a lengthy and time consuming installation for me, because for Windows, it has a lot of pre-requisites. If you are a Mac user, you are lucky. :)
I followed :
There is also a detailed one in github :

3. Config your DB Cloud

Create a user and couple of tables on which we'll write the Microservices.
I created an user with some basic GRANTS


Connected to the DB instance via SQL Developer and created 2 tables :

Now we gonna write some CRUD services on both the tables.

4. Lets explain my NodeJS code

This tutorial from Oracle is excellent for starters :
You should go through it once before you try to understand my NodeJS construction.

JS code:
4i. app.js : Creates a NodeJS server and invokes "./routes/index.js" for REST URI "/" and "./routes/users.js" for URI "/users". All our DB cloud api is in "index.js"
4ii. ./routes/index.js : Defines an API catalogue on "/api/catalogue" and micro services on LEASE_HISTORY and USERS table.
4iii. ./routes/users_crud.js : Defines the CRUD methods for USERS table.
4iv. ./routes/lease_crud.js : Defines the CRUD methods for LEASE_HISTORY table.
4v.  ./routes/dbutil.js : Defines common utility methods for DB operations.

Config files:
4i. manifest.json : Defines the Application Container Cloud NodeJS version and command to start this NodeJS applicaiton. You only need this file if you want to deploy this application to Oracle Application Container Cloud, Otherwise not.
4ii. package.json : Defines application name, command and dependencies.

5. Running this locally:

Run it from command line :

Check in Postman :

6. Upload it in Application Container Cloud :

Follow the next blog on this.