DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Leveraging Query Parameters for Efficient Data Filtering in REST APIs
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Understanding the Fan-Out/Fan-In API Integration Pattern

Trending

  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building a REST API With Feathers.js and SQLite

Building a REST API With Feathers.js and SQLite

This article will teach you how to use tools like Arctype, Feather.js, and SQLite to build a REST API and prototype a production-ready application.

By 
Ekekenta Odionyenfe user avatar
Ekekenta Odionyenfe
Β·
Apr. 29, 22 Β· Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.4K Views

Join the DZone community and get the full member experience.

Join For Free

Looking for a way to build a web application with features like authentication, database setup, and authorization without writing a ton of code and configurations? Ever wanted to create a production-ready app in a matter of days?

Believe it or not, it's possible! This tutorial will show you how to use Feathers.js to create a REST API in minutes. We'll learn about Feathers.js, implement an example API, and share some thoughts and considerations. Let's begin.

What Is Feathers.js?

Feathers is a lightweight web framework for developing real-time apps and REST APIs in JavaScript or TypeScript.

Feathers can interact with any backend technology, supports over a dozen databases, and works with any frontend technology, such as React, VueJS, Angular, and React Native.

Feathers.js is well-known for its ease of use and rapidity of delivery, and extensive documentation. With Feathers, all you need to add a feature is to run a simple command.

Prerequisites

This tutorial is a hands-on demonstration. To get started, I assume you have the following:

  • Node.js installed
  • Arctype installed
  • Insomnia installed
  • Prior knowledge of Node.js and Express.js

What We'll Be Building

You will create a movie rental application to illustrate the capabilities of Feathers.js and Arctype database visualization tools. The administrator will generate movies in this program, and authenticated users will be able to rent them. You'll learn to use Sequelize to correlate tables in Feathers.js, restrict access to specific routes, and link your database to Arctype.

Getting Started

To get started, open your command-line interface and create a folder for this tutorial with the command below:

Shell
 
npm install @feathersjs/feathers --save


Wait for the installation to complete and confirm the installation with the command below:

Shell
 
feathers -V


If everything went well with the installation, you'd see the version number printed out on the console.

Creating an Application

With Feathers installed on your computer, create a folder for this application with the command below:

Shell
 
Mkdir RestWithFeathers && RestWithFeathers


Then, generate a new API application with the command below:

Shell
 
feathers generate app


The above command will prompt you to select the configuration for your application. For the demonstration in this tutorial, your selection should look like the screenshot as shown below:

In the above screenshot, we made the following selections:

  • Selected Javascript as the preferred programming language
  • Specified the name for the application (movie-rental)
  • Selected src the location for the project boilerplates
  • Selected npm as the package manage
  • Enabled user authentication
  • Selected Eslint to analyze our code
  • Choose a username and password authentication strategy.
  • Selected user as the name for our entity
  • Selected Sequelize as the ORM for the application
  • Selected SQLite as our database
  • Specified movieDB as our database name

Once the selections are completed, the command will generate an express-like project structure. Now let's look at the folder structure generated by running the above command.

For this tutorial, we'll look at once we look at the following:

  • config: Contains the configurations files for the application
  • node_modules: A folder to store the list of installed packages required to run the application.
  • public: Contains the static files that can be served to the client.
  • src: Contains the server code for Feathers.js applications
  • src/hooks: Contains the application custom hooks.
  • src/middleware: Contains Express middlewares
  • src/service: Contains our application services
  • src/index.js: The entry file that runs the application
  • src/app.js: Configures our Feathers application
  • src/app.hook.js: Contains hooks that apply to every service
  • src/channels.js: Sets Featherjs event channels
  • test: Contains the test for the application

Now run the server in development mode with the command below:

Shell
 
npm run dev


Running the server in development activates hot reload and console error logging. At this point, the server should be running on port 3030, and a moviedb.sqlite file should be created in your project's root directory.

Creating Services

A service is an object or instance of a class that implements certain methods. Services provide a consistent, protocol-independent interface for interacting with any data. In Feathers, you just need to run a command, and everything is set up for you to create a service. Create a movie service with the command below:

Shell
 
feathers generate service


The above command will prompt you to select the configurations for your service. Your selection should look like the screenshot as shown below:

Here, you selected the ORM for your movie table, the service name, the route URL, and enabled authentication on the movie routes. Once those selections are completed, the command will generate a folder structure below in the src/service folder.

In your movie.hook file, Feathers added the code snippet below, which ensures that before the request passes through this route to the movie service, it has to confirm the user's user access token sent when the user logged in.

Properties files
 
before: {
   all: [],
   find: [ authenticate('jwt') ],
   get: [ authenticate('jwt') ],
   create: [ hashPassword('password') ],
   update: [ hashPassword('password'),  authenticate('jwt') ],
   patch: [ hashPassword('password'),  authenticate('jwt') ],
   remove: [ authenticate('jwt') ]
 },


Next, create a rental service with the command below:

Shell
 
feathers generate service


The above performs the same operation with that movie service, but this time generates a different folder name and files as shown below:

