Registry / database / generic-mongodb-services

generic-mongodb-services

JSON →
library1.16.0jsnpmunverified

Provides a reusable GenericCrudService class that implements common CRUD operations (list, count, exists, create, get, update, patch) for MongoDB collections using the native Node.js MongoDB driver. Version 1.16.0 targets MongoDB driver v3.x (as seen in API docs). It reduces boilerplate by standardizing service-layer database interactions. Notably it uses callbacks/promises (not async/await consistently) and exposes methods like update and patch that directly wrap findOneAndUpdate. Suitable for small to mid-size Node.js APIs wanting to avoid repetitive CRUD code.

npm install generic-mongodb-services
INSTALL
IMPORT
SIG · GENERIC-MONGODB-SE
G
generic-mongodb-services
databasejavascriptv1.16.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.

GenericCrudService
const { GenericCrudService } = require('generic-mongodb-services');
import { GenericCrudService } from 'generic-mongodb-services'; // Package does not export ESM, only CJS require() works
Package exports via module.exports, so CommonJS require is required. No ESM wrapper.
GenericCrudService (TypeScript)
const { GenericCrudService } = require('generic-mongodb-services');
import GenericCrudService from 'generic-mongodb-services'; // Default import not supported
No TypeScript definitions shipped. Use @types/mongodb for client typing, but GenericCrudService is untyped. You can create a declaration file.
GenericCrudService instance
const service = new GenericCrudService(client, 'mydb', 'mycollection');
const service = new GenericCrudService(mongoClient, 'mydb'); // Missing collectionName argument causes runtime error
Constructor requires exactly three arguments: client (MongoClient), databaseName (string), collectionName (string).

Demonstrates full CRUD usage: connect, create, list, update, patch, get, count, exists with GenericCrudService.

const { MongoClient } = require('mongodb'); const { GenericCrudService } = require('generic-mongodb-services'); async function run() { const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const service = new GenericCrudService(client, 'testdb', 'users'); // Create const created = await service.create({ name: 'Alice', age: 30 }); console.log('Created:', created); // List const users = await service.list({ age: { $gte: 20 } }, 10, 0, { name: 1 }); console.log('Users:', users); // Update const updated = await service.update({ _id: created._id }, { $set: { age: 31 } }); console.log('Updated:', updated); // Patch const patched = await service.patch({ _id: created._id }, { city: 'NYC' }); console.log('Patched:', patched); // Get const user = await service.get({ _id: created._id }); console.log('Got:', user); // Count const count = await service.count({}); console.log('Count:', count); // Exists const exists = await service.exists({ _id: created._id }); console.log('Exists:', exists); await client.close(); } run().catch(console.error);
Debug
Known issues
gotchaThe `get` method expects a query object, not a string ID. Using a plain string will cause a MongoDB error.
fix
service.get({ _id: ObjectId(idString) }) — always pass query object with _id
affects: >=1.0.0
gotchaThe `patch` method applies $set to the document, but may overwrite existing fields unintentionally if you pass a full document instead of only changed fields.
fix
Only pass the fields to change in patch, e.g., service.patch(query, { name: 'new' })
affects: >=1.0.0
gotchaMethods use callbacks internally (promisified via .then). Errors are not always caught; you must wrap calls in try/catch or use .catch.
fix
Use async/await with try/catch: try { const r = await service.create(...) } catch(e) { ... }
affects: >=1.0.0
gotchaThe `update` method returns the document before update (by default). To return the updated document, pass { returnOriginal: false } in options.
fix
service.update(query, update, { returnOriginal: false })
affects: >=1.0.0
gotchaThe package does NOT handle ObjectId conversion. You must convert string IDs to ObjectId manually before passing to methods.
fix
const { ObjectId } = require('mongodb'); const query = { _id: new ObjectId(idStr) };
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: client.connect is not a function
Using a MongoClient instance that is not from the mongodb driver (e.g., mongoose client) or not connected before creating service.
fix
Ensure you use `new MongoClient(url)` from 'mongodb' package and call `await client.connect()` before instantiating GenericCrudService.
MongoError: cannot determine a bson type for the provided value
Passing a raw string for _id instead of ObjectId in query.
fix
Convert the string to ObjectId: const { ObjectId } = require('mongodb'); service.get({ _id: new ObjectId(idStr) });
TypeError: service.list is not a function
Attempting to use an ES6 import (import { GenericCrudService } from ...) on a CJS-only module.
fix
Use require: const { GenericCrudService } = require('generic-mongodb-services');
Cannot read property 'collection' of undefined
MongoClient not connected or databaseName is wrong.
fix
Verify client is connected and databaseName/collectionName are correct strings.
Upgrade
Version history
1.16.0latest on npm
Audit
Dependencies
mongodbrequiredPeer dependency required for MongoClient and collection operations
Agent activity
5 hits · last 30 days
node
4
Resources
generic-mongodb-services — npm install generic-mongodb-services · libregistry