Hands-On With Quantum Ledger Database(QLDB)

Salman Dabbakuti
Coinmonks
7 min readMay 27, 2020

--

Image source: Groupemiage

In the previous article, I have explained what is quantum ledger database, benefits, use-cases, and how it differs from regular databases and how it is related to the blockchain. This article is to show how to set up Amazon’s QLDB and use it in our Nodejs application as a Database. I see this as a more realistic implementation. Hopefully this guide will get you on the road to “getting started with QLDB” without too much of a headache.

Recap:

Amazon QLDB is a fully managed ledger database that provides a transparent, immutable, and cryptographically verifiable transaction log ‎owned by a central trusted authority. Amazon QLDB tracks each and every application data change and maintains a complete and verifiable history of changes over time. QLDB fits perfectly for applications that need a scalable centralized ledger database where it needs to record all the transaction history overtime with added cryptographic security.

Getting Started:

First, let’s log into AWS and open the Amazon QLDB from Services:

Then, in the QLDB Dashboard, create a ledger by clicking on “Create ledger”:

It will take around a minute to create a ledger. Once the ledger is created, click on the Query editor from the sidebar and then choose a newly created database from the Ledger dropdown.

On the left just below the ledger dropdown, you can see a list of tables created on a selected database, On the right, there is a query execution pane and just below it, there is an output pane to show the result of a query.

PartiQL: One query language for all your data:

Before going further, we should be familiar with database query language — PartiQL and why Amazon introduced it to a few of its purpose-built databases including QLDB. Amazon announced PartiQL support for its own needs to query and transform vast amounts and varieties of data — not just SQL tabular data, but also nested and semi-structured data. PartiQL has the capability to efficiently query data, regardless of where(SQL Databases or NoSQL Databases) or in what format(JSON document model or table-rows model) it is stored. Since QLDB is a No-SQL Database(Semi-SQL & Semi-NoSQL) that can store semi-structured document model data in a table-rows format, PartiQL perfectly suits to QLDB. PartiQL is backward-compatible with SQL, queries are similar to SQL. So, it can leverage current SQL developers to offer robust ways to query and manage vast amounts and varieties of data.

PartiQL Syntax:

Don’t get scared, we are not here to learn a whole new query language. as we already know, PartiQL is similar to SQL. So let’s have a quick look at PartiQL syntax for CRUD operations.

Source: AWS Docs

Let’s Create, Update, Read, Delete student’s marks from the database.

Create:

In order to store student marks in the database, we first need to create a table. Let’s start creating a table named “Students” with PartiQL. Here is the query to create a table

Easy right?, So, now we have an empty table Students. Let’s add a student’s marks into it. Once the query succeeded, it will return a unique documentId for each document stored.

Read:

We can return all students’ data from a table or return single student data based on defined condition. for example, returning marks of a student named “John Doe” or returning student whose Physics marks are 86.

Update:

Now that you have data to work with, you can start making changes to students’ data. For example, consider you misrecorded John Doe’s Physics marks and wanted to update his Physics marks from 86 to 84. The below query does this. You can also update the whole row of a record too as below.

Delete:

You can use DELETE statement to delete particular student data from a table based on defined condition. for example, if you wanted to delete a student named “John Doe” or you can even drop the whole table as below.

Note: Historical changes of the deleted document still exist on the ledger as long as the table exists.

History:

As we already know, Amazon QLDB stores the complete history of every document in a table. You can see all revisions of each student document you previously inserted, updated, and deleted by using a built-in history function as below. Each change to the document will be represented by unique hash and versions and can easily verifiable and auditable.

Now you might have a basic understanding of QLDB’S PartiQL syntax. If you wanted to learn more about PartiQL operators, data types, statements, See Amazon’s QLDB PartiQL Reference. As of now we only worked with QLDB console right? In the next section, lets actually build a nodejs application that can access our database, query, and update data.

Nodejs Client:

Go to IAM Console and in the users section, click on your preferred user and go to the “Security credentials” tab and click on “Create access key”. once you clicked on, it will generate an access key and in the popup window, it will show generated Access Key Id and Secret key. make sure you copied and keep them safe and confidential as we need these to connect with our QLDB instance.

IAM>>Users>>username>>Security credentials>>Create access key

If Amazon was a car manufacturing company and you wanted to buy a car from them, They’ll ask you to collect car parts from 20 different shops which they also own and ask you to put them together yourself the best you can using the vast documentation they provide. It took me the better part of a week to figure out how to configure a QLDB to Node.js app using official Nodejs driver they provided for deploying and then actually deploy it. It was not up to the mark. So, after a long struggle, I’ve finally found this simpler, straight, and custom QLDB implementation.

Note: The QLDB module that I’m using here is not an official module. However, it uses official AWS SDK and official QLDB nodejs driver and makes the simplest implementation of QLDB Driver. I found it very useful and easier to use.

Installation:

Implementation:

So, in order to connect with our QLDB instance, we need to provide credentials we generated earlier and the region where our QLDB is setup, ledger name. Since QLDB uses Amazon’s Ion document format to store, we first need to convert our JSON data into Ion format using ionize class and then store it on the database. The execute method of qldb module allows you to run whatever the PartiQL query we’ve discussed earlier.

So try to create a crud application(Todo or Notes) using this module and expose it to REST API. I’ve already built a beginner application using this module and exposed it as REST and GraphQL APIs. I recommend you check that out. All the resources related to this article can be found on GitHub here

Get Best Software Deals Directly In Your Inbox

--

--

Salman Dabbakuti
Coinmonks

Curious about decentralized future. Working on Blockchain Technologies. Hit me up, would love to interact!