SlideShare a Scribd company logo
Objection.js
a SQL ORM
Paul Jensen
HELLO
ITS ME AGAIN!
Node.js has plenty of
ORMs
Why consider
another?
Sometimes a fresh take on
an existing challenge can
offer something better
So for your
consideration, I offer
you this…
Objection.js
github.com/vincit/objection.js
Objection.js
• Let’s you create models for tables with ES6 classes and define
relationships between them

• Make queries with Node’s async/await

• Add validation to your models using JSON schema

• Use eager-loading and transactions with your models

• Work with nested documents in rows

• Perform Graph Inserts and Upserts

• and more…
Install
Knex
knexjs.org
Knex
• Rather than reinvent the wheel, Objection uses Knex, a
powerful SQL query builder.

• Besides building SQL queries, Knex is used to establish
database connections, including pooling connections.

• Also, it is used for managing database schemas via
migrations.
Install a DB driver as well
Creating and managing
database schemas
Creating and managing
database schemas
• Database migrations are a good pattern for managing
changes to database schemas over time.

• Objection.js defers to Knex for doing database migrations.

• Knex depends on connecting to an existing database, so
we’d need to create that first (unless it exists already).
Creating a database
Creating migrations
• Migrations allow you to apply a change to a database
schema in a step-by-step fashion.

• The “up” action applies a change (creating a table, adding
or modifying a column, appending an index).

• The “down” action applies the reverse action of the change
(e.g. if a migration’s “up” action creates a table, it’s
equivalent “down” action will drop the table).

• It provides a way to evolve a database schema over time,
and be able to track the state of the database schema
alongside the source code in version control (Git).
Creating migrations
• Objection uses Knex’s in-built support for generating
migrations and applying them, as well as being able to roll
them back.
Creating migrations
Creating migrations
Creating migrations
• Knex will create a migrations folder (unless it exists already)

• It then creates a file in the migrations folder for the
migration

• The filename from the command line is prepended with a
timestamp so that we can organise migrations in
chronological order.

• That determines the order in which migrations are
executed.
The generated migration
Objection.js, a SQL ORM
Apply the migration
Apply the migration
Objection.js, a SQL ORM
Reversing the migration
Reversing the migration
Objection.js, a SQL ORM
Models
Models
• Models are the wrappers around database tables

• They help to encapsulate the business logic within those
tables (relations, validations, indexes, triggers).

• Objection.js allows you to create Models using ES classes
Task model
Queries
A simple SELECT query
can be done with…
A more typical SELECT
query
can be done with…
INSERT queries
can be done with…
UPDATE queries
can be done with…
DELETE queries
can be done with…
Relationships
Relationships
• Objection.js provides a way to define the relationship
between models that is powerful and flexible.

• The relationships allow you to use other features like eager
loading, as well as support Objection.js’ GraphQL plugin.
Relationships
tasks
id
name
due_by
is_done
created_at
updated_at
task_joins
id
dependent_id
dependency_id
created_at
updated_at
1
∞
∞
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Objection.js, a SQL ORM
You can also specify
relationships through
join tables
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Example:
Doing the dishes
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Eager Loading
Objection.js, a SQL ORM
Lifecycle functions
• A way to trigger functions when a record is inserted,
updated, deleted, fetched or validated.

• The lifecycle functions follow a pattern of beforeAction
and afterAction.
Lifecycle functions
• beforeValidate, afterValidate

• beforeInsert, afterInsert

• beforeUpdate, afterUpdate

• beforeDelete, afterDelete

• afterGet
Lifecycle functions
You can execute functions that
trigger during the lifecycle of
inserting and updating records.

You can determine whether
they happen before or after the
lifecycle event.
Lifecycle functions
You can execute functions that
trigger during the lifecycle of
inserting and updating records.

You can determine whether
they happen before or after the
lifecycle event.
Validations
Validations
• Objection allows you to add validation logic to your models
through using JSONSchema.

• The validation logic will raise errors which can then be
intercepted with try/catch
Objection.js, a SQL ORM
If you add jsonSchema to your
models, you get to use some
very cool plugins later on
Graph Inserts
Graph Inserts are cool
🕶
Objection.js, a SQL ORM
Quick demo
Plugins
Plugins
• Objection comes with a few plugins that are worth
checking out

• Two which I can recommend are “objection-graphql” and
“objection-password”
Objection-Password
• Objection password adds quick local authentication to an
application, using Bcrypt to apply the salting and hashing.

• Setting it up is very simple - simply specify the number of
hashing rounds for encrypting the password, and make
your User model an extended class of the Password class
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Objection.js, a SQL ORM
Objection-GraphQL
• Objection-GraphQL allows you to put a GraphQL API on
top of your Objection.js models with a few lines of code.

• You will need to have your models using JSONSchema in
order to get the benefits of this.
Quick little demo
Performance?
Do benchmark it, see
what the performance
impact is like
Wrap up 🌯
Objection.js
github.com/vincit/objection.js
Knex
knexjs.org
Objection-Password
github.com/scoutforpets/objection-password
Objection-GraphQL
github.com/vincit/objection-graphql
One more thing…
After 2½ glorious
years at Starcount
I’m going to launch
my own consultancy
ANEPHENIX
and I’m available for
contracting from mid
June
Thank You

More Related Content

What's hot (13)

PDF
14-Table eggs file ملف بيض المائدة
Dr. Riad Kussaibati
 
PDF
لنطبق معا نظام الهسب و الآيزو 22000 في مؤسستك بسهولة
guestb31f27
 
PPTX
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
SAID MASHATE
 
PPTX
La rééducation du genou du sportif : Rééducation ou Réathlètisation ? - Jean-...
amfck
 