It will also call the jwt authenticate('jwt') function in all the routes. Also, the command will generate the respective models for the services you just created with some boilerplates as shown below:

Creating Database Tables

With the services and models created, modify the model's properties to have the required properties for the movie and rentals table. For the movie model, add the following properties to the properties.

Properties files
 
title: {
    type: DataTypes.STRING,
    allowNull: false,
},
producer: {
    type: DataTypes.STRING,
    allowNull: false,
},
imageURL: {
    type: DataTypes.STRING,
    allowNull: false,
},
createdAt: { type: DataTypes.DATE, defaultValue: Date.now },
     updatedAt: { type: DataTypes.DATE, defaultValue: Date.now },


Then, in the model of the rental, add the following properties.

Properties files
 
quantity: {
       type: DataTypes.INTEGER,
       allowNull: false,
     },
     createdAt: { type: DataTypes.DATE, defaultValue: Date.now },
     updatedAt: { type: DataTypes.DATE, defaultValue: Date.now },


We still need to create an association between the user, movie, and rental model, which brings us to the next section.

Data Relationships

Database relationships are associations formed between tables when data is retrieved using join statements. Relationships are often planned using an ER diagram. 

Our application has a user, a movie, and a rental table. The movie is owned by a rental, and a user owns a rental. The most straightforward approach to maintaining track of this data in each database is to establish a relationship between them, saving the table IDs as a foreign key in the tables with which they are related. So let's go ahead and create a relationship between of three tables. In the models/user.models.js, locate the comments:

JavaScript
 
// Define associations here
// See https://sequelize.org/master/manual/assocs.html


And add the code snippet below.

JavaScript
 
const { rentals } = models;
   users.hasMany(rentals);


You created a one-to-many relationship with the rentals table in the code snippet. This means that one user can have many rentals.

We then will also add the code below to the models/movie.model.js file.

JavaScript
 
const { rentals, movie } = models;
   movie.belongsToMany(rentals, { through: 'MovieRendtals' });


In the above code snippet, we created a many-to-many relationship between the rentals table, meaning a movie can have multiple rentals. In many-to-many relationships, a junction table is created to track the two tables' IDs, in this case, MovieRentals.

Lastly, add the code snippet below to the models/rentals.model.js file.

JavaScript
 
const { users, movie } = models;
   rentals.belongsTo(users);
   rentals.belongsToMany(movie, { through: 'MovieRentals' });


At this point, the tables now have a relationship with each other. Now you can load the data into the tables when you create or fetch the data from any service. That brings us to hooks in Feathers.

Adding Custom Hooks

Hooks are pluggable middleware functions that can be registered before, after, or on errors of a service method. You can register a single hook function or create a chain of them to create complex workflows. You'll create a hook that will load the data associated with each table. In your service/rentals folder, create a get-related.js file and the snippet below:

Properties files
 
module.exports = function (options = {}) {
    return async (context) => {
        const sequelize = context.app.get('sequelizeClient');
        const { users, movie } = sequelize.models;
        context.params.sequelize = {
        include: [{ model: users }, { model: movie }],
        raw: false,
    };
    return context;
    };
};


In the above code, the snippet tells Feathers to load users and movie models whenever a movie is rented. Now update your service/rentals/rental.hooks.js file with the code snippet below. Modify the code inside the before object.

Properties files
 
all: [authenticate('jwt')],
find: [getRelated()],
get: [getRelated()],
create: [getRelated()],
update: [],
patch: [],
remove: []


Test the Application

Now let's test the application with Insomnia. We'll start with the users routes.

Create a User

Create a user on the /users route.

Authenticate a User

Authenticate a user on the /authentication route.

Create a Movie

Create a movie on the /movie route.

Rent a Movie

Rent a movie on the /rentals route. You'll specify the userId, movieId, and quantity fields in this route.

These look good! Now go ahead and test other requests method on each route, like GET, UPDATE, and DELETE.

Connect to Arctype

Connect your database to Arctype to view the tables and data created in your application. You can connect to Arctype by following these steps:

  1. Run Arctype
  2. Click on the SQLite tab.
  3. Click on the Choose SQLite File button
  4. Navigate to the project folder and select the moviedb.sqlite file
  5. Test the connection and save the changes

Once your database is connected to Arctype, you'd see the users, movies, rentals, and MovieRentals tables as shown in the screenshot below:

At this point, your database has successfully been connected to Arctype. You can click on each table to show the data saved in them.

Conclusion

Throughout this tutorial, you've explored Feathers.js by building a demo application. You've learned how to set up a Feathers.js application, create a service, implement authentication/authorization, create custom hooks, and connect to Arctype. Now that you've gotten this knowledge, how do you intend to use Feathers in your next project? Perhaps you can even add an extra feature to this project by forking or cloning the GitHub repository.

API Database REST application Command (computing) Data (computing) Snippet (programming) SQLite

Published at DZone with permission of Ekekenta Odionyenfe. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building REST API Backend Easily With Ballerina Language
  • Leveraging Query Parameters for Efficient Data Filtering in REST APIs
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Understanding the Fan-Out/Fan-In API Integration Pattern

Partner Resources

Γ—

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! πŸ‘€
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!