11

Building a Command Line Tool with Nodejs and Fauna | CSS-Tricks

 2 years ago
source link: https://css-tricks.com/building-a-command-line-tool-with-nodejs-and-fauna/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Building a Command Line Tool with Nodejs and Fauna

❥ Sponsored (Written by Fortune Ikechi)

Command line tools are one of the most popular applications we have today. We use command line tools every day, and they range from git, npm or yarn. Command line tools are very fast and useful for automating applications and workflows.

We will be building a command line tool with Node.js and Fauna for our database in this post. In addition, we will be creating a random quotes application using Node.js, and 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:

Getting Started with Fauna

Register a new account using email credentials or a GitHub account. You can register a new account . Once you have created a new account or signed in, you are going to be welcomed by the dashboard screen:

s_369B760B2B052A894C4A6D0BE72A9A14BDFC8D60CF555C8D9CA1295334687277_1617635952038_faunadashboard.png?resize=768%2C565&ssl=1

Creating a New Fauna Instance

To create a new database instance using services, you have to follow some simple steps. On the dashboard screen, press the button New Database:

s_369B760B2B052A894C4A6D0BE72A9A14BDFC8D60CF555C8D9CA1295334687277_1617636926120_faunadbinstance.png?resize=383%2C550&ssl=1

Next, enter the name of the database and save. Once a database instance is set up, you are ready to access the key. Use access keys 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.

s_3B5DF067B9B1716A74F92E81442091B58B856125C18D3FD50CAA1D3FDB895FDC_1622318296601_faunakeysquoteapp.png?resize=677%2C544&ssl=1

Creating a Collection

Navigate to your dashboard, click on the Collections tab from the side menu, press the New Collection, button, input your desired name for the new collection, and save.

s_3B5DF067B9B1716A74F92E81442091B58B856125C18D3FD50CAA1D3FDB895FDC_1622319100997_collections.png?resize=677%2C741&ssl=1

Creating Indexes

To complete setup, create indexes for our application. Indexes are essential because are done using indexes in Fauna by matching the user input against the tern field. Create an index by navigating to the Indexes tab of our Fauna dashboard.

s_3B5DF067B9B1716A74F92E81442091B58B856125C18D3FD50CAA1D3FDB895FDC_1622373192792_indexes.png?resize=649%2C630&ssl=1

Now, we are ready to build our notes command-line application using Node.js and our database.

Initializing a Node.js App and Installing Dependencies

This section will initialize 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

Getting Started

To get started, let’s create a folder for our application inside the project folder 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 ended by initializing the npm dependencies. Next, add a package to make requests to the quotes server using

npm i axios

Add a package for coloring our texts, is an NPM package that helps us add color to print on the terminal. To add chalk, 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. We added the URL of our database, our database, and using axios, we made a GET request to the URL and added headers to enable us to get our response in json.

To log a quote, we use JavaScript promises to log the quote and its author on our console and 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

s_3B5DF067B9B1716A74F92E81442091B58B856125C18D3FD50CAA1D3FDB895FDC_1622476762593_notesapp.png?resize=1315%2C107&ssl=1

Conclusion

In this article, we learned more about and Node.js command-line tools. 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:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK