Exploring Node, Express, Knex, and Objection

Image for post
Image for post
Photo by Nate Grant on Unsplash

After finally getting comfortable with Ruby on Rails, I decided to teach myself Node.js for my Software Engineering Bootcamp capstone project. I was immediately thrown, what is a runtime? What is Express? Knex? Objection? Rails was a complete package with a little bow on top. Node was clearly much more involved. Let’s dive in and demystify Node, and its corresponding framework, query builder, and ORM.

Let’s start with Node.js. Node is not a language. It is a runtime, in this case, a way to run Javascript outside of the browser. It can be used to write server side applications with access to the operating system, file system, and everything else. It is written in C++, C, and JavaScript and is built on the open source v8 JavaScript engine. Some of the perks of Node include that it is asynchronous, non blocking, and allows us to make fullstack applications with only JavaScript. To install, click here. To run a JavaScript file with Node, simply type the node command followed by the file name. For example, node index.js.

Express.js declares itself a “fast, unopinionated, minimalist web framework for Node.js”. It can be used on top of Node to provide extra web functionality. Express has many built in methods that makes routes extremely easy to work with, as well as creating robust APIs. To get started with Express, run npm init into the command line to create a package.json, which defines your scripts and lists your dependencies. Next, run npm install express to install. By doing this, you are installing Express inside a node_modules directory. Node_modules keep track of all dependencies per project, similar to a gemfile. Next, inside your app, you must include the following code:

const express = require(“express”)
const app = express()

This is importing Express from the node_modules, and then initializing it in “app”. App.listen takes in an argument for what port you want to run the app on. This is the bare minimum needed to create your own Express application.

Next we have Knex.js. Knex self describes as “a ‘batteries included’ SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use”. Basically, it allows you to write SQL queries in your Node application. This is where you will create your migrations and your seeds, as well as run queries such as .select, .insert, .update, and .delete. To get started with Knex.js, run npm install knex followed by npm install database-name. To learn more about getting started with Knex, check out my other blog post here for more information.

Lastly, we have Objection.js. Its claim to fame is that it “is an ORM for Node.js that aims to stay out of your way and make it as easy as possible to use the full power of SQL and the underlying database engine while still making the common stuff easy and enjoyable”. Sounds simple enough, but like me you may be asking, “what is an ORM again?” ORM stands for Object-Relational Mapping. It allows you to query and manipulate data from a database using object oriented programming techniques. Their website also adds that “even though ORM is the best commonly known acronym to describe objection, a more accurate description is to call it a relational query builder. You get all the benefits of an SQL query builder but also a powerful set of tools for working with relations”. Objection is built on Knex, so all the necessary steps mentioned for Knex must precede any work with Objection. Objection allows you to clearly define your models and the relationships within them, as well as fetch, update, insert and delete your related objects. To get started with Objection, run npm install objection in your terminal. Then, include the following:

const {Model} = require(“objection”)

This pulls in “model” from Objection (an export the package provides), establishes your database connection with Knex, and tells the models how they can connect to the database. Remember, you must include the Knex starter code to run Objection, see this for more information.

As you can see, Node is quite involved, but it begins to make a little more sense when you investigate each of its counterparts little by little. Although some may prefer a more opinionated framework like Rails that takes care of everything, some like the flexibility and challenge of getting to know the framework, server, database, and ORM independently.



Software Engineer in Denver, Colorado.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store