Creating REST APIs is a fairly repetitive task. You’re always rewriting the Create, Read, Update, Delete methods…
In my search for a framework capable of generating a REST API, I discovered Feathers, a simple and useful framework for Node.js.
In this article, we’ll show you how to create a REST API from a PostgreSQL database in just a few minutes.

Introduction to Feathers

Feathers.js

Feathers is a framework for creating APIs in TypeScript or JavaScript. It can connect to various SQL (PostgreSQL, MySQL, Oracle, MSSQL…) or NoSQL (MongoDB) databases.
It can generate a REST or real-time API (web-socket), a CLI can be used to speed up development.
Lot of packages provided by the community exist to adding new features to Feather.

Prerequisites

To create a REST API application with Feathers.js, you need Node.js.

First Step with the CLI

I want to create a REST API for my workshop project, the API should exposing a table from my db to list all available workshops.
To start, I’m going to create a project using npm, which installs the CLI locally in the project at the same time.

Run the command:

npm create feathers@latest workshop-api

During the initialisation, answer to the questions :

? Do you want to use JavaScript or TypeScript? TypeScript
? Write a short description Workshop rest API
? Which HTTP framework do you want to use? KoaJS (recommended)
? What APIs do you want to offer? HTTP (REST)
? Which package manager are you using? npm
? Generate end-to-end typed client? Can be used with React, Angular, Vue, React Native, Node.js etc. Yes
? What is your preferred schema (model) definition format? Schemas allow to type, validate, secure and populate your data and configuration TypeBox (recommended)
? Which database are you connecting to? Databases can be added at any time PostgreSQL
? Enter your database connection string postgres://postgres:@localhost:5432/workshop

I choose :

  • TypeScript as language. In my opinion, typing can avoid many bugs
  • KoaJS as HTTP framework (KoaJS is a new framework designed by the team behind Express)
  • Only REST APIs, I have unchecked the real time API, I don’t want to use Web Sockets in my project
  • npm as package manager because, it’s already installed with node.js
  • Generate end-to-end typed client to YES, to be able to use types in front application
  • Typebox as schema (the default schema for Feathers)
  • PostgreSQL as database and the corresponding connection string

Now, initialization is complete. We have the skeleton of our application, but still no exposed service.

Add a service to the application

I use the CLI to generate a new service. This allows me to expose a table from my database via the REST API service.

To do this, into the application folder, simply run the npx command and answer the questions:

npx feathers generate service
? What is the name of your service? workshop
? Which path should the service be registered on? workshop
? Does this service require authentication? No
? What database is the service using? SQL
? Which schema definition format do you want to use? Schemas allow to type, validate, secure and populate data TypeBox  (recommended)

This command generates the files needed to expose a new service. Now I just need to define the structure of my table to have a functional service.

Define the data schema

To add our data model, we edit the generated file src/services/workshop/workshop.schema.ts and update the definition in this block of code:

// Main data model schema
export const workshopSchema = Type.Object(
  {
    id: Type.Number(),
    text: Type.String()
  },
  { $id: 'Workshop', additionalProperties: false }
)

By default the generated schema contains only an id and a text field. We’re going to modify the schema to adapt it to the actual structure of the data in the database.

I used an existing database with a workshop table designed like this:

  • id: primary key, auto increment
  • name: varchar 255, required
  • description: text, required
  • image: varchar 255

I’m changing the schema to match the structure of my database like this :

// Main data model schema
export const workshopSchema = Type.Required(Type.Object(
  {
    id: Type.Number(),
    name: Type.String({
      maxLength: 255
    }),
    description: Type.String({
      maxLength: 4096
    }),
    image: Type.Optional(Type.String({
      format: 'uri'
    }))
  },
  { $id: 'Workshop', additionalProperties: false }
))

In addition, update the columns list in the schema for creation and allowed query properties :

// Schema for creating new entries
export const workshopDataSchema = Type.Pick(workshopSchema, ['name', 'description', 'image'], {
  $id: 'WorkshopData'
})

.....

// Schema for allowed query properties
export const workshopQueryProperties = Type.Pick(workshopSchema, ['id', 'name', 'description', 'image'])

You can use the same process to generate as many services as you like. It’s quick and easy.

Run the Application

Now that the service is configured, I can run your freshly created REST API.

To do that, I run the command:

npm run dev

Once the application has started, simply open a browser and go to the url: http://localhost:3030
This page display only a Feathers.js logo.
To view the data of your db exposed by the service, go to: http://localhost:3030/[my_service name]
In my case, it’s: http://localhost:3030/workshop, the result of the call is a json with the data of my db.

Note about the documentation

The Feathers documentation about applications creation gives this command to start the application:

npm run compile 
npm run migrate 
npm start

The npm run compile an npm start have the same behavior as npm run dev, except that these first commands don’t refresh when code changes. But be careful with npm run migrate.

The npm run migrate, execute Knex.js, a tool for modifying the data structure in the database. Run the migrate command, which deletes nothing, but creates 2 tables in the db for internal use. If you already have a database with data, the migrate command is useless.

Conclusion

Creating a REST API application with Feathers.js is quick and easy, using its cli. It can connect to many databases such as PostgreSQL, MSSQL, Oracle, MongoDB…..
This article was a simple, basic example of API implementation, but Feathers.js can do more. Like authentication, data structure migration or adding a debugging user interface (Swagger) with an additional package.

To continue with Feathers.js

See related articles:

Add a UI to explore the Feathers.js API
Add authentication in a Feathers.js REST API