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

cluster-flow

v1.0.0

Published

clusterflow is an npm library that simplifies the management of connections to multiple MongoDB clusters and provides an API for common operations such as querying data and inserting documents.

Downloads

3

Readme

Cluster Flow

clusterflow is an npm library that simplifies the management of connections to multiple MongoDB clusters and provides an API for common operations such as querying data and inserting documents.

Installation

To install clusterflow, use npm:

npm install clusterflow

Usage

Import the Library

To use clusterflow in your project, import it at the top of your script:

const ClusterFlow = require('clusterflow');

Create an Instance of ClusterFlow

Create an instance of ClusterFlow to manage your connections to multiple clusters:

const clusterFlow = new ClusterFlow();

Add a Cluster Connection

Use the addCluster method to add a connection to a MongoDB cluster:

// Add a cluster connection
await clusterFlow.addCluster('mongodb://server1:port1,server2:port2/?replicaSet=rs0');

Perform a Query

Use the query method to perform a query on a specific cluster, database, and collection:

// Perform a query on the first cluster
const results = await clusterFlow.query(0, 'myDatabase', 'myCollection', { name: 'Alice' });
console.log('Query results:', results);

Insert a Document

Use the insertDocument method to insert a document into a specific cluster, database, and collection:

// Insert a document into the first cluster
const insertResult = await clusterFlow.insertDocument(0, 'myDatabase', 'myCollection', {
    name: 'Bob',
    age: 30,
});
console.log('Insert result:', insertResult);

Close All Connections

When you are done, use the closeAll method to close all cluster connections:

// Close all cluster connections
await clusterFlow.closeAll();

API Documentation

ClusterFlow

  • addCluster(connectionString: string): Promise
    • Adds a connection to a MongoDB cluster using the provided connection string.
  • query(clusterIndex: number, dbName: string, collectionName: string, query: object): Promise<object[]>
    • Performs a query on the specified cluster, database, and collection.
    • clusterIndex: The index of the cluster to query.
    • dbName: The name of the database.
    • collectionName: The name of the collection.
    • query: The query object.
  • insertDocument(clusterIndex: number, dbName: string, collectionName: string, document: object): Promise
    • Inserts a document into the specified cluster, database, and collection.
    • clusterIndex: The index of the cluster to insert the document into.
    • dbName: The name of the database.
    • collectionName: The name of the collection.
    • document: The document to insert.
  • closeAll(): Promise
    • Closes all cluster connections.

Example

Here's a complete example demonstrating how to use the clusterflow library:

const ClusterFlow = require('clusterflow');

(async function() {
    const clusterFlow = new ClusterFlow();

    // Add a cluster connection
    await clusterFlow.addCluster('mongodb://server1:port1,server2:port2/?replicaSet=rs0');

    // Perform a query
    const results = await clusterFlow.query(0, 'myDatabase', 'myCollection', { name: 'Alice' });
    console.log('Query results:', results);

    // Insert a document
    const insertResult = await clusterFlow.insertDocument(0, 'myDatabase', 'myCollection', {
        name: 'Bob',
        age: 30,
    });
    console.log('Insert result:', insertResult);

    // Close all connections
    await clusterFlow.closeAll();
})();

Error Handling

The clusterflow library's methods return promises that may reject if an error occurs. Always handle errors using try/catch blocks:

try {
    const results = await clusterFlow.query(0, 'myDatabase', 'myCollection', { name: 'Alice' });
    console.log('Query results:', results);
} catch (error) {
    console.error('Failed to perform query:', error);
}

Issues

If you encounter any issues while using clusterflow, please report them on the GitHub Issues page.

When reporting an issue, please provide as much detail as possible, including:

  • Steps to reproduce the issue.
  • Error messages and stack traces.
  • Your operating system and Node.js version.

Your feedback helps us improve the library. Thank you!

Contributing

Contributions are welcome! If you would like to contribute to clusterflow, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Commit your changes with descriptive messages.
  4. Push your branch to GitHub.
  5. Submit a pull request.

Please ensure that your code follows the project's coding style and passes all tests.

For more details, please review the contributing guide.

License

clusterflow is released under the MIT License.