Skip to Content
Technical Articles
Author's profile photo Enric Castella Gonzalez

From 0 to Cloud – start your CAPm project locally terminate SCP

Hi!

This post will be a guide to the different methods to test our APIs based on CAPm.

We will start with a project that we will deploy/check in BAS, then we will deploy the project to our subaccount, finally we will make modifications that we will test in BAS with the subaccount’s database

Prerequisites

We create a new project from the template. The fastest way in BAS is “View” – “Find Command” and we look for the command to create project:

The next step is to create a “CAP Project” type project with the following parameters:

We already have our project!

Phase 1 – Test in BAS

We are going to start our API locally. The first step is to install the project dependencies by running the command:

npm install

Once finished, a folder will appear with all the installed libraries

We have everything ready, we only have to execute the command that will start our service, in case of modifications these are automatically updated on our local server.

cds watch

By accessing the local URL, using the “Open in New Tab” button and we will access our local API with local database

And … the debug?

First we will add a breakpoint, for example in the extension of the service, click on the left side of the line

Then we register the project in the “Run configurations” section

Pressing play activates Debug mode with the same URL as if we use “cds watch”:

When executing the query we can see how it stops at the indicated point. The request will be waiting for us to “release” the Debug

Phase 2 – Deploy to cloud

We make the minimum adaptations to our CAPm to deploy in the HANA database. To do this, we will make two small changes to files.

In the Package.json file we substitute these lines of code:

    "cds": {
        "requires": {
            "db": {
                "kind": "sql"
            }
        }
    }

For these others:

    "cds": {
        "hana": {
            "deploy-format": "hdbtable"
        },
        "requires": {
            "db": {
                "kind": "sql",
                "credentials": {
                    "database": "bookshop.db"
                }
            }
        }
    }

(THIS STEP ONLY IN TRIAL ACCOUNTS) we go to the mta.yaml file and modify the final part as indicated in the comments. That is, we remove service: hana and put service: hanatrial.

   type: com.sap.xs.hdi-container
   parameters:
     service: hana  # or 'hanatrial' on trial landscapes
     service-plan: hdi-shared
   properties:
     hdi-service-name: ${service-name}

Finally we generate the deployment files, for this, we execute the command:

cds build --for hana

We do Build and Deploy:

  • Build: Right-click on the mta.yaml file and select Build MTA.

  • Deploy: Once the build is finished, a folder mta_archives will be generated and in the generated mta file, right click and “Deploy MTA Archive”.

Once the deploy is finished we see our new API. We can now run a query:

Phase 3 ( test in BAS & db in cloud)

The next thing will be to Debug or continue with our development locally but using the cloud database.

The first thing is to execute the command to install the NPM dependencies related to the hana client that will allow us to connect from BAS to the database.

npm add @sap/hana-client --save

Once installed, in the root of our project we will create a new file called “default-env.json”.

In the file we will add the information that we will retrieve from the HDI service from our database, if necessary we will create a new Service Key by pressing the “Create” button and giving it a name

we copy the security information:

We add it to the file “default-env.json” embedded in the following structure where the “…” are the part copied previously and in name we must add the name of the HDI container:

{
  "VCAP_SERVICES": {
    "hanatrial": [],
    "hana": [
      {
        "name": "HDI Name",
        "tags": [
          "hana"
        ],
        "credentials": {
            ...
        }
      }
    ]
  }
}

This is the result:

Finally we will have to make a small change in our package.json file, this change will determine if we use the local database or the cloud. In this example we are going to replace it without further ado, although we can also declare environment variables

we are going to replace it in our package.json file:

    "cds": {
        "hana": {
            "deploy-format": "hdbtable"
        },
        "requires": {
            "db": {
                "kind": "sql",
                "credentials": {
                    "database": "bookshop.db"
                }
            }
        }
    }

For this:

    "cds": {
        "requires": {
        "db": {
            "kind": "hana"
        }
        }
    }

Everything is ready, as before, we can use “cds watch” or “Run Configuration” to start the service. The result when making the request is as follows

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.