Slide 1

Slide 1 text

Objection.js a SQL ORM Paul Jensen

Slide 2

Slide 2 text

HELLO ITS ME AGAIN!

Slide 3

Slide 3 text

Node.js has plenty of ORMs

Slide 4

Slide 4 text

Why consider another?

Slide 5

Slide 5 text

Sometimes a fresh take on an existing challenge can offer something better

Slide 6

Slide 6 text

So for your consideration, I offer you this…

Slide 7

Slide 7 text

Objection.js github.com/vincit/objection.js

Slide 8

Slide 8 text

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…

Slide 9

Slide 9 text

Install

Slide 10

Slide 10 text

Knex knexjs.org

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

Install a DB driver as well

Slide 13

Slide 13 text

Creating and managing database schemas

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Creating a database

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Creating migrations • Objection uses Knex’s in-built support for generating migrations and applying them, as well as being able to roll them back.

Slide 18

Slide 18 text

Creating migrations

Slide 19

Slide 19 text

Creating migrations

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

The generated migration

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Apply the migration

Slide 24

Slide 24 text

Apply the migration

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Reversing the migration

Slide 27

Slide 27 text

Reversing the migration

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Models

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Task model

Slide 32

Slide 32 text

Queries

Slide 33

Slide 33 text

A simple SELECT query can be done with…

Slide 34

Slide 34 text

A more typical SELECT query can be done with…

Slide 35

Slide 35 text

INSERT queries can be done with…

Slide 36

Slide 36 text

UPDATE queries can be done with…

Slide 37

Slide 37 text

DELETE queries can be done with…

Slide 38

Slide 38 text

Relationships

Slide 39

Slide 39 text

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.

Slide 40

Slide 40 text

Relationships tasks id name due_by is_done created_at updated_at task_joins id dependent_id dependency_id created_at updated_at 1 ∞ ∞

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

You can also specify relationships through join tables

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Example: Doing the dishes

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Eager Loading

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

Lifecycle functions • beforeValidate, afterValidate • beforeInsert, afterInsert • beforeUpdate, afterUpdate • beforeDelete, afterDelete • afterGet

Slide 55

Slide 55 text

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.

Slide 56

Slide 56 text

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.

Slide 57

Slide 57 text

Validations

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Graph Inserts

Slide 62

Slide 62 text

Graph Inserts are cool

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Quick demo

Slide 65

Slide 65 text

Plugins

Slide 66

Slide 66 text

Plugins • Objection comes with a few plugins that are worth checking out • Two which I can recommend are “objection-graphql” and “objection-password”

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

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.

Slide 72

Slide 72 text

Quick little demo

Slide 73

Slide 73 text

Performance?

Slide 74

Slide 74 text

Do benchmark it, see what the performance impact is like

Slide 75

Slide 75 text

Wrap up

Slide 76

Slide 76 text

Objection.js github.com/vincit/objection.js

Slide 77

Slide 77 text

Knex knexjs.org

Slide 78

Slide 78 text

Objection-Password github.com/scoutforpets/objection-password

Slide 79

Slide 79 text

Objection-GraphQL github.com/vincit/objection-graphql

Slide 80

Slide 80 text

One more thing…

Slide 81

Slide 81 text

After 2½ glorious years at Starcount

Slide 82

Slide 82 text

I’m going to launch my own consultancy

Slide 83

Slide 83 text

ANEPHENIX

Slide 84

Slide 84 text

and I’m available for contracting from mid June

Slide 85

Slide 85 text

Thank You