This is the sequel of CAP PostgreSql, after your completion of your previous exercise (https://blogs.sap.com/2023/10/10/cap-with-postgresql-db-alternate-to-hana-cloud-db/) of bootstrapping up your CAP PostgresSQL, the next logical step is how to configure perform a local run using cds-watch and cds-serve to persist your data onto postgres db.
You should have the skeletal CAP project setup with postgres db.
Before we start the next exercise, you would need to have docker container install on your machine.
Mac OSX – https://docs.docker.com/desktop/install/mac-install/
Windows – https://docs.docker.com/desktop/install/windows-install/
Download DBeaver https://dbeaver.io/download/
Let the fun begin!!!
Step 1: Create pg.yml file within your project
services:
db:
image: postgres:alpine
environment: { POSTGRES_PASSWORD: postgres }
ports: [ '5432:5432' ]
restart: always
Step 2: In your terminal run the docker command to create the postgres db as an image
docker-compose -f pg.yml up -d
Step 3: Connect your DBeaver to confirm that your postgres db has been created successfully, ensure your postgres db container is active and running within your docker container. Your connectionstring for DBeaver should looks like something below.
Host | localhost |
Database | postgres |
Port | 5432 |
Username | postgres |
Password | postgres |
Step 4: ensure your DBeaver is able to establish connection to your postgres db in docker.
Step 5: create a file .cdsrc.json
{
"requires": {
"db": {
"[pg]": {
"kind": "postgres",
"credentials": {
"host": "localhost", "port": 5432,
"user": "postgres",
"password": "postgres",
"database": "postgres"
}
}
}
}
}
Step 6: (Optional) in package.json you can update the following
"cds": {
"requires": {
"db": {
"[development]": { "kind": "postgres", "impl": "@cap-js/postgres", "credentials": { "url": "db.postgres" } },
"[production]": { "kind": "postgres", "impl": "@cap-js/postgres" }
},
"[production]": {
"auth": "dummy"
}
}
}
Step 7: deploy your entities against to your docker postgres db locally.
cds deploy --profile pg
Step 8: Verify your tables are now visible via DBeaver
Step 9: Create some arbritrary entries into the table, run cds watch –profile pg to evaluate the results
cds watch --profile pg
Step 10: update your launch.json to prepare for debug and create an entry into postgres db docker
{
"version": "0.2.0",
"configurations": [
{
"name": "cds serve --profile pg",
"request": "launch",
"type": "node",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "cds",
"args": [
"serve",
"--with-mocks",
"--in-memory?",
"--profile",
"pg"
],
"skipFiles": [
"<node_internals>/**"
]
}
]
}
Drop me your likes – Please follow me for more for the next download.