CLOUD COMPUTING WITH FAUNA CLOUDWARE
Published: 2021-08-29 17:58:00

Author;- Marvel ken.
## Introduction
Fauna is the data API for client-serverless applications. With support for custom business logic and integration with the serverless ecosystem, it enables developers to simplify code and ship faster. Fauna's database is a general-purpose distributed database that supports multiple data models and strong global consistency and supports multiple programming languages. Most importantly, it functions with a pay as you grow feature.
Fauna transforms the traditional DBMS into a Data API that gives you all of the capabilities of an old-guard database, without sacrificing flexibility, scale, and performance. 
Fauna's core functions are inspired by [Calvin a clock-less, strictly-serializable transactional protocol for multi-region environments]( http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf). 
It is easy to use as a document database and you can manage your data from the web interface having a very mature user-friendly UI or the command line.
It's extremely fast and scales infinitely in the cloud. Its greatest flex is its ability to handle complex data modeling use cases.
 Objective - Introduce Fauna cloudware computing to both beginners and experts, exploring its uniqueness among cloud computing services.
## GETTING STARTED
First off, we need an account with Fauna. Sign up at: https://dashboard.fauna.com. 
The UI is pretty friendly, and It also offers tutorials on getting familiar with the Fauna environment. To get this feature click on the question mark (?) button at the top right corner of the site.  
                                                                                                                                                                     
## CREATING FAUNA CLOUDWARE REPOSITORY
Let’s create a database:
First we click on NEW DATABASE
Then enter your database name into the database name field
Check the “pre-populate with data” checkbox
Then click save.

## BROWSING YOUR DATA
             The overview page for your database is displayed after creating your database. If you checked the pre-populated demo checkbox, the database would be populated with some data.
 Then on your left-hand side of your window, you should see a dashboard like menu, containing info about your database and these menu options are: 
- Collections
- Indexes
- Functions
- Shell
- Graphql
- Security

## Collections
Collections are Fauna's version of SQL tables; they are like a way for our database to differentiate different data.
To create a new collection;-
Click on “New Collection” to create a new collection, then click on  “Collection” on the menu by the left to see the documents for each collection selected.

       
## Indexes 
         Indexes help us to browse the data in our collections. They are basically like a tag to a document in the collection. This helps Fauna avoid performing full scans of collections, thereby not affecting performance.
It is similar to SQL foreign keys.
Click on the Index tab to create a new index

## Functions
These are inbuilt functions provided by the Fauna Query Language (FQL). They can be used to query and modify a database
Note FQL is a functional language 
Some functions are 
- Paginate
- Get
- Select
- Match
- Index
- Create
- Collection
- Lambda
- Var
- Join
  
## Shell
This command line (CLI) lets us execute Fauna queries interactively and can be used both on the web or on your local machine when installed.

## GraphQL And Security
This section covers GraphQL, an open source data query and manipulation language. And also Fauna security is designed to make it easy to query one's database from any network connected context.              

## UPLOADING DATA
We can add data to our created collection in our database. We add what’s called a document. (Note TTL)
 This document is represented as a plain javascript object. The data saved does not necessarily follow a rigid structure.
  To do this, we first off: 
Go to our collection and click on new document button
Then we would be presented with an Ide kinda UI with an empty object. Fill in your data following the key-value system in javascript. 
  Below is what our object would look like after filling it with data
```

{
          { 
            name : “Tony”,
            email : Tony@gmail.com
           }
     }

```
      
Then click save the document is then saved in the collection in the format below
```

{
       {
         “ref” : Ref(collection(collection_name), “279291161097536c12”),
            “ts” : 1602612179196000,
              “data” : {
                                “name” : “Tony”,
                                  “email”: “Tony@gmail.com”                                
                               }
       }
    }

```
    Where, in addition to our custom data, we have Ref (Unique reference Id) is used to join data to a unique value in the collections
     The ref function is used to express the reference for a specific document that exists in the current database collection. It is like an id, a unique one.
References serve the same purpose as primary keys in the database systems, and they are used to provide pointers to a specific document.
     The function takes in two arguments, and the first is the collection name and the second is the id of that document, as seen below:
