Registry / database / hydrate-mongodb

hydrate-mongodb

JSON →
library2.4.0jsnpmunverified

An Object Document Mapper (ODM) for MongoDB, inspired by JPA, Hibernate, and Doctrine. Version 2.4.0 supports Node.js with TypeScript and decorators, mapping classes to MongoDB documents, change tracking, optimistic locking, and bulk operations for performance. Peer dependency on mongodb ^3.0.1. Key differentiators: class-based mapping with inheritance, decorator support, and bulk write synchronization.

npm install hydrate-mongodb
INSTALL
IMPORT
SIG · HYDRATE-MONGODB
H
hydrate-mongodb
databasejavascriptv2.4.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.

Hydrate
import { Hydrate } from 'hydrate-mongodb'
import Hydrate from 'hydrate-mongodb'
Named export, not default.
Configuration
import { Configuration } from 'hydrate-mongodb'
const { Configuration } = require('hydrate-mongodb')
Works with require, but ESM is preferred for TypeScript.
SessionFactory
import { createSessionFactory } from 'hydrate-mongodb'
import { SessionFactory } from 'hydrate-mongodb'
createSessionFactory is a function, not a class to import directly.

Shows setup with MongoClient, configuration, entity definition, and persisting a document.

import { MongoClient } from 'mongodb'; import { Configuration, Hydrate, createSessionFactory } from 'hydrate-mongodb'; async function main() { const client = await MongoClient.connect('mongodb://localhost:27017', { useUnifiedTopology: true }); const config = new Configuration(); config.databaseName = 'test'; const sessionFactory = await createSessionFactory(config, client); // Define a model @Hydrate.Entity() @Hydrate.Collection('users') class User { @Hydrate.Field() name: string; @Hydrate.Field() email: string; } const session = await sessionFactory.createSession(); const user = new User(); user.name = 'John'; user.email = 'john@example.com'; await session.persist(user); await session.flush(); console.log('User saved!'); } main().catch(console.error);
Debug
Known issues
breakingcreateSessionFactory now takes MongoClient instead of Db
fix
Pass MongoClient (from new driver) instead of Db.
affects: >=2.0
breakingDatabase name must be specified explicitly
fix
Specify db name in Configuration or @Collection decorator.
affects: >=2.0
breakingSessionFactory.connection is now a MongoClient object
fix
Access connection via MongoClient API.
affects: >=2.0
gotchaDecorators require TypeScript or Babel with decorator support
fix
Enable 'experimentalDecorators' in tsconfig.json or use Babel plugin.
affects: >=1.0
Errors
Common errors & fixes
TypeError: config.createSessionFactory is not a function
Incorrect import – createSessionFactory is a standalone function, not a method of Configuration.
fix
Import createSessionFactory directly: import { createSessionFactory } from 'hydrate-mongodb'.
MongoError: The 'db' option must be set
Database name not provided when creating session factory.
fix
Set config.databaseName or use @Collection decorator with db name.
Upgrade
Version history
2.4.0latest on npm
Audit
Dependencies
mongodbrequiredpeer dependency for MongoDB native driver v3
Agent activity
7 hits · last 30 days
node
6
Resources
hydrate-mongodb — npm install hydrate-mongodb · libregistry