Registry / database / mongodb-baserepository

mongodb-baserepository

JSON →
library1.0.6jsnpmunverified

mongodb-baserepository (v1.0.6) is a lightweight base repository pattern for MongoDB built on the native MongoDB driver. It provides CRUD operations, query helpers, and a consistent interface for building data access layers. The package uses a class-based approach where you extend the BaseRepository to create entity-specific repositories. It requires Node.js >=0.10.0 and has not been updated since 2015; it is effectively in maintenance mode with no recent releases. Alternatives include more modern repository abstractions like mongodb-repository or using Mongoose's built-in repository pattern.

npm install mongodb-baserepository
INSTALL
IMPORT
SIG · MONGODB-BASEREPOSI
M
mongodb-baserepository
databasejavascriptv1.0.6
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.

BaseRepository
const BaseRepository = require('mongodb-baserepository').BaseRepository;
import { BaseRepository } from 'mongodb-baserepository';
Package is CommonJS only; ESM import not supported.
default
const BaseRepository = require('mongodb-baserepository');
import BaseRepository from 'mongodb-baserepository';
Default export is the BaseRepository class; using named import style is ambiguous.
BaseRepository
const { BaseRepository } = require('mongodb-baserepository');
Named export also works via destructuring.

Shows how to extend BaseRepository, connect to MongoDB, and perform CRUD operations (insert, get, update, delete) using the repository pattern.

const BaseRepository = require('mongodb-baserepository'); const MongoClient = require('mongodb').MongoClient; async function main() { const url = process.env.MONGO_URL || 'mongodb://localhost:27017'; const client = await MongoClient.connect(url); const db = client.db('test'); // Define a repository for a 'users' collection class UserRepository extends BaseRepository { constructor(db) { super(db, 'users'); } } const userRepo = new UserRepository(db); // Create a user const userId = await userRepo.insert({ name: 'Alice', email: 'alice@example.com' }); console.log('Inserted user with id:', userId); // Find by id const user = await userRepo.getById(userId); console.log('Found user:', user); // Update await userRepo.updateById(userId, { $set: { name: 'Alice Updated' } }); // Delete await userRepo.deleteById(userId); client.close(); } main().catch(console.error);
Debug
Known issues
breakingPackage does not support promises natively; callbacks are used. As of Node v4+, you must manually promisify or wrap calls.
fix
Use util.promisify or wrap repository methods in a promise wrapper.
affects: >=1.0.0
deprecatedThe package relies on the old MongoDB driver API (v2.x) and is not compatible with the newer MongoDB Node.js driver v4+ without adapter modifications.
fix
Use an updated fork or migrate to a newer repository library.
affects: >=1.0.0
gotchaCallbacks are in the node style (error first). If you forget to handle the callback, errors may be swallowed.
fix
Always provide an error-first callback or promisify the methods.
affects: >=1.0.0
gotchaThe repository methods do not return the inserted/updated document; they only return the '_id' or raw result. To get the full document, you must query separately.
fix
After insert, call getById with the returned id to retrieve the document.
affects: >=1.0.0
deprecatedNo TypeScript types or definitions are provided; manual ambient declarations are required.
fix
Create a .d.ts file or use @types/mongodb-baserepository (none exists).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Class constructor BaseRepository cannot be invoked without 'new'
Using BaseRepository directly as a function instead of instantiating a subclass.
fix
Always extend BaseRepository and create an instance of the subclass: class UserRepo extends BaseRepository { ... }
Error: Collection name must be provided as a string
Passing an invalid or missing collection name to the constructor.
fix
Ensure the second argument to super() is a non-empty string: super(db, 'users');
TypeError: db.collection is not a function
Passing a MongoClient object instead of a Db instance.
fix
Obtain the Db instance from the client: const db = client.db();
Unhandled promise rejection: MongoError: topology was destroyed
Using the repository after closing the database connection.
fix
Ensure all repository operations complete before calling client.close().
Upgrade
Version history
1.0.6latest on npm
Audit
Dependencies
mongodbrequiredpeer dependency for MongoDB driver
Agent activity
4 hits · last 30 days
node
4
Resources
mongodb-baserepository — npm install mongodb-baserepository · libregistry