Skip to main content
MongoDB is an open-source NoSQL database that stores JSON-like BSON documents. It suits large or loosely structured payloads that do not belong on chain. peaq storage holds a 64-byte key and a 256-byte value, so anything larger lives in your own storage. For how the two layers divide the work, see On-chain vs Off-chain. off-chain-storage-1

Prerequisites

  • Node.js and npm installed.
  • TypeScript and Node.js type declarations installed in your project:
  • Atlas CLI for the Atlas setup in step 4. With Homebrew on macOS or Linux:
For other operating systems, follow the Atlas CLI installation instructions.

Integration tutorial

Store the document in MongoDB and write its hash to peaq storage, keyed by the MongoDB document ID. Later you fetch the document from MongoDB, hash it again, and compare against the on-chain value to prove it was not altered.

1. Install the required packages

2. Create a tsconfig.json file in your project directory

3. Create a file named app.ts

4. Set up MongoDB

You can run MongoDB locally, in the cloud with Atlas, or serverless.
  • Download MongoDB.
  • Run atlas setup to create and configure an Atlas account. It prints your cluster name, cloud provider location, and admin username and password.
  • For Atlas, replace mongodb://localhost:27017 in the step 3 code with your cluster’s mongodb+srv://<username>:<password>@<cluster-host>/ connection string (atlas setup prints the credentials; the Atlas UI shows the host). For a local MongoDB, keep mongodb://localhost:27017. The MongoDB documentation covers the format.
  • Run npx tsc to compile the TypeScript, then rename the generated app.js to app.cjs. The next step imports the class from that file.

5. Connect to peaq

  • Create a file named peaq.js in the same directory.
  • Add "type": "module" to package.json.
  • Paste the code below to open the peaq connection.
signAndSend without a status callback returns a transaction hash before inclusion. The fixed delay above is only a pause before reading. To wait for inclusion, use a signAndSend status callback, check dispatchError, and wait for status.isInBlock (or status.isFinalized for finality) before reading on-chain storage. The script uses the MongoDBStorage class from step 3. The document goes into MongoDB, and its hash goes into peaq storage as the pair MongoDB_Id: Hashed_Data. Reading the document back and re-hashing it shows whether it still matches the on-chain record.