Registry / database / mongo-database-handler

mongo-database-handler

JSON →
library0.0.14jsnpmunverified

A simple MongoDB database handler for Node.js that wraps Mongoose to provide a convenient way to manage models and connect to MongoDB. Version 0.0.14 is the current stable release. It is designed for use with Discord.js, Express.js, and other applications. Ship with TypeScript types. Differentiator: provides automatic model loading from a directory and a template-based model definition approach. Alternative to using Mongoose directly with manual model setup.

npm install mongo-database-handler
INSTALL
IMPORT
SIG · MONGO-DATABASE-HAN
M
mongo-database-handler
databasejavascriptv0.0.14
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.

DatabaseHandler
import { DatabaseHandler } from 'mongo-database-handler'
import DatabaseHandler from 'mongo-database-handler'
The package uses named export, not default. Common mistake is to use default import.
Template
import { Template } from 'mongo-database-handler'
const Template = require('mongo-database-handler').Template
Template is a base class for models. ESM import is correct for TypeScript.
Model
import { Model } from 'mongo-database-handler'
Named export for Model (which is a class that extends Template).

Shows how to initialize the handler, define a model using the Template class, and create a document.

import { DatabaseHandler, Template, Model } from 'mongo-database-handler'; import path from 'path'; import mongoose from 'mongoose'; class MyModel extends Template { constructor() { super( { name: { type: String }, value: { type: String }, }, 'name' ); } } async function main() { await new DatabaseHandler({ connectionURI: process.env.MONGO_URI ?? 'mongodb://localhost:27017/test' }).init(path.join(__dirname, 'models')); const doc = await MyModel.create({ name: 'example', value: 'test' }); console.log(doc); } main();
Debug
Known issues
gotchaThe 'init' function expects a directory path containing model files. If the directory does not exist or is empty, no models will be loaded silently.
fix
Ensure the models directory exists and contains at least one .js or .ts file exporting a class that extends Template.
affects: >=0.0.1
gotchaThe model classes must be exported as default exports from the model files. Named exports will not be detected.
fix
Use 'export default class MyModel extends Template' in model files.
affects: >=0.0.1
deprecatedSupport for CommonJS require may be removed in future versions. The package currently works with require but prefers ESM.
fix
Switch to ESM import syntax.
affects: >=0.0.10
Errors
Common errors & fixes
TypeError: Class extends value undefined is not a constructor or null
Template is not imported correctly or the model file is not exporting a class that extends Template.
fix
Ensure you import Template from 'mongo-database-handler' and extend it: class MyModel extends Template { ... }
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
DatabaseHandler was initialized without a connectionURI option or MONGO_URI environment variable is missing.
fix
Pass a connectionURI option to the DatabaseHandler constructor: new DatabaseHandler({ connectionURI: 'your-mongo-uri' })
Upgrade
Version history
0.0.14latest on npm
Audit
Dependencies
mongooseoptionalUnderlying MongoDB ODM used for database operations
Agent activity
2 hits · last 30 days
node
2
Resources
mongo-database-handler — npm install mongo-database-handler · libregistry