Registry / database / mongodb-dao-es6

mongodb-dao-es6

JSON →
library2.0.2jsnpmunverified

Generic Data Access Object for MongoDB that simplifies CRUD operations with a class-based API. Version 2.0.2, released in 2019, is the latest stable version. It wraps the native MongoDB driver to provide pagination, sorted queries, and formatted output. Compared to lower-level drivers, it reduces boilerplate but lacks active updates and TypeScript support. The package is ES6-native and uses the native MongoDB driver as a peer dependency.

npm install mongodb-dao-es6
INSTALL
IMPORT
SIG · MONGODB-DAO-ES6
M
mongodb-dao-es6
databasejavascriptv2.0.2
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.

BaseDao
import BaseDao from 'mongodb-dao-es6'
const BaseDao = require('mongodb-dao-es6').BaseDao
Default export only. No named export available.
BaseDao (CommonJS)
const BaseDao = require('mongodb-dao-es6').default
const BaseDao = require('mongodb-dao-es6')
In CJS, the import yields a module with a 'default' property due to ESM->CJS transpilation.
MongoClient (from mongodb)
import { MongoClient } from 'mongodb'
const MongoClient = require('mongodb').MongoClient
MongoClient is required as a dependency. Using older style may cause issues with ESM.

Connects to MongoDB, creates a DAO, performs create, find (with filter), and delete operations.

import { MongoClient } from 'mongodb'; import BaseDao from 'mongodb-dao-es6'; async function main() { const uri = process.env.MONGO_URI ?? 'mongodb://localhost:27017'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); await client.connect(); const dao = new BaseDao({ dbName: 'test', collection: 'users', sort: { _id: -1 }, pageSize: 10 }, client); const created = await dao.create({ name: 'Alice', age: 30 }); console.log('Created:', created); dao.match = { name: 'Alice' }; const found = await dao.find(); console.log('Found:', found); const deleted = await dao.delete(); console.log('Deleted:', deleted); await client.close(); } main().catch(console.error);
Debug
Known issues
gotchaConfig properties are set via direct assignment on the instance, not constructor options. Must set `match`, `id`, `sort` etc. before calling find/update/delete.
fix
Assign values to `dao.match`, `dao.id` etc. before each operation.
affects: >=0.0.0
deprecatedConstructor requires `useNewUrlParser` and `useUnifiedTopology` options on MongoClient; these are deprecated in newer mongodb driver versions (4.x+).
fix
Remove these options if using mongodb driver 4.0+; they are now default.
affects: >=2.0.0
gotchaThe `create` method returns an array even for a single document.
fix
Expect an array from `dao.create(data)`; access first element if needed.
affects: >=0.0.0
breakingVersion 2.x changed the export from a CommonJS module to an ESM default export. Breaking for CJS `require` users.
fix
Use `const BaseDao = require('mongodb-dao-es6').default` or switch to ESM `import`.
affects: >=2.0.0
deprecatedPackage has not been updated since 2019. May not be compatible with latest mongodb driver or Node.js versions.
fix
Consider using a more actively maintained DAO like `mongodb-dao` or the native driver directly.
affects: *
gotchaConfig object passed to constructor is not validated. Missing `dbName` or `collection` will cause runtime errors.
fix
Always provide `dbName` and `collection` in config.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: BaseDao is not a constructor
Using `require('mongodb-dao-es6')` which yields the module object, not the constructor directly.
fix
Use `const BaseDao = require('mongodb-dao-es6').default` for CJS or switch to ESM import.
MongoError: Cannot find collection ''
Missing or empty `collection` property in config object.
fix
Ensure config.collection is a non-empty string.
TypeError: client.db is not a function
Passing a `MongoClient` instance that hasn't been connected or is invalid.
fix
Await `client.connect()` before creating BaseDao instance.
Cannot read property '_id' of undefined
Calling `update` or `delete` without setting `dao.id` or after a find with no results.
fix
Set `dao.id` to a valid ObjectId or document _id before calling update/delete.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
mongodbrequiredPeer dependency. Required to create MongoClient instance and pass to BaseDao.
Agent activity
5 hits · last 30 days
node
4
Resources
mongodb-dao-es6 — npm install mongodb-dao-es6 · libregistry