Here are slides from a talk given at LNUG May 2018.
Size: 10.27 MB
Language: en
Added: May 24, 2018
Slides: 43 pages
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
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