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

bhaiclient

v1.0.4

Published

A versatile and efficient solution for applications needing fast, reliable, and persistent data storage.

Downloads

96

Readme

BhaiClient Package Documentation

npm version

Overview

BhaiClient is a versatile and efficient solution for applications needing fast, reliable, and persistent data storage using MongoDB and in-memory caching.

Installation

npm install bhaiclient

Getting Started

Importing and Initializing

import BhaiClient from 'bhaiclient';

const client = new BhaiClient({
  mongoConfigs: [{ uri: 'your_mongo_connection_string', dbName: 'your_database_name' }],
  ttl: 3600, // Time-to-Live for cache entries in seconds
  logger: (message) => console.log('LOG:', message),
  errorHandler: (error) => console.error('ERROR:', error.message)
});

Connecting to MongoDB

(async () => {
  await client.connectToMongo();
})();

API Reference

1. connectToMongo()

Connects to the MongoDB instances specified in the configuration.

await client.connectToMongo();

2. getData(collectionName, key)

Retrieves data from the cache or MongoDB.

  • collectionName: The name of the MongoDB collection.
  • key: The key to retrieve.
const data = await client.getData('your_collection', 'testKey');
console.log(data);

3. setData(collectionName, key, value)

Sets data in the cache and MongoDB.

  • collectionName: The name of the MongoDB collection.
  • key: The key to set.
  • value: The value to set.
await client.setData('your_collection', 'testKey', { some: 'data' });

4. deleteData(collectionName, key)

Deletes data from the cache and MongoDB.

  • collectionName: The name of the MongoDB collection.
  • key: The key to delete.
await client.deleteData('your_collection', 'testKey');

Example Scenarios

Real-Time Applications (Chat Application)

import BhaiClient from 'bhaiclient';

(async () => {
const client = new BhaiClient({
    mongoConfigs: [{ uri: 'your_mongo_connection_string', dbName: 'your_database_name' }],
    ttl: 3600, // 1 hour TTL for chat messages
    logger: (message) => console.log('LOG:', message),
    errorHandler: (error) => console.error('ERROR:', error.message)
  });

  await client.connectToMongo();

  // Set chat message
  await client.setData('chat', 'message123', { from: 'user123', to: 'user456', message: 'Hello!' });

  // Get chat message
  const message = await client.getData('chat', 'message123');
  console.log('Chat Message:', message);

  // Delete chat message
  await client.deleteData('chat', 'message123');
})();

IoT (Sensor Data)

import BhaiClient from './src/index';

(async () => {
  const client = new BhaiClient({
    mongoConfigs: [{ uri: 'your_mongo_connection_string', dbName: 'your_database_name' }],
    ttl: 600, // 10 minutes TTL for sensor data
    logger: (message) => console.log('LOG:', message),
    errorHandler: (error) => console.error('ERROR:', error.message)
  });

  await client.connectToMongo();

  // Set sensor data
  await client.setData('sensors', 'sensor123', { temperature: 22.5, humidity: 60 });

  // Get sensor data
  const sensorData = await client.getData('sensors', 'sensor123');
  console.log('Sensor Data:', sensorData);

  // Delete sensor data
  await client.deleteData('sensors', 'sensor123');
})();

E-commerce (User Cart)

import BhaiClient from './src/index';

(async () => {
  const client = new BhaiClient({
    mongoConfigs: [{ uri: 'your_mongo_connection_string', dbName: 'your_database_name' }],
    ttl: 86400, // 1 day TTL for user carts
    logger: (message) => console.log('LOG:', message),
    errorHandler: (error) => console.error('ERROR:', error.message)
  });

  await client.connectToMongo();

  // Set user cart
  await client.setData('carts', 'user123', { items: [{ productId: 'prod123', quantity: 2 }] });

  // Get user cart
  const cart = await client.getData('carts', 'user123');
  console.log('User Cart:', cart);

  // Delete user cart
  await client.deleteData('carts', 'user123');
})();

Events

set

Emitted when data is set.

client.on('set', (key, data) => {
  console.log(`Data set for key: ${key}`, data);
});

get

Emitted when data is retrieved.

client.on('get', (key, data) => {
  console.log(`Data retrieved for key: ${key}`, data);
});

delete

Emitted when data is deleted.

client.on('delete', (key) => {
  console.log(`Data deleted for key: ${key}`);
});

cacheMiss

Emitted when a cache miss occurs.

client.on('cacheMiss', (key) => {
  console.log(`Cache miss for key: ${key}`);
});

Performance

BhaiClient package ki performance kaafi depend karti hai aapke use case, hardware, network latency aur data volume par. Lekin overall, yeh package fast aur efficient hai kyunki yeh in-memory caching aur MongoDB ka combination use karta hai.

Expected Performance

  • setData: Typically takes a few milliseconds, depending on MongoDB write speed.
  • getData: If data is in cache, it takes less than 1 millisecond. If data is fetched from MongoDB, it takes a few milliseconds.
  • deleteData: Typically takes a few milliseconds, depending on MongoDB delete speed.

Improving Performance

  1. Optimize MongoDB Indexing: Ensure that your MongoDB collections have appropriate indexes to speed up queries.
  2. Increase Cache TTL: Increase the Time-to-Live (TTL) for cache entries to reduce the frequency of fetching data from MongoDB.
  3. Use Efficient Data Structures: Use efficient data structures and algorithms to manage data within your application.
  4. Monitor Performance: Use monitoring tools to track the performance of your MongoDB cluster and in-memory cache.