Multiple SQL Inserts and Transactions with Objection Js
Let’s assume we have an application where we need to save the information about the sales of a company. For that purpose we have will create a database and two different tables:
1 | CREATE DATABASE sales_company; |
First table called sales save information about the amounts of the sale and in the table sales_details we are going to write the information of the product.
Of course you need more data for in the real world, but for the purposes of this demo it is good enough.
Defining Objection Models
Since we are using Objection as our ORM let’s start creating the two models, one for each table.
1 | // Sale.js |
And now the model for the sales_details table
1 | // SaleDetail.js |
Great, but now we need to establish the relationship between both models. In order to do that let’s use the relationMappings method in the Sale.js file.
1 | // Sale.js |
We are defining a details property (you can name this variable whatever you want) for the Sale model and establishing the relationship between both tables through the id column from the sales table
to the sale_id field of the sales_details table.
Insert records in two tables using insertGraph
Now we can use the insertGraph method to insert data in both tables at the same time.
1 | // Let's create the connection to our database |
Ok now let’s create an object that will represent a new sale
1 | const newSale = { |
A simple javascript object with all the information for our tables, let’s make the insert
1 | // multiple-inserts.js |
Executing this file will show us in the terminal 3 different insert sql queries.
That’s great, but you know things in real life can be messy sometimes, what about if we have some bug in our code and the quantity of one product get the value of null? Let’s see what happen.
1 | // multiple-insert-fail.js |
Running this script will throw an error because the quantity column does not allow nulls.
Ok! An error, but let’s see our sales table just in case.
You can see the records in the sales and sales_details table were created! Not cool objection! Not cool! 😒
You are mad now, but you remember the introduction to SQL course in high school and realize that all was your fault! If you want to avoid this kind of situations you need to use an SQL TRANSACTION!
Transactions in Objection.js
Creating a transaction is kinda easy
1 | // multiple-insert-with-transaction.js |
Time to run this script and an error again, but now you can verify that none record was inserted to the tables 🎊 .
A few things about the transaction method of the Sale model
- You can use any objection model inside the transaction function.
- The trx variable always must be passed to the query method of all the models that are called inside the transaction function
- The transaction will be consider committed if the promise function is resolved, that’s why I return the saleInserted object at the end.
- If any error is thrown inside the function it will automatically *rollback the transaction.
- I am using a MySql database but you can use any supported database for objection like postgres or sqlite.
You can clone and play with the code in the github repo: https://github.com/eperedo/objectionjs-examples/tree/master/src/multiple-insert-transactions