Objection.js, a SQL ORM

paulbjensen 2,306 views 43 slides May 24, 2018
Slide 1
Slide 1 of 85
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85

About This Presentation

Here are slides from a talk given at LNUG May 2018.


Slide Content

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

Apply the migration

Apply the migration

Reversing the migration

Reversing the migration

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

You can also specify
relationships through
join tables

Example:
Doing the dishes

Eager Loading

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

If you add jsonSchema to your
models, you get to use some
very cool plugins later on

Graph Inserts

Graph Inserts are cool
!

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-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