PPTX
2014 - formation à la manipulation des extincteurs
APS&I - Formations
 
PPT
4 طحن القمح
Farhan Alfin
 
PPTX
Formation incendie
Kévin Dessin
 
PDF
أمراض سوء التغذية عند الأطفال
lamiaa Gamal
 
PDF
دليل البيئة و الصحة و السلامة Ehs
دكتور تامر عبدالله شراكى
 
PPTX
Système de notifications
ODC Orange Developer Center
 
PPTX
Planification d'entraînement
Patrice Cossard
 
PDF
2- Heat treatment of hatching eggs during storage المعالجة الحرارية لبيض الف...
Dr. Riad Kussaibati
 
PPTX
Strength training for older adults: What the public health messaging should be
Chris Hattersley
 
14-Table eggs file ملف بيض المائدة
Dr. Riad Kussaibati
 
لنطبق معا نظام الهسب و الآيزو 22000 في مؤسستك بسهولة
guestb31f27
 
666148532-Formation-Habilitation-ELECTRIQUE-ENTREPRISE-MARS-2017.pptx
SAID MASHATE
 
La rééducation du genou du sportif : Rééducation ou Réathlètisation ? - Jean-...
amfck
 
2014 - formation à la manipulation des extincteurs
APS&I - Formations
 
4 طحن القمح
Farhan Alfin
 
Formation incendie
Kévin Dessin
 
أمراض سوء التغذية عند الأطفال
lamiaa Gamal
 
دليل البيئة و الصحة و السلامة Ehs
دكتور تامر عبدالله شراكى
 
Système de notifications
ODC Orange Developer Center
 
Planification d'entraînement
Patrice Cossard
 
2- Heat treatment of hatching eggs during storage المعالجة الحرارية لبيض الف...
Dr. Riad Kussaibati
 
Strength training for older adults: What the public health messaging should be
Chris Hattersley
 

Similar to Objection.js, a SQL ORM (20)

PDF
COSCUP 2019 - The discussion between Knex.js and PostgreSQL
Len Chang
 
PPTX
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
Kongu Engineering College, Perundurai, Erode
 
PPTX
Connect to NoSQL Database using Node JS.pptx
Kongu Engineering College, Perundurai, Erode
 
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Daniel Cousineau
 
PPTX
2019-01-24 Sequelize ORM (Object Relational Mapper): models, migrations, oh my
Ryan M Harrison
 
ZIP
Forget The ORM!
Randal Schwartz
 
PPTX
The tortoise and the ORM
Frikkie van Biljon
 
PDF
From Zero to CRUD with ORM - Led by Annette Liskey.pdf
Ortus Solutions, Corp
 
PPTX
Nosql
Roxana Tadayon
 
PPTX
Nosql
ROXTAD71
 
KEY
MongoDB at CodeMash 2.0.1.0
Mike Dirolf
 
PDF
Node.js
Danilo Sousa
 
PDF
TypeORM으로 보는 마이그레이션과 N+1 문제
Yangwook Jeong
 
PPTX
MongoDB
Rony Gregory
 
PPTX
Mongo db 101 dc group
John Ragan
 
PPT
Design your application using Persistent Graphs and OrientDB
Luca Garulli
 
PDF
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB
 
KEY
Seedhack MongoDB 2011
Rainforest QA
 
PPTX
Mongo db tips and advance features
Sujith Sudhakaran
 
PDF
ITB2017 - Slaying the ORM dragons with cborm
Ortus Solutions, Corp
 
COSCUP 2019 - The discussion between Knex.js and PostgreSQL
Len Chang
 
Connect to NoSQL Database (MongoDB) using Node JS & Connect Node.js with NoSQ...
Kongu Engineering College, Perundurai, Erode
 
Connect to NoSQL Database using Node JS.pptx
Kongu Engineering College, Perundurai, Erode
 
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Daniel Cousineau
 
2019-01-24 Sequelize ORM (Object Relational Mapper): models, migrations, oh my
Ryan M Harrison
 
Forget The ORM!
Randal Schwartz
 
The tortoise and the ORM
Frikkie van Biljon
 
From Zero to CRUD with ORM - Led by Annette Liskey.pdf
Ortus Solutions, Corp
 
Nosql
ROXTAD71
 
MongoDB at CodeMash 2.0.1.0
Mike Dirolf
 
Node.js
Danilo Sousa
 
TypeORM으로 보는 마이그레이션과 N+1 문제
Yangwook Jeong
 
MongoDB
Rony Gregory
 
Mongo db 101 dc group
John Ragan
 
Design your application using Persistent Graphs and OrientDB
Luca Garulli
 
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB
 
Seedhack MongoDB 2011
Rainforest QA
 
Mongo db tips and advance features
Sujith Sudhakaran
 
ITB2017 - Slaying the ORM dragons with cborm
Ortus Solutions, Corp
 
Ad

More from Paul Jensen (6)

PDF
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Paul Jensen
 
PDF
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Paul Jensen
 
PPTX
Lnug jan 2018
Paul Jensen
 
PDF
Desktop apps with node webkit
Paul Jensen
 
PDF
SocketStream
Paul Jensen
 
PDF
Geokit In Social Apps
Paul Jensen
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Paul Jensen
 
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Paul Jensen
 
Lnug jan 2018
Paul Jensen
 
Desktop apps with node webkit
Paul Jensen
 
SocketStream
Paul Jensen
 
Geokit In Social Apps
Paul Jensen
 
Ad

Recently uploaded (20)

PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Tally software_Introduction_Presentation
AditiBansal54083
 

Objection.js, a SQL ORM