Registry / database / ts-mongo-orm

ts-mongo-orm

JSON →
library0.1.0jsnpmunverified

A MongoDB ORM for Node.js and TypeScript (v0.1.0), inspired by ActiveRecord. It provides a type-safe wrapper around MongoDB with decorator-based schema validation using @hapi/joi. The library is under active development (pre-1.0.0) and may have breaking changes. Key differentiators include a separation of document and model classes, fully typed query APIs, and decorators for field validation. Peer dependencies include mongodb, @hapi/joi, and TypeScript >=4.5.5.

npm install ts-mongo-orm
INSTALL
IMPORT
SIG · TS-MONGO-ORM
T
ts-mongo-orm
databasejavascriptv0.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.

DatabaseConnectDefault
✓ import { DatabaseConnectDefault } from 'ts-mongo-orm'
✗ const DatabaseConnectDefault = require('ts-mongo-orm').DatabaseConnectDefault
ESM-only; CommonJS require works but prefer import.
ActiveRecord
✓ import { ActiveRecord } from 'ts-mongo-orm'
✗ import ActiveRecord from 'ts-mongo-orm'
Named export, not default.
Document, Field, ObjectIdField
✓ import { Document, Field, ObjectIdField } from 'ts-mongo-orm'
✗ import { Document, Field } from 'ts-mongo-orm'
ObjectIdField is used for _id Field; also needs mongodb's ObjectId.
ActiveRecord.for
✓ const UserAR = ActiveRecord.for(User, UserModel)
✗ ActiveRecord.for(User)
Requires two arguments: document class and model class. If same, pass same class twice.

Connects to MongoDB, defines a User document with validation, creates a record, finds it, deletes it, and closes the connection.

import { DatabaseConnectDefault, ActiveRecord, Document, Field, ObjectIdField } from 'ts-mongo-orm'; import { ObjectId } from 'mongodb'; import * as Joi from '@hapi/joi'; @Document class User { static databaseName = 'test'; static collectionName = 'users'; @ObjectIdField() _id: ObjectId; @Field(Joi.string().email()) email: string; @Field(Joi.string().min(1)) name: string; constructor() { this._id = new ObjectId(); this.email = ''; this.name = ''; } } export const UserAR = ActiveRecord.for(User, User); (async () => { await DatabaseConnectDefault(process.env.MONGO_URI ?? 'mongodb://localhost:27017/test'); const user = await UserAR.create({ email: 'test@test.com', name: 'Test User' }); console.log('Created user:', user._id); const found = await UserAR.findOne({ email: 'test@test.com' }); console.log('Found:', found?.name); await UserAR.deleteOne({ _id: user._id }); await UserAR.close(); })();
Debug
Known issues
breakingAPI may change without notice before 1.0.0 release
fix
Pin to exact version and test upgrades carefully.
affects: >=0.0.0 <1.0.0
deprecatedUsing @types/hapi__joi is deprecated; prefer newer validation libraries
fix
Consider migrating to zod or joi v17 with built-in types.
affects: >=0.1.0
gotchaActiveRecord.for() requires two arguments; passing one will cause runtime error
fix
Always call ActiveRecord.for(DocumentClass, ModelClass) — use same class for both if no separate model.
affects: >=0.0.0
gotchaDatabaseConnectDefault must be called before any database operations; otherwise queries will fail
fix
Call await DatabaseConnectDefault(uri) at startup, before any ActiveRecord usage.
affects: >=0.0.0
gotchaDecorator @Document must be applied to class; forgetting it will cause validation/ type errors
fix
Add @Document decorator above the class definition.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ts-mongo-orm'
Package not installed or missing peer dependencies
fix
Run 'npm install ts-mongo-orm' and ensure all peer deps are installed: npm install mongodb @hapi/joi @types/mongodb @types/hapi__joi typescript
TypeError: Class extends value undefined is not a constructor or null
Forgetting to import or apply @Document decorator on the model class
fix
Ensure the document class has @Document decorator and both arguments to ActiveRecord.for are defined.
Argument of type 'string' is not assignable to parameter of type 'ObjectId | { _id: ObjectId; }'
Passing a plain string to findOne instead of query object
fix
Use findOne({ email: 'test@test.com' }) instead of findOne('test@test.com')
Cannot find name 'Joi'. Did you mean 'joi'?
Not importing @hapi/joi correctly or missing dependency
fix
Install @hapi/joi and import: import * as Joi from '@hapi/joi'
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
mongodbrequiredNative MongoDB driver for database operations
@types/mongodboptionalTypeScript type definitions for mongodb
@hapi/joirequiredSchema validation for document fields
@types/hapi__joioptionalTypeScript type definitions for @hapi/joi
typescriptoptionalRequired for decorator support and type checking
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
ts-mongo-orm — npm install ts-mongo-orm · libregistry