npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

resilient-node-cache

v1.0.1

Published

TypeScript library for syncing ResilientDB data via WebSocket and HTTP with seamless reconnection.

Downloads

83

Readme

resilient-node-cache

TypeScript library for syncing ResilientDB data via WebSocket and HTTP with seamless reconnection.

License

resilient-node-cache is a library for establishing and managing real-time synchronization between ResilientDB and MongoDB using WebSocket and HTTP. It includes automatic reconnection, batching, and concurrency management for high-performance syncing.

Features

  • Real-time sync between ResilientDB and MongoDB
  • Handles WebSocket and HTTP requests with auto-reconnection
  • Configurable synchronization intervals and batch processing
  • Provides MongoDB querying for ResilientDB transaction data

Installation

To use the library, install it via npm:

npm install resilient-node-cache

Configuration

MongoDB Configuration

The library uses MongoDB to store ResilientDB data. Configure it by specifying:

  • uri: MongoDB connection string
  • dbName: Database name in MongoDB
  • collectionName: Collection name in MongoDB where ResilientDB data is stored

ResilientDB Configuration

For ResilientDB configuration, specify:

  • baseUrl: The base URL for ResilientDB, e.g., resilientdb://localhost:18000
  • httpSecure: Use HTTPS if set to true
  • wsSecure: Use WSS if set to true
  • reconnectInterval: Reconnection interval in milliseconds (optional)
  • fetchInterval: Fetch interval in milliseconds for periodic syncs (optional)

Usage

1. Syncing Data from ResilientDB to MongoDB

Create a sync script to initialize and start the data synchronization from ResilientDB to MongoDB.

// sync.js

const { WebSocketMongoSync } = require('resilient-node-cache');

const mongoConfig = {
  uri: 'mongodb://localhost:27017',
  dbName: 'myDatabase',
  collectionName: 'myCollection',
};

const resilientDBConfig = {
  baseUrl: 'resilientdb://crow.resilientdb.com',
  httpSecure: true,
  wsSecure: true,
};

const sync = new WebSocketMongoSync(mongoConfig, resilientDBConfig);

sync.on('connected', () => {
  console.log('WebSocket connected.');
});

sync.on('data', (newBlocks) => {
  console.log('Received new blocks:', newBlocks);
});

sync.on('error', (error) => {
  console.error('Error:', error);
});

sync.on('closed', () => {
  console.log('Connection closed.');
});

(async () => {
  try {
    await sync.initialize();
    console.log('Synchronization initialized.');
  } catch (error) {
    console.error('Error during sync initialization:', error);
  }
})();

This script will:

  • Initialize the connection to MongoDB and ResilientDB
  • Continuously sync new blocks received from ResilientDB to MongoDB

2. Fetching Transactions by Public Key

After syncing, you can retrieve that data from MongoDB depending on your use case. Here’s a sample script to retrieve all transactions associated with a specified public key:

// fetchTransactionsWithPublicKey.js

const { MongoClient } = require('mongodb');

const mongoConfig = {
  uri: 'mongodb://localhost:27017',
  dbName: 'myDatabase',
  collectionName: 'myCollection',
};

// The publicKey for which to fetch transactions
const targetPublicKey = "8LUKr81SmkdDhuBNAHfH9C8G5m6Cye2mpUggVu61USbD";

(async () => {
  const client = new MongoClient(mongoConfig.uri);

  try {
    await client.connect();
    const db = client.db(mongoConfig.dbName);
    const collection = db.collection(mongoConfig.collectionName);

    console.log('Connected to MongoDB for fetching transactions.');

    // Create an index on transactions.value.inputs.owners_before for optimized querying
    const indexName = await collection.createIndex({ "transactions.value.inputs.owners_before": 1 });
    console.log(`Index created: ${indexName} on transactions.value.inputs.owners_before`);

    // Define aggregation pipeline to fetch all transactions for the specified publicKey in owners_before
    const pipeline = [
      { $unwind: "$transactions" },
      { $unwind: "$transactions.value.inputs" },
      { 
        $match: { 
          "transactions.value.inputs.owners_before": targetPublicKey 
        }
      },
      { $sort: { "transactions.value.asset.data.timestamp": -1 } },
      { $project: { transaction: "$transactions", _id: 0 } }
    ];

    const cursor = collection.aggregate(pipeline);
    const transactions = await cursor.toArray();

    if (transactions.length > 0) {
      console.log('Transactions with the specified publicKey in owners_before:', 
                  JSON.stringify(transactions, null, 2));
    } else {
      console.log(`No transactions found for publicKey: ${targetPublicKey}`);
    }
  } catch (error) {
    console.error('Error fetching transactions:', error);
  } finally {
    await client.close();
  }
})();

API Documentation

Class WebSocketMongoSync

  • constructor(mongoConfig: MongoConfig, resilientDBConfig: ResilientDBConfig):

    • Initializes the sync object with MongoDB and ResilientDB configurations.
  • initialize(): Connects to MongoDB, fetches initial blocks, starts periodic fetching, and opens the WebSocket connection to ResilientDB.

  • close(): Closes the MongoDB and WebSocket connections, stopping the periodic fetching.

MongoDB Collection Structure

Each document in the MongoDB collection corresponds to a ResilientDB block, containing:

  • id: Block identifier
  • createdAt: Timestamp for block creation
  • transactions: Array of transactions in the block, with details for each transaction’s inputs, outputs, and asset metadata

License

This library is licensed under the Apache License, Version 2.0.


Enjoy using resilient-node-cache to seamlessly synchronize your ResilientDB data to MongoDB with real-time updates and efficient querying!