```

{
  Ref(collection_name, document_id)
}.

```

 The “ts” field is a Long or unique number with microsecond resolution, generated by Fauna everytime we save to the database.  It represents the most recent event that modified the document.
 In Fauna, anytime a modification is made in our document, Fauna stores a new copy of the updated document, meaning we can still ask for previous instances and get a snapshot of the database before and after particular transactions were processed.
Now we can also upload data from our local machine. I would be using Node.js and Express to connect to Fauna.
We set up our node project and initialize it, and if you do not node installed to download and install node following this guide (a link to a guide on downloading and installing node)
Then we need an API secret Key which we would be using in our codes. We can get this back at our Fauna account with the website. All you need to do is go to settings, developers, and request your API key then copy it.
Now we install necessary packages, we install Fauna, and express 
Now we  create an src directory and in there create an index.js file 
Now we require express and Fauna and initialize the Fauna Client 
  
```
 {
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key})
   } 
```
The client would connect our source code to the actual database in the cloud using the API secret key we provided
Now we start off our express server

```
{
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key});


app.listen(5000, () => console.log(“API on https//localhost:5000”));
   }

```

Then we import FQL functions from the  faunadb query namespace like so

```
{
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key});
    
    //we import fql functions
    //there are more functions out there we imported only the ones we would be using
     const { Get, Create, Select} = fauna.query;

     app.listen(5000, () => console.log(“API on https//localhost:5000”));
   }  

```

Now let’s create a post endpoint to send data to our database, 
We set up our async function, then we use the create function, then point to the collection we want, and then pass whatever data we want to pass 

```
{
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key});
    
    //we import fql functions
    //there are more functions out there we imported only the ones we would be using
     const { Get, Create, Select} = fauna.query;
   

    //our post request
   app.post(“/user”, async function(req, res) {
   let data = {
     name : “Tony”,
     email : Tony@gmail.com
      }
   Const doc = await Client.query(
   Create(
      Collection(“collection_name”),
     {data}
    )
    )
 res.send(doc);
 })
     app.listen(5000, () => console.log(“API on https//localhost:5000”));
   }
```

Then, let’s use postman to send a post request to that endpoint 
We will return data that looks like this

```
{
“ref” :  (collection(collection_name), id),
“ts” : 22345432125656,
 “data”: {
  “name”:  “Tony”,
   “email”:  “Tony@gmail.com”
     
   } 
```
 
## RETRIEVING DATA 
      To retrieve data, we would need to setup our API endpoint
  Here this endpoint would get a single user
First off we set up our async function and the reason for this is because any query we make to fauna would return a promise
Now we need to set up a GET endpoint to get a user with the Id in the url

```
{
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key});
    
    //we import fql functions
    //there are more functions out there we imported only the ones we would be using
     const { Get, Create, Select} = fauna.query;
   

    //our post request
   app.post(“/user”, async function(req, res) {
   let data = {
     name : “Tony”,
     email : Tony@gmail.com
      }
   Const doc = await Client.query(
   Create(
      Collection(“collection_name”),
     {data}
    )
    )
 res.send(doc);
 })

//our get request
app,get(“/user/:id”), async function(req, res) {
 const doc = await client.query(
Get(
Ref(Collection(“users”),
 req.params.id
)
).catch(e => res.send(e));
Res.send(doc);
)}; 
     app.listen(5000, () => console.log(“API on https//localhost:5000”));
   }
```
We use the Get function we imported to query the database
In the “ref” function, we would pass in the Id of the document we want to retrieve and we did this by passing it to the url and getting the value using req.params.id
Then we make a get request to Localhost:5000/user/copied_id
If everything goes well we should get a document with Tony’s name and Email
 

## MAINTENANCE
 Fauna is deployed and scaled as a collection of nodes, each of which operate within a cluster in an autonomous fashion.  
Note There are no additional pieces of management software, such as a dedicated cluster manager to deploy.  
Fauna provides a database administrator with first class commands to manipulate the cluster.  These commands are designed to establish the FOS methodology, reducing operational complexity with fauna. 
Fauna is fully online This means that Fauna has the ability to be fully operational while the command is executing. Most databases employ the rolling window operational scheme, which makes portions of the system unavailable for a duration while this is taking place.
This limits the database capacity and availability of the command. Fauna ensures that your database is online and available before, during and after the execution of a command
## CONCLUSION 
 Fauna modernizes database operations by making them simpler, efficient, low-cost, and easy to manage.