Registry / database / type-mongodb

type-mongodb

JSON →
library2.0.0-beta.12jsnpmunverified

A TypeScript-first, decorator-based MongoDB ODM (Object-Document Mapper) built on the MongoDB Node.js driver v4. Current version is 2.0.0-beta.12, released as a beta indicating potential breaking changes in future releases. Key differentiators: decorator-driven schema definition, data-mapper pattern with repository classes, automatic type inference from decorators (no separate schema files), support for embedded documents and references. Decent alternative to Mongoose for teams preferring TypeScript-native ODM with strict type safety. Encourages domain-driven design and works with active record and data-mapper patterns. Requires reflect-metadata polyfill. Release cadence: beta releases every few months.

npm install type-mongodb
INSTALL
IMPORT
SIG · TYPE-MONGODB
T
type-mongodb
databasejavascriptv2.0.0-beta.12
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.

Entity
import { Entity } from 'type-mongodb'
const Entity = require('type-mongodb').Entity
ESM-only since v2.0.0-beta. TypeScript decorator export.
getRepository
import { getRepository } from 'type-mongodb'
import { Repository } from 'type-mongodb'
getRepository is a function, not a class. Repository instances are created via getRepository().
createConnection
import { createConnection } from 'type-mongodb'
import { connect } from 'type-mongodb'
Use createConnection() to establish a MongoDB connection. 'connect' does not exist.

Defines a User entity using decorators, establishes a MongoDB connection, saves and retrieves documents using a repository.

import 'reflect-metadata'; import { createConnection, Entity, PrimaryGeneratedColumn, Column, getRepository } from 'type-mongodb'; @Entity('users') class User { @PrimaryGeneratedColumn('uuid') _id: string; @Column() name: string; @Column({ type: 'int' }) age: number; } async function main() { const connection = await createConnection({ host: 'localhost', port: 27017, database: 'test' }); const userRepository = getRepository(User); const user = new User(); user.name = 'Alice'; user.age = 30; await userRepository.save(user); const users = await userRepository.find(); console.log(users); await connection.close(); } main().catch(console.error);
Debug
Known issues
breakingv2.0.0-beta changed from CommonJS to ESM only. Use import syntax instead of require(). This breaks Node.js versions <12 and projects not configured for ES modules.
fix
Convert your project to use ES modules (type: module in package.json) and replace require() with import statements.
affects: >=2.0.0-beta <3
breakingv2.0.0-beta dropped support for MongoDB driver v3. Only MongoDB driver ^4.1.0 is supported now.
fix
Update your mongodb package to version ^4.1.0 or later.
affects: >=2.0.0-beta
deprecatedThe 'useNewUrlParser' option is deprecated in MongoDB driver v4. type-mongodb no longer uses it.
fix
Remove useNewUrlParser, useUnifiedTopology options from connection config.
affects: >=2.0.0-beta <2.0.0
gotchaThis package requires reflect-metadata to be imported at the top of the entry file before using decorators. Omitting it causes 'Reflect is not defined' errors.
fix
Add import 'reflect-metadata' as the first line of your application entry file.
affects: >=1.0.0
gotchaColumn types are limited to MongoDB BSON types. Using unsupported types (e.g., 'bigint') may cause runtime errors.
fix
Use compatible types: string, number, boolean, Date, ObjectId, Buffer, etc.
affects: >=1.0.0 <3
Errors
Common errors & fixes
Reflect is not defined
reflect-metadata polyfill not imported at application entry point.
fix
Add import 'reflect-metadata' at the very top of your main file.
MongoError: auth fails
Incorrect database credentials or authentication mechanism not supported by MongoDB driver v4.
fix
Verify username, password, and database name. For SCRAM-SHA-256, ensure MongoDB server version >=4.0.
Cannot use import statement outside a module
Running ES module import syntax in a CommonJS project.
fix
Add type: module to package.json or rename file to .mjs.
Upgrade
Version history
2.0.0-beta.12latest on npm
Audit
Dependencies
mongodbrequiredCore database driver for MongoDB connections and operations. Required peer dependency.
reflect-metadatarequiredRequired polyfill for experimental decorator support in TypeScript. Must be imported early in application entry point.
Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
type-mongodb — npm install type-mongodb · libregistry