Categories Uncategorized

Integrating Blockchain secured data with node.js and EbakusNG

EbakusNG* is a next generation blockchain software that, among other things, can be used as a secure drop-in replacement for traditional databases.

Suppose you need a secure datastore for your application’s medical records. These are patient’s sensitive data, that need to be verifiably correct and secured across multiple clinics in a hospital and possibly securely and trustlessly shared with insurance companies. Let’s see how this can be implemented in a node.js application using EbakusNG as a blockchain enabled database.

Installing the node.js library

In order to use the EbakusDB through Node.js you first have to install the client library. For doing this you simply run:

$ npm install node-ebakusdb --save

Defining the database schema

Before continuing with the actual code and how to use the library, we have to define some models that we are going to use in our DB. For this post let’s use the following ones:

const schemas = {
  Patients: {
    type: 'table',
    name: 'Patients',
    inputs: [
      { name: 'Id', type: 'string' },
      { name: 'FirstName', type: 'string' },
      { name: 'LastName', type: 'string' },
      { name: 'Email', type: 'string' },
      { name: 'SocialSecurityNumber', type: 'string' },
      // ...
    ],
  },
  Exams: {
    type: 'table',
    name: 'Exams',
    inputs: [
      { name: 'Id', type: 'string' },
      { name: 'PatientId', type: 'string' },
      { name: 'Date', type: 'uint64' },
      { name: 'Info', type: 'string' },
      // ...
    ],
  },
}

The above schema describes two tables, one for Patients and one for medical exams they might have performed.

Creating our connection object

Now that we have our models let’s create a client connection object.

For this type of application you might want to run private ebakus nodes deployed at various clinics. This is as easy to do as launching a mysql server, and we will cover this in a next tutorial. So stay tuned. 

const ebakusdb = require('ebakusdb')
const client = ebakusdb.createClient({
  secure: true,
  host: 'rpc.ebakus-testnet.com',
  port: 443,
  localSigning: true,
  keystoreV3: '<fullpath>/examples/keystoreV3.json',
  keystoreV3Pass: '123',
  schema: schemas // as defined previously
})

So are we ready? In some sense, yes. You have a connection to a blockchain network that has a DB. You can now send commands to the database.

Creating the tables

Let’s create our tables in the DB. In the `createTable` command we can also pass our indexed fields, which will be used for `WHERE` and `ORDER BY` queries.

For the `Patients` table we would like to query for patients using their emails or social security numbers. While for the `Exams` table we would like to find all records for a patient ordered by date entered in descending order.

Keep in mind that this needs to happen once for your dApp.

try {
  await client.createTable('Patients'
                          ['Email','SocialSecurityNumber']);
  await client.createTable('Exams', 
                          ['PatientId', 'Date']);
} catch (err) {
  // will fail if the table is already created
  console.error('Failed to create tables:', err.message);
}

This is so cool, you have your own DB in blockchain already. 🎉

Inserting and updating records

Now it’s time to start writing some data to our DB.

const updated = await client.insertObj('Patients', {
  Id: 'pat-1000', // UNIQUE_PATIENT_ID
  FirstName: 'Harry',
  LastName: 'Kalogirou',
  Email: 'harry@gmail.com',
  SocialSecurityNumber: '1234567890',
});
// if patient exists
if (updated) {
  console.log('Patient has been updated:', err.message);
}
await client.insertObj('Exams', {
  Id: 1,
  PatientId: 'pat-1000',
  Date: '1257894000',
  Info: '...',
});

Reading records back

All good, our data is in the blockchain, how can we read it back. Let’s assume that we don’t know Patient’s Id, but only his email. And that we want to get his exams.

const patient = await client.get('Patients',
                                 'Email == harry@ebakus.com');
if (!patient) {
  console.log('Failed to insert exam:', err.message);
}
console.log('Our patient:', patient);
const exams = client.select('Exams', 
                            `PatientId == ${patient.Id}`, 
                            'Date DESC');
for await (const exam of exams) {
  console.log('Exam:', exam);
}

Deleting records

All good, but we want to delete an exam. We can delete it, but keep in mind that what we write in the blockchain is immutable and we can’t remove it from the blockchains history.

const deleted = client.deleteObj('Exams', { Id: 1 });

Conclusions

Within this 2 pager walkthrough we covered the basic CRUD operations on our database. Our goal of transparently using blockchain technology in our datastore is achieved. We hope that simple, understandable and clean Javascript APIs to the datastore will enable new levels of blockchain secured infrastructure being deployed. 

Developers can reuse their understanding of database systems and easily create new systems, and transition legacy databases to the new standard. 

With higher standards in data integrity and security already being in high demand, either by governments pressure, or by pure private sector maturing to understand the liabilities that our modern world and data management exposes them to, we expect legacy database systems to take the backseat in favour of blockchain enabled databases. 
 

More articles are coming, with tutorials on how to use EbakusNG are coming so please make sure to follow me around in social media:

Twitter: https://twitter.com/harkal
Medium: @harkal

* EbakusNG is not to be confused with Ebakus which is based on my original work in blockchain tech. Since Ebakus was discontinued by its company after launching the public network in 2020, EbakusNG (next generation) embodies the next iteration of the software, and includes all advancements made since then. 

 

About the author