Registry / database / mongo-models

mongo-models

JSON →
library3.0.4jsnpmunverified

Lightweight MongoDB ODM using JavaScript classes and Joi schema validation, targeting Node.js >=8 with MongoDB 3.x. v3.0.4 is current stable, with infrequent releases. Differentiates from Mongoose by being minimal and class-based, and from the native MongoDB driver by providing model abstractions, schema enforcement via Joi, and automatic document instantiation.

npm install mongo-models
INSTALL
IMPORT
SIG · MONGO-MODELS
M
mongo-models
databasejavascriptv3.0.4
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.

MongoModels
const MongoModels = require('mongo-models')
Package does not ship ES modules; default CommonJS export only.
connect
const { connect } = require('mongo-models')
import { connect } from 'mongo-models'
No ESM support; destructure from CommonJS require.
Model class
class Customer extends require('mongo-models') { ... }
import MongoModels from 'mongo-models'; class Customer extends MongoModels { ... }
Use require() directly; ESM import will cause a runtime error.

Demonstrates creating a model class with Joi schema, inserting a document, and connecting/disconnecting to MongoDB.

const MongoModels = require('mongo-models'); const Joi = require('joi'); const schema = Joi.object({ _id: Joi.object(), name: Joi.string().required(), email: Joi.string().email(), phone: Joi.string() }); class User extends MongoModels { static create(name, email, phone) { const document = new User({ name, email, phone }); return this.insertOne(document); } } User.collectionName = 'users'; User.schema = schema; (async () => { await MongoModels.connect({ uri: process.env.MONGODB_URI ?? '', db: process.env.MONGODB_NAME ?? '' }, {}); const users = await User.create('Alice', 'alice@example.com', '555-1234'); console.log(users[0]); await MongoModels.disconnect(); })();
Debug
Known issues
breakingv3.x peer dependency mongodb@3.x.x only, not compatible with mongodb 4.x or 5.x.
fix
Use mongodb@3.6.x with Node.js >=8, or stay on v2.x if upgrading mongodb.
affects: >=3.0.0
gotchaStatic collectionName and schema properties must be set on the model class before any operations; forgetting them leads to undefined behavior.
fix
Always define Customer.collectionName = 'customers' and Customer.schema = joiSchema after the class definition.
affects: >=1.0.0
deprecatedconnect() with object parameter changed from v2 to v3: v2 used { uri, db } but v3 expects { uri: string, db: string } as first argument.
fix
Pass the connection object as first argument and options as second: MongoModels.connect({ uri, db }, {}).
affects: >=3.0.0
breakinginsertOne() now returns an array of inserted documents (v3) instead of a single document (v2).
fix
Use const [doc] = await Model.insertOne(data); or destructure result array.
affects: >=3.0.0
gotchaModel.find() returns plain objects, not model instances, unless you override the static method.
fix
To get instances, iterate results and manually construct: results.map(doc => new Model(doc)).
affects: >=1.0.0
gotchaSchema validation via Joi is only triggered on insertOne/insertMany, not on find/findOne/update operations.
fix
Manually validate before updates using Joi.validate or set up hooks.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: MongoModels.connect is not a function
Importing ESM style (import) when package is CommonJS-only.
fix
Use const MongoModels = require('mongo-models');
Error: MongoModels is not a constructor
Forgot to require the package before extending.
fix
Ensure you have const MongoModels = require('mongo-models'); before class definition.
MongoError: cannot determine collection name
Collection name not set on model class.
fix
Add Model.collectionName = 'your_collection_name'; after class definition.
TypeError: Cannot read property '_bsontype' of undefined
Using mongodb@4.x which changes BSON types; mongo-models v3 requires mongodb@3.x.
fix
Downgrade mongodb to 3.6.x or upgrade mongo-models to a version supporting 4.x.
Upgrade
Version history
3.0.4latest on npm
Audit
Dependencies
mongodbrequiredPeer dependency required for MongoDB driver version 3.x.
joioptionalUsed for schema validation of model documents.
Agent activity
4 hits · last 30 days
node
4
Resources
mongo-models — npm install mongo-models · libregistry