<?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: samaby213</title>
    <description>The latest articles on DEV Community by samaby213 (@samaby213).</description>
    <link>https://dev.to/samaby213</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%2F709961%2F58daabf3-092e-44ec-875e-cd2d82288942.png</url>
      <title>DEV Community: samaby213</title>
      <link>https://dev.to/samaby213</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samaby213"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide To Fauna</title>
      <dc:creator>samaby213</dc:creator>
      <pubDate>Mon, 20 Sep 2021 18:03:32 +0000</pubDate>
      <link>https://dev.to/samaby213/a-beginner-s-guide-to-fauna-3ckg</link>
      <guid>https://dev.to/samaby213/a-beginner-s-guide-to-fauna-3ckg</guid>
      <description>&lt;p&gt;After having a hard time trying to grasp Fauna and serverless databases, I came up with the idea of demystifying what I have learned and the quickest way to get up and running with the Fauna.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who is this for?
&lt;/h2&gt;

&lt;p&gt;Those new to serverless databases who would like to try out Fauna to use in a project. You need basic programming knowledge to walk you through the lesson.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we will show how to create a basic Twitter social graph and get it on the web using NodeJS.&lt;/p&gt;

&lt;p&gt;Fauna is a next generation cloud database that simplifies the complexity of building complex relationships. It uses the same codebase as SQL, but is completely serverless and fast.&lt;br&gt;
This tutorial shows how to create a basic social graph with Fauna, which is powered by Node.js. You will also learn how to query the database using the Fauna Query Language.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initial setup
&lt;/h2&gt;

&lt;p&gt;Start by creating a Node project, then install the Fauna JS package and Express.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y

npm install faunadb express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initialize Fauna
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_1VeT3Tm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wm1e4dxmas1ydg57zhy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_1VeT3Tm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wm1e4dxmas1ydg57zhy2.png" alt="fauna"&gt;&lt;/a&gt;&lt;br&gt;
From the Fauna security tab, create a server key. Initialize the client with your server key, then import the FQL functions required for this demo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js 

const faunadb = require('faunadb');
const client = new faunadb.Client({ secret: 'YOUR-KEY' })

// FQL functions
const {
    Ref,
    Paginate,
    Get,
    Match,
    Select,
    Index,
    Create,
    Collection,
    Join,
    Call,
    Function: Fn,
} = faunadb.query;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Initialize Express
&lt;/h2&gt;

&lt;p&gt;Express will be used to serve the API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js 
const app = require('express')();

app.listen(5000, () =&amp;gt; console.log('API on http://localhost:5000'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An API like Insomnia is recommended when making a request to the API at port &lt;code&gt;http://localhost:5000&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Database structure
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NoIXQEgr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejpog6h1bs5fflchc8vl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NoIXQEgr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejpog6h1bs5fflchc8vl.png" alt="fauna"&gt;&lt;/a&gt;&lt;br&gt;
The database contains three collections - users, tweets, and relationships. Create three collections: users, tweets, and relationships.&lt;/p&gt;
&lt;h2&gt;
  
  
  Users and tweets
&lt;/h2&gt;

&lt;p&gt;In the next section, we will create an API that will allow users to read and write tweets to Fauna.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a tweet
&lt;/h2&gt;

&lt;p&gt;We want to associate many tweets to this user account, so next we Create a document with your username as the username.So we go to our dashboard and create a document with our name as username.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fYInrrWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fxnah5eco1qqqnfitst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fYInrrWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fxnah5eco1qqqnfitst.png" alt="fauna"&gt;&lt;/a&gt;&lt;br&gt;
A relationship can be established between a user and a document by retrieving its data from the Create function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js 
app.post('/tweet', async (req, res) =&amp;gt; {

    const data = {
        user: Select('ref', Get(Match(Index('users_by_name'), 'fireship_dev'))),
        text: 'Hello world!'
    }

    const doc = await client.query(
        Create(
            Collection('tweets'),
            { data }
        )
    )

    res.send(doc)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Read a tweet by ID
&lt;/h2&gt;

&lt;p&gt;Reading a document by its ID does not require an index. Doing so can be done by pointing to its ID and creating a new document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js 
app.get('/tweet/:id', async (req, res) =&amp;gt; {

    const doc = await client.query(
        Get(
            Ref(
                Collection('tweets'),
                req.params.id
            )
        )
    )

    res.send(doc)

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Query a user’s tweets
&lt;/h2&gt;

&lt;p&gt;Creating an index for each tweet document that a user has tweeted is required. The index will return all the documents that contain the user's name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js
app.get('/tweet', async (req, res) =&amp;gt; {

    const docs = await client.query(
        Paginate(
            Match(
                Index('tweets_by_user'), 
                Select('ref', Get(Match(Index('users_by_name'), 'fireship_dev')))
            )
        )
    )

    res.send(docs)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fauna functions
&lt;/h2&gt;

&lt;p&gt;The code presented above duplicates the following line of FQL several times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select('ref', Get(Match(Index('users_by_name'), '&amp;lt;username&amp;gt;')))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fauna Functions are a way to extract the logic of your system to the cloud. They can be used to reduce duplicated and improve maintainability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a function
&lt;/h2&gt;

&lt;p&gt;Extract the duplicated code from the Fauna function. The function returns the username and the full document reference.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IFz_g7DQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqlqo41dkrge5uboatfv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IFz_g7DQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqlqo41dkrge5uboatfv.png" alt="fauna"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Call a function
&lt;/h2&gt;

&lt;p&gt;A Call can be used to execute this function in a query. For example, let's refactor the previous example 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;// src/index.js
const {
    Call,
    Function: Fn,
} = faunadb.query;

app.get('/tweet', async (req, res) =&amp;gt; {

    const docs = await client.query(
        Paginate(
            Match(
                Index('tweets_by_user'), 
                Call(Fn("getUser"), '&amp;lt;username&amp;gt;v')
            )
        )
    )

    res.send(docs)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  User-to-user relationships
&lt;/h2&gt;

&lt;p&gt;The following section shows a graph where users can connect to other users and query their tweets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a relationship
&lt;/h2&gt;

&lt;p&gt;Two user reference are contained in a relationship document — the follower and followee.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/index.js
app.post('/relationship', async (req, res) =&amp;gt; {


    const data = {
        follower: Call(Fn("getUser"), 'bob'),
        followee: Call(Fn("getUser"), '&amp;lt;username&amp;gt;')
    }
    const doc = await client.query(
        Create(
            Collection('relationships'),
            { data }
        )
    )

    res.send(doc)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Query a feed of tweets
&lt;/h2&gt;

&lt;p&gt;You probably want to query the tweets of followed users after establishing a relationship with them. To do so, create an index called &lt;code&gt;followees_by_follower&lt;/code&gt; with a search term and a value of follower.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/index.js
app.get('/feed', async (req, res) =&amp;gt; {
    const docs = await client.query(
        Paginate(
            Join(
                Match(
                    Index('followees_by_follower'),
                    Call(Fn("getUser"), 'bob')
                ),
                Index('tweets_by_user'),
            )
        )
    )

    res.send(docs)
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we learned how to create a basic Twitter social graph and get it on the web using Node.js.&lt;/p&gt;

&lt;p&gt;Written in connection with the &lt;a href="https://fauna.com/blog/write-with-fauna?utm_source=devto&amp;amp;utm_medium=writewithfauna&amp;amp;utm_campaign=WritewithFauna_BeginnersGuide_AmarachiKalu"&gt;Write with Fauna Program&lt;/a&gt;. &lt;/p&gt;

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