Ballerina REST API service with Cosmos DB connector — Part1

How to structure an HTTP Service in Ballerina??

Back in the game…

What is this AZURE COSMOS DB????

Is it possible to use Azure Cosmos DB with Ballerina??

Before start using Ballerina Cosmos Connector….

Navigation panel in Azure
Keys view
import ballerinax/azure_cosmosdb as cosmosdb;
cosmosdb:Configuration config = {
baseUrl: "BASE_URI_VALUE",
primaryKeyOrResourceToken: "TOKEN_VALUE"
};
cosmosdb:ManagementClient managementClient = check new (config);
cosmosdb:DataPlaneClient azureCosmosClient = check new (config);

Database Administrative operations.

1. Create a new database

2. Get or Check for an existing database

3. Delete an existing database

4. Create a new container

5. Get or Check for an existing container

6. List all existing containers

7. Delete an existing container

bal run admin_db.balCompiling source
admin_db.bal
Running executabletime = 2021-04-07 16:11:52,162 level = INFO module = "" message = "Reading database by id"
time = 2021-04-07 16:11:54,233 level = ERROR module = "" message = "Message: {"Errors":["Resource Not Found. Learn more: https:\/\/aka.ms\/cosmosdb-tsg-not-found"]}
time = 2021-04-07 16:11:54,235 level = INFO module = "" message = "Creating database"
time = 2021-04-07 16:11:54,609 level = INFO module = "" message = "{"id":"Medium_sample_db","resourceId":"-ZFPAA==","selfReference":"dbs/-ZFPAA==/","eTag":""0000fa00-0000-0700-0000-606d8c720000""}"
time = 2021-04-07 16:11:54,610 level = INFO module = "" message = "Success!"
time = 2021-04-07 16:11:54,611 level = INFO module = "" message = "{"id":"Medium_sample_db","resourceId":"-ZFPAA==","selfReference":"dbs/-ZFPAA==/","eTag":""0000fa00-0000-0700-0000-606d8c720000""}"
time = 2021-04-07 16:11:54,612 level = INFO module = "" message = "Success!"
time = 2021-04-07 16:11:54,614 level = INFO module = "" message = "Reading container info"
time = 2021-04-07 16:11:54,978 level = ERROR module = "" message = "Message: {"Errors":["Resource Not Found. Learn more: https:\/\/aka.ms\/cosmosdb-tsg-not-found"]}
time = 2021-04-07 16:11:56,116 level = INFO module = "" message = "{"id":"sample_data","indexingPolicy":{"indexingMode":"consistent","automatic":true,"includedPaths":[],"excludedPaths":[{"path":"/"_etag"/?"}]},"partitionKey":{"paths":["/id"],"kind":"Hash","keyVersion":1},"resourceId":"-ZFPAOIRyuE=","selfReference":"dbs/-ZFPAA==/colls/-ZFPAOIRyuE=/","eTag":""0000fc00-0000-0700-0000-606d8c730000""}"
time = 2021-04-07 16:11:56,118 level = INFO module = "" message = "Success!"
Newly created database and container inside the Cosmos DB account.

Undergraduate of University of Moratuwa, Software Engineer at WSO2, LC manager at AIESEC, Baseball player