HXE – OData Service v4 from NodeJs
Happy 2019 everyone. I hope your year has started on the right path. Mine is starting slow but I feel it will be a good one. I have many goals I would like to accomplish, and this blog is one of them. I am sharing this blog also as I prepare for the SIT Guadalajara – thanks Jhon for your continuous promotion of the event. Also, read the Spanish version of the same blog or another one from my HANA Express edition blog series
Before I get into the details, I would like to also thank Thomas/Rich for the openSAP course on update Q1/2019. I am currently taking the course and when I heard Rich/Thomas about the odata v4 on a node module, it really caught my attention, so I decided to investigate further.
I started by downloading the HANA express edition (https://developers.sap.com/topics/sap-hana-express.html) – on my prev. blog series (https://blogs.sap.com/2018/09/19/my-experience-with-hana2-xe-sp3-series/) , I had H2SP3 rev 31… now, I have rev 35.. yes there are some nice enhancements since rev 31 – thank you thank you. Even for the small exercise I did here, I can tell there are nice improvements behind the scenes as I am able to build modules faster than before. (I still have the same settings on my vm from before – 24GB RAM, 4 Core, etc)
The Business Application module template à this is a new wizard added and very easy to follow. See the steps below to showcase the odata v4 service on a ndoejs module.
Then, provide a name for your project
(select enable uaa service)
Notice there are a few package.json files… at different levels
Svc – package.json à notice the express dependency and this is how the odata service is served using the express module.
Inside the db module, there is the entry point for our cds data models. In my example I am using the default model provide and also added a new entity and a new view. In the opensap course, Thomas shows how to import other cds models using the import statement. This is very helpful as an application grows larger and models cannot be all in the same file.
The db module also has a folder called gen which holds the generated files when the cds models are built (similar to metadata)
Now, if you want to see what’s in the svc module – logically, the entry point to our service from a url point of view (http/https).
The svc module also has a cds file that defines the service itself as shown below.
Things to notice here are:
- The using keyword in case we need to expose our CDS models – from our db module.
- The additional entity created in this service also works as an entity from the database and it follows the same rules and syntax.
Starting from the default configuration, I wanted to run my service and see what all this noise on odata v4 via the nodejs module was all about –
Until I got an error due to the wave-uaa didn’t exist (default name).
So, I went back to the mta.yaml file where these dependencies are created by default and noticed the wave-uaa name listed.
So I opened to the admin cockpit using the default admin account:
xsa_admin à service market place à create instance of service and called it the same name as the service in my mta.yaml
Then I went back to the app as xsa_Dev account
Re-ran the service and then I saw the successful message and clicking on the link opened the service entry point with the definition shown below
If you click on $metadata (navigates to https://hxehost:51045/catalog/$metadata) you may see the odata version – very excited to finally see v4 and the advantages it has over v4 on XS classic.
Similarly, clicking on BookInfo, it will retrieve the data on that end point.
If you click on book info you will see the following
Let’s see how many records are there, (there should be 0) since only structures are created. So let’s go to the DB explorer and add some data
select an hdi container – – ok
and select the Tables from the db container
Select a table and see its definition
Once opening the data view, we can add some records with the plus sign.. and entering data (2) and finally save the data with the save icon.
Then in the catalog service bookinfo
Finally go back to the browser and let’s run some queries.. the first one is to see the number of records coming from the end point
And the raw data
Some other odata query just to test it out… and let me also tell you that the payload is even smaller than odata v2… there seems to be less overhead on the payload.
Now, lets extend our service and let’s add a new entity and view.. then we can rebuild ( * make sure you build CDS from the project to generate the metadata files that get deployed to the gen folder )
Also, notice the association in my entity.
Now let’s populate some sample data… (and introduce some error to see the capabilities from the tool)
*hover over the red message and see the error “illegal date value” since I used month 13 (typo). Then I fixed it and saved it so back in the svc and re-run it.
dang it, I thought the CDS entities and view were going to be shown now, so what went wrong? I went back to the odata video on week 2 and it seems like I needed to list the entity or view with the using keyword and project it on the service as shown here
Re-building and starting the svc module gives me what I expected. Now I can see the table and the alias view.
Looking at it from the browser displays the 2 end points
And if we want to see the data in the other end point (the view output including the association defined on my data model cds file)
This is pretty cool. It seems like on my next writing I will need to see how I can consume these B@d@ss models on an app. In case you want to see the code, check out the github repo here –
Any feedback and other things you would like to see are welcome – please let me know and I hope I can continue sharing some knowledge on the latest CDS / odata v4 and app dev on HANA XSA. Happy dev!