Having submitted my pull request for refactoring an applications API from .NET to TypeScript into the big API as a service, now it’s time for the suggestions from the other devs. A lot of the reccomendations involved clarifying the GraphQL Schema’s documentation in regards to the business logic, how ever I also need to create transactions for when I’m accessing the database.
You want to use database transactions to protect the integrity of your database in case of an interruption or error when altering or creating data inside the database. In a relational database, a good transaction is Atomic, Consistent, Isolated and Durable (ACID).
Atomicity – A transaction is always completed or rolled back, there are no in betweens.
Consistentcy – The transaction is fully compliant with the database constraints.
Isolated – A transaction is not available to other transactions unless the original transaction is completed or rolled back.
Durability – Once a transaction is completed, the data is guaranteed to be saved in the database, even if an error occured immediately after the transaction. Any changes the transaction has made in the database must be permanent. If the transaction has succeeded, the transaction did in fact succeeded.
In my case, inside my GraphQL resolvers I am using KnexJS to communicate with the database. Looking at the official documentation for Knex, I can see an example of what I need to do here.
knex.insert({name: 'Old Books'}, 'id')
.into('catalogues')
.transacting(trx)
.then(function(ids) {
books.forEach((book) => book.catalogue_id = ids[0]);
return knex('books').insert(books).transacting(trx);
})
.then(trx.commit)
.catch(trx.rollback);
As you can see, if there is an error during the insert the catch( ) does a rollback to the state prior to the transaction.
I also did two deployments with one of the other devs, including the printing multiple sheets functionality I added to an application. I believe this is currently being tested by someone. The deployments were through IIS on a Windows Server and were pretty straight forward, essentially copying some files over with a few config bits and pieces.