Building REST API for Google Firestore using App Engine

Reading database configuration parameters stored in Google Firestore

Soumendra Mishra
Google Cloud - Community

--

Configuration parameters are settings, that allows an application to determine essential values to function. Determination for configuration store and its management depends on various factors including the environment, deployment methodologies and type of configuration elements. In large scale distributed environment, database is the way to go unless an automated config file management system in place.

Following factors influence the decision to use Firestore as configuration store:
■ A scalable and serverless document database
■ Direct connectivity to web and mobile applications
■ Data protection with enriched security and data validation rules
■ Unified integration with Google Cloud services

Data Flow Diagram

The data flow diagram depicts configuration parameters setup pertaining to multiple databases. The Rest-API call use the hierarchy to read specific database information and return as JSON.

Configuration Store for Multiple Databases

Source Code

In this blog, Google App Engine is used for developing and hosting web applications in Google-managed platform. App Engine services can be written in different programming languages and in this solution, node.js is used as a code-base.

app.js: This file contains the JavaScript code to start a server and respond to requests. The following code responds to requests from web clients via a server that runs on port 8080.

'use strict';require('dotenv').config();
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const app = express();
const fsReadController = require('./controllers/fsReadController');app.use(bodyParser.json());
app.use(cors());
app.get('/firestore/:collection/:database/:db', (req, res) => {
fsReadController.index(req, res);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
module.exports = app;

fsReadController.js : This code is used to read the input parameter as request and retrieves data stored in Cloud Firestore. The response from the API call is in JSON string format.

'use strict';const { Firestore } = require("@google-cloud/firestore");
const firestore = new Firestore();
async function getFireStoreDocument(docPath) {
const document = firestore.doc(docPath);
let doc = await document.get();
return doc;
}
exports.index = async function (req, res) {
const collection = req.params.collection;
const database = req.params.database;
const db = req.params.db;
const docPath = collection + "/" + database + "/dbname/" + db;
try {
const doc = await getFireStoreDocument(docPath);
const docData = JSON.stringify(doc.data(), null, 4);
res.status(200).send(docData);
}
catch (err) {
console.error(err);
res.status(500).send(err.message);
}
};

Deploying the Application to App Engine

  1. Download source code from GitHub repository
  2. Change to application folder [$ cd cloud-firestore-api]
  3. Deploy application [$ gcloud app deploy]

Test Result

Request: (replace the “targeturl” with actual URL)

curl --request GET 'https://targeturl/firestore/dbconfig/oracle/db1'

Response:

{
"SID": "orcl",
"Password": "password",
"HostName": "db1.example.com",
"UserName": "dbadmin",
"Schema": "dbadmin",
"PORT": "1531",
"Version": "12.2.0.3"
}

Source Code Repository

https://github.com/soumendra-mishra/cloud-firestore-api.git

--

--

Soumendra Mishra
Google Cloud - Community

Passionate Leader, Technology Enthusiast, Innovator, and Mentor