This is an application which uses Node.js to connect to IBM Db2 Warehouse on Cloud for CRUD(create, update, delete) operations. The application showcases how you can create a high performant CRUD application using Node.js and IBM Db2 Warehouse on Cloud.
git clone https://github.com/IBM/crud-using-nodejs-and-db2.git
Once we have cloned our repository, the next thing we have to do is create our database that will hold our house sales data. There are two ways we can create our database. One way is creating IBM Db2 Warehouse on Cloud. This database will be hosted on the cloud. However, if you prefer to have your database on premise or locally, we can also use the Db2 Docker Image.
Choose which type of database you would like and follow the corresponding instructions:
Create the Db2 Warehouse on Cloud service and make sure to note the credentials using the following link:
Instead of creating the Db2 Warehouse on Cloud service, we can also have our database instantiated locally by using the free IBM Db2 Docker Image.
Prerequisite:
Steps to get your db2 running locally:
db2
db2
is locateddocker pull ibmcom/db2
docker run -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=hackathon -e DBNAME= homesalesdb -v db2:/database ibmcom/db2
docker exec -ti mydb2 bash -c "su - db2inst1"
Once this is done, it will create a db2 docker container with the follow customizations:
localhost
50000
homesalesdb
db2inst1
hackathon
Now that we have created our databases, we need to import the data from the csv file into our database. We will be creating a schema called DB2WML
. The two tables we will create are HOME_SALES
and HOME_ADDRESS
. HOME_SALES
will store the data we retrieve from our csv file. HOME_ADDRESS
is going to be the addresses associated with each home.
Depending on which type you have (Cloud or On-Premise), the steps will be a little different. Please follow the corresponding steps:
In the Db2 warehouse resource page, click on Manage
and go to DB2 console by clicking the button Open Console
. In the console do the following to load your data.
Load
from the hamburger menu.Browse files
or you can drag files, select the data/home-sales-training-data.csv and click Next
DB2WML
by clicking + New Schema
HOME_SALES
by clicking + New Table
on the schema you created and click Next
Next
Begin Load
to load the dataWe also need to create a table for HOME_ADDRESS
, which will store the addresses of each house data. We won't be able to use the same instructions we used for HOME_SALES
since we have no data to load.
Run SQL
from the hamburger menu.Blank
, which will open a blank sql editorCREATE TABLE DB2WML.HOME_ADDRESS (ADDRESS1 VARCHAR(50), ADDRESS2 VARCHAR(50), CITY VARCHAR(50), STATE VARCHAR(5), ZIPCODE INTEGER, COUNTRY VARCHAR(50), HOME_ID INTEGER)
Once this is done it will create a table HOME_SALES
and HOME_ADDRESS
under schema DB2WML
which will be used by the Node.js application.
Exit out of the container shell by CONTROL-C. Load the sample data into the onprem Db2 database:
docker cp data/home-sales-training-data.csv mydb2:home-sales-training-data.csv
Run the container and enter into the container shell:
docker exec -ti mydb2 bash -c "su - db2inst1"
Steps To Create Schema and Tables:
homesalesdb
NOTE: This command may not work for sometime, since the container takes some time to create the database. If this command doesn work, please wait a couple of minutes and then try again.db2 connect to homesalesdb
DB2WML
db2 'CREATE SCHEMA DB2WML'
HOME_SALES
and HOME_ADDRESS
within Schema DB2WML
db2 'CREATE TABLE DB2WML.HOME_SALES (ID SMALLINT, LOTAREA INTEGER, BLDGTYPE VARCHAR(6),HOUSESTYLE VARCHAR(6), OVERALLCOND INTEGER, YEARBUILT INTEGER, ROOFSTYLE VARCHAR(7), EXTERCOND VARCHAR(2), FOUNDATION VARCHAR(6), BSMTCOND VARCHAR(2), HEATING VARCHAR(4), HEATINGQC VARCHAR(2),CENTRALAIR VARCHAR(1), ELECTRICAL VARCHAR(5), FULLBATH INTEGER, HALFBATH INTEGER, BEDROOMABVGR INTEGER, KITCHENABVGR VARCHAR(2), KITCHENQUAL VARCHAR(2), TOTRMSABVGRD INTEGER, FIREPLACES INTEGER, FIREPLACEQU VARCHAR(2), GARAGETYPE VARCHAR(7), GARAGEFINISH VARCHAR(3), GARAGECARS INTEGER, GARAGECOND VARCHAR(2), POOLAREA INTEGER, POOLQC VARCHAR(2), FENCE VARCHAR(6), MOSOLD INTEGER, YRSOLD INTEGER, SALEPRICE INTEGER )'
db2 'CREATE TABLE DB2WML.HOME_ADDRESS (ADDRESS1 VARCHAR(50), ADDRESS2 VARCHAR(50), CITY VARCHAR(50), STATE VARCHAR(5), ZIPCODE INTEGER, COUNTRY VARCHAR(50), HOME_ID INTEGER)'
HOME_SALES
db2 'IMPORT FROM ../../../home-sales-training-data.csv OF DEL SKIPCOUNT 1 INSERT INTO DB2WML.HOME_SALES'
Copy the local env.sample
file and rename it .env
:
cp env.sample .env
Update the .env
file with the credentials from your Assistant service.
# Copy this file to .env and replace the credentials with
# your own before starting the app.
DB_DATABASE=<database name>
DB_HOSTNAME=<hostname>
DB_PORT=50000
DB_UID=<username>
DB_PWD=<password>
npm install
yarn install
In a two separate terminals run the following:
ng serve --open
and
node server.js
You can go to the UI by running the following URL in the browser: http://localhost:8888
This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.