Building a command line tool with Nodejs and Fauna
Written in connection with the Write With Fauna program.
Command line tools are one of the most popular applications we have today, in fact we use command line tools everyday, they range from git, npm or yarn. Command line tools are very fast and useful for automating applications and workflows
In this post, we will be building a command line tool with Node.js and Fauna for our database. We will be building a random qoutes application using Node.js, we will add permission and a keyword for our app.
Prerequisites
To take full advantage of this tutorial, make sure you have the following installed on your local development environment:
-
Node.js version >=
16.x.x
installed. - Have access to one package manager such as
[npm](https://www.npmjs.com/)
or[yarn](https://classic.yarnpkg.com/en/)
. - Access to FaunaDB dashboard.
Why FaunaDB?
Below are some points on why you should use FaunaDB
- Fauna supports the ACID convention which include Atomicity, Consistency, Isolation and Durability.
- Scalability: Fauna scales well according to the demand and load balances.
- Ease of use: Fuana is quite easy to use for serverless applications.
- Supports GraphQl: With Fauna, you can import your graphQL schemas and test on Fauna’s graphQL playground.
Why FaunaDB?
Below are some points on why you should use FaunaDB
- Fauna supports the ACID convention which include Atomicity, Consistency, Isolation and Durability.
- Scalability: Fauna scales well according to the demand and load balances.
- Ease of use: Fuana is quite easy to use for serverless applications.
- Supports GraphQl: With Fauna, you can import your graphQL schemas and test on Fauna’s graphQL playground.
Getting started with FaunaDB
To get started with Fauna, first register a new account either using email credentials or using a GitHub account. You can register a new account here. Once you have created a new account or signed in, you are going to be welcomed by the dashboard screen:
Creating a new FaunaDB instance
To create a new database instance using FaunaDB services, you have to follow some simple steps. On the dashboard screen, press the button New Database
:
Next, enter the name of the database and save
. Once a database instance is set up, you are ready to access key. Fauna’s access keys is used to connect authorization and a connection to the database from a single page application. To create your access key, navigate to the side menu and go to the Security
tab and click on the New Key
button.
Creating a Collection
Navigate to your Fauna dashboard and click on Collections
tab from the side menu and press the button for New Collection
, input your desired name for the new collection and save.
Creating Indexes
To complete our Fauna setup, let’s create indexes for our application, Indexes are important because searching documents are done using indexes in Fauna by matching the user input against the tern
field.
To create an index, navigate to the Indexes
tab of our Fauna dashboard and click on Indexes
.
Now we are ready to build our notes command line application using Node.js and our fauna database.
Initializing a Node.js app and installing dependencies
In this section, we are going to initilize a Node.js application and install the dependencies we need using the NPM
package. We are also going to build a simple quotes application from this link.
Getting Started
To get started, first let’s create a folder for our application and inside the project folder. Let’s do that using the code block below on our terminal
mkdir quotes_cli
cd quotes_cli
touch quotes_app
npm init -y
In the code block above, we created a new directory, navigated into the directory and created a new file called quotes_app
and we ended by initializing the npm
dependencies.
Next, we will add a package to make requests to the quotes server, we will use [axios](https://www.npmjs.com/package/axios)
.
npm i axios
We’ll also need to add a package for coloring our texts, chalk is an npm
package that helps us add color to print on the terminal. To add chalk
, let’s use the code block below
npm i chalk
Let’s also import a dotenv
package using the code block
npm i dotenv
Building the Quotes app
In our quotes_app
file, let’s add the code block below
const axios = require('axios')
const chalk = require('chalk');
const dotenv = require('dotenv');
const url = process.env.APP_URL
axios({
method: 'get',
url: url,
headers: { 'Accept': 'application/json' },
}).then(res => {
const quote = res.data.contents.quotes[0].quote
const author = res.data.contents.quotes[0].author
const log = chalk.red(`${quote} - ${author}`)
console.log(log)
}).catch(err => {
const log = chalk.red(err)
console.log(log)
})
In the code block above, we imported axios
, chalk
and dotenv
. Next, we added the URL of our database, our fauna database and using axios
, we made a GET
request to the URL
and we also added headers
to enable us get our response in json
.
To log a quote, we use JavaScript promises to log the quote and it’s author on our console. we also added a catch
method for catching errors.
Before we run, let’s change the permissions on our file using the code below
chmod +x quotes_app
Next, run the application using our keyword below
./quotes_app
We should get a result similar to the image below
Conclusion
In this article we learnt more about Fauna and Node.js command line tools, Fauna can be used for serverless storage for Node.js applications.
You can extend the application to be able to add date reminders in real time.
Here is a list of some resources that you might like after reading this post:
Discussion (0)