bhaiclient
v1.0.6
Published
A versatile and efficient solution for applications needing fast, reliable, and persistent data storage.
Downloads
15
Maintainers
Readme
Here's the fixed version of the BhaiClient package documentation:
BhaiClient Package Documentation
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 'bhaiclient';
(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 'bhaiclient';
(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
The performance of the BhaiClient
package largely depends on your use case, hardware, network latency, and data volume. However, overall, this package is fast and efficient due to its combination of in-memory caching and MongoDB.
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
- Optimize MongoDB Indexing: Ensure that your MongoDB collections have appropriate indexes to speed up queries.
- Increase Cache TTL: Increase the Time-to-Live (TTL) for cache entries to reduce the frequency of fetching data from MongoDB.
- Use Efficient Data Structures: Use efficient data structures and algorithms to manage data within your application.
- Monitor Performance: Use monitoring tools to track the performance of your MongoDB cluster and in-memory cache.