MongoDB
What is it?
MongoDB is a popular open-source NoSQL (non-relational) database management system. It is designed to handle large volumes of unstructured or semi-structured data, making it well-suited for applications with rapidly evolving schemas or complex data models. MongoDB stores data in flexible, JSON-like BSON (Binary JSON) documents, and its architecture allows for horizontal scaling, enabling efficient handling of growing datasets. It supports dynamic queries, indexing, and provides features like replication and sharding for high availability and scalability. MongoDB is commonly used in modern web development and other scenarios where flexible and scalable data storage is essential.
The purpose of integration is that peaq storage does not store big data. It should be handled by your off-chain storage. On-chain storage data should be small so it doesn’t delay execution.
On-Chain Storage
Smart Contracts: On-chain storage primarily involves the use of smart contracts. Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They reside on the blockchain and can store small amounts of data.
State Variables: State variables are used to store persistent data. These variables hold information that needs to be preserved across transactions and blocks.
Immutable Ledger: The blockchain itself serves as an immutable ledger that records the history of all transactions. Each block contains a reference to the previous block, creating a chain of blocks that cannot be altered without changing all subsequent blocks.
Off-chain Storage:
Databases: Off-chain storage involves the use of traditional databases or distributed databases to store large amounts of data that do not need to be recorded on the blockchain itself.
Decentralized Storage Networks: Some blockchain projects leverage decentralized storage networks like IPFS (InterPlanetary File System) or Swarm. These networks store data across multiple nodes, providing a decentralized and distributed storage solution.
Oracles: Off-chain data can also be brought on-chain through oracles. Oracles are entities or services that provide external information to smart contracts or programs running on-chain, enabling them to interact with off-chain data.
Architectural Flow:
Data Generation: Data is generated either on-chain through transactions or off-chain through external sources.
On-chain Storage: Relevant data is stored on-chain using smart contracts and state variables. This data is typically small and critical for the execution of on-chain logic and external services business logic that require data to be immutable.
Off-chain Storage: Large or less critical data is stored off-chain in databases or decentralized storage networks. This data may include files, images, or other information not necessary for the consensus mechanism.
Interaction: Smart contracts may interact with off-chain data using oracles, fetching external information and incorporating it into their logic. Or external services may interact with on-chain data to execute essential business logic.
Immutable Record: All on-chain data is recorded in blocks on the blockchain, creating an immutable and transparent record of transactions and changes to on-chain state.
Integration tutorial
For data verification and immutability checks, you can store the hash of the data being stored off-chain on our chain (in the DID Document/peaq storage). By doing this, you can fetch the hash from the chain and use it to verify the data being fetched from your off-chain storage. You can also map the keys of the on-chain key/value pair with the off-chain storage key. Below is an example code of using our peaqStorage with MongoDB off-chain storage:
1. Install the required packages:
2. Create a tsconfig.json file in your project directory with the following configuration:
3. Create a file named app.ts with the following code:
4. MongoDB Setup
You either have the option to deploy your MongoDB database locally (self-managed), to the cloud (using MongoDB Atlas), serverless (pay-as-you-go), or you can use the community edition.
The next step in the process is to download MongoDB to set your connection string and configure your database.
- Follow the steps to download MongoDB
- Run the cmd
atlas setup
to create and configure an atlas account- After executing the cmd your terminal will output your cluster name, cloud provider location, and display the admin username and password.
- Using the code from step 3 above, change the connection string to include your username and password. The MongoDB documentation gives an explanation on how to construct this string.
- Run the cmd
tsc
to compile your typescript code. Change the generatedapp.js
file to have the new nameapp.cjs
. This will be needed for the next step when we import the MongoDB class into the script that interacts with peaq.
4. Connect with peaq network:
- Create a new file in the same repository and call it
peaq.js
- In package.json add
"type": "module"
- Copy and paste the below code to initialize peaq network connection
The code above creates an instance of the MongoDBStorage() Class that was created and generated in step 3. It stores the ‘big data’ in MongoDB. The hash of that data is sent to peaq storage as a
pair of MongoDB_Id: Hashed_Data
. The data is then retrieved from the MongoDB based on the ID and the hashed data is read, then compared, to show the immutability.
Summary
In this documentation, MongoDB is presented as the off-chain storage solution to complement the peaq network’s on-chain storage, which is limited to small, critical data maintained via smart contracts and state variables on an immutable ledger.