Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objection.js, a SQL ORM

Objection.js, a SQL ORM

Slides from a talk given at LNUG May 2018

Paul Jensen

May 23, 2018
Tweet

More Decks by Paul Jensen

Other Decks in Programming

Transcript

  1. 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…
  2. 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.
  3. 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).
  4. 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).
  5. Creating migrations • Objection uses Knex’s in-built support for generating

    migrations and applying them, as well as being able to roll them back.
  6. 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.
  7. 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
  8. 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.
  9. Relationships tasks id name due_by is_done created_at updated_at task_joins id

    dependent_id dependency_id created_at updated_at 1 ∞ ∞
  10. 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.
  11. 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.
  12. 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.
  13. 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
  14. If you add jsonSchema to your models, you get to

    use some very cool plugins later on
  15. Plugins • Objection comes with a few plugins that are

    worth checking out • Two which I can recommend are “objection-graphql” and “objection-password”
  16. 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
  17. 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.