FeathersJS with SQL (ObjectionJS)

Guru Prasad Mohapatra
Smartters’ Studio
4 min readMay 25, 2021

Introduction

FeatherJS is a NodeJS framework famous for it’s automated DB Operation Generators in MongoDB.But Is it helpful in Relational Databases ?? The answer is yes, it is. We will learn how to create a project with Objection.js(A SQL Friendly ORM for NodeJS)

NOTE : This Article is gonna be long as there are a lot of features to put in but i will try to make as smaller as possible with more images and small pieces of code

Create a Project

if you are new to FeathersJS you can learn how to create and setup project here.

Adding A Service

create a service and give Objection as the Database Connection.

P.S : The Databases must be created before manually

feathers generate service

Generating the Model

you can get the auto generated model at

your_project →src →models →your_service.model.js

In our case it is in feathers-objection-demo →src →models → author.model.js

Model Structure

The model file is basically divided into 2 parts

  • JSON Schema Class

It is basically a class where we can define our database structure along with the validations.

  • Database Creation

As a SQL based Database we must create the tables before running the application.

It will create the table if the table doesn’t exist.

Running The App

yarn dev
Create a record
Get All Records
Update A Record
Delete A Record

Popular Queries

By Default by Objection and feathers we get many useful predefined queries

  • $eq → Equal To
  • $ne → Not Equal To
  • $gt → Greater Than
  • $gte → Greater Than Equal To
  • $lt → Less Than
  • $lte → Less Than Equal To
  • $in → The value is in the array specified
  • $nin → The value is not in the array specified
  • $like → The value is like the special structure
  • $notLike → The value is not like the special structure
  • $or → Join multiple conditions with or
  • $and → Join multiple conditions with and
  • $sort → sort the data with specified field given

Relations Between Tables

We can define Relations between 2 tables in the models for many extra functionalities

Let’s Take another model “Books” in our project.

Don’t forget to write export before the class

Let’s put the relation in the author model.We can do it with a predefined method called relationMappings().

To see the magic we need to allow some query parameters in the Service .

When we pass the $eager parameter with the relation name we can get the relation data.

Conditions in Joins

We can put conditions on joined tables too

We have to add some extra options in the service

Then we can put conditions on the joined table too

Conclusion

Now, our “FeathersJS with SQL(ObjectionJS)” is complete. I hope that you have received some useful information from this article.You can get the complete code from our GitHub repo.Also you can learn more about it in the official documentation here.

You can follow me on Twitter and LinkedIn . Also, don’t forget to checkout our Website. Thank you for reading, if you enjoyed the article make sure to show me some love by hitting that clap (👏) button!

Happy coding…

Guru Prasad Mohapatra

--

--