This content originally appeared on DEV Community and was authored by p-ken1
Fauna is the data API for client-serverless applications. With support for custom business logic and integration with the serverless ecosystem, enables developers to simplify code and ship faster. Fauna database is a general purpose distributed database, supporting multiple data models and strong global consistency also supporting multiple programming languages and most importantly it implements 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 core functions are inspired by Calvin, a clock-less, strictly-serializable transactional protocol for multi-region environments.
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 modelling use cases.
Objective Introducing Faunadb 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 Fauna. 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 thesite.
Creating FaunaDB cloudware repository
Now lets 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” to 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.
Example of an overview page
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
•functions
• Indexes
•Shell
•GraphQl
• Security
Example of an dashboard page
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. Collections shouldn't be a hassle to understand if you are familiar with mongodb.
Indexes
Indexes help us to browse the data in our collections. They are basically like
a tag to a document in the collection. These helps fauna to 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 is the command line (CLI) that lets us execute fauna queries interactively
and again 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 read more. And also Fauna security, designed to make it easy to query
one database from any network connected context Read more.
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 kind of 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) It 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, they are used to provide pointers to a specific document. The function
takes in two arguments, the first is the collection name and the second is the
id of that document as seen below.
{
Ref(Collection(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 getting a snapshot of the database
before and after particular transactions where processed. Now we can also upload
data from our local machine . I would be using Node.js and Express to connect to
fauna
First off we set up our node project and initialize it , and if you do not
node installed 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 our fauna account with the website all you need
do is go to Settings then developers then request for your API key then copy
it.Now we install necessary packages, we install Faunadb, and express
Now we create an src directory and in there create an index.js file
Now we require express and Faunadb and Initialize faunadb Client
Example
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 which we provided.
- Now we start off our express server.
Example
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
Example
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 lets create a post endpoint to send data to our database,
We setup 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
Example
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
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 end point
We would be returned data
It looks like this
“ref” : (collection(collection_name), id),
“ts” : 22345432125656,
“data”:{
“name”: “Tony”,
“email”: “Tony@gmail.com”
}
Retrieving dataTo retrieve data we would need to setup our API end
point 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 promiseNow we need to set up a GET endpoint to get a user with the Id in the url
We do this below
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.idThen 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
Like this blow
ref: Ref(collection(collection_name), "Id"),
ts: 1233263743819,
data: {
name: "Tony",
email: "Tony@gmail.com"
}
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 References: Fauna website
This content originally appeared on DEV Community and was authored by p-ken1
p-ken1 | Sciencx (2021-08-29T17:58:21+00:00) CLOUD COMPUTING WITH FAUNADB CLOUDWARE. Retrieved from https://www.scien.cx/2021/08/29/cloud-computing-with-faunadb-cloudware/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.