Create a Decentralized Database using Tangle and IPFS for beginners

Yehia Tarek
Coinmonks
4 min readDec 17, 2019

--

In this tutorial, I am going to create a Database system for decentralized applications to do that I will use the MAM communication protocol and for storage, I will use IPFS.

If you are not familiar with MAM its simply create a Merkel Tree to save hashes. The hash of the root is capable of accessing all other hashes using side-key in our case as we use Restricted MAM. So IPFS will upload files then the returned hashed will be saved on MAM.

How the database will work

For sure our database will be in JSON format.

1- Upload JSON data to the IPFS and get the returned hash to access the files later.

2- Send the IPFS using MAM restricted.

To get Data:-

1-Fetch data using mama restricted and get the uploaded IPFS hash.

2- Fetch the hash of the IPFS to get the uploaded JSON data.

Let's Start Coding

The IPFS code to upload data. Check function on the documentation here.

The IPFS code to fetch data cat.js Check function on the documentation here

The MAM code

For the MAM code, we will create 4 files:-

1-IotaGlobale.js: Add all global variables used on the 2 push and fetch functions

2-pushData.js: Upload to MAM

3-fetchRoot.js: Fetch data from MAM

4-mangeMam.js: Combine the 3 above files

`IotaGlobale.js`Important note! It cost me three days to solve the duplication of Merkel tree. The State of Merkel tree must remain global or each time you call the function all your previous data will be lost.

IotaGlobale.js:

Upload data to MAM ‘pushData.js’

Fetch data from MAM ‘fetchRoot.js’

manageMam.js

You can check documentation examples for restricted MAM here.

Now, we are done with all the basic modules. Let’s create our ‘index.js’.

The first three lines are to import the two IPFS modules and the MAM manager.

Then, to create a database we will make a create function

Then to read data

Update data

Delete data

The whole project is included in this repository https://github.com/yehia67/IPFS-Tangle-Database-System/tree/master

Get Best Software Deals Directly In Your Inbox

--

--