This document explains the steps in configuring the PostgreSQL database in the cloud application programming model based on the CAP Documentation- Using PostgreSQL.
Initialise the cap project using the command cds init in the root directory of the project folder.
Execute the command npm install to install the dependencies.
You can now start the cap server using the command cds watch. This will start the cap server on http://localhost:4004.
The service would now be empty without any entities defined.
To add the entity definition, create a file called schema.cds in the db folder and define the following entity type.
namespace com.demo.timesheet; using {cuid} from '@sap/cds/common'; entity Projects : cuid { project : String(50); projectDesc : String(100); customer : String(20); start_date : Date; end_date : Date; }
Create a service file to expose the entity. To create a service file, create a file named schema.cds in the folder srv and implement the following code to expose the entity.
using {com.demo.timesheet as timesheet} from '../db/schema'; service Projects { entity Projects as projection on timesheet.Projects; annotate Projects with @odata.draft.enabled; }
Pre-requisite: Download and install the Docker Desktop on your machine.
Import the image for PostgreSQL database and supporting tool Adminer to access the database. To add the image, create a file named docker-compose.yaml and add the following services.
version: '3.1' services: db: image: postgres:alpine restart: always environment: POSTGRES_PASSWORD: 'postgres' ports: - '5432:5432' adminer: image: adminer restart: always ports: - 8080:8080
In the above yaml definition, we are adding import for two services .
POSTGRES_USER : '<Username>' POSTGRES_PASSWORD: '<Password>' POSTGRES_DATABASE: '<Database>'
To start the container, in the terminal navigate to the root folder of the application, where the docker-compose.yaml file is created.
Run the command docker compose up to start the docker container. Alternatively you can also run the command docker-compose -f pg.yml up -d
-f : Provide the file name containing the docker services
-d: Optional, run the container in the background and detached mode.
Open the docker desktop and you will able to see two new images created for the database postgres and adminer in the images section.
In the container section, you will be able to see the running container for the project.
Click on the hyperlink in the adminer instance to open the local host on port 8080.
In the adminer login page, enter the username as postgres and password as postgres to login to the database.
After login, you must now be able to see the databases available inside the container.
This step of creating the new database can be done at the time of instantiating the container itself by specifiying the environment variable POSTGRES_DATABASE in the docker-compose.yaml. In this exercise, we will create a database from the adminer.
Click on create database, and enter the name for database.
To add postgres to the Cap application, execute the command
cds add postgres
This step will add the plugin @cap-js/postgres to the dependencies.
For more information, refer to the link below:
https://cap.cloud.sap/docs/guides/databases-postgres
Note: In my project, the version of @sap/cds was at ^6.8.4. The @cap-js/postgres require the dependency of the @sap/cds to be ^7.x.x. I have upgraded the package to install the postgres db.
Execute the command npm install to install the dependent package @cap-js/postgres if it is not already installed to the node_modules.
Package.json
{ "name": "cap-pg", "version": "1.0.0", "description": "A simple CAP project.", "repository": "<Add your repository here>", "license": "UNLICENSED", "private": true, "dependencies": { "@sap/cds": "^7.9.0", "express": "^4", "latest": "^0.2.0", "@cap-js/postgres": "^1" }, "devDependencies": { "sqlite3": "^5.0.4" }, "scripts": { "start": "cds run" } }
In the package.json or .cdsrc.json add the required configuration to connect to the PostgreSQL database.
{ "requires": { "db": { "kind": "postgres", "impl": "@cap-js/postgres", "credentials": { "host": "localhost", "port": 5432, "user": "postgres", "password": "postgres", "database": "Projects" } } } }
To deploy the tables the postgres database, execute the command cds build and then cds deploy.
In the adminer, you can see the tables created in the database.
Execute the following commands to build and deploy the application.
Set the endpoint where you want to deploy the service.
cf api <endpoint>
Login and authenticate.
cf login / cf login --sso
Add xsuaa to the application.
cds add xsuaa
This step will create a file called xs-secuirty.json. Here you can define the roles and scopse needed for the application.
{ "scopes": [], "attributes": [], "role-templates": [] }
Add the mta configuration file by executing the command cds add mta. This will create a file called mta.yaml and add the required configuration.
_schema-version: '3.1' ID: cap-pg version: 1.0.0 description: "A simple CAP project." parameters: enable-parallel-deployments: true build-parameters: before-all: - builder: custom commands: - npm ci - npx cds build --production modules: - name: cap-pg-srv type: nodejs path: gen/srv parameters: buildpack: nodejs_buildpack readiness-health-check-type: http readiness-health-check-http-endpoint: /health build-parameters: builder: npm provides: - name: srv-api # required by consumers of CAP services (e.g. approuter) properties: srv-url: ${default-url} requires: - name: cap-pg-postgres - name: cap-pg-auth - name: cap-pg-postgres-deployer type: nodejs path: gen/pg parameters: buildpack: nodejs_buildpack no-route: true no-start: true tasks: - name: deploy-to-postgresql command: npm start requires: - name: cap-pg-postgres resources: - name: cap-pg-postgres type: org.cloudfoundry.managed-service parameters: service: postgresql-db service-plan: trial - name: cap-pg-auth type: org.cloudfoundry.managed-service parameters: service: xsuaa service-plan: application path: ./xs-security.json config: xsappname: cap-pg-${org}-${space} tenant-mode: dedicated
Build the tar file by executing command mbt build.
Deploy the application by executing the command cf deploy mta_archives/<name of build>.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
5 | |
3 | |
3 | |
3 |