Registry / database / mongodb-handler

mongodb-handler

JSON →
library3.0.0jsnpmunverified

The mongodb-handler package (v3.0.0) is a lightweight wrapper around the official MongoDB Node.js driver, designed to simplify common database operations like insert, update, delete, and aggregation. It provides a global event listener to notify on operations and supports both callback and Promise/async-await patterns. The module uses the MDBHANDLER_CONSTRING environment variable for configuration, defaulting to 'mongodb://localhost:27017/mdbtest'. It offers an options object to define collection, document, query, and userId for event tracking. The package is released infrequently with no recent updates; v2 introduced promises and event listener improvements. Compared to Mongoose or the native driver, it reduces boilerplate but lacks schema validation and is less actively maintained.

npm install mongodb-handler
INSTALL
IMPORT
SIG · MONGODB-HANDLER
M
mongodb-handler
databasejavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default export
const mdbhandler = require('mongodb-handler');
const mdbhandler = require('mongodb-handler').start(); // common mistake: calling start() at import
The module exports a single object with methods like insert, update, delete, aggregate, and start(). In CommonJS, require it directly.
start()
const mdbhandler = require('mongodb-handler'); const connection = mdbhandler.start();
const { start } = require('mongodb-handler'); // not a named export
start() initializes the connection and returns an EventEmitter. It should be called once.
events
connection.on('ready', (msg) => console.log(msg));
mdbhandler.on('ready', ...) // events are on the connection object, not directly on mdbhandler
The start() returns an EventEmitter that emits 'ready', 'insert', 'delete', 'update', and 'bulkinsert' events.

Shows how to start the global event listener, connect to MongoDB, insert a document with a callback, and listen for operation events.

const mdbhandler = require('mongodb-handler'); // Start connection const connection = mdbhandler.start(); connection.on('ready', (message) => { console.log('Connected:', message); // Insert a document const options = { collection: 'test', doc: { name: 'John', age: 30 } }; mdbhandler.insert(options, (err, result) => { if (err) throw err; console.log('Inserted:', result); }); }); connection.on('insert', (message) => { console.log('Insert event:', message); }); // Error handling connection.on('error', (err) => { console.error('Connection error:', err); });
Debug
Known issues
deprecatedThe package is not actively maintained. It may not support newer MongoDB versions or Node.js releases.
fix
Consider using the official MongoDB driver directly or Mongoose for production workloads.
affects: >=3.0.0
gotchaThe module requires the environment variable MDBHANDLER_CONSTRING to be set, otherwise it defaults to 'mongodb://localhost:27017/mdbtest', which might not be your intended database.
fix
Set the environment variable: process.env.MDBHANDLER_CONSTRING = 'your_connection_string';
affects: *
gotchaEvent listener must be started with mdbhandler.start() before any operations. Calling insert/update before start() will fail.
fix
Always call mdbhandler.start() at the beginning of your app and wait for the 'ready' event.
affects: *
breakingv3.0.0 may have changed internal APIs. If upgrading from v1, note that v1 did not use promises.
fix
Check the changelog for migration steps; v2+ supports promises.
affects: >=2.0.0
Errors
Common errors & fixes
MongoError: connect ECONNREFUSED 127.0.0.1:27017
MongoDB server is not running or connection string is incorrect.
fix
Ensure MongoDB is running on localhost:27017, or set the MDBHANDLER_CONSTRING environment variable to your correct connection URI.
TypeError: Cannot read property 'insert' of undefined
The module was not properly required or started.
fix
Ensure you require the module correctly: const mdbhandler = require('mongodb-handler'); and call start() before using its methods.
ReferenceError: mdbhandler is not defined
The module is not imported or required.
fix
Add const mdbhandler = require('mongodb-handler'); at the top of your file.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
mongodbrequiredThe official MongoDB driver used for all database operations.
Agent activity
2 hits · last 30 days
node
2
Resources
mongodb-handler — npm install mongodb-handler · libregistry