Registry / database / mongodb-typescript

mongodb-typescript

JSON →
library3.1.0jsnpmunverified

mongodb-typescript is a lightweight wrapper around the official MongoDB Node.js driver that hydrates plain MongoDB documents into TypeScript-defined class instances. Version 3.1.0 requires mongodb ^4.8.0 and reflect-metadata ^0.1.13 as peer dependencies. It uses class-transformer under the hood to map between plain objects and classes, preserving methods, getters, and nested structures. Unlike Mongoose, it does not enforce validation, schema stripping, or middleware. Key features include decorators (@id, @nested, @ref, @ignore), automatic hydration/dehydration, population of referenced collections, and schema index definitions. The package ships TypeScript types and is designed for developers who want strong typing without the overhead of a full ODM. Release cadence is irregular; latest update in 2022.

npm install mongodb-typescript
INSTALL
IMPORT
SIG · MONGODB-TYPESCRIPT
M
mongodb-typescript
databasejavascriptv3.1.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.

Repository
import { Repository } from 'mongodb-typescript'
const { Repository } = require('mongodb-typescript')
Package is ESM-only; require() will fail unless using dynamic import or bundler.
id
import { id } from 'mongodb-typescript'
import { @id } from 'mongodb-typescript'
@id is a decorator, not a symbol to be imported; the correct import is the decorator function named 'id'.
nested
import { nested } from 'mongodb-typescript'
import { Nested } from 'mongodb-typescript'
Decorator names are lowercase (e.g., nested, objectId, ref).

Connects to MongoDB, defines a User class with @id decorator, inserts a document, and retrieves it with methods intact.

import { ObjectId } from 'mongodb'; import { MongoClient } from 'mongodb'; import { id, Repository } from 'mongodb-typescript'; import 'reflect-metadata'; class User { @id id: ObjectId; name: string; age: number = 15; hello() { return `Hello, my name is ${this.name} and I am ${this.age} years old`; } } const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const repository = new Repository<User>(User, client, { dbName: 'test', collectionName: 'users' }); const user = new User(); user.name = 'tom'; await repository.insert(user); const saved = await repository.findById(user.id); console.log(saved.hello()); await client.close();
Debug
Known issues
breakingMinimum mongodb driver version is now 4.8.0 (was 3.x).
fix
Update mongodb to ^4.8.0 in package.json.
affects: >=3.0.0
deprecatedThe 'c' property on Repository (accessing underlying collection) is deprecated in v3. Use 'collection' instead.
fix
Replace repository.c with repository.collection.
affects: >=3.0.0
breakingPackage is now ESM-only. CommonJS require() will fail.
fix
Use import syntax or set "type": "module" in package.json, or use dynamic import.
affects: >=3.0.0
gotchaAll ObjectId properties (except @id) must be decorated with @objectId for correct hydration.
fix
Add @objectId decorator to non-primary ObjectId fields.
affects: *
gotchaThe @id decorator is required on exactly one field; hydration will fail if missing.
fix
Ensure your entity class has exactly one field decorated with @id.
affects: *
gotchaDecorators require experimentalDecorators and emitDecoratorMetadata in tsconfig.json.
fix
Add "experimentalDecorators": true and "emitDecoratorMetadata": true to tsconfig.json.
affects: *
Errors
Common errors & fixes
TypeError: Class constructor X cannot be invoked without 'new'
Importing as CommonJS (require) when package is ESM-only.
fix
Use import syntax: import { Repository } from 'mongodb-typescript'
Reflect.metadata is not a function
Missing import of reflect-metadata or not loaded before using decorators.
fix
Add import 'reflect-metadata' at the top of your entry file.
Cannot find module 'mongodb-typescript'
Package not installed or incorrect import path.
fix
Run npm install mongodb-typescript and check import path.
Error: Property 'age' does not exist on type 'User'
TypeScript thinks the property is undefined due to lack of initialization or decorator issue.
fix
Initialize the property with a default value: age: number = 15;
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies
mongodbrequiredOfficial MongoDB driver used for database operations
reflect-metadatarequiredRequired for experimental decorators and metadata reflection
Agent activity
2 hits · last 30 days
node
2
Resources