Registry /
database / generic-mongodb-services
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);
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.
fixEnsure 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.
fixConvert 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.
fixUse require: const { GenericCrudService } = require('generic-mongodb-services'); Cannot read property 'collection' of undefined
MongoClient not connected or databaseName is wrong.
fixVerify client is connected and databaseName/collectionName are correct strings.
Audit
Dependencies
mongodbrequiredPeer dependency required for MongoClient and collection operations