Exploring Node, Express, Knex, and Objection

Rachel Emmer
4 min readJun 4, 2020

--

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()
app.listen(4000)

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”)Model.knex(database)

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.

Resources:

https://nodejs.org/
http://knexjs.org/
https://expressjs.com/
https://vincit.github.io/objection.js/

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rachel Emmer
Rachel Emmer

Written by Rachel Emmer

Software Engineer in Denver, Colorado.

No responses yet

Write a response