<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: p-ken1</title>
    <description>The latest articles on DEV Community by p-ken1 (@pken1).</description>
    <link>https://dev.to/pken1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F695586%2F934b7f20-607c-4d2c-8652-37a7b20ffe11.jpg</url>
      <title>DEV Community: p-ken1</title>
      <link>https://dev.to/pken1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pken1"/>
    <language>en</language>
    <item>
      <title>CLOUD COMPUTING WITH FAUNA CLOUDWARE</title>
      <dc:creator>p-ken1</dc:creator>
      <pubDate>Sun, 29 Aug 2021 17:58:00 +0000</pubDate>
      <link>https://dev.to/pken1/cloud-computing-with-faunadb-cloudware-fbk</link>
      <guid>https://dev.to/pken1/cloud-computing-with-faunadb-cloudware-fbk</guid>
      <description>&lt;p&gt;Author;- Marvel ken.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;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.&lt;br&gt;
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. &lt;br&gt;
Fauna's core functions are inspired by &lt;a href="http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf"&gt;Calvin a clock-less, strictly-serializable transactional protocol for multi-region environments&lt;/a&gt;. &lt;br&gt;
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.&lt;br&gt;
It's extremely fast and scales infinitely in the cloud. Its greatest flex is its ability to handle complex data modeling use cases.&lt;br&gt;
 Objective - Introduce Fauna cloudware computing to both beginners and experts, exploring its uniqueness among cloud computing services.&lt;/p&gt;
&lt;h2&gt;
  
  
  GETTING STARTED
&lt;/h2&gt;

&lt;p&gt;First off, we need an account with Fauna. Sign up at: &lt;a href="https://dashboard.fauna.com"&gt;https://dashboard.fauna.com&lt;/a&gt;. &lt;br&gt;
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.  &lt;/p&gt;
&lt;h2&gt;
  
  
  CREATING FAUNA CLOUDWARE REPOSITORY
&lt;/h2&gt;

&lt;p&gt;Let’s create a database:&lt;br&gt;
First we click on NEW DATABASE&lt;br&gt;
Then enter your database name into the database name field&lt;br&gt;
Check the “pre-populate with data” checkbox&lt;br&gt;
Then click save.&lt;/p&gt;
&lt;h2&gt;
  
  
  BROWSING YOUR DATA
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;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: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collections&lt;/li&gt;
&lt;li&gt;Indexes&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Shell&lt;/li&gt;
&lt;li&gt;Graphql&lt;/li&gt;
&lt;li&gt;Security&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Collections
&lt;/h2&gt;

&lt;p&gt;Collections are Fauna's version of SQL tables; they are like a way for our database to differentiate different data.&lt;br&gt;
To create a new collection;-&lt;br&gt;
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.&lt;/p&gt;
&lt;h2&gt;
  
  
  Indexes
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It is similar to SQL foreign keys.&lt;br&gt;
Click on the Index tab to create a new index&lt;/p&gt;
&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;p&gt;These are inbuilt functions provided by the Fauna Query Language (FQL). They can be used to query and modify a database&lt;br&gt;
Note FQL is a functional language &lt;br&gt;
Some functions are &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Paginate&lt;/li&gt;
&lt;li&gt;Get&lt;/li&gt;
&lt;li&gt;Select&lt;/li&gt;
&lt;li&gt;Match&lt;/li&gt;
&lt;li&gt;Index&lt;/li&gt;
&lt;li&gt;Create&lt;/li&gt;
&lt;li&gt;Collection&lt;/li&gt;
&lt;li&gt;Lambda&lt;/li&gt;
&lt;li&gt;Var&lt;/li&gt;
&lt;li&gt;Join&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Shell
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;
  
  
  GraphQL And Security
&lt;/h2&gt;

&lt;p&gt;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.              &lt;/p&gt;
&lt;h2&gt;
  
  
  UPLOADING DATA
&lt;/h2&gt;

&lt;p&gt;We can add data to our created collection in our database. We add what’s called a document. (Note TTL)&lt;br&gt;
 This document is represented as a plain javascript object. The data saved does not necessarily follow a rigid structure.&lt;br&gt;
  To do this, we first off: &lt;br&gt;
Go to our collection and click on new document button&lt;br&gt;
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. &lt;br&gt;
  Below is what our object would look like after filling it with data&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
          { 
            name : “Tony”,
            email : Tony@gmail.com
           }
     }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then click save the document is then saved in the collection in the format below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
       {
         “ref” : Ref(collection(collection_name), “279291161097536c12”),
            “ts” : 1602612179196000,
              “data” : {
                                “name” : “Tony”,
                                  “email”: “Tony@gmail.com”                                
                               }
       }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;References serve the same purpose as primary keys in the database systems, and they are used to provide pointers to a specific document.&lt;br&gt;
     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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
  Ref(collection_name, document_id)
}.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
 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.&lt;br&gt;
Now we can also upload data from our local machine. I would be using Node.js and Express to connect to Fauna.&lt;br&gt;
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)&lt;br&gt;
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.&lt;br&gt;
Now we install necessary packages, we install Fauna, and express &lt;br&gt;
Now we  create an src directory and in there create an index.js file &lt;br&gt;
Now we require express and Fauna and initialize the Fauna Client&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key})
   } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client would connect our source code to the actual database in the cloud using the API secret key we provided&lt;br&gt;
Now we start off our express server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
      const app = require(“express”);
      const faunadb  = require(“faunadb”);
      //now we initialize faunadb client
     const client = new faunadb.Client({secret: “Your_Key});


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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we import FQL functions from the  faunadb query namespace like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
      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, () =&amp;gt; console.log(“API on https//localhost:5000”));
   }  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s create a post endpoint to send data to our database, &lt;br&gt;
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&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
      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, () =&amp;gt; console.log(“API on https//localhost:5000”));
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, let’s use postman to send a post request to that endpoint &lt;br&gt;
We will return data that looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
“ref” :  (collection(collection_name), id),
“ts” : 22345432125656,
 “data”: {
  “name”:  “Tony”,
   “email”:  “Tony@gmail.com”

   } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RETRIEVING DATA
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  To retrieve data, we would need to setup our API endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here this endpoint would get a single user&lt;br&gt;
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&lt;br&gt;
Now we need to set up a GET endpoint to get a user with the Id in the url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
      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 =&amp;gt; res.send(e));
Res.send(doc);
)}; 
     app.listen(5000, () =&amp;gt; console.log(“API on https//localhost:5000”));
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the Get function we imported to query the database&lt;br&gt;
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&lt;br&gt;
Then we make a get request to Localhost:5000/user/copied_id&lt;br&gt;
If everything goes well we should get a document with Tony’s name and Email&lt;/p&gt;

&lt;h2&gt;
  
  
  MAINTENANCE
&lt;/h2&gt;

&lt;p&gt;Fauna is deployed and scaled as a collection of nodes, each of which operate within a cluster in an autonomous fashion.&lt;br&gt;&lt;br&gt;
Note There are no additional pieces of management software, such as a dedicated cluster manager to deploy.&lt;br&gt;&lt;br&gt;
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. &lt;br&gt;
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.&lt;br&gt;
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&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;Fauna modernizes database operations by making them simpler, efficient, low-cost, and easy to manage. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
