Building a Serverless API With Dotnet and Cloud Run
Lets create a simple Notes API using Google Cloud Run and Fire store.
We will start with the architecture diagram to get a high-level overview of the application we will build in this blog.
The last blog post explained setting up your local system to gcloud and deploying the first project. So I highly recommend looking at the blog post for more details on the initial setup, but I will try to add more pointers here.
Firestore
Firestore is a document database from Gcloud and it’s really simple to work with and has a lot of great features.
You can achieve a decent scale system free of cost, and if it crosses that, you will be paying less for the same.
Let us enable the Firestore for our Gcloud Project(If you want to understand about project and Setup, do read this Page)
Enabling the Firestore via Console is just searching from the project. You can do this through the command line using.
gcloud enable firestore.googleapis.com
Once enabled, you need a native store as the option.
Once it’s selected, you can create a database in the location of your choice. I use Mumbai as it’s near to me, but that’s more costly than US options.
Authentication
Google cloud uses a different Authentication methodology
You don’t need to provide any authentication if App and DB(Firestore) are running on the Google Cloud Platform
When running the locally, you can create a service account and set the Environment Variable for the same. You can use the following Page to Set it in your system.
https://cloud.google.com/docs/authentication/getting-started#create-service-account-console
CRUD with Firestore
We will run the code local first so add the service account to the environment variable. The next step, install a Nuget package.
Install-Package Google.Cloud.Firestore -Version 2.5.0
NuGet Package is a High-level API in that it abstracts away the API calls to the Firestore.
Firestore is a Document database which means it can store any object and retrieve it by its Id or its property, and it uses the code first model. So, let’s start with a Notes class and design a data model.
Initialize the Database
You can initialize FirestoreDB with a single line of code.
You can get the project ID by running the following command.
gcloud projects list
Troubleshooting: If you are running locally and unable to connect, check if the GOOGLE_APPLICATION_CREDENTIALS are available. If not, set it using the Visual Studio Debug option https://zntoyn.com/changing-the-environment-variable-in-asp-net-core-6-visual-studio-2022
Saving a Note
Firestore has a concept of Collection, which can be similar to Table in SQL, and Documents are the rows with Id as the primary key.
Reading the Note
Documents are retrieved using the Path from the collection to document Id. So let’s get the Notes using the ID.
Firestore API generates the ID field, generally strings in nature. There are multiple ways to query the document, and I am moving it to a different blog post.
Updating a Note
Firestore has two APIs for changing the data.
Patch - Here, you have to only pass the changed value for the API, and it merges.
Put - Here, the entire value gets changed. You can also ask for a merge. using the Set options
I can provide the entire object by reading, or Just creating an anonymous object works too.
Deleting the Notes
You can delete the notes as long as you have the reference to the document.
Now that we have understood how Firestore works. In the next blog post, we will create the API and Repository layer and deploy it to the cloud run.
I have to divide the blog post as it’s already a long read. Thanks for reading.