Registry / database / ts-mongodb-orm

ts-mongodb-orm

JSON →
library2.0.1jsnpmunverified

A TypeScript ORM for MongoDB with zero production dependencies, compatible with the official mongodb driver. Current stable version is 2.0.1, released in September 2023. It provides decorator-based schema definition, CRUD operations, transaction support, and atomic locks. Designed to be intuitive for users familiar with the mongodb driver. Active development with regular updates.

npm install ts-mongodb-orm
INSTALL
IMPORT
SIG · TS-MONGODB-ORM
T
ts-mongodb-orm
databasejavascriptv2.0.1
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.

createConnection
import { createConnection } from 'ts-mongodb-orm'
const { createConnection } = require('ts-mongodb-orm')
ESM-only; CommonJS require fails. Use import syntax.
Document
import { Document } from 'ts-mongodb-orm'
import { Document } from 'mongodb'
Decorator for class schema, not to be confused with MongoDB's Document type.
Field
import { Field } from 'ts-mongodb-orm'
import { Field } from 'typeorm'
Decorator for defining fields on the document class.

Shows decorator-based schema definition, connection setup, repository creation, and CRUD operations.

import { Binary, createConnection, Document, Field, Index, ObjectId } from 'ts-mongodb-orm'; import { MongoClient } from 'mongodb'; @Index({ numberValue: -1 }) @Document({ collectionName: 'QuickStart' }) class QuickStart { @Field() public _id!: ObjectId; @Field() public stringValue?: string; @Field() public numberValue: number = 0; @Field() public booleanValue: boolean = false; @Field({ index: -1 }) public dateValue: Date = new Date(); @Field() public arrayValue: number[] = [1, 2, 3]; @Field() public objectArrayValue: any = {}; @Field() public binary: Binary = new Binary(Buffer.alloc(0)); } async function quickStart() { const uri = process.env.MONGO_URI ?? 'mongodb://localhost:27017'; const connection = await createConnection({ uri, dbName: 'test', mongoClientOptions: { w: 'majority', ignoreUndefined: true, serverSelectionTimeoutMS: 5000 } }); const repository = connection.getRepository(QuickStart); await repository.createCollection({ strict: true }); await repository.syncIndex(); const doc = new QuickStart(); doc.stringValue = 'hello'; await repository.insert(doc); console.log('Inserted document with ID:', doc._id); const found = await repository.findOne(doc._id); console.log('Found:', found); } quickStart().catch(console.error);
Debug
Known issues
breakingv2.0.0 requires mongodb@4.x and drops support for mongodb@3.x
fix
Upgrade mongodb to ^4.1.0 and update import paths
affects: >=2.0.0
deprecatedv1.x uses mongodb@3.x and is no longer maintained
fix
Upgrade to ts-mongodb-orm@2 and mongodb@4
affects: <2.0.0
gotchaDecorators require experimentalDecorators and emitDecoratorMetadata in tsconfig.json
fix
Set 'experimentalDecorators': true, 'emitDecoratorMetadata': true in tsconfig.json
affects: >=0
gotchaCollection name defaults to class name; override via @Document({collectionName: 'custom'})
fix
Use @Document decorator with explicit collectionName
affects: >=0
gotchacreateCollection with strict: true throws if collection exists; use {strict: false} to skip error
fix
Use createCollection({strict: false}) or catch error
affects: >=0
Errors
Common errors & fixes
Cannot find namespace 'ObjectId'
ObjectId not imported from ts-mongodb-orm
fix
import { ObjectId } from 'ts-mongodb-orm'
Reflect.metadata polyfill required for decorators
Missing reflect-metadata or experimental options
fix
npm install reflect-metadata and import it at entry point; set experimentalDecorators and emitDecoratorMetadata in tsconfig
MongoServerError: Authentication failed
Invalid credentials in connection URI
fix
Check MONGO_URI environment variable or hardcoded credentials
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
mongodbrequiredpeer dependency; the ORM wraps the official mongodb driver
Agent activity
20 hits · last 30 days
node
16
Meta
2
OpenAI (training)
1
Resources
ts-mongodb-orm — npm install ts-mongodb-orm · libregistry